2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010 Juliusz Chroboczek.
|
|
|
|
// 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>
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <cstdint>
|
2022-09-29 14:12:33 +00:00
|
|
|
#include <cstring> /* memcmp(), memset() */
|
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
|
|
|
|
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-02-18 00:23:47 +00:00
|
|
|
#include "tr-utp.h"
|
2022-03-15 14:52:16 +00:00
|
|
|
#include "utils.h"
|
2011-01-09 21:48:06 +00:00
|
|
|
|
2011-02-18 00:43:47 +00:00
|
|
|
/* Since we use a single UDP socket in order to implement multiple
|
2022-10-11 15:39:41 +00:00
|
|
|
µTP sockets, try to set up huge buffers. */
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2022-06-01 16:56:59 +00:00
|
|
|
static auto constexpr RecvBufferSize = 4 * 1024 * 1024;
|
|
|
|
static auto constexpr SendBufferSize = 1 * 1024 * 1024;
|
|
|
|
static auto constexpr SmallBufferSize = 32 * 1024;
|
2011-02-18 00:43:47 +00:00
|
|
|
|
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);
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2022-06-01 16:56:59 +00:00
|
|
|
int size = large ? RecvBufferSize : SmallBufferSize;
|
2021-10-23 15:43:15 +00:00
|
|
|
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
|
|
|
{
|
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;
|
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
|
|
|
{
|
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)
|
|
|
|
{
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
void tr_session::tr_udp_core::set_socket_buffers()
|
2011-02-18 00:43:47 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
bool const utp = session_.allowsUTP();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
if (udp_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
::set_socket_buffers(udp_socket_, utp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-21 18:25:53 +00:00
|
|
|
if (udp6_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
::set_socket_buffers(udp6_socket_, utp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:43:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
static tr_socket_t rebind_ipv6_impl(in6_addr sin6_addr, tr_port port)
|
|
|
|
{
|
|
|
|
auto const sock = socket(PF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if (sock == TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
return TR_BAD_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
/* Since we always open an IPv4 socket on the same port,
|
|
|
|
* this shouldn't matter. But I'm superstitious. */
|
|
|
|
int one = 1;
|
|
|
|
(void)setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char const*>(&one), sizeof(one));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto sin6 = sockaddr_in6{};
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_addr = sin6_addr;
|
|
|
|
sin6.sin6_port = port.network();
|
|
|
|
if (::bind(sock, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1)
|
|
|
|
{
|
|
|
|
tr_netCloseSocket(sock);
|
|
|
|
return TR_BAD_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
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. */
|
2022-09-21 18:25:53 +00:00
|
|
|
void tr_session::tr_udp_core::rebind_ipv6(bool force)
|
2011-01-09 21:48:36 +00:00
|
|
|
{
|
2022-09-22 04:59:31 +00:00
|
|
|
auto const ipv6 = tr_globalIPv6(&session_);
|
2011-01-09 21:48:36 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2022-09-22 04:59:31 +00:00
|
|
|
if (!ipv6 || (!force && udp6_socket_ == TR_BAD_SOCKET))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-22 04:59:31 +00:00
|
|
|
udp6_bound_.reset();
|
2011-01-09 21:48:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-22 04:59:31 +00:00
|
|
|
if (udp6_bound_ && memcmp(&*udp6_bound_, &*ipv6, sizeof(*ipv6)) == 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
|
|
|
}
|
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
auto const sock = rebind_ipv6_impl(*ipv6, udp_port_);
|
|
|
|
if (sock == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
/* Something went wrong. It's difficult to recover, so let's
|
|
|
|
* simply set things up so that we try again next time. */
|
|
|
|
auto const error_code = errno;
|
|
|
|
auto ipv6_readable = std::array<char, INET6_ADDRSTRLEN>{};
|
|
|
|
evutil_inet_ntop(AF_INET6, &*ipv6, std::data(ipv6_readable), std::size(ipv6_readable));
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't rebind IPv6 socket {address}: {error} ({error_code})"),
|
|
|
|
fmt::arg("address", std::data(ipv6_readable)),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
udp6_bound_.reset();
|
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
if (udp6_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
tr_netCloseSocket(udp6_socket_);
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
udp6_socket_ = sock;
|
2022-09-22 04:59:31 +00:00
|
|
|
udp6_bound_ = ipv6;
|
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
|
|
|
{
|
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) };
|
|
|
|
auto const rc = recvfrom(
|
|
|
|
s,
|
|
|
|
reinterpret_cast<char*>(std::data(buf)),
|
|
|
|
std::size(buf) - 1,
|
|
|
|
0,
|
|
|
|
reinterpret_cast<sockaddr*>(&from),
|
|
|
|
&fromlen);
|
2011-04-24 19:12:28 +00:00
|
|
|
|
2022-10-11 15:39:41 +00:00
|
|
|
/* Since most packets we receive here are µTP, make quick inline
|
2022-03-15 14:52:16 +00:00
|
|
|
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
|
2022-10-11 15:39:41 +00:00
|
|
|
- the above cannot be µTP packets, since these start with a 4-bit
|
2011-04-28 02:51:07 +00:00
|
|
|
version number (1). */
|
2022-11-11 16:09:24 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (rc > 0)
|
|
|
|
{
|
|
|
|
if (buf[0] == 'd')
|
|
|
|
{
|
2022-11-11 16:09:24 +00:00
|
|
|
if (session->dht_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-11 16:09:24 +00:00
|
|
|
buf[rc] = '\0'; // libdht requires zero-terminated messages
|
|
|
|
session->dht_->handleMessage(std::data(buf), rc, reinterpret_cast<sockaddr*>(&from), fromlen);
|
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)
|
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
if (!session->announcer_udp_->handleMessage(std::data(buf), rc))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Couldn't parse UDP tracker packet.");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
if (session->allowsUTP() && (session->utp_context != nullptr))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
if (!tr_utpPacket(std::data(buf), rc, (struct sockaddr*)&from, fromlen, session))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Unexpected UDP packet");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
|
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-09-21 18:25:53 +00:00
|
|
|
udp_socket_ = socket(PF_INET, SOCK_DGRAM, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
if (udp_socket_ == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(_("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
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
auto const [public_addr, is_default] = session_.publicAddress(TR_AF_INET);
|
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;
|
2022-08-14 19:41:57 +00:00
|
|
|
if (!is_default)
|
2021-10-23 15:43:15 +00:00
|
|
|
{
|
2022-09-29 14:12:33 +00:00
|
|
|
sin.sin_addr = public_addr.addr.addr4;
|
2021-10-23 15:43:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
sin.sin_port = udp_port_.network();
|
|
|
|
int const rc = bind(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)
|
|
|
|
{
|
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})"),
|
2022-12-08 22:44:19 +00:00
|
|
|
fmt::arg("address", public_addr.display_name(udp_port_)),
|
2022-03-17 00:23:44 +00:00
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2022-09-21 18:25:53 +00:00
|
|
|
tr_netCloseSocket(udp_socket_);
|
|
|
|
udp_socket_ = TR_BAD_SOCKET;
|
2021-10-23 15:43:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
udp4_event_.reset(event_new(session_.eventBase(), udp_socket_, EV_READ | EV_PERSIST, event_callback, &session_));
|
2021-10-23 15:43:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 15:43:15 +00:00
|
|
|
// IPV6
|
|
|
|
|
2022-09-22 04:59:31 +00:00
|
|
|
if (tr_globalIPv6(nullptr).has_value())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
rebind_ipv6(true);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
if (udp6_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
udp6_event_.reset(event_new(session_.eventBase(), udp6_socket_, EV_READ | EV_PERSIST, event_callback, &session_));
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
2011-01-09 21:48:36 +00:00
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
set_socket_buffers();
|
|
|
|
set_socket_tos();
|
2011-02-18 00:43:47 +00:00
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
if (udp4_event_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
event_add(udp4_event_.get(), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-11-06 21:11:30 +00:00
|
|
|
if (udp6_event_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 21:11:30 +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
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
tr_netCloseSocket(udp6_socket_);
|
|
|
|
udp6_socket_ = TR_BAD_SOCKET;
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 21:11:30 +00:00
|
|
|
udp4_event_.reset();
|
|
|
|
|
|
|
|
if (udp_socket_ != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
tr_netCloseSocket(udp_socket_);
|
|
|
|
udp_socket_ = TR_BAD_SOCKET;
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 04:59:31 +00:00
|
|
|
udp6_bound_.reset();
|
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
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
std::array<char, std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1> peer = {};
|
|
|
|
|
|
|
|
if (to->sa_family == AF_INET)
|
|
|
|
{
|
|
|
|
if (udp_socket_ != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
if (::sendto(udp_socket_, static_cast<char const*>(buf), buflen, 0, to, tolen) == -1)
|
2022-09-22 04:59:31 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
error = -1;
|
2022-09-22 04:59:31 +00:00
|
|
|
}
|
2022-09-21 18:25:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = -1;
|
|
|
|
errno = EBADF;
|
|
|
|
}
|
|
|
|
if (error == -1)
|
|
|
|
{
|
|
|
|
evutil_inet_ntop(
|
|
|
|
AF_INET,
|
|
|
|
&((reinterpret_cast<struct sockaddr_in const*>(to))->sin_addr),
|
|
|
|
std::data(peer),
|
|
|
|
std::size(peer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (to->sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
if (udp6_socket_ != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
if (::sendto(udp6_socket_, static_cast<char const*>(buf), buflen, 0, to, tolen) == -1)
|
2022-09-22 04:59:31 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
error = -1;
|
2022-09-22 04:59:31 +00:00
|
|
|
}
|
2022-09-21 18:25:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = -1;
|
|
|
|
errno = EBADF;
|
|
|
|
}
|
|
|
|
if (error == -1)
|
|
|
|
{
|
|
|
|
evutil_inet_ntop(
|
|
|
|
AF_INET6,
|
|
|
|
&((reinterpret_cast<struct sockaddr_in6 const*>(to))->sin6_addr),
|
|
|
|
std::data(peer),
|
|
|
|
std::size(peer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
error = -1;
|
|
|
|
errno = EAFNOSUPPORT;
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 18:25:53 +00:00
|
|
|
if (error == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
"Couldn't send to {address}: {errno} ({error})",
|
|
|
|
fmt::arg("address", std::data(peer)),
|
|
|
|
fmt::arg("errno", errno),
|
|
|
|
fmt::arg("error", tr_strerror(errno))));
|
2011-01-09 21:48:36 +00:00
|
|
|
}
|
2011-01-09 21:48:33 +00:00
|
|
|
}
|