2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 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
|
2022-01-25 04:25:55 +00:00
|
|
|
#include <string>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <vector>
|
2008-01-10 18:52:46 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <winsock2.h> /* struct in_addr */
|
2008-10-23 02:37:21 +00:00
|
|
|
#endif
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2011-03-24 22:45:04 +00:00
|
|
|
#include "net.h" /* tr_address */
|
2013-02-03 22:40:00 +00:00
|
|
|
#include "peer-common.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "peer-socket.h"
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup peers Peers
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2021-10-11 21:54:16 +00:00
|
|
|
class tr_peerIo;
|
2021-10-07 13:33:55 +00:00
|
|
|
class tr_peerMsgs;
|
2021-10-10 00:13:40 +00:00
|
|
|
class tr_swarm;
|
2011-02-18 00:23:51 +00:00
|
|
|
struct UTPSocket;
|
2021-10-06 22:24:04 +00:00
|
|
|
struct peer_atom;
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_peerMgr;
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_peer_stat;
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
tr_pex(tr_address addr_in, tr_port port_in, uint8_t flags_in = {})
|
|
|
|
: addr{ std::move(addr_in) }
|
|
|
|
, port{ port_in }
|
|
|
|
, flags{ flags_in }
|
|
|
|
{
|
|
|
|
}
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-10-24 18:40:12 +00:00
|
|
|
template<typename OutputIt>
|
2022-12-09 02:27:52 +00:00
|
|
|
OutputIt to_compact_ipv4(OutputIt out) const
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
return this->addr.to_compact_ipv4(out, this->port);
|
2022-10-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputIt>
|
2022-12-09 02:27:52 +00:00
|
|
|
OutputIt to_compact_ipv6(OutputIt out) const
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
return this->addr.to_compact_ipv6(out, this->port);
|
2022-10-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputIt>
|
2022-12-09 02:27:52 +00:00
|
|
|
static OutputIt to_compact_ipv4(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)
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
out = pex[i].to_compact_ipv4(out);
|
2022-10-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputIt>
|
2022-12-09 02:27:52 +00:00
|
|
|
static OutputIt to_compact_ipv6(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)
|
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
out = pex[i].to_compact_ipv6(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-04-21 23:37:02 +00:00
|
|
|
template<typename OutputIt>
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] OutputIt display_name(OutputIt out) const
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return addr.display_name(out, port);
|
2022-01-25 04:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] std::string display_name() const
|
2022-01-25 04:25:55 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return addr.display_name(port);
|
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 // <=>
|
|
|
|
{
|
|
|
|
if (auto const i = addr.compare(that.addr); i != 0)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port != that.port)
|
|
|
|
{
|
|
|
|
return port < that.port ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return addr.is_valid_for_peers(port);
|
|
|
|
}
|
|
|
|
|
2022-05-27 01:29:10 +00:00
|
|
|
tr_address addr = {};
|
|
|
|
tr_port port = {}; /* this field is in network byte order */
|
|
|
|
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
|
|
|
{
|
2022-12-09 02:27:52 +00:00
|
|
|
return pex != nullptr && pex->addr.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-05-26 17:17:03 +00:00
|
|
|
void tr_peerMgrSetUtpSupported(tr_torrent* tor, tr_address const& addr);
|
2011-02-18 00:41:06 +00:00
|
|
|
|
2022-05-26 17:17:03 +00:00
|
|
|
void tr_peerMgrSetUtpFailed(tr_torrent* tor, tr_address const& addr, bool failed);
|
2011-02-18 00:43:31 +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
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
size_t tr_peerMgrAddPex(tr_torrent* tor, uint8_t from, tr_pex const* pex, size_t n_pex);
|
|
|
|
|
|
|
|
void tr_peerMgrSetSwarmIsAllSeeds(tr_torrent* tor);
|
2010-04-20 21:54:03 +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_peerMgrStartTorrent(tr_torrent* tor);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerMgrStopTorrent(tr_torrent* tor);
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerMgrRemoveTorrent(tr_torrent* tor);
|
2011-02-24 14:35:45 +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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerMgrOnTorrentGotMetainfo(tr_torrent* tor);
|
2008-10-20 03:47:48 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_peerMgrOnBlocklistChanged(tr_peerMgr* mgr);
|
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
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
[[nodiscard]] tr_bytes_per_second_t tr_peerGetPieceSpeedBytesPerSecond(
|
|
|
|
tr_peer const* peer,
|
|
|
|
uint64_t now,
|
|
|
|
tr_direction direction);
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerMgrClearInterest(tr_torrent* tor);
|
2015-01-02 11:15:31 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
void tr_peerMgrGotBadPiece(tr_torrent* tor, tr_piece_index_t piece_index);
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerMgrPieceCompleted(tr_torrent* tor, tr_piece_index_t pieceIndex);
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|