2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2010 Juliusz Chroboczek.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <csignal> /* sig_atomic_t */
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib> /* atoi() */
|
|
|
|
#include <cstring> /* memcpy(), memset(), memchr(), strlen() */
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-02-07 04:28:36 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2022-01-31 19:34:04 +00:00
|
|
|
#include <thread>
|
2014-12-13 15:22:39 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#undef gai_strerror
|
|
|
|
#define gai_strerror gai_strerrorA
|
2010-06-22 00:12:52 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <sys/socket.h> /* socket(), bind() */
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h> /* sockaddr_in */
|
2010-06-22 00:12:52 +00:00
|
|
|
#endif
|
2009-05-19 18:38:26 +00:00
|
|
|
|
|
|
|
#include <dht/dht.h>
|
|
|
|
|
2022-03-16 00:51:36 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "transmission.h"
|
2021-12-17 04:51:53 +00:00
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-09-21 18:01:36 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2009-05-20 15:56:58 +00:00
|
|
|
#include "net.h"
|
2021-12-17 04:51:53 +00:00
|
|
|
#include "peer-mgr.h"
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "session.h"
|
2021-12-17 04:51:53 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "tr-dht.h"
|
2021-12-17 04:51:53 +00:00
|
|
|
#include "trevent.h"
|
2009-05-19 18:38:26 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
static struct event* dht_timer = nullptr;
|
2009-05-19 18:38:26 +00:00
|
|
|
static unsigned char myid[20];
|
2021-09-15 00:18:09 +00:00
|
|
|
static tr_session* session_ = nullptr;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void timer_callback(evutil_socket_t s, short type, void* ignore);
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct bootstrap_closure
|
|
|
|
{
|
|
|
|
tr_session* session;
|
|
|
|
uint8_t* nodes;
|
|
|
|
uint8_t* nodes6;
|
2017-05-01 15:46:41 +00:00
|
|
|
size_t len;
|
|
|
|
size_t len6;
|
2009-05-19 18:38:26 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool bootstrap_done(tr_session* session, int af)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (af == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return bootstrap_done(session, AF_INET) && bootstrap_done(session, AF_INET6);
|
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2021-10-26 18:02:07 +00:00
|
|
|
int const status = tr_dhtStatus(session, af, nullptr);
|
2009-11-24 01:59:51 +00:00
|
|
|
return status == TR_DHT_STOPPED || status >= TR_DHT_FIREWALLED;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void nap(int roughly_sec)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const roughly_msec = roughly_sec * 1000;
|
|
|
|
int const msec = roughly_msec / 2 + tr_rand_int_weak(roughly_msec);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_wait_msec(msec);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int bootstrap_af(tr_session* session)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bootstrap_done(session, AF_INET6))
|
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
return AF_INET;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
|
|
|
if (bootstrap_done(session, AF_INET))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
return AF_INET6;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void bootstrap_from_name(char const* name, tr_port port, int af)
|
2009-11-24 01:59:51 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
auto hints = addrinfo{};
|
2009-11-24 01:59:51 +00:00
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_family = af;
|
2021-10-27 00:16:56 +00:00
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
/* No, just passing p + 1 to gai won't work. */
|
2021-10-27 00:16:56 +00:00
|
|
|
char pp[10];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(pp, sizeof(pp), "%d", (int)port);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
addrinfo* info = nullptr;
|
2022-02-08 05:44:31 +00:00
|
|
|
if (int const rc = getaddrinfo(name, pp, &hints, &info); rc != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-22 16:45:56 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't look up '{addresss}:{port}': {error} ({error_code})"),
|
|
|
|
fmt::arg("address", name),
|
|
|
|
fmt::arg("port", port),
|
|
|
|
fmt::arg("error", gai_strerror(rc)),
|
|
|
|
fmt::arg("error_code", rc)));
|
2009-11-24 01:59:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-26 18:02:07 +00:00
|
|
|
addrinfo* infop = info;
|
2021-09-15 00:18:09 +00:00
|
|
|
while (infop != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
dht_ping_node(infop->ai_addr, infop->ai_addrlen);
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
nap(15);
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
if (bootstrap_done(session_, af))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
infop = infop->ai_next;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
freeaddrinfo(info);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
static void dht_boostrap_from_file(tr_session* session)
|
|
|
|
{
|
|
|
|
if (bootstrap_done(session, 0))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for a manual bootstrap file.
|
|
|
|
auto const bootstrap_file = tr_strvPath(session->config_dir, "dht.bootstrap");
|
|
|
|
auto in = std::ifstream{ bootstrap_file };
|
|
|
|
if (!in.is_open())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// format is each line has address, a space char, and port number
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Attempting manual bootstrap");
|
2022-02-07 04:28:36 +00:00
|
|
|
auto line = std::string{};
|
|
|
|
while (!bootstrap_done(session, 0) && std::getline(in, line))
|
|
|
|
{
|
|
|
|
auto line_stream = std::istringstream{ line };
|
|
|
|
auto addrstr = std::string{};
|
|
|
|
auto port = tr_port{};
|
|
|
|
line_stream >> addrstr >> port;
|
|
|
|
|
|
|
|
if (line_stream.bad() || std::empty(addrstr) || port <= 0)
|
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(fmt::format(_("Couldn't parse line: '{line}'"), fmt::arg("line", line)));
|
2022-02-07 04:28:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bootstrap_from_name(addrstr.c_str(), port, bootstrap_af(session_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void dht_bootstrap(void* closure)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
auto* const cl = static_cast<struct bootstrap_closure*>(closure);
|
|
|
|
int const num = cl->len / 6;
|
|
|
|
int const num6 = cl->len6 / 18;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
if (session_ != cl->session)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-05-19 18:38:26 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cl->len > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format("Bootstrapping from {} IPv4 nodes", num));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cl->len6 > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format("Bootstrapping from {} IPv6 nodes", num6));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
for (int i = 0; i < std::max(num, num6); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (i < num && !bootstrap_done(cl->session, AF_INET))
|
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
auto port = tr_port{};
|
|
|
|
auto addr = tr_address{};
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2009-11-24 01:59:51 +00:00
|
|
|
addr.type = TR_AF_INET;
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(&addr.addr.addr4, &cl->nodes[i * 6], 4);
|
|
|
|
memcpy(&port, &cl->nodes[i * 6 + 4], 2);
|
|
|
|
port = ntohs(port);
|
2022-01-23 22:47:41 +00:00
|
|
|
tr_dhtAddNode(cl->session, &addr, port, true);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (i < num6 && !bootstrap_done(cl->session, AF_INET6))
|
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
auto port = tr_port{};
|
|
|
|
auto addr = tr_address{};
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2009-11-24 01:59:51 +00:00
|
|
|
addr.type = TR_AF_INET6;
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(&addr.addr.addr6, &cl->nodes6[i * 18], 16);
|
|
|
|
memcpy(&port, &cl->nodes6[i * 18 + 16], 2);
|
|
|
|
port = ntohs(port);
|
2022-01-23 22:47:41 +00:00
|
|
|
tr_dhtAddNode(cl->session, &addr, port, true);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Our DHT code is able to take up to 9 nodes in a row without
|
2010-12-27 19:18:17 +00:00
|
|
|
dropping any. After that, it takes some time to split buckets.
|
2009-11-24 01:59:51 +00:00
|
|
|
So ping the first 8 nodes quickly, then slow down. */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i < 8)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
nap(2);
|
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
nap(15);
|
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
if (bootstrap_done(session_, 0))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-05-19 18:38:26 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!bootstrap_done(cl->session, 0))
|
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
dht_boostrap_from_file(cl->session);
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!bootstrap_done(cl->session, 0))
|
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < 6; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-15 19:10:56 +00:00
|
|
|
/* We don't want to abuse our bootstrap nodes, so be very
|
|
|
|
slow. The initial wait is to give other nodes a chance
|
|
|
|
to contact us before we attempt to contact a bootstrap
|
|
|
|
node, for example because we've just been restarted. */
|
2017-04-19 12:04:45 +00:00
|
|
|
nap(40);
|
|
|
|
|
|
|
|
if (bootstrap_done(cl->session, 0))
|
|
|
|
{
|
2011-02-15 19:10:56 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (i == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug("Attempting bootstrap from dht.transmissionbt.com");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
bootstrap_from_name("dht.transmissionbt.com", 6881, bootstrap_af(session_));
|
2011-02-15 19:10:56 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cl->nodes != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(cl->nodes);
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cl->nodes6 != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(cl->nodes6);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(closure);
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Finished bootstrapping");
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_dhtInit(tr_session* ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session_ != nullptr) /* already initialized */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-04 23:26:10 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddInfo(_("Initializing DHT"));
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_env_key_exists("TR_DHT_VERBOSE"))
|
|
|
|
{
|
2009-05-20 04:08:58 +00:00
|
|
|
dht_debug = stderr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
auto const dat_file = tr_strvPath(ss->config_dir, "dht.dat"sv);
|
2021-10-27 00:16:56 +00:00
|
|
|
auto benc = tr_variant{};
|
2021-12-25 21:21:13 +00:00
|
|
|
auto const ok = tr_variantFromFile(&benc, TR_VARIANT_PARSE_BENC, dat_file);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
bool have_id = false;
|
|
|
|
uint8_t* nodes = nullptr;
|
|
|
|
uint8_t* nodes6 = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
size_t len6 = 0;
|
2021-11-18 05:37:35 +00:00
|
|
|
if (ok)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 08:59:44 +00:00
|
|
|
auto sv = std::string_view{};
|
|
|
|
have_id = tr_variantDictFindStrView(&benc, TR_KEY_id, &sv);
|
|
|
|
if (have_id && std::size(sv) == 20)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 08:59:44 +00:00
|
|
|
std::copy(std::begin(sv), std::end(sv), myid);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-11-25 08:59:44 +00:00
|
|
|
uint8_t const* raw = nullptr;
|
2017-04-30 16:25:26 +00:00
|
|
|
if (ss->udp_socket != TR_BAD_SOCKET && tr_variantDictFindRaw(&benc, TR_KEY_nodes, &raw, &len) && len % 6 == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
nodes = static_cast<uint8_t*>(tr_memdup(raw, len));
|
2021-10-27 00:16:56 +00:00
|
|
|
if (nodes == nullptr)
|
|
|
|
{
|
|
|
|
len = 0;
|
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (ss->udp6_socket != TR_BAD_SOCKET && tr_variantDictFindRaw(&benc, TR_KEY_nodes6, &raw, &len6) && len6 % 18 == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
nodes6 = static_cast<uint8_t*>(tr_memdup(raw, len6));
|
2021-10-27 00:16:56 +00:00
|
|
|
if (nodes6 == nullptr)
|
|
|
|
{
|
|
|
|
len6 = 0;
|
|
|
|
}
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantFree(&benc);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (have_id)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Reusing old id");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-20 04:08:58 +00:00
|
|
|
/* Note that DHT ids need to be distributed uniformly,
|
|
|
|
* so it should be something truly random. */
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Generating new id");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_rand_buffer(myid, 20);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 01:58:35 +00:00
|
|
|
if (int rc = dht_init(ss->udp_socket, ss->udp6_socket, myid, nullptr); rc < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
tr_free(nodes6);
|
|
|
|
tr_free(nodes);
|
|
|
|
|
2022-03-16 00:51:36 +00:00
|
|
|
auto const errcode = errno;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format("DHT initialization failed: {} ({})", tr_strerror(errcode), errcode));
|
2021-10-27 00:16:56 +00:00
|
|
|
session_ = nullptr;
|
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
session_ = ss;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
auto* const cl = tr_new(struct bootstrap_closure, 1);
|
2020-08-11 18:11:55 +00:00
|
|
|
cl->session = session_;
|
2009-11-24 01:59:51 +00:00
|
|
|
cl->nodes = nodes;
|
|
|
|
cl->nodes6 = nodes6;
|
|
|
|
cl->len = len;
|
|
|
|
cl->len6 = len6;
|
2022-01-31 19:34:04 +00:00
|
|
|
std::thread(dht_bootstrap, cl).detach();
|
2009-05-20 04:08:58 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
dht_timer = evtimer_new(session_->event_base, timer_callback, session_);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_timerAdd(dht_timer, 0, tr_rand_int_weak(1000000));
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug("DHT initialized");
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_dhtUninit(tr_session* ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
if (session_ != ss)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-05-19 18:38:26 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Uninitializing DHT");
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (dht_timer != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_free(dht_timer);
|
2021-09-15 00:18:09 +00:00
|
|
|
dht_timer = nullptr;
|
2011-07-12 12:26:24 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
|
|
|
/* Since we only save known good nodes, avoid erasing older data if we
|
|
|
|
don't know enough nodes. */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tr_dhtStatus(ss, AF_INET, nullptr) < TR_DHT_FIREWALLED && tr_dhtStatus(ss, AF_INET6, nullptr) < TR_DHT_FIREWALLED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Not saving nodes, DHT not ready");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
auto constexpr MaxNodes = size_t{ 300 };
|
|
|
|
auto constexpr PortLen = size_t{ 2 };
|
|
|
|
auto constexpr CompactAddrLen = size_t{ 4 };
|
|
|
|
auto constexpr CompactLen = size_t{ CompactAddrLen + PortLen };
|
|
|
|
auto constexpr Compact6AddrLen = size_t{ 16 };
|
|
|
|
auto constexpr Compact6Len = size_t{ Compact6AddrLen + PortLen };
|
|
|
|
|
|
|
|
struct sockaddr_in sins[MaxNodes];
|
|
|
|
struct sockaddr_in6 sins6[MaxNodes];
|
|
|
|
int num = MaxNodes;
|
|
|
|
int num6 = MaxNodes;
|
2017-04-19 12:04:45 +00:00
|
|
|
int n = dht_get_nodes(sins, &num, sins6, &num6);
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("Saving {} ({} + {}) nodes", n, num, num6));
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
tr_variant benc;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&benc, 3);
|
|
|
|
tr_variantDictAddRaw(&benc, TR_KEY_id, myid, 20);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (num > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
char compact[MaxNodes * CompactLen];
|
2020-11-04 00:59:19 +00:00
|
|
|
char* out = compact;
|
|
|
|
for (struct sockaddr_in const* in = sins; in < sins + num; ++in)
|
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
memcpy(out, &in->sin_addr, CompactAddrLen);
|
|
|
|
out += CompactAddrLen;
|
|
|
|
memcpy(out, &in->sin_port, PortLen);
|
|
|
|
out += PortLen;
|
2020-11-04 00:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictAddRaw(&benc, TR_KEY_nodes, compact, out - compact);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (num6 > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
char compact6[MaxNodes * Compact6Len];
|
2020-11-04 00:59:19 +00:00
|
|
|
char* out6 = compact6;
|
|
|
|
for (struct sockaddr_in6 const* in = sins6; in < sins6 + num6; ++in)
|
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
memcpy(out6, &in->sin6_addr, Compact6AddrLen);
|
|
|
|
out6 += Compact6AddrLen;
|
|
|
|
memcpy(out6, &in->sin6_port, PortLen);
|
|
|
|
out6 += PortLen;
|
2020-11-04 00:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictAddRaw(&benc, TR_KEY_nodes6, compact6, out6 - compact6);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
auto const dat_file = tr_strvPath(ss->config_dir, "dht.dat");
|
2021-12-25 21:21:13 +00:00
|
|
|
tr_variantToFile(&benc, TR_VARIANT_FMT_BENC, dat_file);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&benc);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dht_uninit();
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("Done uninitializing DHT");
|
2009-08-07 14:38:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
session_ = nullptr;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_dhtEnabled(tr_session const* ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return ss != nullptr && ss == session_;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2009-05-19 19:05:34 +00:00
|
|
|
struct getstatus_closure
|
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
int af;
|
2009-05-19 19:05:34 +00:00
|
|
|
sig_atomic_t status;
|
|
|
|
sig_atomic_t count;
|
|
|
|
};
|
|
|
|
|
2022-03-05 04:26:03 +00:00
|
|
|
static void getstatus(getstatus_closure* const closure)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
int good = 0;
|
|
|
|
int dubious = 0;
|
|
|
|
int incoming = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
dht_nodes(closure->af, &good, &dubious, nullptr, &incoming);
|
2009-05-20 04:08:58 +00:00
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->count = good + dubious;
|
2009-05-19 19:05:34 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (good < 4 || good + dubious <= 8)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_BROKEN;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (good < 40)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_POOR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (incoming < 8)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_FIREWALLED;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
closure->status = TR_DHT_GOOD;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_dhtStatus(tr_session* session, int af, int* nodes_return)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto closure = getstatus_closure{ af, -1, -1 };
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_dhtEnabled(session) || (af == AF_INET && session->udp_socket == TR_BAD_SOCKET) ||
|
|
|
|
(af == AF_INET6 && session->udp6_socket == TR_BAD_SOCKET))
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (nodes_return != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-29 18:31:36 +00:00
|
|
|
*nodes_return = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
return TR_DHT_STOPPED;
|
2009-11-29 18:31:36 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread(session, getstatus, &closure);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while (closure.status < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_wait_msec(50 /*msec*/);
|
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (nodes_return != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
*nodes_return = closure.count;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 19:05:34 +00:00
|
|
|
|
2009-11-24 01:59:51 +00:00
|
|
|
return closure.status;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:30:51 +00:00
|
|
|
tr_port tr_dhtPort(tr_session* ss)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_dhtEnabled(ss) ? ss->udp_port : 0;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_dhtAddNode(tr_session* ss, tr_address const* address, tr_port port, bool bootstrap)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
int af = address->type == TR_AF_INET ? AF_INET : AF_INET6;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_dhtEnabled(ss))
|
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-05-20 04:08:58 +00:00
|
|
|
/* Since we don't want to abuse our bootstrap nodes,
|
|
|
|
* we don't ping them if the DHT is in a good state. */
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bootstrap && (tr_dhtStatus(ss, af, nullptr) >= TR_DHT_FIREWALLED))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
return false;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (address->type == TR_AF_INET)
|
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
struct sockaddr_in sin;
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
2009-11-24 01:59:51 +00:00
|
|
|
sin.sin_family = AF_INET;
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(&sin.sin_addr, &address->addr.addr4, 4);
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
dht_ping_node((struct sockaddr*)&sin, sizeof(sin));
|
2015-05-31 22:13:31 +00:00
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
|
|
|
if (address->type == TR_AF_INET6)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-24 01:59:51 +00:00
|
|
|
struct sockaddr_in6 sin6;
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
2009-11-24 01:59:51 +00:00
|
|
|
sin6.sin6_family = AF_INET6;
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(&sin6.sin6_addr, &address->addr.addr6, 16);
|
|
|
|
sin6.sin6_port = htons(port);
|
|
|
|
dht_ping_node((struct sockaddr*)&sin6, sizeof(sin6));
|
2015-05-31 22:13:31 +00:00
|
|
|
return true;
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return false;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_dhtPrintableStatus(int status)
|
2009-08-07 14:38:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case TR_DHT_STOPPED:
|
|
|
|
return "stopped";
|
|
|
|
|
|
|
|
case TR_DHT_BROKEN:
|
|
|
|
return "broken";
|
|
|
|
|
|
|
|
case TR_DHT_POOR:
|
|
|
|
return "poor";
|
|
|
|
|
|
|
|
case TR_DHT_FIREWALLED:
|
|
|
|
return "firewalled";
|
|
|
|
|
|
|
|
case TR_DHT_GOOD:
|
|
|
|
return "good";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "???";
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void callback(void* /*ignore*/, int event, unsigned char const* info_hash, void const* data, size_t data_len)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
auto hash = tr_sha1_digest_t{};
|
|
|
|
std::copy_n(reinterpret_cast<std::byte const*>(info_hash), std::size(hash), std::data(hash));
|
|
|
|
auto const lock = session_->unique_lock();
|
2022-03-01 23:06:29 +00:00
|
|
|
auto* const tor = session_->torrents().get(hash);
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (event == DHT_EVENT_VALUES || event == DHT_EVENT_VALUES6)
|
|
|
|
{
|
2021-12-15 05:48:56 +00:00
|
|
|
if (tor != nullptr && tor->allowsDht())
|
2009-05-20 04:08:58 +00:00
|
|
|
{
|
2022-02-01 03:11:47 +00:00
|
|
|
auto const pex = event == DHT_EVENT_VALUES ? tr_peerMgrCompactToPex(data, data_len, nullptr, 0) :
|
|
|
|
tr_peerMgrCompact6ToPex(data, data_len, nullptr, 0);
|
|
|
|
tr_peerMgrAddPex(tor, TR_PEER_FROM_DHT, std::data(pex), std::size(pex));
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddDebugTor(
|
|
|
|
tor,
|
|
|
|
fmt::format("Learned {} {} peers from DHT", std::size(pex), event == DHT_EVENT_VALUES6 ? "IPv6" : "IPv4"));
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (event == DHT_EVENT_SEARCH_DONE || event == DHT_EVENT_SEARCH_DONE6)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tor != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (event == DHT_EVENT_SEARCH_DONE)
|
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddTraceTor(tor, "IPv4 DHT announce done");
|
2019-07-14 12:40:41 +00:00
|
|
|
tor->dhtAnnounceInProgress = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddTraceTor(tor, "IPv6 DHT announce done");
|
2019-07-14 12:40:41 +00:00
|
|
|
tor->dhtAnnounce6InProgress = false;
|
2009-11-24 01:59:51 +00:00
|
|
|
}
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
enum class AnnounceResult
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
INVALID,
|
|
|
|
OK,
|
|
|
|
FAILED
|
|
|
|
};
|
2009-06-01 18:24:30 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
static AnnounceResult tr_dhtAnnounce(tr_torrent* tor, int af, bool announce)
|
|
|
|
{
|
2021-12-15 05:48:56 +00:00
|
|
|
if (!tor->allowsDht())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
return AnnounceResult::INVALID;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
int numnodes = 0;
|
2021-10-26 18:02:07 +00:00
|
|
|
int const status = tr_dhtStatus(tor->session, af, &numnodes);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (status == TR_DHT_STOPPED)
|
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
// let the caller believe everything is all right.
|
|
|
|
return AnnounceResult::OK;
|
2009-11-29 18:31:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
if (status < TR_DHT_POOR)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddTraceTor(
|
2021-08-15 09:41:48 +00:00
|
|
|
tor,
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::format(
|
|
|
|
"{} DHT not ready ({}, {} nodes)",
|
|
|
|
af == AF_INET6 ? "IPv6" : "IPv4",
|
|
|
|
tr_dhtPrintableStatus(status),
|
|
|
|
numnodes));
|
2021-10-27 00:16:56 +00:00
|
|
|
return AnnounceResult::FAILED;
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:12:33 +00:00
|
|
|
auto const* dht_hash = reinterpret_cast<unsigned char const*>(std::data(tor->infoHash()));
|
2021-12-21 22:14:15 +00:00
|
|
|
int const rc = dht_search(dht_hash, announce ? tr_sessionGetPeerPort(session_) : 0, af, callback, nullptr);
|
2021-10-27 00:16:56 +00:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddWarnTor(
|
2021-10-27 00:16:56 +00:00
|
|
|
tor,
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::format(
|
|
|
|
_("Unable to announce torrent in DHT with {type}: {error} ({error_code}); state is {state}"),
|
|
|
|
fmt::arg("type", af == AF_INET6 ? "IPv6" : "IPv4"),
|
|
|
|
fmt::arg("state", tr_dhtPrintableStatus(status)),
|
|
|
|
fmt::arg("error_code", error_code),
|
|
|
|
fmt::arg("error", tr_strerror(error_code))));
|
2021-10-27 00:16:56 +00:00
|
|
|
return AnnounceResult::FAILED;
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:51:36 +00:00
|
|
|
tr_logAddTraceTor(
|
2021-10-27 00:16:56 +00:00
|
|
|
tor,
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::format(
|
|
|
|
"Starting {} DHT announce ({}, {} nodes)",
|
|
|
|
af == AF_INET6 ? "IPv6" : "IPv4",
|
|
|
|
tr_dhtPrintableStatus(status),
|
|
|
|
numnodes));
|
2021-10-27 00:16:56 +00:00
|
|
|
|
|
|
|
if (af == AF_INET)
|
|
|
|
{
|
|
|
|
tor->dhtAnnounceInProgress = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tor->dhtAnnounce6InProgress = true;
|
2009-08-07 14:38:09 +00:00
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
return AnnounceResult::OK;
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_dhtUpkeep(tr_session* session)
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2011-03-17 12:34:43 +00:00
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
for (auto* const tor : session->torrents())
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
2021-12-15 05:48:56 +00:00
|
|
|
if (!tor->isRunning || !tor->allowsDht())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-17 12:34:43 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-17 12:34:43 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->dhtAnnounceAt <= now)
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
auto const rc = tr_dhtAnnounce(tor, AF_INET, true);
|
2011-03-17 12:34:43 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
tor->dhtAnnounceAt = now +
|
|
|
|
((rc == AnnounceResult::FAILED) ? 5 + tr_rand_int_weak(5) : 25 * 60 + tr_rand_int_weak(3 * 60));
|
2011-03-17 12:34:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->dhtAnnounce6At <= now)
|
2011-03-17 12:34:43 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
auto const rc = tr_dhtAnnounce(tor, AF_INET6, true);
|
2011-03-17 12:34:43 +00:00
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
tor->dhtAnnounce6At = now +
|
|
|
|
((rc == AnnounceResult::FAILED) ? 5 + tr_rand_int_weak(5) : 25 * 60 + tr_rand_int_weak(3 * 60));
|
2011-03-17 12:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_dhtCallback(unsigned char* buf, int buflen, struct sockaddr* from, socklen_t fromlen, void* sv)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
TR_ASSERT(tr_isSession(static_cast<tr_session*>(sv)));
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
if (sv != session_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-01-09 21:48:46 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:16:56 +00:00
|
|
|
time_t tosleep = 0;
|
2022-02-01 17:30:51 +00:00
|
|
|
int rc = dht_periodic(buf, buflen, from, fromlen, &tosleep, callback, nullptr);
|
2011-01-09 21:48:43 +00:00
|
|
|
|
2022-02-01 17:30:51 +00:00
|
|
|
if (rc < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
2009-05-19 18:38:26 +00:00
|
|
|
tosleep = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
auto const errcode = errno;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format("dht_periodic failed: {} ({})", tr_strerror(errcode), errcode));
|
2022-03-16 00:51:36 +00:00
|
|
|
if (errcode == EINVAL || errcode == EFAULT)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
// TODO: maybe just turn it off instead of crashing?
|
2017-04-19 12:04:45 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
tosleep = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 21:48:46 +00:00
|
|
|
/* Being slightly late is fine,
|
|
|
|
and has the added benefit of adding some jitter. */
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_timerAdd(dht_timer, (int)tosleep, tr_rand_int_weak(1000000));
|
2011-01-09 21:48:46 +00:00
|
|
|
}
|
2009-11-24 01:59:51 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void timer_callback(evutil_socket_t /*s*/, short /*type*/, void* session)
|
2011-01-09 21:48:46 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_dhtCallback(nullptr, 0, nullptr, 0, session);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 21:30:46 +00:00
|
|
|
/* This function should return true when a node is blacklisted. We do
|
|
|
|
not support using a blacklist with the DHT in Transmission, since
|
|
|
|
massive (ab)use of this feature could harm the DHT. However, feel
|
|
|
|
free to add support to your private copy as long as you don't
|
|
|
|
redistribute it. */
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
int dht_blacklisted(sockaddr const* /*sa*/, int /*salen*/)
|
2011-07-25 21:30:46 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-09 21:48:46 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void dht_hash(void* hash_return, int hash_size, void const* v1, int len1, void const* v2, int len2, void const* v3, int len3)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
auto* setme = reinterpret_cast<std::byte*>(hash_return);
|
|
|
|
std::fill_n(static_cast<char*>(hash_return), hash_size, '\0');
|
|
|
|
|
|
|
|
auto const sv1 = std::string_view{ static_cast<char const*>(v1), size_t(len1) };
|
|
|
|
auto const sv2 = std::string_view{ static_cast<char const*>(v2), size_t(len2) };
|
|
|
|
auto const sv3 = std::string_view{ static_cast<char const*>(v3), size_t(len3) };
|
|
|
|
auto const digest = tr_sha1(sv1, sv2, sv3);
|
|
|
|
if (digest)
|
|
|
|
{
|
|
|
|
std::copy_n(std::data(*digest), std::min(size_t(hash_size), std::size(*digest)), setme);
|
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
2009-05-20 03:55:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int dht_random_bytes(void* buf, size_t size)
|
2009-05-20 03:55:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_rand_buffer(buf, size);
|
2009-05-20 03:55:09 +00:00
|
|
|
return size;
|
|
|
|
}
|
2015-06-08 19:54:51 +00:00
|
|
|
|
2018-02-10 08:35:14 +00:00
|
|
|
int dht_sendto(int sockfd, void const* buf, int len, int flags, struct sockaddr const* to, int tolen)
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
return sendto(sockfd, static_cast<char const*>(buf), len, flags, to, tolen);
|
2018-02-10 08:35:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
extern "C" int dht_gettimeofday(struct timeval* tv, struct timezone* tz)
|
2015-06-08 19:54:51 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(tz == nullptr);
|
2022-02-01 01:02:59 +00:00
|
|
|
*tv = tr_gettimeofday();
|
|
|
|
return 0;
|
2015-06-08 19:54:51 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2015-06-08 19:54:51 +00:00
|
|
|
#endif
|