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-02-18 00:23:47 +00:00
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
#include <cstdint>
|
2022-08-11 17:28:37 +00:00
|
|
|
#include <chrono>
|
2011-03-24 21:49:42 +00:00
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2012-05-30 17:47:29 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "transmission.h"
|
2022-03-17 00:23:44 +00:00
|
|
|
|
2022-10-01 14:12:49 +00:00
|
|
|
#include "crypto-utils.h" // tr_rand_int_weak()
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "net.h"
|
2022-07-08 15:13:22 +00:00
|
|
|
#include "peer-io.h"
|
2011-02-18 00:23:58 +00:00
|
|
|
#include "peer-mgr.h"
|
2017-06-28 15:46:06 +00:00
|
|
|
#include "peer-socket.h"
|
2022-03-17 00:23:44 +00:00
|
|
|
#include "session.h"
|
2022-10-01 14:12:49 +00:00
|
|
|
#include "timer.h"
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "tr-utp.h"
|
2011-02-18 01:06:42 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
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
|
|
|
|
|
2011-02-18 00:24:04 +00:00
|
|
|
/* Greg says 50ms works for them. */
|
2022-08-11 17:28:37 +00:00
|
|
|
static auto constexpr UtpInterval = 50ms;
|
2011-02-18 00:24:04 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
static void utp_on_accept(tr_session* const session, UTPSocket* const s)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2022-09-23 05:51:15 +00:00
|
|
|
auto from_storage = sockaddr_storage{};
|
2022-07-09 23:44:20 +00:00
|
|
|
auto* const from = (struct sockaddr*)&from_storage;
|
|
|
|
socklen_t fromlen = sizeof(from_storage);
|
2022-09-23 05:51:15 +00:00
|
|
|
auto addr = tr_address{};
|
|
|
|
auto port = tr_port{};
|
2022-07-09 23:44:20 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
if (!session->allowsUTP())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-08 15:13:22 +00:00
|
|
|
utp_close(s);
|
2011-02-18 00:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
utp_getpeername(s, from, &fromlen);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-09 23:44:20 +00:00
|
|
|
if (!tr_address_from_sockaddr_storage(&addr, &port, &from_storage))
|
2011-03-04 21:00:52 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(_("Unknown socket family"));
|
2022-07-08 15:13:22 +00:00
|
|
|
utp_close(s);
|
2022-07-09 23:44:20 +00:00
|
|
|
return;
|
2011-02-18 00:23:58 +00:00
|
|
|
}
|
2022-07-09 23:44:20 +00:00
|
|
|
|
2022-10-14 02:25:02 +00:00
|
|
|
session->addIncoming(addr, port, tr_peer_socket_utp_create(s));
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
#ifdef TR_UTP_TRACE
|
|
|
|
|
|
|
|
static void utp_log(tr_session* const /*session*/, char const* const msg)
|
|
|
|
{
|
2022-10-11 15:39:41 +00:00
|
|
|
fmt::print(stderr, FMT_STRING("[µTP] {}\n"), msg);
|
2022-07-08 15:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static uint64 utp_callback(utp_callback_arguments* args)
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
utp_log(session, args->buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case UTP_ON_ACCEPT:
|
|
|
|
utp_on_accept(session, args->socket);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UTP_SENDTO:
|
|
|
|
utp_send_to(session, args->buf, args->len, args->u1.address, args->u2.address_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
static void reset_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{};
|
|
|
|
auto const random_percent = tr_rand_int_weak(1000) / 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
|
|
|
|
{
|
2022-10-11 15:39:41 +00:00
|
|
|
/* If somebody has disabled µTP, then we still want to run
|
2022-07-08 15:13:22 +00:00
|
|
|
utp_check_timeouts, in order to let closed sockets finish
|
2011-02-18 00:43:27 +00:00
|
|
|
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
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
session->utp_timer->startSingleShot(interval);
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
static 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_internal.cpp says "Should be called each time the UDP socket is drained" but it's tricky with libevent */
|
|
|
|
utp_issue_deferred_acks(session->utp_context);
|
|
|
|
|
|
|
|
utp_check_timeouts(session->utp_context);
|
2021-09-12 17:41:49 +00:00
|
|
|
reset_timer(session);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
void tr_utpInit(tr_session* session)
|
|
|
|
{
|
|
|
|
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-08-29 20:58:18 +00:00
|
|
|
tr_peerIo::utpInit(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;
|
|
|
|
}
|
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
bool tr_utpPacket(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-08-14 01:04:36 +00:00
|
|
|
if (!ss->isClosed() && !ss->utp_timer)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
ss->utp_timer = ss->timerMaker().create(timer_callback, ss);
|
2017-04-19 12:04:45 +00:00
|
|
|
reset_timer(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);
|
|
|
|
|
|
|
|
/* utp_internal.cpp says "Should be called each time the UDP socket is drained" but it's tricky with libevent */
|
|
|
|
utp_issue_deferred_acks(ss->utp_context);
|
|
|
|
|
|
|
|
return ret != 0;
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 10:31:33 +00:00
|
|
|
void tr_utpClose(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 */
|