transmission/libtransmission/tr-udp.cc

432 lines
11 KiB
C++

/*
Copyright (c) 2010 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <cstring> /* memcmp(), memcpy(), memset() */
#include <cstdlib> /* malloc(), free() */
#ifdef _WIN32
#include <io.h> /* dup2() */
#else
#include <unistd.h> /* dup2() */
#endif
#include <event2/event.h>
#include <cstdint>
#include <libutp/utp.h>
#include "transmission.h"
#include "log.h"
#include "net.h"
#include "session.h"
#include "tr-assert.h"
#include "tr-dht.h"
#include "tr-utp.h"
#include "tr-udp.h"
/* Since we use a single UDP socket in order to implement multiple
uTP sockets, try to set up huge buffers. */
#define RECV_BUFFER_SIZE (4 * 1024 * 1024)
#define SEND_BUFFER_SIZE (1 * 1024 * 1024)
#define SMALL_BUFFER_SIZE (32 * 1024)
static void set_socket_buffers(tr_socket_t fd, bool large)
{
int rbuf = 0;
int sbuf = 0;
socklen_t rbuf_len = sizeof(rbuf);
socklen_t sbuf_len = sizeof(sbuf);
char err_buf[512];
int size = large ? RECV_BUFFER_SIZE : SMALL_BUFFER_SIZE;
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&size), sizeof(size));
if (rc < 0)
{
tr_logAddNamedError("UDP", "Failed to set receive buffer: %s", tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
}
size = large ? SEND_BUFFER_SIZE : SMALL_BUFFER_SIZE;
rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char const*>(&size), sizeof(size));
if (rc < 0)
{
tr_logAddNamedError("UDP", "Failed to set send buffer: %s", tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
}
if (large)
{
rc = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>(&rbuf), &rbuf_len);
if (rc < 0)
{
rbuf = 0;
}
rc = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&sbuf), &sbuf_len);
if (rc < 0)
{
sbuf = 0;
}
if (rbuf < RECV_BUFFER_SIZE)
{
tr_logAddNamedError("UDP", "Failed to set receive buffer: requested %d, got %d", RECV_BUFFER_SIZE, rbuf);
#ifdef __linux__
tr_logAddNamedInfo("UDP", "Please add the line \"net.core.rmem_max = %d\" to /etc/sysctl.conf", RECV_BUFFER_SIZE);
#endif
}
if (sbuf < SEND_BUFFER_SIZE)
{
tr_logAddNamedError("UDP", "Failed to set send buffer: requested %d, got %d", SEND_BUFFER_SIZE, sbuf);
#ifdef __linux__
tr_logAddNamedInfo("UDP", "Please add the line \"net.core.wmem_max = %d\" to /etc/sysctl.conf", SEND_BUFFER_SIZE);
#endif
}
}
}
void tr_udpSetSocketBuffers(tr_session* session)
{
bool utp = tr_sessionIsUTPEnabled(session);
if (session->udp_socket != TR_BAD_SOCKET)
{
set_socket_buffers(session->udp_socket, utp);
}
if (session->udp6_socket != TR_BAD_SOCKET)
{
set_socket_buffers(session->udp6_socket, utp);
}
}
void tr_udpSetSocketTOS(tr_session* session)
{
if (session->peerSocketTOS == 0)
{
return;
}
if (session->udp_socket != TR_BAD_SOCKET)
{
tr_netSetTOS(session->udp_socket, session->peerSocketTOS, TR_AF_INET);
}
if (session->udp6_socket != TR_BAD_SOCKET)
{
tr_netSetTOS(session->udp6_socket, session->peerSocketTOS, TR_AF_INET6);
}
}
/* BEP-32 has a rather nice explanation of why we need to bind to one
IPv6 address, if I may say so myself. */
// TODO: remove goto, it prevents reducing scope of local variables
static void rebind_ipv6(tr_session* ss, bool force)
{
bool is_default = false;
tr_address const* public_addr = nullptr;
struct sockaddr_in6 sin6;
unsigned char const* ipv6 = tr_globalIPv6();
tr_socket_t s = TR_BAD_SOCKET;
int rc = -1;
int one = 1;
/* We currently have no way to enable or disable IPv6 after initialisation.
No way to fix that without some surgery to the DHT code itself. */
if (ipv6 == nullptr || (!force && ss->udp6_socket == TR_BAD_SOCKET))
{
if (ss->udp6_bound != nullptr)
{
free(ss->udp6_bound);
ss->udp6_bound = nullptr;
}
return;
}
if (ss->udp6_bound != nullptr && memcmp(ipv6, ss->udp6_bound, 16) == 0)
{
return;
}
s = socket(PF_INET6, SOCK_DGRAM, 0);
if (s == TR_BAD_SOCKET)
{
goto FAIL;
}
#ifdef IPV6_V6ONLY
/* Since we always open an IPv4 socket on the same port, this
shouldn't matter. But I'm superstitious. */
(void)setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char const*>(&one), sizeof(one));
#endif
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
if (ipv6 != nullptr)
{
memcpy(&sin6.sin6_addr, ipv6, 16);
}
sin6.sin6_port = htons(ss->udp_port);
public_addr = tr_sessionGetPublicAddress(ss, TR_AF_INET6, &is_default);
if (public_addr != nullptr && !is_default)
{
sin6.sin6_addr = public_addr->addr.addr6;
}
rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
if (rc == -1)
{
goto FAIL;
}
if (ss->udp6_socket == TR_BAD_SOCKET)
{
ss->udp6_socket = s;
}
else
{
/* FIXME: dup2 doesn't work for sockets on Windows */
rc = dup2(s, ss->udp6_socket);
if (rc == -1)
{
goto FAIL;
}
tr_netCloseSocket(s);
}
if (ss->udp6_bound == nullptr)
{
ss->udp6_bound = static_cast<unsigned char*>(malloc(16));
}
if (ss->udp6_bound != nullptr)
{
memcpy(ss->udp6_bound, ipv6, 16);
}
return;
FAIL:
/* Something went wrong. It's difficult to recover, so let's simply
set things up so that we try again next time. */
tr_logAddNamedError("UDP", "Couldn't rebind IPv6 socket");
if (s != TR_BAD_SOCKET)
{
tr_netCloseSocket(s);
}
if (ss->udp6_bound != nullptr)
{
free(ss->udp6_bound);
ss->udp6_bound = nullptr;
}
}
static void event_callback(evutil_socket_t s, [[maybe_unused]] short type, void* vsession)
{
TR_ASSERT(tr_isSession(static_cast<tr_session*>(vsession)));
TR_ASSERT(type == EV_READ);
unsigned char buf[4096];
struct sockaddr_storage from;
auto* session = static_cast<tr_session*>(vsession);
socklen_t fromlen = sizeof(from);
int rc = recvfrom(s, reinterpret_cast<char*>(buf), 4096 - 1, 0, (struct sockaddr*)&from, &fromlen);
/* Since most packets we receive here are µTP, make quick inline
checks for the other protocols. The logic is as follows:
- all DHT packets start with 'd'
- all UDP tracker packets start with a 32-bit (!) "action", which
is between 0 and 3
- the above cannot be µTP packets, since these start with a 4-bit
version number (1). */
if (rc > 0)
{
if (buf[0] == 'd')
{
if (tr_sessionAllowsDHT(session))
{
buf[rc] = '\0'; /* required by the DHT code */
tr_dhtCallback(buf, rc, (struct sockaddr*)&from, fromlen, vsession);
}
}
else if (rc >= 8 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] <= 3)
{
rc = tau_handle_message(session, buf, rc);
if (rc == 0)
{
tr_logAddNamedDbg("UDP", "Couldn't parse UDP tracker packet.");
}
}
else
{
if (tr_sessionIsUTPEnabled(session))
{
rc = tr_utpPacket(buf, rc, (struct sockaddr*)&from, fromlen, session);
if (rc == 0)
{
tr_logAddNamedDbg("UDP", "Unexpected UDP packet");
}
}
}
}
}
void tr_udpInit(tr_session* ss)
{
TR_ASSERT(ss->udp_socket == TR_BAD_SOCKET);
TR_ASSERT(ss->udp6_socket == TR_BAD_SOCKET);
ss->udp_port = tr_sessionGetPeerPort(ss);
if (ss->udp_port <= 0)
{
return;
}
ss->udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
if (ss->udp_socket == TR_BAD_SOCKET)
{
tr_logAddNamedError("UDP", "Couldn't create IPv4 socket");
}
else
{
auto is_default = bool{};
tr_address const* public_addr = tr_sessionGetPublicAddress(ss, TR_AF_INET, &is_default);
auto sin = sockaddr_in{};
sin.sin_family = AF_INET;
if (public_addr != nullptr && !is_default)
{
memcpy(&sin.sin_addr, &public_addr->addr.addr4, sizeof(struct in_addr));
}
sin.sin_port = htons(ss->udp_port);
int const rc = bind(ss->udp_socket, (struct sockaddr*)&sin, sizeof(sin));
if (rc == -1)
{
tr_logAddNamedError("UDP", "Couldn't bind IPv4 socket");
tr_netCloseSocket(ss->udp_socket);
ss->udp_socket = TR_BAD_SOCKET;
}
else
{
ss->udp_event = event_new(ss->event_base, ss->udp_socket, EV_READ | EV_PERSIST, event_callback, ss);
if (ss->udp_event == nullptr)
{
tr_logAddNamedError("UDP", "Couldn't allocate IPv4 event");
}
}
}
// IPV6
if (tr_globalIPv6() != nullptr)
{
rebind_ipv6(ss, true);
}
if (ss->udp6_socket != TR_BAD_SOCKET)
{
ss->udp6_event = event_new(ss->event_base, ss->udp6_socket, EV_READ | EV_PERSIST, event_callback, ss);
if (ss->udp6_event == nullptr)
{
tr_logAddNamedError("UDP", "Couldn't allocate IPv6 event");
}
}
tr_udpSetSocketBuffers(ss);
tr_udpSetSocketTOS(ss);
if (ss->isDHTEnabled)
{
tr_dhtInit(ss);
}
if (ss->udp_event != nullptr)
{
event_add(ss->udp_event, nullptr);
}
if (ss->udp6_event != nullptr)
{
event_add(ss->udp6_event, nullptr);
}
}
void tr_udpUninit(tr_session* ss)
{
tr_dhtUninit(ss);
if (ss->udp_socket != TR_BAD_SOCKET)
{
tr_netCloseSocket(ss->udp_socket);
ss->udp_socket = TR_BAD_SOCKET;
}
if (ss->udp_event != nullptr)
{
event_free(ss->udp_event);
ss->udp_event = nullptr;
}
if (ss->udp6_socket != TR_BAD_SOCKET)
{
tr_netCloseSocket(ss->udp6_socket);
ss->udp6_socket = TR_BAD_SOCKET;
}
if (ss->udp6_event != nullptr)
{
event_free(ss->udp6_event);
ss->udp6_event = nullptr;
}
if (ss->udp6_bound != nullptr)
{
free(ss->udp6_bound);
ss->udp6_bound = nullptr;
}
}