2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Juliusz Chroboczek.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2011-01-09 21:48:33 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
#include <array>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cerrno>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <cstddef>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <netinet/in.h> // IPV6_V6ONLY, IPPROTO_IPV6
|
|
|
|
#include <sys/socket.h> // setsockopt, SOL_SOCKET, bind
|
|
|
|
#endif
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2011-01-09 21:48:51 +00:00
|
|
|
#include <event2/event.h>
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <fmt/core.h>
|
2012-05-30 17:47:29 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/session.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-utp.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2011-01-09 21:48:06 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
namespace
|
|
|
|
{
|
2024-03-25 01:48:23 +00:00
|
|
|
|
|
|
|
// Since we use a single UDP socket in order to implement multiple
|
|
|
|
// µTP sockets, try to set up huge buffers.
|
2023-01-07 14:27:54 +00:00
|
|
|
void set_socket_buffers(tr_socket_t fd, bool large)
|
2011-02-18 00:43:47 +00:00
|
|
|
{
|
2023-01-07 14:27:54 +00:00
|
|
|
static auto constexpr RecvBufferSize = 4 * 1024 * 1024;
|
|
|
|
static auto constexpr SendBufferSize = 1 * 1024 * 1024;
|
|
|
|
static auto constexpr SmallBufferSize = 32 * 1024;
|
|
|
|
|
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);
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2022-06-01 16:56:59 +00:00
|
|
|
int size = large ? RecvBufferSize : SmallBufferSize;
|
2024-03-25 01:48:23 +00:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&size), sizeof(size)) < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format("Couldn't set receive buffer: {}", tr_net_strerror(sockerrno)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2022-06-01 16:56:59 +00:00
|
|
|
size = large ? SendBufferSize : SmallBufferSize;
|
2024-03-25 01:48:23 +00:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char const*>(&size), sizeof(size)) < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format("Couldn't set send buffer: {}", tr_net_strerror(sockerrno)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (large)
|
|
|
|
{
|
2024-03-25 01:48:23 +00:00
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>(&rbuf), &rbuf_len) < 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
|
|
|
}
|
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&sbuf), &sbuf_len) < 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
|
|
|
|
2022-06-01 16:56:59 +00:00
|
|
|
if (rbuf < RecvBufferSize)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-01 16:56:59 +00:00
|
|
|
tr_logAddDebug(fmt::format("Couldn't set receive buffer: requested {}, got {}", RecvBufferSize, rbuf));
|
2011-02-18 00:43:47 +00:00
|
|
|
#ifdef __linux__
|
2022-06-01 16:56:59 +00:00
|
|
|
tr_logAddDebug(fmt::format("Please add the line 'net.core.rmem_max = {}' to /etc/sysctl.conf", RecvBufferSize));
|
2011-02-18 00:43:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:56:59 +00:00
|
|
|
if (sbuf < SendBufferSize)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-01 16:56:59 +00:00
|
|
|
tr_logAddDebug(fmt::format("Couldn't set send buffer: requested {}, got {}", SendBufferSize, sbuf));
|
2011-02-18 00:43:47 +00:00
|
|
|
#ifdef __linux__
|
2022-06-01 16:56:59 +00:00
|
|
|
tr_logAddDebug(fmt::format("Please add the line 'net.core.wmem_max = {}' to /etc/sysctl.conf", SendBufferSize));
|
2011-02-18 00:43:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
void event_callback(evutil_socket_t s, [[maybe_unused]] short type, void* vsession)
|
2011-01-09 21:48:46 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(vsession != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(type == EV_READ);
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
auto buf = std::array<unsigned char, 8192>{};
|
2022-09-23 05:51:15 +00:00
|
|
|
auto from = sockaddr_storage{};
|
2022-11-11 16:09:24 +00:00
|
|
|
auto fromlen = socklen_t{ sizeof(from) };
|
2023-01-01 23:16:13 +00:00
|
|
|
auto* const from_sa = reinterpret_cast<sockaddr*>(&from);
|
2024-03-25 01:48:23 +00:00
|
|
|
auto* const session = static_cast<tr_session*>(vsession);
|
|
|
|
auto got_utp_packet = false;
|
2023-01-01 23:16:13 +00:00
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
for (;;)
|
2023-01-01 23:16:13 +00:00
|
|
|
{
|
2024-03-25 01:48:23 +00:00
|
|
|
auto const n_read = recvfrom(s, reinterpret_cast<char*>(std::data(buf)), std::size(buf) - 1, 0, from_sa, &fromlen);
|
|
|
|
if (n_read <= 0)
|
|
|
|
{
|
|
|
|
if (got_utp_packet)
|
|
|
|
{
|
|
|
|
// To reduce protocol overhead, we wait until we've read all UDP packets
|
|
|
|
// we can, then send one ACK for each µTP socket that received packet(s).
|
|
|
|
tr_utp_issue_deferred_acks(session);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-04-24 19:12:28 +00:00
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
// 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' (100)
|
|
|
|
// - 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
|
|
|
|
// "type" between 0 and 4, followed by a 4-bit version number (1)
|
|
|
|
if (buf[0] == 'd')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-25 01:48:23 +00:00
|
|
|
if (session->dht_)
|
|
|
|
{
|
|
|
|
buf[n_read] = '\0'; // libdht requires zero-terminated messages
|
|
|
|
session->dht_->handle_message(std::data(buf), n_read, from_sa, fromlen);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2024-03-25 01:48:23 +00:00
|
|
|
else if (n_read >= 8 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] <= 3)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-05-26 20:43:55 +00:00
|
|
|
if (!session->announcer_udp_->handle_message(std::data(buf), n_read, from_sa, fromlen))
|
2024-03-25 01:48:23 +00:00
|
|
|
{
|
|
|
|
tr_logAddTrace("Couldn't parse UDP tracker packet.");
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2024-03-25 01:48:23 +00:00
|
|
|
else if (session->allowsUTP() && session->utp_context != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-25 01:48:23 +00:00
|
|
|
if (tr_utp_packet(std::data(buf), n_read, from_sa, fromlen, session))
|
|
|
|
{
|
|
|
|
got_utp_packet = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_logAddTrace("Unexpected UDP packet");
|
|
|
|
}
|
2011-03-04 21:38:04 +00:00
|
|
|
}
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
}
|
2023-01-07 14:27:54 +00:00
|
|
|
} // namespace
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2022-12-28 08:03:35 +00:00
|
|
|
// BEP-32 explains why we need to bind to one IPv6 address
|
|
|
|
|
2022-10-15 13:22:43 +00:00
|
|
|
tr_session::tr_udp_core::tr_udp_core(tr_session& session, tr_port udp_port)
|
|
|
|
: udp_port_{ udp_port }
|
|
|
|
, session_{ session }
|
2011-01-09 21:48:33 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
if (std::empty(udp_port_))
|
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
|
|
|
|
2022-12-28 08:03:35 +00:00
|
|
|
if (auto sock = socket(PF_INET, SOCK_DGRAM, 0); sock != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-26 02:10:06 +00:00
|
|
|
(void)evutil_make_listen_socket_reuseable(sock);
|
2023-01-02 02:36:20 +00:00
|
|
|
|
2023-06-20 15:51:07 +00:00
|
|
|
auto const addr = session_.bind_address(TR_AF_INET);
|
2023-08-18 03:13:01 +00:00
|
|
|
auto const [ss, sslen] = tr_socket_address::to_sockaddr(addr, udp_port_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
if (evutil_make_socket_nonblocking(sock) != 0)
|
|
|
|
{
|
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't make IPv4 socket non-blocking {address}: {error} ({error_code})"),
|
|
|
|
fmt::arg("address", tr_socket_address::display_name(addr, udp_port_)),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
|
|
|
|
|
|
|
tr_net_close_socket(sock);
|
|
|
|
}
|
|
|
|
else if (bind(sock, reinterpret_cast<sockaddr const*>(&ss), sslen) != 0)
|
2021-10-23 15:43:15 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("Couldn't bind IPv4 socket {address}: {error} ({error_code})"),
|
2023-08-18 03:13:01 +00:00
|
|
|
fmt::arg("address", tr_socket_address::display_name(addr, udp_port_)),
|
2022-03-17 00:23:44 +00:00
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2022-12-28 08:03:35 +00:00
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
tr_net_close_socket(sock);
|
2021-10-23 15:43:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
tr_logAddInfo(fmt::format("Bound UDP IPv4 address {:s}", tr_socket_address::display_name(addr, udp_port_)));
|
2022-12-28 08:03:35 +00:00
|
|
|
session_.setSocketTOS(sock, TR_AF_INET);
|
|
|
|
set_socket_buffers(sock, session_.allowsUTP());
|
|
|
|
udp4_socket_ = sock;
|
2023-05-06 04:11:05 +00:00
|
|
|
udp4_event_.reset(event_new(session_.event_base(), udp4_socket_, EV_READ | EV_PERSIST, event_callback, &session_));
|
2022-12-28 08:03:35 +00:00
|
|
|
event_add(udp4_event_.get(), nullptr);
|
2021-10-23 15:43:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 03:05:28 +00:00
|
|
|
if (!session.has_ip_protocol(TR_AF_INET6))
|
2023-01-04 03:12:05 +00:00
|
|
|
{
|
|
|
|
// no IPv6; do nothing
|
|
|
|
}
|
|
|
|
else if (auto sock = socket(PF_INET6, SOCK_DGRAM, 0); sock != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-26 02:10:06 +00:00
|
|
|
(void)evutil_make_listen_socket_reuseable(sock);
|
|
|
|
(void)evutil_make_listen_socket_ipv6only(sock);
|
2023-01-02 02:36:20 +00:00
|
|
|
|
2023-06-20 15:51:07 +00:00
|
|
|
auto const addr = session_.bind_address(TR_AF_INET6);
|
2023-08-18 03:13:01 +00:00
|
|
|
auto const [ss, sslen] = tr_socket_address::to_sockaddr(addr, udp_port_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
if (evutil_make_socket_nonblocking(sock) != 0)
|
|
|
|
{
|
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't make IPv6 socket non-blocking {address}: {error} ({error_code})"),
|
|
|
|
fmt::arg("address", tr_socket_address::display_name(addr, udp_port_)),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
|
|
|
|
|
|
|
tr_net_close_socket(sock);
|
|
|
|
}
|
|
|
|
else if (bind(sock, reinterpret_cast<sockaddr const*>(&ss), sslen) != 0)
|
2022-12-28 08:03:35 +00:00
|
|
|
{
|
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't bind IPv6 socket {address}: {error} ({error_code})"),
|
2023-08-18 03:13:01 +00:00
|
|
|
fmt::arg("address", tr_socket_address::display_name(addr, udp_port_)),
|
2022-12-28 08:03:35 +00:00
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
tr_net_close_socket(sock);
|
2022-12-28 08:03:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
tr_logAddInfo(fmt::format("Bound UDP IPv6 address {:s}", tr_socket_address::display_name(addr, udp_port_)));
|
2022-12-28 08:03:35 +00:00
|
|
|
session_.setSocketTOS(sock, TR_AF_INET6);
|
|
|
|
set_socket_buffers(sock, session_.allowsUTP());
|
|
|
|
udp6_socket_ = sock;
|
2023-05-06 04:11:05 +00:00
|
|
|
udp6_event_.reset(event_new(session_.event_base(), udp6_socket_, EV_READ | EV_PERSIST, event_callback, &session_));
|
2022-12-28 08:03:35 +00:00
|
|
|
event_add(udp6_event_.get(), nullptr);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-09 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
tr_session::tr_udp_core::~tr_udp_core()
|
2011-01-09 21:48:33 +00:00
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
udp6_event_.reset();
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
if (udp6_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
tr_net_close_socket(udp6_socket_);
|
2022-09-21 18:25:53 +00:00
|
|
|
udp6_socket_ = TR_BAD_SOCKET;
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 21:11:30 +00:00
|
|
|
udp4_event_.reset();
|
|
|
|
|
2022-12-28 08:03:35 +00:00
|
|
|
if (udp4_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
tr_net_close_socket(udp4_socket_);
|
2022-12-28 08:03:35 +00:00
|
|
|
udp4_socket_ = TR_BAD_SOCKET;
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
2022-09-21 18:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::tr_udp_core::sendto(void const* buf, size_t buflen, struct sockaddr const* to, socklen_t const tolen) const
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
auto const addrport = tr_socket_address::from_sockaddr(to);
|
2023-01-01 23:16:13 +00:00
|
|
|
if (to->sa_family != AF_INET && to->sa_family != AF_INET6)
|
2022-09-21 18:25:53 +00:00
|
|
|
{
|
2023-01-01 23:16:13 +00:00
|
|
|
errno = EAFNOSUPPORT;
|
2022-09-21 18:25:53 +00:00
|
|
|
}
|
2023-01-05 00:18:22 +00:00
|
|
|
else if (auto const sock = to->sa_family == AF_INET ? udp4_socket_ : udp6_socket_; sock == TR_BAD_SOCKET)
|
2022-09-21 18:25:53 +00:00
|
|
|
{
|
2023-01-04 03:12:05 +00:00
|
|
|
// don't warn on bad sockets; the system may not support IPv6
|
|
|
|
return;
|
2022-09-21 18:25:53 +00:00
|
|
|
}
|
2023-05-05 17:17:40 +00:00
|
|
|
else if (
|
2023-07-12 22:29:47 +00:00
|
|
|
addrport && addrport->address().is_global_unicast_address() &&
|
2023-08-18 03:13:01 +00:00
|
|
|
!session_.global_source_address(tr_af_to_ip_protocol(to->sa_family)))
|
2023-05-05 17:17:40 +00:00
|
|
|
{
|
|
|
|
// don't try to connect to a global address if we don't have connectivity to public internet
|
|
|
|
return;
|
|
|
|
}
|
2023-01-01 23:16:13 +00:00
|
|
|
else if (::sendto(sock, static_cast<char const*>(buf), buflen, 0, to, tolen) != -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-01 23:16:13 +00:00
|
|
|
return;
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 23:16:13 +00:00
|
|
|
auto display_name = std::string{};
|
2023-05-05 17:17:40 +00:00
|
|
|
if (addrport)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
display_name = addrport->display_name();
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
2023-01-01 23:16:13 +00:00
|
|
|
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
"Couldn't send to {address}: {errno} ({error})",
|
|
|
|
fmt::arg("address", display_name),
|
|
|
|
fmt::arg("errno", errno),
|
|
|
|
fmt::arg("error", tr_strerror(errno))));
|
2011-01-09 21:48:33 +00:00
|
|
|
}
|