2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2010-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-08-06 01:37:21 +00:00
|
|
|
#include <algorithm> // for std::find_if()
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <array>
|
|
|
|
#include <chrono> // operator""ms, literals
|
|
|
|
#include <climits> // CHAR_BIT
|
|
|
|
#include <cstddef> // std::byte
|
|
|
|
#include <cstdint> // uint32_t, uint64_t
|
|
|
|
#include <cstring> // memcpy()
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-11-16 21:13:31 +00:00
|
|
|
#include <future>
|
2022-01-23 05:41:01 +00:00
|
|
|
#include <list>
|
2022-04-02 14:06:02 +00:00
|
|
|
#include <memory>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <utility>
|
2021-09-30 19:00:02 +00:00
|
|
|
#include <vector>
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2022-11-17 00:03:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#undef gai_strerror
|
|
|
|
#define gai_strerror gai_strerrorA
|
2023-07-06 15:00:07 +00:00
|
|
|
#else
|
|
|
|
#include <netdb.h> // gai_strerror()
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <netinet/in.h> // IPPROTO_UDP, in_addr
|
|
|
|
#include <sys/socket.h> // sockaddr_storage, AF_INET
|
2022-11-17 00:03:48 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_ANNOUNCER_MODULE
|
2017-11-14 20:21:28 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/announcer.h"
|
|
|
|
#include "libtransmission/announcer-common.h"
|
|
|
|
#include "libtransmission/crypto-utils.h" // for tr_rand_obj()
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/interned-string.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
2023-07-06 15:00:07 +00:00
|
|
|
#include "libtransmission/net.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/peer-mgr.h" // for tr_pex::fromCompact4()
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-buffer.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/web-utils.h"
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
#define logwarn(interned, msg) tr_logAddWarn(msg, (interned).sv())
|
|
|
|
#define logdbg(interned, msg) tr_logAddDebug(msg, (interned).sv())
|
|
|
|
#define logtrace(interned, msg) tr_logAddTrace(msg, (interned).sv())
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
// size defined by bep15
|
2021-10-06 14:26:07 +00:00
|
|
|
using tau_connection_t = uint64_t;
|
2022-10-21 18:15:14 +00:00
|
|
|
using tau_transaction_t = uint32_t;
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
using InBuf = libtransmission::BufferReader<std::byte>;
|
2023-06-28 00:06:25 +00:00
|
|
|
using PayloadBuffer = libtransmission::StackBuffer<4096, std::byte>;
|
2023-05-18 21:56:29 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
constexpr auto TauConnectionTtlSecs = time_t{ 45 };
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
auto tau_transaction_new()
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-11-25 21:04:37 +00:00
|
|
|
return tr_rand_obj<tau_transaction_t>();
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
// used in the "action" field of a request. Values defined in bep 15.
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tau_action_t
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TAU_ACTION_CONNECT = 0,
|
2011-03-13 00:18:11 +00:00
|
|
|
TAU_ACTION_ANNOUNCE = 1,
|
2017-04-19 12:04:45 +00:00
|
|
|
TAU_ACTION_SCRAPE = 2,
|
|
|
|
TAU_ACTION_ERROR = 3
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- SCRAPE
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
struct tau_scrape_request
|
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
tau_scrape_request(tr_scrape_request const& in, tr_scrape_response_func on_response)
|
|
|
|
: on_response_{ std::move(on_response) }
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
this->response.scrape_url = in.scrape_url;
|
|
|
|
this->response.row_count = in.info_hash_count;
|
|
|
|
for (int i = 0; i < this->response.row_count; ++i)
|
|
|
|
{
|
|
|
|
this->response.rows[i].info_hash = in.info_hash[i];
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
// build the payload
|
2023-06-25 17:33:32 +00:00
|
|
|
auto buf = PayloadBuffer{};
|
2023-01-28 02:12:09 +00:00
|
|
|
buf.add_uint32(TAU_ACTION_SCRAPE);
|
|
|
|
buf.add_uint32(transaction_id);
|
2022-11-16 21:13:31 +00:00
|
|
|
for (int i = 0; i < in.info_hash_count; ++i)
|
|
|
|
{
|
|
|
|
buf.add(in.info_hash[i]);
|
|
|
|
}
|
|
|
|
this->payload.insert(std::end(this->payload), std::begin(buf), std::end(buf));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
[[nodiscard]] auto has_callback() const noexcept
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
return !!on_response_;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:47:04 +00:00
|
|
|
void requestFinished() const
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
if (on_response_)
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
on_response_(response);
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fail(bool did_connect, bool did_timeout, std::string_view errmsg)
|
|
|
|
{
|
|
|
|
response.did_connect = did_connect;
|
|
|
|
response.did_timeout = did_timeout;
|
|
|
|
response.errmsg = errmsg;
|
|
|
|
requestFinished();
|
|
|
|
}
|
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
void onResponse(tau_action_t action, InBuf& buf)
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
|
|
|
response.did_connect = true;
|
|
|
|
response.did_timeout = false;
|
|
|
|
|
|
|
|
if (action == TAU_ACTION_SCRAPE)
|
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
for (int i = 0; i < response.row_count && std::size(buf) >= sizeof(uint32_t) * 3U; ++i)
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
|
|
|
auto& row = response.rows[i];
|
2023-01-28 02:12:09 +00:00
|
|
|
row.seeders = buf.to_uint32();
|
|
|
|
row.downloads = buf.to_uint32();
|
|
|
|
row.leechers = buf.to_uint32();
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
requestFinished();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
std::string const errmsg = action == TAU_ACTION_ERROR && !std::empty(buf) ? buf.to_string() : _("Unknown error");
|
2022-01-23 05:41:01 +00:00
|
|
|
fail(true, false, errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto expiresAt() const noexcept
|
2022-11-30 01:38:53 +00:00
|
|
|
{
|
|
|
|
return created_at_ + TR_SCRAPE_TIMEOUT_SEC.count();
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
std::vector<std::byte> payload;
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
time_t sent_at = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
tau_transaction_t const transaction_id = tau_transaction_new();
|
2011-03-14 14:15:58 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
tr_scrape_response response = {};
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
private:
|
2022-11-30 01:38:53 +00:00
|
|
|
time_t const created_at_ = tr_time();
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
tr_scrape_response_func on_response_;
|
2022-11-16 21:13:31 +00:00
|
|
|
};
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- ANNOUNCE
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
struct tau_announce_request
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
tau_announce_request(uint32_t announce_ip, tr_announce_request const& in, tr_announce_response_func on_response)
|
|
|
|
: on_response_{ std::move(on_response) }
|
2022-11-16 21:13:31 +00:00
|
|
|
{
|
2023-01-01 19:22:50 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0015.html sets key size at 32 bits
|
|
|
|
static_assert(sizeof(tr_announce_request::key) * CHAR_BIT == 32);
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
response.info_hash = in.info_hash;
|
|
|
|
|
|
|
|
// build the payload
|
2023-06-25 17:33:32 +00:00
|
|
|
auto buf = PayloadBuffer{};
|
2023-01-28 02:12:09 +00:00
|
|
|
buf.add_uint32(TAU_ACTION_ANNOUNCE);
|
|
|
|
buf.add_uint32(transaction_id);
|
2022-11-16 21:13:31 +00:00
|
|
|
buf.add(in.info_hash);
|
|
|
|
buf.add(in.peer_id);
|
2023-01-28 02:12:09 +00:00
|
|
|
buf.add_uint64(in.down);
|
|
|
|
buf.add_uint64(in.leftUntilComplete);
|
|
|
|
buf.add_uint64(in.up);
|
|
|
|
buf.add_uint32(get_tau_announce_event(in.event));
|
|
|
|
buf.add_uint32(announce_ip);
|
|
|
|
buf.add_uint32(in.key);
|
|
|
|
buf.add_uint32(in.numwant);
|
|
|
|
buf.add_port(in.port);
|
2022-11-16 21:13:31 +00:00
|
|
|
payload.insert(std::end(payload), std::begin(buf), std::end(buf));
|
|
|
|
}
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
[[nodiscard]] auto has_callback() const noexcept
|
2022-11-16 21:13:31 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
return !!on_response_;
|
2022-11-16 21:13:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:47:04 +00:00
|
|
|
void requestFinished() const
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
if (on_response_)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
on_response_(this->response);
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
void fail(bool did_connect, bool did_timeout, std::string_view errmsg)
|
|
|
|
{
|
|
|
|
this->response.did_connect = did_connect;
|
|
|
|
this->response.did_timeout = did_timeout;
|
|
|
|
this->response.errmsg = errmsg;
|
|
|
|
this->requestFinished();
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
void onResponse(tau_action_t action, InBuf& buf)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
auto const buflen = std::size(buf);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
this->response.did_connect = true;
|
|
|
|
this->response.did_timeout = false;
|
|
|
|
|
|
|
|
if (action == TAU_ACTION_ANNOUNCE && buflen >= 3 * sizeof(uint32_t))
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
response.interval = buf.to_uint32();
|
|
|
|
response.leechers = buf.to_uint32();
|
|
|
|
response.seeders = buf.to_uint32();
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
response.pex = tr_pex::from_compact_ipv4(std::data(buf), std::size(buf), nullptr, 0);
|
2022-01-23 05:41:01 +00:00
|
|
|
requestFinished();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
std::string const errmsg = action == TAU_ACTION_ERROR && !std::empty(buf) ? buf.to_string() : _("Unknown error");
|
2022-01-23 05:41:01 +00:00
|
|
|
fail(true, false, errmsg);
|
|
|
|
}
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto expiresAt() const noexcept
|
2022-11-30 01:38:53 +00:00
|
|
|
{
|
|
|
|
return created_at_ + TR_ANNOUNCE_TIMEOUT_SEC.count();
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
enum tau_announce_event
|
|
|
|
{
|
|
|
|
// Used in the "event" field of an announce request.
|
|
|
|
// These values come from BEP 15
|
|
|
|
TAU_ANNOUNCE_EVENT_NONE = 0,
|
|
|
|
TAU_ANNOUNCE_EVENT_COMPLETED = 1,
|
|
|
|
TAU_ANNOUNCE_EVENT_STARTED = 2,
|
|
|
|
TAU_ANNOUNCE_EVENT_STOPPED = 3
|
|
|
|
};
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
std::vector<std::byte> payload;
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
time_t sent_at = 0;
|
2022-11-16 21:13:31 +00:00
|
|
|
tau_transaction_t const transaction_id = tau_transaction_new();
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_announce_response response = {};
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
private:
|
|
|
|
[[nodiscard]] static constexpr tau_announce_event get_tau_announce_event(tr_announce_event e)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
switch (e)
|
|
|
|
{
|
|
|
|
case TR_ANNOUNCE_EVENT_COMPLETED:
|
|
|
|
return TAU_ANNOUNCE_EVENT_COMPLETED;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
case TR_ANNOUNCE_EVENT_STARTED:
|
|
|
|
return TAU_ANNOUNCE_EVENT_STARTED;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
case TR_ANNOUNCE_EVENT_STOPPED:
|
|
|
|
return TAU_ANNOUNCE_EVENT_STOPPED;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
default:
|
|
|
|
return TAU_ANNOUNCE_EVENT_NONE;
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 01:38:53 +00:00
|
|
|
time_t const created_at_ = tr_time();
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
tr_announce_response_func on_response_;
|
2022-11-16 21:13:31 +00:00
|
|
|
};
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- TRACKER
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
struct tau_tracker
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
using Mediator = tr_announcer_udp::Mediator;
|
|
|
|
|
|
|
|
tau_tracker(Mediator& mediator, tr_interned_string key_in, tr_interned_string host_in, tr_port port_in)
|
2022-11-16 21:13:31 +00:00
|
|
|
: key{ key_in }
|
2022-10-21 18:15:14 +00:00
|
|
|
, host{ host_in }
|
|
|
|
, port{ port_in }
|
2022-11-16 21:13:31 +00:00
|
|
|
, mediator_{ mediator }
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
void sendto(std::byte const* buf, size_t buflen)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
TR_ASSERT(addr_);
|
|
|
|
if (!addr_)
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
return;
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
auto const& [ss, sslen] = *addr_;
|
|
|
|
mediator_.sendto(buf, buflen, reinterpret_cast<sockaddr const*>(&ss), sslen);
|
|
|
|
}
|
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
void on_connection_response(tau_action_t action, InBuf& buf)
|
2022-11-16 21:13:31 +00:00
|
|
|
{
|
|
|
|
this->connecting_at = 0;
|
|
|
|
this->connection_transaction_id = 0;
|
|
|
|
|
|
|
|
if (action == TAU_ACTION_CONNECT)
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
this->connection_id = buf.to_uint64();
|
2022-11-16 21:13:31 +00:00
|
|
|
this->connection_expiration_time = tr_time() + TauConnectionTtlSecs;
|
|
|
|
logdbg(this->key, fmt::format("Got a new connection ID from tracker: {}", this->connection_id));
|
|
|
|
}
|
|
|
|
else if (action == TAU_ACTION_ERROR)
|
|
|
|
{
|
2023-07-04 19:03:45 +00:00
|
|
|
std::string errmsg = !std::empty(buf) ? buf.to_string() : _("Connection failed");
|
2022-11-16 21:13:31 +00:00
|
|
|
this->failAll(true, false, errmsg);
|
2023-07-04 19:03:45 +00:00
|
|
|
logdbg(this->key, std::move(errmsg));
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
this->upkeep();
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
void upkeep(bool timeout_reqs = true)
|
2022-10-21 18:15:14 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
|
|
|
|
// do we have a DNS request that's ready?
|
|
|
|
if (addr_pending_dns_ && addr_pending_dns_->wait_for(0ms) == std::future_status::ready)
|
2022-11-12 15:53:09 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
addr_ = addr_pending_dns_->get();
|
|
|
|
addr_pending_dns_.reset();
|
|
|
|
addr_expires_at_ = now + DnsRetryIntervalSecs;
|
2022-11-12 15:53:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
// are there any requests pending?
|
|
|
|
if (this->isIdle())
|
2022-10-21 18:15:14 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
return;
|
2022-10-21 18:15:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
// update the addr if our lookup is past its shelf date
|
2022-11-30 03:05:11 +00:00
|
|
|
if (!addr_pending_dns_ && addr_expires_at_ <= now)
|
2022-11-16 21:13:31 +00:00
|
|
|
{
|
2022-11-18 02:23:54 +00:00
|
|
|
addr_.reset();
|
2022-11-16 21:13:31 +00:00
|
|
|
addr_pending_dns_ = std::async(std::launch::async, lookup, this->host, this->port, this->key);
|
|
|
|
return;
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
logtrace(
|
|
|
|
this->key,
|
|
|
|
fmt::format(
|
|
|
|
"connected {} ({} {}) -- connecting_at {}",
|
2022-11-18 02:23:54 +00:00
|
|
|
is_connected(now),
|
2022-11-16 21:13:31 +00:00
|
|
|
this->connection_expiration_time,
|
|
|
|
now,
|
|
|
|
this->connecting_at));
|
|
|
|
|
|
|
|
/* also need a valid connection ID... */
|
2022-11-18 02:23:54 +00:00
|
|
|
if (addr_ && !is_connected(now) && this->connecting_at == 0)
|
2022-11-16 21:13:31 +00:00
|
|
|
{
|
|
|
|
this->connecting_at = now;
|
|
|
|
this->connection_transaction_id = tau_transaction_new();
|
|
|
|
logtrace(this->key, fmt::format("Trying to connect. Transaction ID is {}", this->connection_transaction_id));
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-06-25 17:33:32 +00:00
|
|
|
auto buf = PayloadBuffer{};
|
2023-01-28 02:12:09 +00:00
|
|
|
buf.add_uint64(0x41727101980LL);
|
|
|
|
buf.add_uint32(TAU_ACTION_CONNECT);
|
|
|
|
buf.add_uint32(this->connection_transaction_id);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
this->sendto(std::data(buf), std::size(buf));
|
2022-11-16 21:13:31 +00:00
|
|
|
}
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
if (timeout_reqs)
|
|
|
|
{
|
2022-11-18 02:23:54 +00:00
|
|
|
timeout_requests(now);
|
2022-11-16 21:13:31 +00:00
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
if (addr_ && is_connected(now))
|
2022-11-16 21:13:31 +00:00
|
|
|
{
|
|
|
|
send_requests();
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 17:16:49 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
private:
|
|
|
|
using Sockaddr = std::pair<sockaddr_storage, socklen_t>;
|
|
|
|
using MaybeSockaddr = std::optional<Sockaddr>;
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
[[nodiscard]] constexpr bool is_connected(time_t now) const noexcept
|
|
|
|
{
|
|
|
|
return connection_id != tau_connection_t{} && now < connection_expiration_time;
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
[[nodiscard]] static MaybeSockaddr lookup(tr_interned_string host, tr_port port, tr_interned_string logname)
|
2022-11-15 17:16:49 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
auto szport = std::array<char, 16>{};
|
|
|
|
*fmt::format_to(std::data(szport), FMT_STRING("{:d}"), port.host()) = '\0';
|
2022-11-04 18:29:56 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
auto hints = addrinfo{};
|
2023-02-06 05:31:10 +00:00
|
|
|
hints.ai_family = AF_INET; // https://github.com/transmission/transmission/issues/4719
|
2022-11-16 21:13:31 +00:00
|
|
|
hints.ai_protocol = IPPROTO_UDP;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
addrinfo* info = nullptr;
|
|
|
|
if (int const rc = getaddrinfo(host.c_str(), std::data(szport), &hints, &info); rc != 0)
|
|
|
|
{
|
|
|
|
logwarn(
|
|
|
|
logname,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't look up '{address}:{port}': {error} ({error_code})"),
|
|
|
|
fmt::arg("address", host.sv()),
|
|
|
|
fmt::arg("port", port.host()),
|
|
|
|
fmt::arg("error", gai_strerror(rc)),
|
2022-11-17 00:03:48 +00:00
|
|
|
fmt::arg("error_code", static_cast<int>(rc))));
|
2022-11-16 21:13:31 +00:00
|
|
|
return {};
|
|
|
|
}
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
auto ss = sockaddr_storage{};
|
|
|
|
auto const len = info->ai_addrlen;
|
|
|
|
memcpy(&ss, info->ai_addr, len);
|
|
|
|
freeaddrinfo(info);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
logdbg(logname, "DNS lookup succeeded");
|
|
|
|
return std::make_pair(ss, len);
|
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-11-30 03:05:11 +00:00
|
|
|
[[nodiscard]] bool isIdle() const noexcept
|
|
|
|
{
|
|
|
|
return std::empty(announces) && std::empty(scrapes) && !addr_pending_dns_;
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
void failAll(bool did_connect, bool did_timeout, std::string_view errmsg)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
for (auto& req : this->scrapes)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
req.fail(did_connect, did_timeout, errmsg);
|
2011-03-20 15:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
for (auto& req : this->announces)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
req.fail(did_connect, did_timeout, errmsg);
|
2011-03-20 15:28:41 +00:00
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
this->scrapes.clear();
|
|
|
|
this->announces.clear();
|
2011-03-20 15:28:41 +00:00
|
|
|
}
|
2022-01-23 05:41:01 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
///
|
2011-03-20 15:28:41 +00:00
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
void timeout_requests(time_t now)
|
2011-03-20 15:28:41 +00:00
|
|
|
{
|
2022-11-30 01:38:53 +00:00
|
|
|
if (this->connecting_at != 0 && this->connecting_at + ConnectionRequestTtl < now)
|
2022-11-16 21:13:31 +00:00
|
|
|
{
|
2023-06-25 17:33:32 +00:00
|
|
|
auto empty_buf = PayloadBuffer{};
|
2022-11-16 21:13:31 +00:00
|
|
|
on_connection_response(TAU_ACTION_ERROR, empty_buf);
|
|
|
|
}
|
2011-03-20 15:28:41 +00:00
|
|
|
|
2022-11-30 03:05:11 +00:00
|
|
|
timeout_requests(this->announces, now, "announce");
|
|
|
|
timeout_requests(this->scrapes, now, "scrape");
|
2011-03-20 15:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
template<typename T>
|
2022-11-30 03:05:11 +00:00
|
|
|
void timeout_requests(std::list<T>& requests, time_t now, std::string_view name)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
for (auto it = std::begin(requests); it != std::end(requests);)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-30 03:05:11 +00:00
|
|
|
if (auto& req = *it; req.expiresAt() <= now)
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
logtrace(this->key, fmt::format("timeout {} req {}", name, fmt::ptr(&req)));
|
2022-01-23 05:41:01 +00:00
|
|
|
req.fail(false, true, "");
|
2022-11-16 21:13:31 +00:00
|
|
|
it = requests.erase(it);
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
2011-03-20 15:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
///
|
|
|
|
|
|
|
|
void send_requests()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
TR_ASSERT(!addr_pending_dns_);
|
|
|
|
TR_ASSERT(addr_);
|
|
|
|
TR_ASSERT(this->connecting_at == 0);
|
|
|
|
TR_ASSERT(this->connection_expiration_time > tr_time());
|
|
|
|
|
|
|
|
send_requests(this->announces);
|
|
|
|
send_requests(this->scrapes);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void send_requests(std::list<T>& reqs)
|
|
|
|
{
|
|
|
|
auto const now = tr_time();
|
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
for (auto it = std::begin(reqs); it != std::end(reqs);)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
auto& req = *it;
|
2022-11-16 21:13:31 +00:00
|
|
|
|
|
|
|
if (req.sent_at != 0) // it's already been sent; we're awaiting a response
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
++it;
|
|
|
|
continue;
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
2022-11-16 21:13:31 +00:00
|
|
|
|
|
|
|
logdbg(this->key, fmt::format("sending req {}", fmt::ptr(&req)));
|
|
|
|
req.sent_at = now;
|
|
|
|
send_request(std::data(req.payload), std::size(req.payload));
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
if (req.has_callback())
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
|
|
|
++it;
|
2022-11-16 21:13:31 +00:00
|
|
|
continue;
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
2022-11-16 21:13:31 +00:00
|
|
|
|
|
|
|
// no response needed, so we can remove it now
|
|
|
|
it = reqs.erase(it);
|
2011-03-20 15:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
void send_request(std::byte const* payload, size_t payload_len)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
logdbg(this->key, fmt::format("sending request w/connection id {}", this->connection_id));
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-06-25 17:33:32 +00:00
|
|
|
auto buf = PayloadBuffer{};
|
2023-01-28 02:12:09 +00:00
|
|
|
buf.add_uint64(this->connection_id);
|
2022-11-16 21:13:31 +00:00
|
|
|
buf.add(payload, payload_len);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
this->sendto(std::data(buf), std::size(buf));
|
2022-02-18 00:38:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
public:
|
|
|
|
tr_interned_string const key;
|
|
|
|
tr_interned_string const host;
|
|
|
|
tr_port const port;
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
time_t connecting_at = 0;
|
|
|
|
time_t connection_expiration_time = 0;
|
|
|
|
tau_connection_t connection_id = {};
|
|
|
|
tau_transaction_t connection_transaction_id = {};
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
std::list<tau_announce_request> announces;
|
|
|
|
std::list<tau_scrape_request> scrapes;
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
private:
|
|
|
|
Mediator& mediator_;
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
std::optional<std::future<MaybeSockaddr>> addr_pending_dns_ = {};
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
MaybeSockaddr addr_ = {};
|
|
|
|
time_t addr_expires_at_ = 0;
|
|
|
|
|
2022-11-30 01:38:53 +00:00
|
|
|
static inline constexpr auto DnsRetryIntervalSecs = time_t{ 3600 };
|
|
|
|
static inline constexpr auto ConnectionRequestTtl = int{ 30 };
|
2022-11-16 21:13:31 +00:00
|
|
|
};
|
2017-07-04 18:22:26 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- SESSION
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
class tr_announcer_udp_impl final : public tr_announcer_udp
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
public:
|
|
|
|
explicit tr_announcer_udp_impl(Mediator& mediator)
|
|
|
|
: mediator_{ mediator }
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
void announce(tr_announce_request const& request, tr_announce_response_func on_response) override
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
auto* const tracker = getTrackerFromUrl(request.announce_url);
|
|
|
|
if (tracker == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
// Since size of IP field is only 4 bytes long, we can only announce IPv4 addresses
|
2023-05-06 04:11:05 +00:00
|
|
|
auto const addr = mediator_.announce_ip();
|
2022-12-09 02:27:52 +00:00
|
|
|
uint32_t const announce_ip = addr && addr->is_ipv4() ? addr->addr.addr4.s_addr : 0;
|
2022-11-19 05:00:25 +00:00
|
|
|
tracker->announces.emplace_back(announce_ip, request, std::move(on_response));
|
2022-11-16 21:13:31 +00:00
|
|
|
tracker->upkeep(false);
|
2021-11-02 23:00:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
void scrape(tr_scrape_request const& request, tr_scrape_response_func on_response) override
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
auto* const tracker = getTrackerFromUrl(request.scrape_url);
|
|
|
|
if (tracker == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
tracker->scrapes.emplace_back(request, std::move(on_response));
|
2022-11-16 21:13:31 +00:00
|
|
|
tracker->upkeep(false);
|
2022-10-21 18:15:14 +00:00
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
void upkeep() override
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
for (auto& tracker : trackers_)
|
2022-01-23 05:41:01 +00:00
|
|
|
{
|
2022-11-16 21:13:31 +00:00
|
|
|
tracker.upkeep();
|
2022-01-23 05:41:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-17 18:51:31 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
// @brief process an incoming udp message if it's a tracker response.
|
|
|
|
// @return true if msg was a tracker response; false otherwise
|
2023-05-06 04:11:05 +00:00
|
|
|
bool handle_message(uint8_t const* msg, size_t msglen) override
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
if (msglen < sizeof(uint32_t) * 2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
// extract the action_id and see if it makes sense
|
2023-06-25 17:33:32 +00:00
|
|
|
auto buf = PayloadBuffer{};
|
2022-10-21 18:15:14 +00:00
|
|
|
buf.add(msg, msglen);
|
2023-01-28 02:12:09 +00:00
|
|
|
auto const action_id = static_cast<tau_action_t>(buf.to_uint32());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
if (!isResponseMessage(action_id, msglen))
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
return false;
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
/* extract the transaction_id and look for a match */
|
2023-01-28 02:12:09 +00:00
|
|
|
tau_transaction_t const transaction_id = buf.to_uint32();
|
2022-10-21 18:15:14 +00:00
|
|
|
|
|
|
|
for (auto& tracker : trackers_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
// is it a connection response?
|
|
|
|
if (tracker.connecting_at != 0 && transaction_id == tracker.connection_transaction_id)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
logtrace(tracker.key, fmt::format("{} is my connection request!", transaction_id));
|
2022-11-16 21:13:31 +00:00
|
|
|
tracker.on_connection_response(action_id, buf);
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
// is it a response to one of this tracker's announces?
|
|
|
|
if (auto& reqs = tracker.announces; !std::empty(reqs))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
if (auto it = std::find_if(
|
|
|
|
std::begin(reqs),
|
|
|
|
std::end(reqs),
|
|
|
|
[&transaction_id](auto const& req) { return req.transaction_id == transaction_id; });
|
|
|
|
it != std::end(reqs))
|
2022-10-21 18:15:14 +00:00
|
|
|
{
|
|
|
|
logtrace(tracker.key, fmt::format("{} is an announce request!", transaction_id));
|
|
|
|
auto req = *it;
|
|
|
|
it = reqs.erase(it);
|
|
|
|
req.onResponse(action_id, buf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is it a response to one of this tracker's scrapes?
|
|
|
|
if (auto& reqs = tracker.scrapes; !std::empty(reqs))
|
|
|
|
{
|
2023-09-01 21:51:58 +00:00
|
|
|
if (auto it = std::find_if(
|
|
|
|
std::begin(reqs),
|
|
|
|
std::end(reqs),
|
|
|
|
[&transaction_id](auto const& req) { return req.transaction_id == transaction_id; });
|
|
|
|
it != std::end(reqs))
|
2022-10-21 18:15:14 +00:00
|
|
|
{
|
|
|
|
logtrace(tracker.key, fmt::format("{} is a scrape request!", transaction_id));
|
|
|
|
auto req = *it;
|
|
|
|
it = reqs.erase(it);
|
|
|
|
req.onResponse(action_id, buf);
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
/* no match... */
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
private:
|
|
|
|
// Finds the tau_tracker struct that corresponds to this url.
|
|
|
|
// If it doesn't exist yet, create one.
|
|
|
|
tau_tracker* getTrackerFromUrl(tr_interned_string announce_url)
|
2021-11-02 23:00:01 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
// build a lookup key for this tracker
|
|
|
|
auto const parsed = tr_urlParseTracker(announce_url);
|
|
|
|
TR_ASSERT(parsed);
|
|
|
|
if (!parsed)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-11-02 23:00:01 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
// see if we already have it
|
|
|
|
auto const key = tr_announcerGetKey(*parsed);
|
|
|
|
for (auto& tracker : trackers_)
|
|
|
|
{
|
|
|
|
if (tracker.key == key)
|
|
|
|
{
|
|
|
|
return &tracker;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
// we don't have it -- build a new one
|
2023-08-18 03:13:01 +00:00
|
|
|
trackers_.emplace_back(mediator_, key, tr_interned_string(parsed->host), tr_port::from_host(parsed->port));
|
2022-10-21 18:15:14 +00:00
|
|
|
auto* const tracker = &trackers_.back();
|
|
|
|
logtrace(tracker->key, "New tau_tracker created");
|
|
|
|
return tracker;
|
2021-11-02 23:00:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
[[nodiscard]] static constexpr bool isResponseMessage(tau_action_t action, size_t msglen) noexcept
|
|
|
|
{
|
|
|
|
if (action == TAU_ACTION_CONNECT)
|
|
|
|
{
|
|
|
|
return msglen == 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == TAU_ACTION_ANNOUNCE)
|
|
|
|
{
|
|
|
|
return msglen >= 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == TAU_ACTION_SCRAPE)
|
|
|
|
{
|
|
|
|
return msglen >= 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == TAU_ACTION_ERROR)
|
|
|
|
{
|
|
|
|
return msglen >= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
std::list<tau_tracker> trackers_;
|
|
|
|
|
|
|
|
Mediator& mediator_;
|
|
|
|
};
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
std::unique_ptr<tr_announcer_udp> tr_announcer_udp::create(Mediator& mediator)
|
|
|
|
{
|
|
|
|
return std::make_unique<tr_announcer_udp_impl>(mediator);
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|