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-02-18 00:23:47 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
#include <chrono>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2011-03-24 21:49:42 +00:00
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
#include <fmt/core.h>
|
2024-01-07 20:21:05 +00:00
|
|
|
#include <fmt/format.h> // fmt::ptr
|
2022-03-17 00:23:44 +00:00
|
|
|
|
2012-05-30 17:47:29 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/crypto-utils.h" // tr_rand_int()
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/peer-io.h"
|
|
|
|
#include "libtransmission/peer-socket.h"
|
|
|
|
#include "libtransmission/session.h"
|
2023-11-21 15:02:03 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/tr-utp.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2011-02-18 01:06:42 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-03-10 05:51:14 +00:00
|
|
|
#ifndef WITH_UTP
|
2011-02-18 00:23:47 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
void utp_close(UTPSocket* socket)
|
2011-02-18 01:06:42 +00:00
|
|
|
{
|
2022-07-08 15:13:22 +00:00
|
|
|
tr_logAddTrace(fmt::format("utp_close({}) was called.", fmt::ptr(socket)));
|
2011-02-18 01:06:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
void utp_read_drained(UTPSocket* socket)
|
2011-02-18 01:18:51 +00:00
|
|
|
{
|
2022-07-08 15:13:22 +00:00
|
|
|
tr_logAddTrace(fmt::format("utp_read_drained({}) was called.", fmt::ptr(socket)));
|
2011-02-18 01:18:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
ssize_t utp_write(UTPSocket* socket, void* buf, size_t count)
|
|
|
|
{
|
|
|
|
tr_logAddTrace(fmt::format("utp_write({}, {}, {}) was called.", fmt::ptr(socket), fmt::ptr(socket), count));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_utpInit(tr_session* /*session*/)
|
2011-02-18 01:18:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
bool tr_utpPacket(
|
2021-10-24 16:41:54 +00:00
|
|
|
unsigned char const* /*buf*/,
|
|
|
|
size_t /*buflen*/,
|
|
|
|
sockaddr const* /*from*/,
|
|
|
|
socklen_t /*fromlen*/,
|
|
|
|
tr_session* /*ss*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
struct UTPSocket* utp_create_socket(struct_utp_context* /*ctx*/)
|
2011-02-18 16:01:52 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-02-18 16:01:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
int utp_connect(UTPSocket* /*socket*/, sockaddr const* /*to*/, socklen_t /*tolen*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-08 15:13:22 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
void tr_utpClose(tr_session* /*session*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
2011-02-18 00:24:04 +00:00
|
|
|
/* Greg says 50ms works for them. */
|
2023-01-07 18:58:16 +00:00
|
|
|
auto constexpr UtpInterval = 50ms;
|
2011-02-18 00:24:04 +00:00
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
void utp_on_accept(tr_session* const session, UTPSocket* const utp_sock)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2022-09-23 05:51:15 +00:00
|
|
|
auto from_storage = sockaddr_storage{};
|
2023-01-28 21:26:23 +00:00
|
|
|
auto* const from = reinterpret_cast<sockaddr*>(&from_storage);
|
2022-07-09 23:44:20 +00:00
|
|
|
socklen_t fromlen = sizeof(from_storage);
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
if (!session->allowsUTP() || tr_peer_socket::limit_reached(session))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-14 06:20:39 +00:00
|
|
|
utp_close(utp_sock);
|
2011-02-18 00:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2022-10-14 06:20:39 +00:00
|
|
|
utp_getpeername(utp_sock, from, &fromlen);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-08-18 03:13:01 +00:00
|
|
|
if (auto addrport = tr_socket_address::from_sockaddr(reinterpret_cast<struct sockaddr*>(&from_storage)); addrport)
|
2022-10-14 06:20:39 +00:00
|
|
|
{
|
2023-07-05 01:29:12 +00:00
|
|
|
session->addIncoming({ *addrport, utp_sock });
|
2022-10-14 06:20:39 +00:00
|
|
|
}
|
|
|
|
else
|
2011-03-04 21:00:52 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(_("Unknown socket family"));
|
2022-10-14 06:20:39 +00:00
|
|
|
utp_close(utp_sock);
|
2011-02-18 00:23:58 +00:00
|
|
|
}
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
void utp_send_to(
|
2022-09-06 17:52:58 +00:00
|
|
|
tr_session const* const ss,
|
2022-07-08 15:13:22 +00:00
|
|
|
uint8_t const* const buf,
|
|
|
|
size_t const buflen,
|
|
|
|
struct sockaddr const* const to,
|
|
|
|
socklen_t const tolen)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2022-09-21 18:25:53 +00:00
|
|
|
ss->udp_core_->sendto(buf, buflen, to, tolen);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
uint64 utp_callback(utp_callback_arguments* args)
|
2022-07-08 15:13:22 +00:00
|
|
|
{
|
|
|
|
auto* const session = static_cast<tr_session*>(utp_context_get_userdata(args->context));
|
|
|
|
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-07-08 15:13:22 +00:00
|
|
|
TR_ASSERT(session->utp_context == args->context);
|
|
|
|
|
|
|
|
switch (args->callback_type)
|
|
|
|
{
|
|
|
|
#ifdef TR_UTP_TRACE
|
|
|
|
case UTP_LOG:
|
2024-01-06 20:05:18 +00:00
|
|
|
fmt::print(stderr, "[µTP] {}\n", args->buf);
|
2022-07-08 15:13:22 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case UTP_ON_ACCEPT:
|
|
|
|
utp_on_accept(session, args->socket);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UTP_SENDTO:
|
2023-02-18 02:59:15 +00:00
|
|
|
utp_send_to(session, args->buf, args->len, args->address, args->address_len);
|
2022-07-08 15:13:22 +00:00
|
|
|
break;
|
2023-11-21 15:02:03 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2022-07-08 15:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-18 16:03:59 +00:00
|
|
|
void restart_timer(tr_session* session)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
auto interval = std::chrono::milliseconds{};
|
2022-12-21 15:58:32 +00:00
|
|
|
auto const random_percent = tr_rand_int(1000U) / 1000.0;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
if (session->allowsUTP())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
static auto constexpr MinInterval = UtpInterval * 0.5;
|
|
|
|
static auto constexpr MaxInterval = UtpInterval * 1.5;
|
|
|
|
auto const target = MinInterval + random_percent * (MaxInterval - MinInterval);
|
|
|
|
interval = std::chrono::duration_cast<std::chrono::milliseconds>(target);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-25 01:48:23 +00:00
|
|
|
// If somebody has disabled µTP, then we still want to run
|
|
|
|
// utp_check_timeouts, in order to let closed sockets finish
|
|
|
|
// gracefully and so on. However, since we're not particularly
|
|
|
|
// interested in that happening in a timely manner, we might as
|
|
|
|
// well use a large timeout.
|
2022-08-11 17:28:37 +00:00
|
|
|
static auto constexpr MinInterval = 2s;
|
|
|
|
static auto constexpr MaxInterval = 3s;
|
|
|
|
auto const target = MinInterval + random_percent * (MaxInterval - MinInterval);
|
|
|
|
interval = std::chrono::duration_cast<std::chrono::milliseconds>(target);
|
2011-02-18 00:43:27 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
session->utp_timer->start_single_shot(interval);
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
void timer_callback(void* vsession)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2022-07-08 15:13:22 +00:00
|
|
|
|
|
|
|
utp_check_timeouts(session->utp_context);
|
2023-02-18 16:03:59 +00:00
|
|
|
restart_timer(session);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
} // namespace
|
2011-02-18 00:23:47 +00:00
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
void tr_utp_init(tr_session* session)
|
2022-07-08 15:13:22 +00:00
|
|
|
{
|
|
|
|
if (session->utp_context != nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* const ctx = utp_init(2);
|
|
|
|
|
|
|
|
if (ctx == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
utp_context_set_userdata(ctx, session);
|
|
|
|
utp_set_callback(ctx, UTP_ON_ACCEPT, &utp_callback);
|
|
|
|
utp_set_callback(ctx, UTP_SENDTO, &utp_callback);
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_peerIo::utp_init(ctx);
|
2022-07-08 15:13:22 +00:00
|
|
|
|
|
|
|
#ifdef TR_UTP_TRACE
|
|
|
|
utp_set_callback(ctx, UTP_LOG, &utp_callback);
|
|
|
|
utp_context_set_option(ctx, UTP_LOG_NORMAL, 1);
|
|
|
|
utp_context_set_option(ctx, UTP_LOG_MTU, 1);
|
|
|
|
utp_context_set_option(ctx, UTP_LOG_DEBUG, 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
session->utp_context = ctx;
|
2023-02-18 16:03:59 +00:00
|
|
|
session->utp_timer = session->timerMaker().create(timer_callback, session);
|
|
|
|
restart_timer(session);
|
2022-07-08 15:13:22 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
bool tr_utp_packet(unsigned char const* buf, size_t buflen, struct sockaddr const* from, socklen_t fromlen, tr_session* ss)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2022-07-08 15:13:22 +00:00
|
|
|
auto const ret = utp_process_udp(ss->utp_context, buf, buflen, from, fromlen);
|
|
|
|
|
|
|
|
return ret != 0;
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 01:48:23 +00:00
|
|
|
void tr_utp_issue_deferred_acks(tr_session* ss)
|
|
|
|
{
|
|
|
|
utp_issue_deferred_acks(ss->utp_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_utp_close(tr_session* session)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
session->utp_timer.reset();
|
2022-07-08 15:13:22 +00:00
|
|
|
|
|
|
|
if (session->utp_context != nullptr)
|
|
|
|
{
|
|
|
|
utp_context_set_userdata(session->utp_context, nullptr);
|
|
|
|
utp_destroy(session->utp_context);
|
|
|
|
session->utp_context = nullptr;
|
|
|
|
}
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
|
|
|
#endif /* #ifndef WITH_UTP ... else */
|