2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +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.
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint8_t, uint64_t
|
2023-07-14 12:06:25 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <limits>
|
|
|
|
#include <optional>
|
2022-01-25 04:25:55 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <vector>
|
2008-01-10 18:52:46 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h" // tr_block_span_t (ptr only)
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
#include "libtransmission/blocklist.h"
|
2023-10-14 17:58:15 +00:00
|
|
|
#include "libtransmission/handshake.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/net.h" /* tr_address */
|
2023-07-14 12:06:25 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h" /* tr_compare_3way */
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup peers Peers
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
class tr_peer;
|
|
|
|
class tr_peer_socket;
|
2023-07-14 12:06:25 +00:00
|
|
|
struct tr_peerMgr;
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_peer_stat;
|
2023-07-08 15:24:03 +00:00
|
|
|
struct tr_session;
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_torrent;
|
|
|
|
|
2010-12-14 18:33:48 +00:00
|
|
|
/* added_f's bitwise-or'ed flags */
|
2008-04-19 15:07:59 +00:00
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* true if the peer supports encryption */
|
|
|
|
ADDED_F_ENCRYPTION_FLAG = 1,
|
|
|
|
/* true if the peer is a seed or partial seed */
|
|
|
|
ADDED_F_SEED_FLAG = 2,
|
2022-10-11 15:39:41 +00:00
|
|
|
/* true if the peer supports µTP */
|
2017-04-19 12:04:45 +00:00
|
|
|
ADDED_F_UTP_FLAGS = 4,
|
|
|
|
/* true if the peer has holepunch support */
|
|
|
|
ADDED_F_HOLEPUNCH = 8,
|
|
|
|
/* true if the peer telling us about this peer
|
|
|
|
* initiated the connection (implying that it is connectible) */
|
|
|
|
ADDED_F_CONNECTABLE = 16
|
2008-04-19 15:07:59 +00:00
|
|
|
};
|
|
|
|
|
2023-07-14 12:06:25 +00:00
|
|
|
/**
|
|
|
|
* Peer information that should be retained even when not connected,
|
|
|
|
* e.g. to help us decide which peers to connect to.
|
|
|
|
*/
|
|
|
|
class tr_peer_info
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tr_peer_info(tr_socket_address socket_address, uint8_t pex_flags, tr_peer_from from)
|
2023-08-01 14:56:26 +00:00
|
|
|
: listen_socket_address_{ socket_address }
|
2023-07-14 12:06:25 +00:00
|
|
|
, from_first_{ from }
|
|
|
|
, from_best_{ from }
|
|
|
|
{
|
2023-08-01 14:56:26 +00:00
|
|
|
TR_ASSERT(!std::empty(socket_address.port()));
|
2023-08-18 03:13:01 +00:00
|
|
|
++n_known_connectable;
|
2023-08-01 14:56:26 +00:00
|
|
|
set_pex_flags(pex_flags);
|
|
|
|
}
|
2023-07-14 12:06:25 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_peer_info(tr_address address, uint8_t pex_flags, tr_peer_from from)
|
|
|
|
: listen_socket_address_{ address, tr_port{} }
|
|
|
|
, from_first_{ from }
|
|
|
|
, from_best_{ from }
|
|
|
|
{
|
2023-07-14 12:06:25 +00:00
|
|
|
set_pex_flags(pex_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_peer_info(tr_peer_info&&) = delete;
|
|
|
|
tr_peer_info(tr_peer_info const&) = delete;
|
|
|
|
tr_peer_info& operator=(tr_peer_info&&) = delete;
|
|
|
|
tr_peer_info& operator=(tr_peer_info const&) = delete;
|
|
|
|
|
|
|
|
~tr_peer_info()
|
|
|
|
{
|
2023-08-01 14:56:26 +00:00
|
|
|
if (!std::empty(listen_socket_address_.port()))
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
[[maybe_unused]] auto const n_prev = n_known_connectable--;
|
2023-08-01 14:56:26 +00:00
|
|
|
TR_ASSERT(n_prev > 0U);
|
|
|
|
}
|
2023-07-14 12:06:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
[[nodiscard]] static auto known_connectable_count() noexcept
|
2023-07-14 12:06:25 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return n_known_connectable;
|
2023-07-14 12:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
[[nodiscard]] constexpr auto const& listen_socket_address() const noexcept
|
2023-07-14 12:06:25 +00:00
|
|
|
{
|
2023-08-01 14:56:26 +00:00
|
|
|
return listen_socket_address_;
|
2023-07-14 12:06:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
[[nodiscard]] constexpr auto const& listen_address() const noexcept
|
2023-07-14 12:06:25 +00:00
|
|
|
{
|
2023-08-01 14:56:26 +00:00
|
|
|
return listen_socket_address_.address();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto listen_port() const noexcept
|
|
|
|
{
|
|
|
|
return listen_socket_address_.port();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_listen_port(tr_port port_in) noexcept
|
|
|
|
{
|
|
|
|
if (!std::empty(port_in))
|
|
|
|
{
|
|
|
|
auto& port = listen_socket_address_.port_;
|
|
|
|
if (std::empty(port)) // increment known connectable peers if we did not know the listening port of this peer before
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
++n_known_connectable;
|
2023-08-01 14:56:26 +00:00
|
|
|
}
|
|
|
|
port = port_in;
|
|
|
|
}
|
2023-07-14 12:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto display_name() const
|
|
|
|
{
|
2023-08-01 14:56:26 +00:00
|
|
|
return listen_socket_address_.display_name();
|
2023-07-14 12:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto from_first() const noexcept
|
|
|
|
{
|
|
|
|
return from_first_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto from_best() const noexcept
|
|
|
|
{
|
|
|
|
return from_best_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void found_at(tr_peer_from from) noexcept
|
|
|
|
{
|
|
|
|
from_best_ = std::min(from_best_, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
constexpr void set_seed(bool seed = true) noexcept
|
|
|
|
{
|
|
|
|
is_seed_ = seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_seed() const noexcept
|
|
|
|
{
|
|
|
|
return is_seed_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
void set_connectable(bool value = true) noexcept
|
|
|
|
{
|
|
|
|
is_connectable_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& is_connectable() const noexcept
|
|
|
|
{
|
|
|
|
return is_connectable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
void set_utp_supported(bool value = true) noexcept
|
|
|
|
{
|
|
|
|
is_utp_supported_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto supports_utp() const noexcept
|
|
|
|
{
|
|
|
|
return is_utp_supported_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto compare_by_failure_count(tr_peer_info const& that) const noexcept
|
|
|
|
{
|
|
|
|
return tr_compare_3way(num_consecutive_fails_, that.num_consecutive_fails_);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto compare_by_piece_data_time(tr_peer_info const& that) const noexcept
|
|
|
|
{
|
|
|
|
return tr_compare_3way(piece_data_at_, that.piece_data_at_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
constexpr auto set_connected(time_t now, bool is_connected = true) noexcept
|
|
|
|
{
|
|
|
|
connection_changed_at_ = now;
|
|
|
|
|
|
|
|
is_connected_ = is_connected;
|
|
|
|
|
|
|
|
if (is_connected_)
|
|
|
|
{
|
|
|
|
num_consecutive_fails_ = {};
|
|
|
|
piece_data_at_ = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_connected() const noexcept
|
|
|
|
{
|
|
|
|
return is_connected_;
|
|
|
|
}
|
|
|
|
|
2023-10-14 17:58:15 +00:00
|
|
|
[[nodiscard]] auto has_handshake() const noexcept
|
|
|
|
{
|
|
|
|
return static_cast<bool>(outgoing_handshake_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void start_handshake(Args&&... args)
|
|
|
|
{
|
|
|
|
TR_ASSERT(!outgoing_handshake_);
|
|
|
|
if (!outgoing_handshake_)
|
|
|
|
{
|
|
|
|
outgoing_handshake_ = std::make_unique<tr_handshake>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_handshake() noexcept
|
|
|
|
{
|
|
|
|
outgoing_handshake_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto is_in_use() const noexcept
|
|
|
|
{
|
|
|
|
return is_connected() || has_handshake();
|
|
|
|
}
|
|
|
|
|
2023-07-14 12:06:25 +00:00
|
|
|
// ---
|
|
|
|
|
2023-10-31 23:20:01 +00:00
|
|
|
[[nodiscard]] bool is_blocklisted(libtransmission::Blocklists const& blocklist) const
|
|
|
|
{
|
|
|
|
if (!blocklisted_.has_value())
|
|
|
|
{
|
|
|
|
blocklisted_ = blocklist.contains(listen_address());
|
|
|
|
}
|
|
|
|
|
|
|
|
return *blocklisted_;
|
|
|
|
}
|
2023-07-14 12:06:25 +00:00
|
|
|
|
|
|
|
void set_blocklisted_dirty()
|
|
|
|
{
|
|
|
|
blocklisted_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
constexpr void ban() noexcept
|
|
|
|
{
|
|
|
|
is_banned_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_banned() const noexcept
|
|
|
|
{
|
|
|
|
return is_banned_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto connection_attempt_time() const noexcept
|
|
|
|
{
|
|
|
|
return connection_attempted_at_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_connection_attempt_time(time_t value) noexcept
|
|
|
|
{
|
|
|
|
connection_attempted_at_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_latest_piece_data_time(time_t value) noexcept
|
|
|
|
{
|
|
|
|
piece_data_at_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool has_transferred_piece_data() const noexcept
|
|
|
|
{
|
|
|
|
return piece_data_at_ != time_t{};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto reconnect_interval_has_passed(time_t const now) const noexcept
|
|
|
|
{
|
|
|
|
auto const interval = now - std::max(connection_attempted_at_, connection_changed_at_);
|
|
|
|
return interval >= get_reconnect_interval_secs(now);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr std::optional<time_t> idle_secs(time_t now) const noexcept
|
|
|
|
{
|
|
|
|
if (!is_connected_)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return now - std::max(piece_data_at_, connection_changed_at_);
|
|
|
|
}
|
|
|
|
|
2023-10-19 18:05:19 +00:00
|
|
|
[[nodiscard]] auto is_inactive(time_t const now) const noexcept
|
|
|
|
{
|
|
|
|
return !is_in_use() && now - connection_changed_at_ >= UselessThresSecs;
|
|
|
|
}
|
|
|
|
|
2023-07-14 12:06:25 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
constexpr void on_connection_failed() noexcept
|
|
|
|
{
|
|
|
|
if (num_consecutive_fails_ != std::numeric_limits<decltype(num_consecutive_fails_)>::max())
|
|
|
|
{
|
|
|
|
++num_consecutive_fails_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto connection_failure_count() const noexcept
|
|
|
|
{
|
|
|
|
return num_consecutive_fails_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2023-08-05 17:37:33 +00:00
|
|
|
constexpr void set_pex_flags(uint8_t pex_flags) noexcept
|
2023-07-14 12:06:25 +00:00
|
|
|
{
|
2023-08-05 17:37:33 +00:00
|
|
|
pex_flags_ = pex_flags;
|
2023-07-14 12:06:25 +00:00
|
|
|
|
|
|
|
if ((pex_flags & ADDED_F_CONNECTABLE) != 0U)
|
|
|
|
{
|
|
|
|
set_connectable();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pex_flags & ADDED_F_UTP_FLAGS) != 0U)
|
|
|
|
{
|
|
|
|
set_utp_supported();
|
|
|
|
}
|
|
|
|
|
|
|
|
is_seed_ = (pex_flags & ADDED_F_SEED_FLAG) != 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr uint8_t pex_flags() const noexcept
|
|
|
|
{
|
|
|
|
auto ret = pex_flags_;
|
|
|
|
|
|
|
|
if (is_connectable_)
|
|
|
|
{
|
|
|
|
if (*is_connectable_)
|
|
|
|
{
|
|
|
|
ret |= ADDED_F_CONNECTABLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret &= ~ADDED_F_CONNECTABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_utp_supported_)
|
|
|
|
{
|
|
|
|
if (*is_utp_supported_)
|
|
|
|
{
|
|
|
|
ret |= ADDED_F_UTP_FLAGS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret &= ~ADDED_F_UTP_FLAGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_seed_)
|
|
|
|
{
|
|
|
|
ret |= ADDED_F_SEED_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
// ---
|
2023-07-14 12:06:25 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
// merge two peer info objects that supposedly describes the same peer
|
2023-10-14 17:58:15 +00:00
|
|
|
void merge(tr_peer_info& that) noexcept;
|
2023-07-14 12:06:25 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
private:
|
2023-07-14 12:06:25 +00:00
|
|
|
[[nodiscard]] constexpr time_t get_reconnect_interval_secs(time_t const now) const noexcept
|
|
|
|
{
|
|
|
|
// if we were recently connected to this peer and transferring piece
|
|
|
|
// data, try to reconnect to them sooner rather that later -- we don't
|
|
|
|
// want network troubles to get in the way of a good peer.
|
|
|
|
auto const unreachable = is_connectable_ && !*is_connectable_;
|
|
|
|
if (!unreachable && now - piece_data_at_ <= MinimumReconnectIntervalSecs * 2)
|
|
|
|
{
|
|
|
|
return MinimumReconnectIntervalSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, the interval depends on how many times we've tried
|
|
|
|
// and failed to connect to the peer. Penalize peers that were
|
|
|
|
// unreachable the last time we tried
|
|
|
|
auto step = this->num_consecutive_fails_;
|
|
|
|
if (unreachable)
|
|
|
|
{
|
|
|
|
step += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (step)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return 0U;
|
|
|
|
case 1:
|
|
|
|
return 10U;
|
|
|
|
case 2:
|
|
|
|
return 60U * 2U;
|
|
|
|
case 3:
|
|
|
|
return 60U * 15U;
|
|
|
|
case 4:
|
|
|
|
return 60U * 30U;
|
|
|
|
case 5:
|
|
|
|
return 60U * 60U;
|
|
|
|
default:
|
|
|
|
return 60U * 120U;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the minimum we'll wait before attempting to reconnect to a peer
|
|
|
|
static auto constexpr MinimumReconnectIntervalSecs = time_t{ 5U };
|
2023-10-19 18:05:19 +00:00
|
|
|
static auto constexpr UselessThresSecs = time_t{ 24 * 60 * 60 };
|
2023-07-14 12:06:25 +00:00
|
|
|
|
2023-08-18 03:13:01 +00:00
|
|
|
static auto inline n_known_connectable = size_t{};
|
2023-07-14 12:06:25 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
// if the port is 0, it SHOULD mean we don't know this peer's listen socket address
|
|
|
|
tr_socket_address listen_socket_address_;
|
2023-07-14 12:06:25 +00:00
|
|
|
|
|
|
|
time_t connection_attempted_at_ = {};
|
|
|
|
time_t connection_changed_at_ = {};
|
|
|
|
time_t piece_data_at_ = {};
|
|
|
|
|
|
|
|
mutable std::optional<bool> blocklisted_;
|
|
|
|
std::optional<bool> is_connectable_;
|
|
|
|
std::optional<bool> is_utp_supported_;
|
|
|
|
|
|
|
|
tr_peer_from from_first_; // where the peer was first found
|
|
|
|
tr_peer_from from_best_; // the "best" place where this peer was found
|
|
|
|
|
|
|
|
uint8_t num_consecutive_fails_ = {};
|
|
|
|
uint8_t pex_flags_ = {};
|
|
|
|
|
|
|
|
bool is_banned_ = false;
|
|
|
|
bool is_connected_ = false;
|
|
|
|
bool is_seed_ = false;
|
2023-10-14 17:58:15 +00:00
|
|
|
|
|
|
|
std::unique_ptr<tr_handshake> outgoing_handshake_;
|
2023-07-14 12:06:25 +00:00
|
|
|
};
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_pex
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-05-27 01:29:10 +00:00
|
|
|
tr_pex() = default;
|
|
|
|
|
2023-08-18 03:13:01 +00:00
|
|
|
explicit tr_pex(tr_socket_address socket_address_in, uint8_t flags_in = {})
|
|
|
|
: socket_address{ socket_address_in }
|
2022-05-27 01:29:10 +00:00
|
|
|
, flags{ flags_in }
|
|
|
|
{
|
|
|
|
}
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-10-24 18:40:12 +00:00
|
|
|
template<typename OutputIt>
|
2023-08-18 03:13:01 +00:00
|
|
|
OutputIt to_compact(OutputIt out) const
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return socket_address.to_compact(out);
|
2022-10-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputIt>
|
2023-08-18 03:13:01 +00:00
|
|
|
static OutputIt to_compact(OutputIt out, tr_pex const* pex, size_t n_pex)
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < n_pex; ++i)
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
out = pex[i].to_compact(out);
|
2022-10-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
[[nodiscard]] static std::vector<tr_pex> from_compact_ipv4(
|
2022-10-24 18:40:12 +00:00
|
|
|
void const* compact,
|
|
|
|
size_t compact_len,
|
|
|
|
uint8_t const* added_f,
|
|
|
|
size_t added_f_len);
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
[[nodiscard]] static std::vector<tr_pex> from_compact_ipv6(
|
2022-10-24 18:40:12 +00:00
|
|
|
void const* compact,
|
|
|
|
size_t compact_len,
|
|
|
|
uint8_t const* added_f,
|
|
|
|
size_t added_f_len);
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] std::string display_name() const
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return socket_address.display_name();
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
2022-05-27 01:29:10 +00:00
|
|
|
|
|
|
|
[[nodiscard]] int compare(tr_pex const& that) const noexcept // <=>
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return socket_address.compare(that.socket_address);
|
2022-05-27 01:29:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
[[nodiscard]] bool operator==(tr_pex const& that) const noexcept
|
|
|
|
{
|
|
|
|
return compare(that) == 0;
|
|
|
|
}
|
|
|
|
|
2022-05-27 01:29:10 +00:00
|
|
|
[[nodiscard]] bool operator<(tr_pex const& that) const noexcept
|
|
|
|
{
|
|
|
|
return compare(that) < 0;
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:27:52 +00:00
|
|
|
[[nodiscard]] bool is_valid_for_peers() const noexcept
|
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return socket_address.is_valid_for_peers();
|
2022-12-09 02:27:52 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 03:13:01 +00:00
|
|
|
tr_socket_address socket_address;
|
|
|
|
|
2022-05-27 01:29:10 +00:00
|
|
|
uint8_t flags = 0;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr bool tr_isPex(tr_pex const* pex)
|
2011-01-16 15:47:09 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return pex != nullptr && pex->socket_address.is_valid();
|
2011-01-16 15:47:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] tr_peerMgr* tr_peerMgrNew(tr_session* session);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerMgrFree(tr_peerMgr* manager);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::vector<tr_block_span_t> tr_peerMgrGetNextRequests(tr_torrent* torrent, tr_peer const* peer, size_t numwant);
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] bool tr_peerMgrDidPeerRequest(tr_torrent const* torrent, tr_peer const* peer, tr_block_index_t block);
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_peerMgrClientSentRequests(tr_torrent* torrent, tr_peer* peer, tr_block_span_t span);
|
2021-11-19 18:37:38 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] size_t tr_peerMgrCountActiveRequestsToPeer(tr_torrent const* torrent, tr_peer const* peer);
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2022-12-06 16:28:28 +00:00
|
|
|
void tr_peerMgrAddIncoming(tr_peerMgr* manager, tr_peer_socket&& socket);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-07-14 12:06:25 +00:00
|
|
|
size_t tr_peerMgrAddPex(tr_torrent* tor, tr_peer_from from, tr_pex const* pex, size_t n_pex);
|
2021-09-09 20:25:30 +00:00
|
|
|
|
2010-01-05 23:47:50 +00:00
|
|
|
enum
|
2009-11-20 07:47:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_PEERS_CONNECTED,
|
|
|
|
TR_PEERS_INTERESTING
|
2009-11-20 07:47:31 +00:00
|
|
|
};
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::vector<tr_pex> tr_peerMgrGetPeers(
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_torrent const* tor,
|
|
|
|
uint8_t address_type,
|
|
|
|
uint8_t peer_list_mode,
|
2022-05-27 01:29:10 +00:00
|
|
|
size_t max_peer_count);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerMgrAddTorrent(tr_peerMgr* manager, struct tr_torrent* tor);
|
2007-09-27 03:03:38 +00:00
|
|
|
|
2022-06-11 17:06:07 +00:00
|
|
|
// return the number of connected peers that have `piece`, or -1 if we already have it
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] int8_t tr_peerMgrPieceAvailability(tr_torrent const* tor, tr_piece_index_t piece);
|
2022-06-11 17:06:07 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_peerMgrTorrentAvailability(tr_torrent const* tor, int8_t* tab, unsigned int n_tabs);
|
2010-06-14 12:01:50 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] uint64_t tr_peerMgrGetDesiredAvailable(tr_torrent const* tor);
|
2008-10-20 03:47:48 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
[[nodiscard]] struct tr_peer_stat* tr_peerMgrPeerStats(tr_torrent const* tor, size_t* setme_count);
|
2009-01-13 16:32:43 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] tr_webseed_view tr_peerMgrWebseed(tr_torrent const* tor, size_t i);
|
2010-09-25 00:34:15 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|