2011-01-09 21:48:33 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstring> /* memcmp(), memcpy(), memset() */
|
|
|
|
#include <cstdlib> /* malloc(), free() */
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <io.h> /* dup2() */
|
2014-12-13 15:22:39 +00:00
|
|
|
#else
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <unistd.h> /* dup2() */
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2011-03-24 21:49:42 +00:00
|
|
|
|
2011-01-09 21:48:51 +00:00
|
|
|
#include <event2/event.h>
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstdint>
|
2012-05-30 17:47:29 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2011-01-09 21:48:33 +00:00
|
|
|
#include "transmission.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2011-01-09 21:48:33 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "session.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2011-01-09 21:48:33 +00:00
|
|
|
#include "tr-dht.h"
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "tr-utp.h"
|
2011-01-09 21:48:06 +00:00
|
|
|
#include "tr-udp.h"
|
|
|
|
|
2011-02-18 00:43:47 +00:00
|
|
|
/* 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)
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
static void set_socket_buffers(tr_socket_t fd, bool large)
|
2011-02-18 00:43:47 +00:00
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
int rbuf = 0;
|
|
|
|
int sbuf = 0;
|
2017-05-01 15:46:41 +00:00
|
|
|
socklen_t rbuf_len = sizeof(rbuf);
|
|
|
|
socklen_t sbuf_len = sizeof(sbuf);
|
2015-07-01 00:54:41 +00:00
|
|
|
char err_buf[512];
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
int size = large ? RECV_BUFFER_SIZE : SMALL_BUFFER_SIZE;
|
|
|
|
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&size), sizeof(size));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (rc < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddNamedError("UDP", "Failed to set receive buffer: %s", tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
|
|
|
|
}
|
2011-02-18 00:43:47 +00:00
|
|
|
|
|
|
|
size = large ? SEND_BUFFER_SIZE : SMALL_BUFFER_SIZE;
|
2021-09-12 17:41:49 +00:00
|
|
|
rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char const*>(&size), sizeof(size));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (rc < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddNamedError("UDP", "Failed to set send buffer: %s", tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (large)
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
rc = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>(&rbuf), &rbuf_len);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
if (rc < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-18 00:43:47 +00:00
|
|
|
rbuf = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
rc = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&sbuf), &sbuf_len);
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (rc < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-18 00:43:47 +00:00
|
|
|
sbuf = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (rbuf < RECV_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UDP", "Failed to set receive buffer: requested %d, got %d", RECV_BUFFER_SIZE, rbuf);
|
2011-02-18 00:43:47 +00:00
|
|
|
#ifdef __linux__
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedInfo("UDP", "Please add the line \"net.core.rmem_max = %d\" to /etc/sysctl.conf", RECV_BUFFER_SIZE);
|
2011-02-18 00:43:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (sbuf < SEND_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UDP", "Failed to set send buffer: requested %d, got %d", SEND_BUFFER_SIZE, sbuf);
|
2011-02-18 00:43:47 +00:00
|
|
|
#ifdef __linux__
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedInfo("UDP", "Please add the line \"net.core.wmem_max = %d\" to /etc/sysctl.conf", SEND_BUFFER_SIZE);
|
2011-02-18 00:43:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_udpSetSocketBuffers(tr_session* session)
|
2011-02-18 00:43:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool utp = tr_sessionIsUTPEnabled(session);
|
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (session->udp_socket != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_socket_buffers(session->udp_socket, utp);
|
|
|
|
}
|
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (session->udp6_socket != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_socket_buffers(session->udp6_socket, utp);
|
|
|
|
}
|
2011-02-18 00:43:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 01:59:31 +00:00
|
|
|
void tr_udpSetSocketTOS(tr_session* session)
|
|
|
|
{
|
2021-11-14 06:41:44 +00:00
|
|
|
auto const tos = session->peerSocketTos();
|
|
|
|
|
|
|
|
if (tos)
|
2021-10-27 01:59:31 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->udp_socket != TR_BAD_SOCKET)
|
|
|
|
{
|
2021-11-14 06:41:44 +00:00
|
|
|
tr_netSetTOS(session->udp_socket, tos, TR_AF_INET);
|
2021-10-27 01:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (session->udp6_socket != TR_BAD_SOCKET)
|
|
|
|
{
|
2021-11-14 06:41:44 +00:00
|
|
|
tr_netSetTOS(session->udp6_socket, tos, TR_AF_INET6);
|
2021-10-27 01:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 21:48:36 +00:00
|
|
|
/* BEP-32 has a rather nice explanation of why we need to bind to one
|
|
|
|
IPv6 address, if I may say so myself. */
|
2021-10-23 15:43:15 +00:00
|
|
|
// TODO: remove goto, it prevents reducing scope of local variables
|
2017-04-19 12:04:45 +00:00
|
|
|
static void rebind_ipv6(tr_session* ss, bool force)
|
2011-01-09 21:48:36 +00:00
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
bool is_default = false;
|
|
|
|
tr_address const* public_addr = nullptr;
|
2011-01-09 21:48:36 +00:00
|
|
|
struct sockaddr_in6 sin6;
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned char const* ipv6 = tr_globalIPv6();
|
2015-03-18 07:34:26 +00:00
|
|
|
tr_socket_t s = TR_BAD_SOCKET;
|
2021-10-23 15:43:15 +00:00
|
|
|
int rc = -1;
|
2011-01-09 21:48:36 +00:00
|
|
|
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. */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ipv6 == nullptr || (!force && ss->udp6_socket == TR_BAD_SOCKET))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_bound != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
free(ss->udp6_bound);
|
2021-09-15 00:18:09 +00:00
|
|
|
ss->udp6_bound = nullptr;
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-01-09 21:48:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_bound != nullptr && memcmp(ipv6, ss->udp6_bound, 16) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-01-09 21:48:36 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = socket(PF_INET6, SOCK_DGRAM, 0);
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (s == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-09 21:48:36 +00:00
|
|
|
|
|
|
|
#ifdef IPV6_V6ONLY
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Since we always open an IPv4 socket on the same port, this
|
|
|
|
shouldn't matter. But I'm superstitious. */
|
2021-09-12 17:41:49 +00:00
|
|
|
(void)setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char const*>(&one), sizeof(one));
|
2011-01-09 21:48:36 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
2011-01-09 21:48:36 +00:00
|
|
|
sin6.sin6_family = AF_INET6;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ipv6 != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
memcpy(&sin6.sin6_addr, ipv6, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
sin6.sin6_port = htons(ss->udp_port);
|
|
|
|
public_addr = tr_sessionGetPublicAddress(ss, TR_AF_INET6, &is_default);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (public_addr != nullptr && !is_default)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-01-22 13:49:15 +00:00
|
|
|
sin6.sin6_addr = public_addr->addr.addr6;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
|
2011-01-22 13:49:15 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (rc == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ss->udp6_socket == TR_BAD_SOCKET)
|
|
|
|
{
|
2011-01-09 21:48:36 +00:00
|
|
|
ss->udp6_socket = s;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-13 15:22:39 +00:00
|
|
|
/* FIXME: dup2 doesn't work for sockets on Windows */
|
2017-04-19 12:04:45 +00:00
|
|
|
rc = dup2(s, ss->udp6_socket);
|
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (rc == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_netCloseSocket(s);
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_bound == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
ss->udp6_bound = static_cast<unsigned char*>(malloc(16));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_bound != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
memcpy(ss->udp6_bound, ipv6, 16);
|
|
|
|
}
|
2011-01-09 21:48:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
FAIL:
|
2011-01-09 21:48:36 +00:00
|
|
|
/* Something went wrong. It's difficult to recover, so let's simply
|
|
|
|
set things up so that we try again next time. */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedError("UDP", "Couldn't rebind IPv6 socket");
|
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (s != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_netCloseSocket(s);
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_bound != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
free(ss->udp6_bound);
|
2021-09-15 00:18:09 +00:00
|
|
|
ss->udp6_bound = nullptr;
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-09 21:48:33 +00:00
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static void event_callback(evutil_socket_t s, [[maybe_unused]] short type, void* vsession)
|
2011-01-09 21:48:46 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
TR_ASSERT(tr_isSession(static_cast<tr_session*>(vsession)));
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(type == EV_READ);
|
|
|
|
|
2011-04-27 05:03:10 +00:00
|
|
|
unsigned char buf[4096];
|
2011-01-09 21:48:46 +00:00
|
|
|
struct sockaddr_storage from;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
socklen_t fromlen = sizeof(from);
|
|
|
|
int rc = recvfrom(s, reinterpret_cast<char*>(buf), 4096 - 1, 0, (struct sockaddr*)&from, &fromlen);
|
2011-04-24 19:12:28 +00:00
|
|
|
|
2011-04-28 02:51:07 +00:00
|
|
|
/* Since most packets we receive here are µTP, make quick inline
|
|
|
|
checks for the other protocols. The logic is as follows:
|
2020-11-02 15:16:12 +00:00
|
|
|
- all DHT packets start with 'd'
|
2011-04-28 02:51:07 +00:00
|
|
|
- all UDP tracker packets start with a 32-bit (!) "action", which
|
2020-11-02 15:16:12 +00:00
|
|
|
is between 0 and 3
|
2011-04-28 02:51:07 +00:00
|
|
|
- the above cannot be µTP packets, since these start with a 4-bit
|
|
|
|
version number (1). */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (rc > 0)
|
|
|
|
{
|
|
|
|
if (buf[0] == 'd')
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
if (tr_sessionAllowsDHT(session))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-07-12 12:26:24 +00:00
|
|
|
buf[rc] = '\0'; /* required by the DHT code */
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_dhtCallback(buf, rc, (struct sockaddr*)&from, fromlen, vsession);
|
2011-07-12 12:26:24 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (rc >= 8 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] <= 3)
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
rc = tau_handle_message(session, buf, rc);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (rc == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddNamedDbg("UDP", "Couldn't parse UDP tracker packet.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
if (tr_sessionIsUTPEnabled(session))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
rc = tr_utpPacket(buf, rc, (struct sockaddr*)&from, fromlen, session);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (rc == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddNamedDbg("UDP", "Unexpected UDP packet");
|
|
|
|
}
|
2011-07-12 12:26:24 +00:00
|
|
|
}
|
2011-03-04 21:38:04 +00:00
|
|
|
}
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
}
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_udpInit(tr_session* ss)
|
2011-01-09 21:48:33 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(ss->udp_socket == TR_BAD_SOCKET);
|
|
|
|
TR_ASSERT(ss->udp6_socket == TR_BAD_SOCKET);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ss->udp_port = tr_sessionGetPeerPort(ss);
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ss->udp_port <= 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-01-09 21:48:36 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ss->udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
if (ss->udp_socket == TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UDP", "Couldn't create IPv4 socket");
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
2021-10-23 15:43:15 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
auto is_default = bool{};
|
|
|
|
tr_address const* public_addr = tr_sessionGetPublicAddress(ss, TR_AF_INET, &is_default);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
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));
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
sin.sin_port = htons(ss->udp_port);
|
|
|
|
int const rc = bind(ss->udp_socket, (struct sockaddr*)&sin, sizeof(sin));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
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);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
if (ss->udp_event == nullptr)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UDP", "Couldn't allocate IPv4 event");
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
// IPV6
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tr_globalIPv6() != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_event == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddNamedError("UDP", "Couldn't allocate IPv6 event");
|
|
|
|
}
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_udpSetSocketBuffers(ss);
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2021-10-27 01:59:31 +00:00
|
|
|
tr_udpSetSocketTOS(ss);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ss->isDHTEnabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_dhtInit(ss);
|
|
|
|
}
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp_event != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
event_add(ss->udp_event, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_event != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
event_add(ss->udp6_event, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-09 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_udpUninit(tr_session* ss)
|
2011-01-09 21:48:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_dhtUninit(ss);
|
2011-07-12 12:26:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ss->udp_socket != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
tr_netCloseSocket(ss->udp_socket);
|
2015-03-18 07:34:26 +00:00
|
|
|
ss->udp_socket = TR_BAD_SOCKET;
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp_event != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_free(ss->udp_event);
|
2021-09-15 00:18:09 +00:00
|
|
|
ss->udp_event = nullptr;
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ss->udp6_socket != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
tr_netCloseSocket(ss->udp6_socket);
|
2015-03-18 07:34:26 +00:00
|
|
|
ss->udp6_socket = TR_BAD_SOCKET;
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_event != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_free(ss->udp6_event);
|
2021-09-15 00:18:09 +00:00
|
|
|
ss->udp6_event = nullptr;
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->udp6_bound != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
free(ss->udp6_bound);
|
2021-09-15 00:18:09 +00:00
|
|
|
ss->udp6_bound = nullptr;
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
2011-01-09 21:48:33 +00:00
|
|
|
}
|