2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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.
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2022-08-11 17:28:37 +00:00
|
|
|
#include <array>
|
2023-05-13 00:31:59 +00:00
|
|
|
#include <bitset>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cerrno>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint> // uint8_t, uint32_t, int64_t
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2024-03-16 00:52:09 +00:00
|
|
|
#include <deque>
|
2022-05-27 01:29:10 +00:00
|
|
|
#include <iterator>
|
2021-10-07 13:33:55 +00:00
|
|
|
#include <memory> // std::unique_ptr
|
2021-11-19 18:37:38 +00:00
|
|
|
#include <optional>
|
2022-08-29 02:51:26 +00:00
|
|
|
#include <queue>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <ratio>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <tuple>
|
2024-03-16 00:52:09 +00:00
|
|
|
#include <unordered_map>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-05-27 01:29:10 +00:00
|
|
|
#include <vector>
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-04-01 23:00:51 +00:00
|
|
|
|
2023-12-01 22:48:16 +00:00
|
|
|
#include <small/vector.hpp>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/bitfield.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/block-info.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/cache.h"
|
|
|
|
#include "libtransmission/crypto-utils.h"
|
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-08 15:24:03 +00:00
|
|
|
#include "libtransmission/peer-common.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/peer-io.h"
|
|
|
|
#include "libtransmission/peer-mgr.h"
|
|
|
|
#include "libtransmission/peer-msgs.h"
|
|
|
|
#include "libtransmission/quark.h"
|
|
|
|
#include "libtransmission/session.h"
|
|
|
|
#include "libtransmission/timer.h"
|
|
|
|
#include "libtransmission/torrent-magnet.h"
|
|
|
|
#include "libtransmission/torrent.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-buffer.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/variant.h"
|
|
|
|
#include "libtransmission/version.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
struct tr_error;
|
|
|
|
|
2014-06-29 01:43:27 +00:00
|
|
|
#ifndef EBADMSG
|
2017-04-19 12:04:45 +00:00
|
|
|
#define EBADMSG EINVAL
|
2014-06-29 01:43:27 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
using namespace std::literals;
|
2023-06-27 15:51:20 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-06-27 15:51:20 +00:00
|
|
|
// initial capacity is big enough to hold a BtPeerMsgs::Piece message
|
|
|
|
using MessageBuffer = libtransmission::StackBuffer<tr_block_info::BlockSize + 16U, std::byte, std::ratio<5, 1>>;
|
2023-05-18 21:56:29 +00:00
|
|
|
using MessageReader = libtransmission::BufferReader<std::byte>;
|
|
|
|
using MessageWriter = libtransmission::BufferWriter<std::byte>;
|
2022-08-11 17:28:37 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
// these values are hardcoded by various BEPs as noted
|
2022-02-09 02:25:19 +00:00
|
|
|
namespace BtPeerMsgs
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0003.html#peer-messages
|
2022-02-09 02:25:19 +00:00
|
|
|
auto constexpr Choke = uint8_t{ 0 };
|
|
|
|
auto constexpr Unchoke = uint8_t{ 1 };
|
|
|
|
auto constexpr Interested = uint8_t{ 2 };
|
|
|
|
auto constexpr NotInterested = uint8_t{ 3 };
|
|
|
|
auto constexpr Have = uint8_t{ 4 };
|
|
|
|
auto constexpr Bitfield = uint8_t{ 5 };
|
|
|
|
auto constexpr Request = uint8_t{ 6 };
|
|
|
|
auto constexpr Piece = uint8_t{ 7 };
|
|
|
|
auto constexpr Cancel = uint8_t{ 8 };
|
2022-04-21 15:58:13 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0005.html#bittorrent-protocol-extension
|
2022-02-09 02:25:19 +00:00
|
|
|
auto constexpr Port = uint8_t{ 9 };
|
|
|
|
|
|
|
|
// https://www.bittorrent.org/beps/bep_0006.html
|
|
|
|
auto constexpr FextSuggest = uint8_t{ 13 };
|
|
|
|
auto constexpr FextHaveAll = uint8_t{ 14 };
|
|
|
|
auto constexpr FextHaveNone = uint8_t{ 15 };
|
|
|
|
auto constexpr FextReject = uint8_t{ 16 };
|
|
|
|
auto constexpr FextAllowedFast = uint8_t{ 17 };
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
2022-02-09 02:25:19 +00:00
|
|
|
// see also LtepMessageIds below
|
|
|
|
auto constexpr Ltep = uint8_t{ 20 };
|
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
[[nodiscard]] constexpr std::string_view debug_name(uint8_t type) noexcept
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Bitfield:
|
|
|
|
return "bitfield"sv;
|
|
|
|
case Cancel:
|
|
|
|
return "cancel"sv;
|
|
|
|
case Choke:
|
|
|
|
return "choke"sv;
|
|
|
|
case FextAllowedFast:
|
|
|
|
return "fext-allow-fast"sv;
|
|
|
|
case FextHaveAll:
|
|
|
|
return "fext-have-all"sv;
|
|
|
|
case FextHaveNone:
|
|
|
|
return "fext-have-none"sv;
|
|
|
|
case FextReject:
|
|
|
|
return "fext-reject"sv;
|
|
|
|
case FextSuggest:
|
|
|
|
return "fext-suggest"sv;
|
|
|
|
case Have:
|
|
|
|
return "have"sv;
|
|
|
|
case Interested:
|
|
|
|
return "interested"sv;
|
|
|
|
case Ltep:
|
|
|
|
return "ltep"sv;
|
|
|
|
case NotInterested:
|
|
|
|
return "not-interested"sv;
|
|
|
|
case Piece:
|
|
|
|
return "piece"sv;
|
|
|
|
case Port:
|
|
|
|
return "port"sv;
|
|
|
|
case Request:
|
|
|
|
return "request"sv;
|
|
|
|
case Unchoke:
|
|
|
|
return "unchoke"sv;
|
|
|
|
default:
|
|
|
|
return "unknown"sv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
} // namespace BtPeerMsgs
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
namespace LtepMessages
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
2022-02-09 02:25:19 +00:00
|
|
|
auto constexpr Handshake = uint8_t{ 0 };
|
|
|
|
|
|
|
|
} // namespace LtepMessages
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
2021-10-21 00:39:05 +00:00
|
|
|
// Client-defined extension message IDs that we tell peers about
|
|
|
|
// in the LTEP handshake and will respond to when sent in an LTEP
|
|
|
|
// message.
|
2024-01-08 14:32:58 +00:00
|
|
|
enum LtepMessageIds : uint8_t
|
2021-10-21 00:39:05 +00:00
|
|
|
{
|
|
|
|
// we support peer exchange (bep 11)
|
2022-02-09 02:25:19 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0011.html
|
2017-04-19 12:04:45 +00:00
|
|
|
UT_PEX_ID = 1,
|
2021-10-21 00:39:05 +00:00
|
|
|
|
|
|
|
// we support sending metadata files (bep 9)
|
2022-02-09 02:25:19 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0009.html
|
2021-10-21 00:39:05 +00:00
|
|
|
// see also MetadataMsgType below
|
2017-04-19 12:04:45 +00:00
|
|
|
UT_METADATA_ID = 3,
|
2021-10-21 00:39:05 +00:00
|
|
|
};
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0009.html
|
2022-02-09 02:25:19 +00:00
|
|
|
namespace MetadataMsgType
|
2021-10-21 00:39:05 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
|
2024-02-13 16:42:19 +00:00
|
|
|
auto constexpr Request = 0;
|
|
|
|
auto constexpr Data = 1;
|
|
|
|
auto constexpr Reject = 2;
|
2022-02-09 02:25:19 +00:00
|
|
|
|
|
|
|
} // namespace MetadataMsgType
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto constexpr MinChokePeriodSec = time_t{ 10 };
|
2021-10-21 00:39:05 +00:00
|
|
|
|
|
|
|
// idle seconds before we send a keepalive
|
2024-03-16 00:52:09 +00:00
|
|
|
auto constexpr KeepaliveIntervalSecs = time_t{ 100 };
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto constexpr MetadataReqQ = size_t{ 64U };
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2024-02-13 16:42:19 +00:00
|
|
|
auto constexpr ReqQ = 512;
|
2021-10-21 00:39:05 +00:00
|
|
|
|
|
|
|
// when we're making requests from another peer,
|
|
|
|
// batch them together to send enough requests to
|
|
|
|
// meet our bandwidth goals for the next N seconds
|
2024-03-16 00:52:09 +00:00
|
|
|
auto constexpr RequestBufSecs = time_t{ 10 };
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2021-09-16 16:22:33 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto constexpr MaxPexPeerCount = size_t{ 50U };
|
2021-09-16 16:22:33 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2021-09-16 16:22:33 +00:00
|
|
|
|
2024-01-08 14:32:58 +00:00
|
|
|
enum class EncryptionPreference : uint8_t
|
2013-02-03 22:45:32 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
Unknown,
|
|
|
|
Yes,
|
|
|
|
No
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2013-02-03 22:45:32 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2008-03-01 14:09:18 +00:00
|
|
|
|
2009-11-08 23:20:00 +00:00
|
|
|
struct peer_request
|
|
|
|
{
|
2022-04-29 17:44:22 +00:00
|
|
|
uint32_t index = 0;
|
|
|
|
uint32_t offset = 0;
|
|
|
|
uint32_t length = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] auto constexpr operator==(peer_request const& that) const noexcept
|
|
|
|
{
|
|
|
|
return this->index == that.index && this->offset == that.offset && this->length == that.length;
|
|
|
|
}
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] static auto from_block(tr_torrent const& tor, tr_block_index_t block) noexcept
|
|
|
|
{
|
|
|
|
auto const loc = tor.block_loc(block);
|
|
|
|
return peer_request{ loc.piece, loc.piece_offset, tor.block_size(block) };
|
|
|
|
}
|
|
|
|
};
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2007-11-16 20:40:03 +00:00
|
|
|
/* this is raw, unchanged data from the peer regarding
|
|
|
|
* the current message that it's sending us. */
|
|
|
|
struct tr_incoming
|
|
|
|
{
|
2023-03-03 23:43:51 +00:00
|
|
|
std::optional<uint32_t> length; // the full message payload length. Includes the +1 for id length
|
|
|
|
std::optional<uint8_t> id; // the protocol message, e.g. BtPeerMsgs::Piece
|
2023-05-18 21:56:29 +00:00
|
|
|
MessageBuffer payload;
|
2023-02-14 19:50:28 +00:00
|
|
|
|
|
|
|
struct incoming_piece_data
|
|
|
|
{
|
|
|
|
explicit incoming_piece_data(uint32_t block_size)
|
2023-05-13 19:16:00 +00:00
|
|
|
: buf{ std::make_unique<Cache::BlockData>(block_size) }
|
2023-05-13 00:31:59 +00:00
|
|
|
, block_size_{ block_size }
|
2023-02-14 19:50:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-13 00:31:59 +00:00
|
|
|
[[nodiscard]] bool add_span(size_t begin, size_t end)
|
|
|
|
{
|
|
|
|
if (begin > end || end > block_size_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; begin < end; ++begin)
|
|
|
|
{
|
|
|
|
have_.set(begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto has_all() const noexcept
|
|
|
|
{
|
|
|
|
return have_.count() >= block_size_;
|
|
|
|
}
|
|
|
|
|
2023-05-13 19:16:00 +00:00
|
|
|
std::unique_ptr<Cache::BlockData> buf;
|
2023-05-13 00:31:59 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::bitset<tr_block_info::BlockSize> have_;
|
2024-03-16 00:52:09 +00:00
|
|
|
uint32_t const block_size_;
|
2023-02-14 19:50:28 +00:00
|
|
|
};
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
std::unordered_map<tr_block_index_t, incoming_piece_data> blocks;
|
2007-11-16 20:40:03 +00:00
|
|
|
};
|
|
|
|
|
2022-07-02 19:42:16 +00:00
|
|
|
#define myLogMacro(msgs, level, text) \
|
|
|
|
do \
|
|
|
|
{ \
|
2023-04-18 04:11:09 +00:00
|
|
|
if (tr_logLevelIsActive(level)) \
|
2022-07-02 19:42:16 +00:00
|
|
|
{ \
|
|
|
|
tr_logAddMessage( \
|
|
|
|
__FILE__, \
|
|
|
|
__LINE__, \
|
|
|
|
(level), \
|
2024-03-16 00:52:09 +00:00
|
|
|
fmt::format("{:s} [{:s}]: {:s}", (msgs)->display_name(), (msgs)->user_agent().sv(), text), \
|
|
|
|
(msgs)->tor_.name()); \
|
2022-07-02 19:42:16 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define logdbg(msgs, text) myLogMacro(msgs, TR_LOG_DEBUG, text)
|
|
|
|
#define logtrace(msgs, text) myLogMacro(msgs, TR_LOG_TRACE, text)
|
2023-03-03 23:43:51 +00:00
|
|
|
#define logwarn(msgs, text) myLogMacro(msgs, TR_LOG_WARN, text)
|
2022-07-02 19:42:16 +00:00
|
|
|
|
2023-04-14 21:03:08 +00:00
|
|
|
using ReadResult = std::pair<ReadState, size_t /*n_piece_data_bytes_read*/>;
|
|
|
|
|
2008-12-11 17:02:34 +00:00
|
|
|
/**
|
|
|
|
* Low-level communication state information about a connected peer.
|
|
|
|
*
|
|
|
|
* This structure remembers the low-level protocol states that we're
|
|
|
|
* in with this peer, such as active requests, pex messages, and so on.
|
|
|
|
* Its fields are all private to peer-msgs.c.
|
|
|
|
*
|
|
|
|
* Data not directly involved with sending & receiving messages is
|
|
|
|
* stored in tr_peer, where it can be accessed by both peermsgs and
|
|
|
|
* the peer manager.
|
|
|
|
*
|
|
|
|
* @see tr_peer
|
2023-07-14 12:06:25 +00:00
|
|
|
* @see tr_peer_info
|
2008-12-11 17:02:34 +00:00
|
|
|
*/
|
2022-04-29 17:44:22 +00:00
|
|
|
class tr_peerMsgsImpl final : public tr_peerMsgs
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_peerMsgsImpl(
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_torrent& torrent_in,
|
2024-05-26 00:13:15 +00:00
|
|
|
std::shared_ptr<tr_peer_info> peer_info_in,
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> io_in,
|
2023-04-14 21:03:08 +00:00
|
|
|
tr_interned_string client,
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_peer_callback_bt callback,
|
2022-09-07 16:04:28 +00:00
|
|
|
void* callback_data)
|
2024-05-26 00:13:15 +00:00
|
|
|
: tr_peerMsgs{ torrent_in, std::move(peer_info_in), client, io_in->is_encrypted(), io_in->is_incoming(), io_in->is_utp() }
|
2024-03-16 00:52:09 +00:00
|
|
|
, tor_{ torrent_in }
|
|
|
|
, io_{ std::move(io_in) }
|
|
|
|
, have_{ torrent_in.piece_count() }
|
2021-10-07 13:33:55 +00:00
|
|
|
, callback_{ callback }
|
2022-09-07 16:04:28 +00:00
|
|
|
, callback_data_{ callback_data }
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (tor_.allows_pex())
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
pex_timer_ = session->timerMaker().create([this]() { send_ut_pex(); });
|
2023-05-06 04:11:05 +00:00
|
|
|
pex_timer_->start_repeating(SendPexInterval);
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (io_->supports_ltep())
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
send_ltep_handshake();
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_bitfield();
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (session->allowsDHT() && io_->supports_dht())
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_port(session->udpPort());
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
io_->set_callbacks(can_read, did_write, got_error, this);
|
|
|
|
update_desired_request_count();
|
2023-04-14 21:03:08 +00:00
|
|
|
|
|
|
|
update_active();
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:03:28 +00:00
|
|
|
tr_peerMsgsImpl(tr_peerMsgsImpl&&) = delete;
|
|
|
|
tr_peerMsgsImpl(tr_peerMsgsImpl const&) = delete;
|
|
|
|
tr_peerMsgsImpl& operator=(tr_peerMsgsImpl&&) = delete;
|
|
|
|
tr_peerMsgsImpl& operator=(tr_peerMsgsImpl const&) = delete;
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
~tr_peerMsgsImpl() override
|
|
|
|
{
|
|
|
|
set_active(TR_UP, false);
|
|
|
|
set_active(TR_DOWN, false);
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (io_)
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
io_->clear();
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
|
|
|
|
2023-11-12 20:38:27 +00:00
|
|
|
[[nodiscard]] Speed get_piece_speed(uint64_t now, tr_direction dir) const override
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return io_->get_piece_speed(now, dir);
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] size_t active_req_count(tr_direction dir) const noexcept override
|
2022-04-29 17:44:22 +00:00
|
|
|
{
|
2022-06-29 01:48:39 +00:00
|
|
|
switch (dir)
|
|
|
|
{
|
|
|
|
case TR_CLIENT_TO_PEER: // requests we sent
|
2024-03-16 00:52:09 +00:00
|
|
|
return tr_peerMgrCountActiveRequestsToPeer(&tor_, this);
|
2022-06-29 01:48:39 +00:00
|
|
|
|
|
|
|
case TR_PEER_TO_CLIENT: // requests they sent
|
|
|
|
return std::size(peer_requested_);
|
|
|
|
|
|
|
|
default:
|
|
|
|
TR_ASSERT(0);
|
|
|
|
return {};
|
|
|
|
}
|
2022-04-29 17:44:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
[[nodiscard]] tr_socket_address socket_address() const override
|
2022-06-02 02:33:33 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return io_->socket_address();
|
2022-06-02 02:33:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] std::string display_name() const override
|
2022-06-02 02:33:33 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
return socket_address().display_name();
|
2022-06-02 02:33:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 05:26:10 +00:00
|
|
|
[[nodiscard]] tr_bitfield const& has() const noexcept override
|
2022-06-27 19:12:31 +00:00
|
|
|
{
|
2022-11-23 05:26:10 +00:00
|
|
|
return have_;
|
2022-06-27 19:12:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
2022-06-27 19:12:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void on_torrent_got_metainfo() noexcept override
|
2022-06-27 19:12:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
update_active();
|
2022-06-27 19:12:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
void cancel_block_request(tr_block_index_t block) override
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
cancels_sent_to_peer.add(tr_time(), 1);
|
|
|
|
protocol_send_cancel(peer_request::from_block(tor_, block));
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_choke(bool peer_is_choked) override
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const now = tr_time();
|
|
|
|
auto const fibrillation_time = now - MinChokePeriodSec;
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (choke_changed_at_ > fibrillation_time)
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, fmt::format("Not changing choke to {} to avoid fibrillation", peer_is_choked));
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
2023-04-14 21:03:08 +00:00
|
|
|
else if (this->peer_is_choked() != peer_is_choked)
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2023-04-14 21:03:08 +00:00
|
|
|
set_peer_choked(peer_is_choked);
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0006.html#reject-request
|
|
|
|
// A peer SHOULD choke first and then reject requests so that
|
|
|
|
// the peer receiving the choke does not re-request the pieces.
|
|
|
|
protocol_send_choke(peer_is_choked);
|
2023-04-14 21:03:08 +00:00
|
|
|
if (peer_is_choked)
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
reject_all_requests();
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
choke_changed_at_ = now;
|
2021-10-07 13:33:55 +00:00
|
|
|
update_active(TR_CLIENT_TO_PEER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void pulse() override;
|
2021-10-07 13:33:55 +00:00
|
|
|
|
|
|
|
void on_piece_completed(tr_piece_index_t piece) override
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_have(piece);
|
2021-10-07 13:33:55 +00:00
|
|
|
|
|
|
|
// since we have more pieces now, we might not be interested in this peer
|
2024-03-16 00:52:09 +00:00
|
|
|
update_interest();
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_interested(bool interested) override
|
|
|
|
{
|
2023-04-14 21:03:08 +00:00
|
|
|
if (client_is_interested() != interested)
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2023-04-14 21:03:08 +00:00
|
|
|
set_client_interested(interested);
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_interest(interested);
|
2021-10-07 13:33:55 +00:00
|
|
|
update_active(TR_PEER_TO_CLIENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void request_blocks(tr_block_span_t const* block_spans, size_t n_spans) override
|
2022-06-20 04:08:58 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
TR_ASSERT(tor_.client_can_download());
|
2023-04-14 21:03:08 +00:00
|
|
|
TR_ASSERT(client_is_interested());
|
|
|
|
TR_ASSERT(!client_is_choked());
|
2022-07-02 19:42:16 +00:00
|
|
|
|
|
|
|
for (auto const *span = block_spans, *span_end = span + n_spans; span != span_end; ++span)
|
|
|
|
{
|
|
|
|
for (auto [block, block_end] = *span; block < block_end; ++block)
|
|
|
|
{
|
|
|
|
// Note that requests can't cross over a piece boundary.
|
|
|
|
// So if a piece isn't evenly divisible by the block size,
|
|
|
|
// we need to split our block request info per-piece chunks.
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const byte_begin = tor_.block_loc(block).byte;
|
|
|
|
auto const block_size = tor_.block_size(block);
|
2022-07-02 19:42:16 +00:00
|
|
|
auto const byte_end = byte_begin + block_size;
|
|
|
|
for (auto offset = byte_begin; offset < byte_end;)
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const loc = tor_.byte_loc(offset);
|
2022-07-02 19:42:16 +00:00
|
|
|
auto const left_in_block = block_size - loc.block_offset;
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const left_in_piece = tor_.piece_size(loc.piece) - loc.piece_offset;
|
2022-07-02 19:42:16 +00:00
|
|
|
auto const req_len = std::min(left_in_block, left_in_piece);
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_request({ loc.piece, loc.piece_offset, req_len });
|
2022-07-02 19:42:16 +00:00
|
|
|
offset += req_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_peerMgrClientSentRequests(&tor_, this, *span);
|
2022-07-02 19:42:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void update_active()
|
2022-07-04 18:03:32 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
update_active(TR_UP);
|
|
|
|
update_active(TR_DOWN);
|
2022-07-04 18:03:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void update_active(tr_direction direction)
|
|
|
|
{
|
|
|
|
TR_ASSERT(tr_isDirection(direction));
|
|
|
|
set_active(direction, calculate_active(direction));
|
|
|
|
}
|
2022-08-11 17:28:37 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] bool calculate_active(tr_direction direction) const
|
2022-09-01 21:37:11 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (direction == TR_CLIENT_TO_PEER)
|
2022-09-01 21:37:11 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return peer_is_interested() && !peer_is_choked();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TR_PEER_TO_CLIENT
|
|
|
|
|
|
|
|
if (!tor_.has_metainfo())
|
|
|
|
{
|
|
|
|
return true;
|
2022-09-01 21:37:11 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
|
|
|
|
auto const active = client_is_interested() && !client_is_choked();
|
|
|
|
TR_ASSERT(!active || !tor_.is_done());
|
|
|
|
return active;
|
2022-09-01 21:37:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
private:
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
void update_interest()
|
2022-07-04 18:03:32 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
// TODO(ckerr) -- might need to poke the mgr on startup
|
|
|
|
|
|
|
|
// additional note (tearfur)
|
|
|
|
// by "poke the mgr", Charles probably meant calling isPeerInteresting(),
|
|
|
|
// then pass the result to set_interesting()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
[[nodiscard]] bool is_valid_request(peer_request const& req) const;
|
|
|
|
|
|
|
|
void reject_all_requests()
|
|
|
|
{
|
|
|
|
auto& queue = peer_requested_;
|
|
|
|
|
|
|
|
if (auto const must_send_rej = io_->supports_fext(); must_send_rej)
|
2022-07-04 18:03:32 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
std::for_each(std::begin(queue), std::end(queue), [this](peer_request const& req) { protocol_send_reject(req); });
|
2022-07-04 18:03:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
queue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool can_add_request_from_peer(peer_request const& req);
|
|
|
|
|
|
|
|
void on_peer_made_request(peer_request const& req)
|
|
|
|
{
|
|
|
|
if (can_add_request_from_peer(req))
|
2022-07-04 18:03:32 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
peer_requested_.emplace_back(req);
|
2022-07-04 18:03:32 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
else if (io_->supports_fext())
|
2022-07-04 18:03:32 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_reject(req);
|
2022-07-04 18:03:32 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
}
|
2022-07-04 18:03:32 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// how many blocks could we request from this peer right now?
|
|
|
|
[[nodiscard]] size_t max_available_reqs() const;
|
2023-03-12 04:25:21 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void update_desired_request_count()
|
|
|
|
{
|
|
|
|
desired_request_count_ = max_available_reqs();
|
2022-07-04 18:03:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void update_block_requests();
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<int64_t> pop_next_metadata_request()
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
auto& reqs = peer_requested_metadata_pieces_;
|
|
|
|
|
|
|
|
if (std::empty(reqs))
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return {};
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto next = reqs.front();
|
|
|
|
reqs.pop();
|
|
|
|
return next;
|
|
|
|
}
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void update_metadata_requests(time_t now) const;
|
|
|
|
[[nodiscard]] size_t add_next_metadata_piece();
|
|
|
|
[[nodiscard]] size_t add_next_block(time_t now_sec, uint64_t now_msec);
|
|
|
|
[[nodiscard]] size_t fill_output_buffer(time_t now_sec, uint64_t now_msec);
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
void send_ltep_handshake();
|
|
|
|
void parse_ltep_handshake(MessageReader& payload);
|
|
|
|
void parse_ut_metadata(MessageReader& payload_in);
|
|
|
|
void parse_ut_pex(MessageReader& payload);
|
|
|
|
void parse_ltep(MessageReader& payload);
|
|
|
|
|
|
|
|
void send_ut_pex();
|
|
|
|
|
|
|
|
int client_got_block(std::unique_ptr<Cache::BlockData> block_data, tr_block_index_t block);
|
|
|
|
ReadResult read_piece_data(MessageReader& payload);
|
|
|
|
ReadResult process_peer_message(uint8_t id, MessageReader& payload);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
size_t protocol_send_keepalive() const;
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
size_t protocol_send_message(uint8_t type, Args const&... args) const;
|
|
|
|
|
|
|
|
size_t protocol_send_reject(peer_request const& req) const
|
|
|
|
{
|
|
|
|
TR_ASSERT(io_->supports_fext());
|
|
|
|
return protocol_send_message(BtPeerMsgs::FextReject, req.index, req.offset, req.length);
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t protocol_send_cancel(peer_request const& req) const
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return protocol_send_message(BtPeerMsgs::Cancel, req.index, req.offset, req.length);
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t protocol_send_request(peer_request const& req) const
|
2023-04-14 21:03:08 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
TR_ASSERT(is_valid_request(req));
|
|
|
|
return protocol_send_message(BtPeerMsgs::Request, req.index, req.offset, req.length);
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t protocol_send_port(tr_port const port) const
|
|
|
|
{
|
|
|
|
return protocol_send_message(BtPeerMsgs::Port, port.host());
|
2023-04-14 21:03:08 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t protocol_send_have(tr_piece_index_t const index) const
|
|
|
|
{
|
|
|
|
static_assert(sizeof(tr_piece_index_t) == sizeof(uint32_t));
|
|
|
|
return protocol_send_message(BtPeerMsgs::Have, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t protocol_send_choke(bool const choke) const
|
|
|
|
{
|
|
|
|
return protocol_send_message(choke ? BtPeerMsgs::Choke : BtPeerMsgs::Unchoke);
|
|
|
|
}
|
|
|
|
|
|
|
|
void protocol_send_interest(bool const b) const
|
|
|
|
{
|
|
|
|
protocol_send_message(b ? BtPeerMsgs::Interested : BtPeerMsgs::NotInterested);
|
|
|
|
}
|
|
|
|
|
|
|
|
void protocol_send_bitfield();
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
void publish(tr_peer_event const& peer_event)
|
|
|
|
{
|
|
|
|
if (callback_ != nullptr)
|
|
|
|
{
|
|
|
|
(*callback_)(this, peer_event, callback_data_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
static void did_write(tr_peerIo* /*io*/, size_t bytes_written, bool was_piece_data, void* vmsgs);
|
|
|
|
static ReadState can_read(tr_peerIo* io, void* vmsgs, size_t* piece);
|
|
|
|
static void got_error(tr_peerIo* /*io*/, tr_error const& /*error*/, void* vmsgs);
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
2009-06-24 04:14:39 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
bool peer_supports_pex_ = false;
|
|
|
|
bool peer_supports_metadata_xfer_ = false;
|
|
|
|
bool client_sent_ltep_handshake_ = false;
|
2013-02-03 22:45:32 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t desired_request_count_ = 0;
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
uint8_t ut_pex_id_ = 0;
|
|
|
|
uint8_t ut_metadata_id_ = 0;
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_port dht_port_;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
EncryptionPreference encryption_preference_ = EncryptionPreference::Unknown;
|
2022-06-29 01:48:39 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_torrent& tor_;
|
2008-03-01 14:09:18 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
std::shared_ptr<tr_peerIo> const io_;
|
2022-05-27 01:29:10 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
std::deque<peer_request> peer_requested_;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
std::array<std::vector<tr_pex>, NUM_TR_AF_INET_TYPES> pex_;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
std::queue<int64_t> peer_requested_metadata_pieces_;
|
2008-05-31 00:16:26 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
time_t client_sent_at_ = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
time_t choke_changed_at_ = 0;
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_incoming incoming_ = {};
|
|
|
|
|
|
|
|
// if the peer supports the Extension Protocol in BEP 10 and
|
|
|
|
// supplied a reqq argument, it's stored here.
|
|
|
|
std::optional<size_t> reqq_;
|
2009-06-15 02:22:41 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
std::unique_ptr<libtransmission::Timer> pex_timer_;
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
tr_bitfield have_;
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_peer_callback_bt const callback_;
|
2022-09-07 16:04:28 +00:00
|
|
|
void* const callback_data_;
|
2022-06-27 19:12:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// seconds between periodic send_ut_pex() calls
|
2022-08-11 17:28:37 +00:00
|
|
|
static auto constexpr SendPexInterval = 90s;
|
2021-10-07 13:33:55 +00:00
|
|
|
};
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] constexpr bool is_message_length_correct(tr_torrent const& tor, uint8_t id, uint32_t len)
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case BtPeerMsgs::Choke:
|
|
|
|
case BtPeerMsgs::Unchoke:
|
|
|
|
case BtPeerMsgs::Interested:
|
|
|
|
case BtPeerMsgs::NotInterested:
|
|
|
|
case BtPeerMsgs::FextHaveAll:
|
|
|
|
case BtPeerMsgs::FextHaveNone:
|
|
|
|
return len == 1U;
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
case BtPeerMsgs::Have:
|
|
|
|
case BtPeerMsgs::FextSuggest:
|
|
|
|
case BtPeerMsgs::FextAllowedFast:
|
|
|
|
return len == 5U;
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
case BtPeerMsgs::Bitfield:
|
2024-03-16 00:52:09 +00:00
|
|
|
return !tor.has_metainfo() || len == 1 + ((tor.piece_count() + 7U) / 8U);
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
case BtPeerMsgs::Request:
|
|
|
|
case BtPeerMsgs::Cancel:
|
|
|
|
case BtPeerMsgs::FextReject:
|
|
|
|
return len == 13U;
|
|
|
|
|
|
|
|
case BtPeerMsgs::Piece:
|
|
|
|
len -= sizeof(id) + sizeof(uint32_t /*piece*/) + sizeof(uint32_t /*offset*/);
|
|
|
|
return len <= tr_block_info::BlockSize;
|
|
|
|
|
|
|
|
case BtPeerMsgs::Port:
|
|
|
|
return len == 3U;
|
|
|
|
|
|
|
|
case BtPeerMsgs::Ltep:
|
|
|
|
return len >= 2U;
|
|
|
|
|
|
|
|
default: // unrecognized message
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
namespace protocol_send_message_helpers
|
|
|
|
{
|
|
|
|
[[nodiscard]] constexpr auto get_param_length(uint8_t param) noexcept
|
|
|
|
{
|
|
|
|
return sizeof(param);
|
|
|
|
}
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
[[nodiscard]] constexpr auto get_param_length(uint16_t param) noexcept
|
|
|
|
{
|
|
|
|
return sizeof(param);
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
[[nodiscard]] constexpr auto get_param_length(uint32_t param) noexcept
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
return sizeof(param);
|
|
|
|
}
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto get_param_length(T const& param) noexcept
|
|
|
|
{
|
|
|
|
return std::size(param);
|
2009-05-19 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
// ---
|
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
void add_param(MessageWriter& buffer, uint8_t param) noexcept
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
buffer.add_uint8(param);
|
|
|
|
}
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
void add_param(MessageWriter& buffer, uint16_t param) noexcept
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
|
|
|
buffer.add_uint16(param);
|
|
|
|
}
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
void add_param(MessageWriter& buffer, uint32_t param) noexcept
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
|
|
|
buffer.add_uint32(param);
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-14 22:24:42 +00:00
|
|
|
template<typename T>
|
2023-05-18 21:56:29 +00:00
|
|
|
void add_param(MessageWriter& buffer, T const& param) noexcept
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
buffer.add(param);
|
|
|
|
}
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
// ---
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
[[nodiscard]] std::string log_param(uint8_t param)
|
|
|
|
{
|
|
|
|
return fmt::format(" {:d}", static_cast<int>(param));
|
2008-12-02 17:10:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
[[nodiscard]] std::string log_param(uint16_t param)
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
return fmt::format(" {:d}", static_cast<int>(param));
|
|
|
|
}
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
[[nodiscard]] std::string log_param(uint32_t param)
|
|
|
|
{
|
|
|
|
return fmt::format(" {:d}", static_cast<int>(param));
|
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] std::string log_param(T const& /*unused*/)
|
|
|
|
{
|
|
|
|
return " []";
|
|
|
|
}
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
template<typename... Args>
|
|
|
|
[[nodiscard]] std::string build_log_message(uint8_t type, Args const&... args)
|
|
|
|
{
|
|
|
|
auto text = fmt::format("sending '{:s}'", BtPeerMsgs::debug_name(type));
|
|
|
|
(text.append(log_param(args)), ...);
|
|
|
|
return text;
|
2008-12-02 17:10:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-14 22:24:42 +00:00
|
|
|
template<typename... Args>
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t build_peer_message(MessageWriter& out, uint8_t type, Args const&... args)
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
auto msg_len = sizeof(type);
|
|
|
|
((msg_len += get_param_length(args)), ...);
|
|
|
|
out.add_uint32(msg_len);
|
|
|
|
out.add_uint8(type);
|
|
|
|
(add_param(out, args), ...);
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
return msg_len;
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
|
|
|
} // namespace protocol_send_message_helpers
|
|
|
|
|
|
|
|
template<typename... Args>
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t tr_peerMsgsImpl::protocol_send_message(uint8_t type, Args const&... args) const
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
|
|
|
using namespace protocol_send_message_helpers;
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, build_log_message(type, args...));
|
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
auto out = MessageBuffer{};
|
2024-03-16 00:52:09 +00:00
|
|
|
[[maybe_unused]] auto const msg_len = build_peer_message(out, type, args...);
|
|
|
|
TR_ASSERT(is_message_length_correct(tor_, type, msg_len));
|
2023-04-15 00:31:09 +00:00
|
|
|
auto const n_bytes_added = std::size(out);
|
2024-03-16 00:52:09 +00:00
|
|
|
io_->write(out, type == BtPeerMsgs::Piece);
|
2023-04-15 00:31:09 +00:00
|
|
|
return n_bytes_added;
|
|
|
|
}
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::protocol_send_bitfield()
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
bool const fext = io_->supports_fext();
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (fext && tor_.has_all())
|
|
|
|
{
|
|
|
|
protocol_send_message(BtPeerMsgs::FextHaveAll);
|
|
|
|
}
|
|
|
|
else if (fext && tor_.has_none())
|
|
|
|
{
|
|
|
|
protocol_send_message(BtPeerMsgs::FextHaveNone);
|
|
|
|
}
|
|
|
|
else if (!tor_.has_none())
|
|
|
|
{
|
|
|
|
// https://www.bittorrent.org/beps/bep_0003.html#peer-messages
|
|
|
|
// Downloaders which don't have anything yet may skip the 'bitfield' message.
|
|
|
|
protocol_send_message(BtPeerMsgs::Bitfield, tor_.create_piece_bitfield());
|
|
|
|
}
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t tr_peerMsgsImpl::protocol_send_keepalive() const
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "sending 'keepalive'");
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto out = MessageBuffer{};
|
|
|
|
out.add_uint32(0);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const n_bytes_added = std::size(out);
|
|
|
|
io_->write(out, false);
|
|
|
|
return n_bytes_added;
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
2008-12-22 00:52:44 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::parse_ltep(MessageReader& payload)
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
TR_ASSERT(!std::empty(payload));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const ltep_msgid = payload.to_uint8();
|
2023-04-15 00:31:09 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (ltep_msgid == LtepMessages::Handshake)
|
|
|
|
{
|
|
|
|
logtrace(this, "got ltep handshake");
|
|
|
|
parse_ltep_handshake(payload);
|
2023-04-15 00:31:09 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (io_->supports_ltep())
|
|
|
|
{
|
|
|
|
send_ltep_handshake();
|
|
|
|
send_ut_pex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ltep_msgid == UT_PEX_ID)
|
|
|
|
{
|
|
|
|
logtrace(this, "got ut pex");
|
|
|
|
peer_supports_pex_ = true;
|
|
|
|
parse_ut_pex(payload);
|
|
|
|
}
|
|
|
|
else if (ltep_msgid == UT_METADATA_ID)
|
|
|
|
{
|
|
|
|
logtrace(this, "got ut metadata");
|
|
|
|
peer_supports_metadata_xfer_ = true;
|
|
|
|
parse_ut_metadata(payload);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logtrace(this, fmt::format("skipping unknown ltep message ({:d})", static_cast<int>(ltep_msgid)));
|
|
|
|
}
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::parse_ut_pex(MessageReader& payload)
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (!tor_.allows_pex())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (auto var = tr_variant_serde::benc().inplace().parse(payload.to_string_view()); var)
|
|
|
|
{
|
|
|
|
uint8_t const* added = nullptr;
|
|
|
|
auto added_len = size_t{};
|
|
|
|
if (tr_variantDictFindRaw(&*var, TR_KEY_added, &added, &added_len))
|
|
|
|
{
|
|
|
|
uint8_t const* added_f = nullptr;
|
|
|
|
auto added_f_len = size_t{};
|
|
|
|
if (!tr_variantDictFindRaw(&*var, TR_KEY_added_f, &added_f, &added_f_len))
|
|
|
|
{
|
|
|
|
added_f_len = 0;
|
|
|
|
added_f = nullptr;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto pex = tr_pex::from_compact_ipv4(added, added_len, added_f, added_f_len);
|
|
|
|
pex.resize(std::min(MaxPexPeerCount, std::size(pex)));
|
|
|
|
tr_peerMgrAddPex(&tor_, TR_PEER_FROM_PEX, std::data(pex), std::size(pex));
|
|
|
|
}
|
2023-12-04 17:45:37 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (tr_variantDictFindRaw(&*var, TR_KEY_added6, &added, &added_len))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
uint8_t const* added_f = nullptr;
|
|
|
|
auto added_f_len = size_t{};
|
|
|
|
if (!tr_variantDictFindRaw(&*var, TR_KEY_added6_f, &added_f, &added_f_len))
|
|
|
|
{
|
|
|
|
added_f_len = 0;
|
|
|
|
added_f = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pex = tr_pex::from_compact_ipv6(added, added_len, added_f, added_f_len);
|
|
|
|
pex.resize(std::min(MaxPexPeerCount, std::size(pex)));
|
|
|
|
tr_peerMgrAddPex(&tor_, TR_PEER_FROM_PEX, std::data(pex), std::size(pex));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::send_ut_pex()
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
// only send pex if both the torrent and peer support it
|
|
|
|
if (!peer_supports_pex_ || !tor_.allows_pex())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2007-09-29 00:46:41 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-29 00:46:41 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
static auto constexpr MaxPexAdded = size_t{ 50U };
|
|
|
|
static auto constexpr MaxPexDropped = size_t{ 50U };
|
2007-09-29 00:46:41 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto map = tr_variant::Map{ 4U };
|
|
|
|
auto tmpbuf = small::vector<std::byte, std::max(MaxPexAdded, MaxPexDropped) * tr_socket_address::CompactSockAddrMaxBytes>{};
|
|
|
|
for (uint8_t i = 0; i < NUM_TR_AF_INET_TYPES; ++i)
|
2023-06-19 00:06:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
static auto constexpr AddedMap = std::array{ TR_KEY_added, TR_KEY_added6 };
|
|
|
|
static auto constexpr AddedFMap = std::array{ TR_KEY_added_f, TR_KEY_added6_f };
|
|
|
|
static auto constexpr DroppedMap = std::array{ TR_KEY_dropped, TR_KEY_dropped6 };
|
|
|
|
auto const ip_type = static_cast<tr_address_type>(i);
|
|
|
|
|
|
|
|
auto& old_pex = pex_[i];
|
|
|
|
auto new_pex = tr_peerMgrGetPeers(&tor_, ip_type, TR_PEERS_CONNECTED, MaxPexPeerCount);
|
|
|
|
auto added = std::vector<tr_pex>{};
|
|
|
|
added.reserve(std::size(new_pex));
|
|
|
|
std::set_difference(
|
|
|
|
std::begin(new_pex),
|
|
|
|
std::end(new_pex),
|
|
|
|
std::begin(old_pex),
|
|
|
|
std::end(old_pex),
|
|
|
|
std::back_inserter(added));
|
|
|
|
auto dropped = std::vector<tr_pex>{};
|
|
|
|
dropped.reserve(std::size(old_pex));
|
|
|
|
std::set_difference(
|
|
|
|
std::begin(old_pex),
|
|
|
|
std::end(old_pex),
|
|
|
|
std::begin(new_pex),
|
|
|
|
std::end(new_pex),
|
|
|
|
std::back_inserter(dropped));
|
|
|
|
|
|
|
|
// Some peers give us error messages if we send
|
|
|
|
// more than this many peers in a single pex message.
|
|
|
|
// https://wiki.theory.org/BitTorrentPeerExchangeConventions
|
|
|
|
added.resize(std::min(std::size(added), MaxPexAdded));
|
|
|
|
dropped.resize(std::min(std::size(dropped), MaxPexDropped));
|
|
|
|
|
|
|
|
logtrace(
|
|
|
|
this,
|
|
|
|
fmt::format(
|
|
|
|
"pex: old {:s} peer count {:d}, new peer count {:d}, added {:d}, dropped {:d}",
|
|
|
|
tr_ip_protocol_to_sv(ip_type),
|
|
|
|
std::size(old_pex),
|
|
|
|
std::size(new_pex),
|
|
|
|
std::size(added),
|
|
|
|
std::size(dropped)));
|
|
|
|
|
|
|
|
// if there's nothing to send, then we're done
|
|
|
|
if (std::empty(added) && std::empty(dropped))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update msgs
|
|
|
|
std::swap(old_pex, new_pex);
|
|
|
|
|
|
|
|
// build the pex payload
|
|
|
|
if (!std::empty(added))
|
|
|
|
{
|
|
|
|
// "added"
|
|
|
|
tmpbuf.clear();
|
|
|
|
tmpbuf.reserve(std::size(added) * tr_socket_address::CompactSockAddrBytes[i]);
|
|
|
|
tr_pex::to_compact(std::back_inserter(tmpbuf), std::data(added), std::size(added));
|
|
|
|
TR_ASSERT(std::size(tmpbuf) == std::size(added) * tr_socket_address::CompactSockAddrBytes[i]);
|
|
|
|
map.try_emplace(AddedMap[i], std::string_view{ reinterpret_cast<char*>(std::data(tmpbuf)), std::size(tmpbuf) });
|
|
|
|
|
|
|
|
// "added.f"
|
|
|
|
tmpbuf.resize(std::size(added));
|
|
|
|
auto* begin = std::data(tmpbuf);
|
|
|
|
auto* walk = begin;
|
|
|
|
for (auto const& p : added)
|
|
|
|
{
|
|
|
|
*walk++ = std::byte{ p.flags };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const f_len = static_cast<size_t>(walk - begin);
|
|
|
|
TR_ASSERT(f_len == std::size(added));
|
|
|
|
map.try_emplace(AddedFMap[i], std::string_view{ reinterpret_cast<char*>(begin), f_len });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!std::empty(dropped))
|
|
|
|
{
|
|
|
|
// "dropped"
|
|
|
|
tmpbuf.clear();
|
|
|
|
tmpbuf.reserve(std::size(dropped) * tr_socket_address::CompactSockAddrBytes[i]);
|
|
|
|
tr_pex::to_compact(std::back_inserter(tmpbuf), std::data(dropped), std::size(dropped));
|
|
|
|
TR_ASSERT(std::size(tmpbuf) == std::size(dropped) * tr_socket_address::CompactSockAddrBytes[i]);
|
|
|
|
map.try_emplace(DroppedMap[i], std::string_view{ reinterpret_cast<char*>(std::data(tmpbuf)), std::size(tmpbuf) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol_send_message(BtPeerMsgs::Ltep, ut_pex_id_, tr_variant_serde::benc().to_string(tr_variant{ std::move(map) }));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_peerMsgsImpl::send_ltep_handshake()
|
|
|
|
{
|
|
|
|
if (client_sent_ltep_handshake_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logtrace(this, "sending an ltep handshake");
|
|
|
|
client_sent_ltep_handshake_ = true;
|
|
|
|
|
|
|
|
/* decide if we want to advertise metadata xfer support (BEP 9) */
|
|
|
|
bool const allow_metadata_xfer = tor_.is_public();
|
|
|
|
|
|
|
|
/* decide if we want to advertise pex support */
|
|
|
|
bool const allow_pex = tor_.allows_pex();
|
|
|
|
|
|
|
|
auto val = tr_variant{};
|
|
|
|
tr_variantInitDict(&val, 8);
|
|
|
|
tr_variantDictAddBool(&val, TR_KEY_e, session->encryptionMode() != TR_CLEAR_PREFERRED);
|
|
|
|
|
|
|
|
// If connecting to global peer, then use global address
|
|
|
|
// Otherwise we are connecting to local peer, use bind address directly
|
|
|
|
if (auto const addr = io_->address().is_global_unicast_address() ? session->global_address(TR_AF_INET) :
|
|
|
|
session->bind_address(TR_AF_INET);
|
|
|
|
addr && !addr->is_any())
|
|
|
|
{
|
|
|
|
TR_ASSERT(addr->is_ipv4());
|
|
|
|
tr_variantDictAddRaw(&val, TR_KEY_ipv4, &addr->addr.addr4, sizeof(addr->addr.addr4));
|
|
|
|
}
|
|
|
|
if (auto const addr = io_->address().is_global_unicast_address() ? session->global_address(TR_AF_INET6) :
|
|
|
|
session->bind_address(TR_AF_INET6);
|
|
|
|
addr && !addr->is_any())
|
|
|
|
{
|
|
|
|
TR_ASSERT(addr->is_ipv6());
|
|
|
|
tr_variantDictAddRaw(&val, TR_KEY_ipv6, &addr->addr.addr6, sizeof(addr->addr.addr6));
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.bittorrent.org/beps/bep_0009.html
|
2021-10-21 00:39:05 +00:00
|
|
|
// It also adds "metadata_size" to the handshake message (not the
|
|
|
|
// "m" dictionary) specifying an integer value of the number of
|
|
|
|
// bytes of the metadata.
|
2024-03-16 00:52:09 +00:00
|
|
|
if (auto const info_dict_size = tor_.info_dict_size(); allow_metadata_xfer && tor_.has_metainfo() && info_dict_size > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-08 20:05:38 +00:00
|
|
|
tr_variantDictAddInt(&val, TR_KEY_metadata_size, info_dict_size);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
2021-10-21 00:39:05 +00:00
|
|
|
// Local TCP listen port. Allows each side to learn about the TCP
|
|
|
|
// port number of the other side. Note that there is no need for the
|
|
|
|
// receiving side of the connection to send this extension message,
|
|
|
|
// since its port number is already known.
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_variantDictAddInt(&val, TR_KEY_p, session->advertisedPeerPort().host());
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
2021-10-21 00:39:05 +00:00
|
|
|
// An integer, the number of outstanding request messages this
|
|
|
|
// client supports without dropping any. The default in in
|
|
|
|
// libtorrent is 250.
|
|
|
|
tr_variantDictAddInt(&val, TR_KEY_reqq, ReqQ);
|
|
|
|
|
2023-01-01 18:20:46 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
|
|
|
// A string containing the compact representation of the ip address this peer sees
|
|
|
|
// you as. i.e. this is the receiver's external ip address (no port is included).
|
|
|
|
// This may be either an IPv4 (4 bytes) or an IPv6 (16 bytes) address.
|
|
|
|
{
|
|
|
|
auto buf = std::array<std::byte, TR_ADDRSTRLEN>{};
|
|
|
|
auto const begin = std::data(buf);
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const end = io_->address().to_compact(begin);
|
2023-01-01 18:20:46 +00:00
|
|
|
auto const len = end - begin;
|
2023-08-18 03:13:01 +00:00
|
|
|
TR_ASSERT(len == tr_address::CompactAddrBytes[0] || len == tr_address::CompactAddrBytes[1]);
|
2023-01-01 18:20:46 +00:00
|
|
|
tr_variantDictAddRaw(&val, TR_KEY_yourip, begin, len);
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
2021-10-21 00:39:05 +00:00
|
|
|
// Client name and version (as a utf-8 string). This is a much more
|
|
|
|
// reliable way of identifying the client than relying on the
|
|
|
|
// peer id encoding.
|
2023-10-24 18:49:23 +00:00
|
|
|
tr_variantDictAddStrView(&val, TR_KEY_v, TR_NAME " " USERAGENT_PREFIX);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0021.html
|
2021-10-21 00:39:05 +00:00
|
|
|
// A peer that is a partial seed SHOULD include an extra header in
|
|
|
|
// the extension handshake 'upload_only'. Setting the value of this
|
|
|
|
// key to 1 indicates that this peer is not interested in downloading
|
|
|
|
// anything.
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_variantDictAddBool(&val, TR_KEY_upload_only, tor_.is_done());
|
2021-10-21 00:39:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (allow_metadata_xfer || allow_pex)
|
|
|
|
{
|
|
|
|
tr_variant* m = tr_variantDictAddDict(&val, TR_KEY_m, 2);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (allow_metadata_xfer)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(m, TR_KEY_ut_metadata, UT_METADATA_ID);
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (allow_pex)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddInt(m, TR_KEY_ut_pex, UT_PEX_ID);
|
|
|
|
}
|
2011-07-25 22:33:07 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_message(BtPeerMsgs::Ltep, LtepMessages::Handshake, tr_variant_serde::benc().to_string(val));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::parse_ltep_handshake(MessageReader& payload)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-05-17 02:46:41 +00:00
|
|
|
auto const handshake_sv = payload.to_string_view();
|
2022-04-16 00:56:16 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
auto var = tr_variant_serde::benc().inplace().parse(handshake_sv);
|
2023-08-21 21:16:54 +00:00
|
|
|
if (!var || !var->holds_alternative<tr_variant::Map>())
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "GET extended-handshake, couldn't get dictionary");
|
2007-09-20 16:32:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, fmt::format("here is the base64-encoded handshake: [{:s}]", tr_base64_encode(handshake_sv)));
|
2007-09-29 00:46:41 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// does the peer prefer encrypted connections?
|
2021-10-21 17:40:36 +00:00
|
|
|
auto pex = tr_pex{};
|
2023-08-18 03:13:01 +00:00
|
|
|
auto& [addr, port] = pex.socket_address;
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto e = int64_t{}; tr_variantDictFindInt(&*var, TR_KEY_e, &e))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
encryption_preference_ = e != 0 ? EncryptionPreference::Yes : EncryptionPreference::No;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (encryption_preference_ == EncryptionPreference::Yes)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-09 05:45:16 +00:00
|
|
|
pex.flags |= ADDED_F_ENCRYPTION_FLAG;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-09 05:45:16 +00:00
|
|
|
}
|
2007-09-29 00:46:41 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// check supported messages for utorrent pex
|
|
|
|
peer_supports_pex_ = false;
|
|
|
|
peer_supports_metadata_xfer_ = false;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
if (tr_variant* sub = nullptr; tr_variantDictFindDict(&*var, TR_KEY_m, &sub))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto ut_pex = int64_t{}; tr_variantDictFindInt(sub, TR_KEY_ut_pex, &ut_pex))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
peer_supports_pex_ = ut_pex != 0;
|
|
|
|
ut_pex_id_ = static_cast<uint8_t>(ut_pex);
|
|
|
|
logtrace(this, fmt::format("msgs->ut_pex is {:d}", ut_pex_id_));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto ut_metadata = int64_t{}; tr_variantDictFindInt(sub, TR_KEY_ut_metadata, &ut_metadata))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
peer_supports_metadata_xfer_ = ut_metadata != 0;
|
|
|
|
ut_metadata_id_ = static_cast<uint8_t>(ut_metadata);
|
|
|
|
logtrace(this, fmt::format("msgs->ut_metadata_id_ is {:d}", ut_metadata_id_));
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto ut_holepunch = int64_t{}; tr_variantDictFindInt(sub, TR_KEY_ut_holepunch, &ut_holepunch))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-07-14 12:06:25 +00:00
|
|
|
// Transmission doesn't support this extension yet.
|
|
|
|
// But its presence does indicate µTP supports,
|
|
|
|
// which we do care about...
|
2024-03-16 00:52:09 +00:00
|
|
|
peer_info->set_utp_supported(true);
|
2011-02-18 00:43:39 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// look for metainfo size (BEP 9)
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto metadata_size = int64_t{}; tr_variantDictFindInt(&*var, TR_KEY_metadata_size, &metadata_size))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
if (!tr_metadata_download::is_valid_metadata_size(metadata_size))
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
peer_supports_metadata_xfer_ = false;
|
2024-01-03 03:04:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
tor_.maybe_start_metadata_transfer(metadata_size);
|
2024-01-03 03:04:17 +00:00
|
|
|
}
|
2011-03-02 15:00:12 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// look for upload_only (BEP 21)
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto upload_only = int64_t{}; tr_variantDictFindInt(&*var, TR_KEY_upload_only, &upload_only))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-09 20:25:30 +00:00
|
|
|
pex.flags |= ADDED_F_SEED_FLAG;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-02 19:46:51 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0010.html
|
2023-04-14 21:03:08 +00:00
|
|
|
// Client name and version (as a utf-8 string). This is a much more
|
|
|
|
// reliable way of identifying the client than relying on the
|
|
|
|
// peer id encoding.
|
2023-08-17 16:02:45 +00:00
|
|
|
if (auto sv = std::string_view{}; tr_variantDictFindStrView(&*var, TR_KEY_v, &sv))
|
2023-04-14 21:03:08 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
set_user_agent(tr_interned_string{ sv });
|
2023-04-14 21:03:08 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* get peer's listening port */
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto p = int64_t{}; tr_variantDictFindInt(&*var, TR_KEY_p, &p) && p > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
port.set_host(p);
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotPort(port));
|
|
|
|
logtrace(this, fmt::format("peer's port is now {:d}", p));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 03:13:01 +00:00
|
|
|
std::byte const* addr_compact = nullptr;
|
2021-10-21 17:40:36 +00:00
|
|
|
auto addr_len = size_t{};
|
2024-03-16 00:52:09 +00:00
|
|
|
if (io_->is_incoming() && tr_variantDictFindRaw(&*var, TR_KEY_ipv4, &addr_compact, &addr_len) &&
|
2023-08-18 03:13:01 +00:00
|
|
|
addr_len == tr_address::CompactAddrBytes[TR_AF_INET])
|
2009-12-14 02:07:47 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
std::tie(addr, std::ignore) = tr_address::from_compact_ipv4(addr_compact);
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_peerMgrAddPex(&tor_, TR_PEER_FROM_LTEP, &pex, 1);
|
2009-11-09 05:45:16 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (io_->is_incoming() && tr_variantDictFindRaw(&*var, TR_KEY_ipv6, &addr_compact, &addr_len) &&
|
2023-08-18 03:13:01 +00:00
|
|
|
addr_len == tr_address::CompactAddrBytes[TR_AF_INET6])
|
2009-12-14 02:07:47 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
std::tie(addr, std::ignore) = tr_address::from_compact_ipv6(addr_compact);
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_peerMgrAddPex(&tor_, TR_PEER_FROM_LTEP, &pex, 1);
|
2009-11-09 05:45:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 20:23:10 +00:00
|
|
|
/* get peer's maximum request queue size */
|
2024-03-16 00:52:09 +00:00
|
|
|
if (auto reqq_in = int64_t{}; tr_variantDictFindInt(&*var, TR_KEY_reqq, &reqq_in))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
reqq_ = reqq_in;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::parse_ut_metadata(MessageReader& payload_in)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
int64_t msg_type = -1;
|
|
|
|
int64_t piece = -1;
|
|
|
|
int64_t total_size = 0;
|
|
|
|
|
2023-05-17 02:46:41 +00:00
|
|
|
auto const tmp = payload_in.to_string_view();
|
2023-03-03 23:43:51 +00:00
|
|
|
auto const* const msg_end = std::data(tmp) + std::size(tmp);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
auto serde = tr_variant_serde::benc();
|
|
|
|
if (auto var = serde.inplace().parse(tmp); var)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
(void)tr_variantDictFindInt(&*var, TR_KEY_msg_type, &msg_type);
|
|
|
|
(void)tr_variantDictFindInt(&*var, TR_KEY_piece, &piece);
|
|
|
|
(void)tr_variantDictFindInt(&*var, TR_KEY_total_size, &total_size);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, fmt::format("got ut_metadata msg: type {:d}, piece {:d}, total_size {:d}", msg_type, piece, total_size));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
if (msg_type == MetadataMsgType::Reject)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
// no-op
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto const piece_len = msg_end - serde.end();
|
|
|
|
msg_type == MetadataMsgType::Data && piece * MetadataPieceSize + piece_len <= total_size)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
tor_.set_metadata_piece(piece, serde.end(), piece_len);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
if (msg_type == MetadataMsgType::Request)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (piece >= 0 && tor_.has_metainfo() && tor_.is_public() && std::size(peer_requested_metadata_pieces_) < MetadataReqQ)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
peer_requested_metadata_pieces_.push(piece);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
/* send a rejection message */
|
2021-10-21 17:40:36 +00:00
|
|
|
auto v = tr_variant{};
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variantInitDict(&v, 2);
|
2022-02-09 02:25:19 +00:00
|
|
|
tr_variantDictAddInt(&v, TR_KEY_msg_type, MetadataMsgType::Reject);
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variantDictAddInt(&v, TR_KEY_piece, piece);
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_message(BtPeerMsgs::Ltep, ut_metadata_id_, serde.to_string(v));
|
2021-11-18 05:37:35 +00:00
|
|
|
}
|
2010-04-30 00:27:15 +00:00
|
|
|
}
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
ReadResult tr_peerMsgsImpl::process_peer_message(uint8_t id, MessageReader& payload)
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
bool const fext = io_->supports_fext();
|
2007-10-27 15:45:03 +00:00
|
|
|
|
2021-10-21 17:40:36 +00:00
|
|
|
auto ui32 = uint32_t{};
|
2009-01-02 23:28:57 +00:00
|
|
|
|
2022-04-05 01:37:12 +00:00
|
|
|
logtrace(
|
2024-03-16 00:52:09 +00:00
|
|
|
this,
|
2023-03-03 23:43:51 +00:00
|
|
|
fmt::format(
|
|
|
|
"got peer msg '{:s}' ({:d}) with payload len {:d}",
|
|
|
|
BtPeerMsgs::debug_name(id),
|
|
|
|
static_cast<int>(id),
|
|
|
|
std::size(payload)));
|
2007-10-27 15:45:03 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (!is_message_length_correct(tor_, id, sizeof(id) + std::size(payload)))
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
2022-04-05 01:37:12 +00:00
|
|
|
logdbg(
|
2024-03-16 00:52:09 +00:00
|
|
|
this,
|
2023-03-03 23:43:51 +00:00
|
|
|
fmt::format(
|
|
|
|
"bad msg: '{:s}' ({:d}) with payload len {:d}",
|
|
|
|
BtPeerMsgs::debug_name(id),
|
|
|
|
static_cast<int>(id),
|
|
|
|
std::size(payload)));
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotError(EMSGSIZE));
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_ERR, {} };
|
2007-10-27 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (id)
|
2007-10-27 15:45:03 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Choke:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "got Choke");
|
|
|
|
set_client_choked(true);
|
2007-11-10 04:56:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!fext)
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotChoke());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-11-10 04:56:27 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
update_active(TR_PEER_TO_CLIENT);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Unchoke:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "got Unchoke");
|
|
|
|
set_client_choked(false);
|
|
|
|
update_active(TR_PEER_TO_CLIENT);
|
|
|
|
update_desired_request_count();
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Interested:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "got Interested");
|
|
|
|
set_peer_interested(true);
|
|
|
|
update_active(TR_CLIENT_TO_PEER);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::NotInterested:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "got Not Interested");
|
|
|
|
set_peer_interested(false);
|
|
|
|
update_active(TR_CLIENT_TO_PEER);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Have:
|
2023-03-03 23:43:51 +00:00
|
|
|
ui32 = payload.to_uint32();
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, fmt::format("got Have: {:d}", ui32));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (tor_.has_metainfo() && ui32 >= tor_.piece_count())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotError(ERANGE));
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_ERR, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* a peer can send the same HAVE message twice... */
|
2024-03-16 00:52:09 +00:00
|
|
|
if (!have_.test(ui32))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
have_.set(ui32);
|
|
|
|
publish(tr_peer_event::GotHave(ui32));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-17 05:14:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2007-11-10 04:56:27 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Bitfield:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "got a bitfield");
|
|
|
|
have_ = tr_bitfield{ tor_.has_metainfo() ? tor_.piece_count() : std::size(payload) * 8 };
|
|
|
|
have_.set_raw(reinterpret_cast<uint8_t const*>(std::data(payload)), std::size(payload));
|
|
|
|
publish(tr_peer_event::GotBitfield(&have_));
|
2023-05-18 21:56:29 +00:00
|
|
|
break;
|
2007-11-10 04:56:27 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Request:
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
struct peer_request r;
|
2023-03-03 23:43:51 +00:00
|
|
|
r.index = payload.to_uint32();
|
|
|
|
r.offset = payload.to_uint32();
|
|
|
|
r.length = payload.to_uint32();
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, fmt::format("got Request: {:d}:{:d}->{:d}", r.index, r.offset, r.length));
|
|
|
|
on_peer_made_request(r);
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Cancel:
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
struct peer_request r;
|
2023-03-03 23:43:51 +00:00
|
|
|
r.index = payload.to_uint32();
|
|
|
|
r.offset = payload.to_uint32();
|
|
|
|
r.length = payload.to_uint32();
|
2024-03-16 00:52:09 +00:00
|
|
|
cancels_sent_to_client.add(tr_time(), 1);
|
|
|
|
logtrace(this, fmt::format("got a Cancel {:d}:{:d}->{:d}", r.index, r.offset, r.length));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto& requests = peer_requested_;
|
2022-04-29 17:44:22 +00:00
|
|
|
if (auto iter = std::find(std::begin(requests), std::end(requests), r); iter != std::end(requests))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-29 17:44:22 +00:00
|
|
|
requests.erase(iter);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-29 17:44:22 +00:00
|
|
|
// bep6: "Even when a request is cancelled, the peer
|
|
|
|
// receiving the cancel should respond with either the
|
|
|
|
// corresponding reject or the corresponding piece"
|
|
|
|
if (fext)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_reject(r);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-08 23:20:00 +00:00
|
|
|
}
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Piece:
|
2024-03-16 00:52:09 +00:00
|
|
|
return read_piece_data(payload);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Port:
|
2024-03-16 00:52:09 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0005.html
|
2022-04-21 15:58:13 +00:00
|
|
|
// Peers supporting the DHT set the last bit of the 8-byte reserved flags
|
|
|
|
// exchanged in the BitTorrent protocol handshake. Peer receiving a handshake
|
|
|
|
// indicating the remote peer supports the DHT should send a PORT message.
|
|
|
|
// It begins with byte 0x09 and has a two byte payload containing the UDP
|
|
|
|
// port of the DHT node in network byte order.
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "Got a BtPeerMsgs::Port");
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
auto const hport = payload.to_uint16();
|
2023-08-18 03:13:01 +00:00
|
|
|
if (auto const dht_port = tr_port::from_host(hport); !std::empty(dht_port))
|
2022-04-21 15:58:13 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
dht_port_ = dht_port;
|
|
|
|
session->maybe_add_dht_node(io_->address(), dht_port_);
|
2022-04-21 15:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::FextSuggest:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "Got a BtPeerMsgs::FextSuggest");
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fext)
|
|
|
|
{
|
2023-03-03 23:43:51 +00:00
|
|
|
auto const piece = payload.to_uint32();
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotSuggest(piece));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotError(EMSGSIZE));
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_ERR, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::FextAllowedFast:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "Got a BtPeerMsgs::FextAllowedFast");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (fext)
|
|
|
|
{
|
2023-03-03 23:43:51 +00:00
|
|
|
auto const piece = payload.to_uint32();
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotAllowedFast(piece));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotError(EMSGSIZE));
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_ERR, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::FextHaveAll:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "Got a BtPeerMsgs::FextHaveAll");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (fext)
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
have_.set_has_all();
|
|
|
|
publish(tr_peer_event::GotHaveAll());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotError(EMSGSIZE));
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_ERR, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::FextHaveNone:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "Got a BtPeerMsgs::FextHaveNone");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (fext)
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
have_.set_has_none();
|
|
|
|
publish(tr_peer_event::GotHaveNone());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotError(EMSGSIZE));
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_ERR, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::FextReject:
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-03-01 14:09:18 +00:00
|
|
|
struct peer_request r;
|
2023-03-03 23:43:51 +00:00
|
|
|
r.index = payload.to_uint32();
|
|
|
|
r.offset = payload.to_uint32();
|
|
|
|
r.length = payload.to_uint32();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fext)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotRejected(tor_.block_info(), tor_.piece_loc(r.index, r.offset).block));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
publish(tr_peer_event::GotError(EMSGSIZE));
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_ERR, {} };
|
2008-12-02 17:10:54 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2007-11-10 04:56:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case BtPeerMsgs::Ltep:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "Got a BtPeerMsgs::Ltep");
|
|
|
|
parse_ltep(payload);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2007-11-10 04:56:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, fmt::format("peer sent us an UNKNOWN: {:d}", static_cast<int>(id)));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
return { READ_NOW, {} };
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
ReadResult tr_peerMsgsImpl::read_piece_data(MessageReader& payload)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
// <index><begin><block>
|
|
|
|
auto const piece = payload.to_uint32();
|
|
|
|
auto const offset = payload.to_uint32();
|
|
|
|
auto const len = std::size(payload);
|
|
|
|
|
|
|
|
auto const loc = tor_.piece_loc(piece, offset);
|
|
|
|
auto const block = loc.block;
|
|
|
|
auto const block_size = tor_.block_size(block);
|
|
|
|
|
|
|
|
logtrace(this, fmt::format("got {:d} bytes for req {:d}:{:d}->{:d}", len, piece, offset, len));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (loc.block_offset + len > block_size)
|
|
|
|
{
|
|
|
|
logwarn(this, fmt::format("got unaligned piece {:d}:{:d}->{:d}", piece, offset, len));
|
|
|
|
return { READ_ERR, len };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tr_peerMgrDidPeerRequest(&tor_, this, block))
|
|
|
|
{
|
|
|
|
logwarn(this, fmt::format("got unrequested piece {:d}:{:d}->{:d}", piece, offset, len));
|
|
|
|
return { READ_ERR, len };
|
|
|
|
}
|
|
|
|
|
|
|
|
publish(tr_peer_event::GotPieceData(len));
|
|
|
|
|
|
|
|
if (loc.block_offset == 0U && len == block_size) // simple case: one message has entire block
|
|
|
|
{
|
|
|
|
auto buf = std::make_unique<Cache::BlockData>(block_size);
|
|
|
|
payload.to_buf(std::data(*buf), len);
|
|
|
|
auto const ok = client_got_block(std::move(buf), block) == 0;
|
|
|
|
return { ok ? READ_NOW : READ_ERR, len };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& blocks = incoming_.blocks;
|
|
|
|
auto& incoming_block = blocks.try_emplace(block, block_size).first->second;
|
|
|
|
payload.to_buf(std::data(*incoming_block.buf) + loc.block_offset, len);
|
|
|
|
|
|
|
|
if (!incoming_block.add_span(loc.block_offset, loc.block_offset + len))
|
|
|
|
{
|
|
|
|
return { READ_ERR, len }; // invalid span
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!incoming_block.has_all())
|
|
|
|
{
|
|
|
|
return { READ_LATER, len }; // we don't have the full block yet
|
|
|
|
}
|
|
|
|
|
|
|
|
auto block_buf = std::move(incoming_block.buf);
|
|
|
|
blocks.erase(block); // note: invalidates `incoming_block` local
|
|
|
|
auto const ok = client_got_block(std::move(block_buf), block) == 0;
|
|
|
|
return { ok ? READ_NOW : READ_ERR, len };
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns 0 on success, or an errno on failure
|
|
|
|
int tr_peerMsgsImpl::client_got_block(std::unique_ptr<Cache::BlockData> block_data, tr_block_index_t const block)
|
|
|
|
{
|
|
|
|
auto const n_expected = tor_.block_size(block);
|
2007-11-09 01:22:15 +00:00
|
|
|
|
2022-11-24 16:17:02 +00:00
|
|
|
if (!block_data)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logdbg(this, fmt::format("wrong block size: expected {:d}, got {:d}", n_expected, 0));
|
2022-11-24 16:17:02 +00:00
|
|
|
return EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (std::size(*block_data) != tor_.block_size(block))
|
2022-11-24 16:17:02 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logdbg(this, fmt::format("wrong block size: expected {:d}, got {:d}", n_expected, std::size(*block_data)));
|
2008-10-03 04:49:06 +00:00
|
|
|
return EMSGSIZE;
|
2007-11-13 05:36:43 +00:00
|
|
|
}
|
2007-11-09 01:22:15 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, fmt::format("got block {:d}", block));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (!tr_peerMgrDidPeerRequest(&tor_, this, block))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logdbg(this, "we didn't ask for this message...");
|
2007-11-13 05:36:43 +00:00
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const loc = tor_.block_loc(block);
|
|
|
|
if (tor_.has_piece(loc.piece))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "we did ask for this message, but the piece is already complete...");
|
2010-03-07 17:38:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-09 01:22:15 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
// NB: if writeBlock() fails the torrent may be paused.
|
2024-03-16 00:52:09 +00:00
|
|
|
// If this happens, this object will be destructed and must no longer be used.
|
|
|
|
if (auto const err = session->cache->write_block(tor_.id(), block, std::move(block_data)); err != 0)
|
2022-10-24 21:57:07 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
blame.set(loc.piece);
|
|
|
|
publish(tr_peer_event::GotBlock(tor_.block_info(), block));
|
2022-10-24 21:57:07 +00:00
|
|
|
|
2007-11-13 05:36:43 +00:00
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
void tr_peerMsgsImpl::did_write(tr_peerIo* /*io*/, size_t bytes_written, bool was_piece_data, void* vmsgs)
|
2008-11-17 04:00:57 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
auto* const msgs = static_cast<tr_peerMsgsImpl*>(vmsgs);
|
2013-01-30 18:00:03 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (was_piece_data)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
msgs->publish(tr_peer_event::SentPieceData(bytes_written));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-02-13 18:23:56 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
msgs->pulse();
|
2008-11-17 04:00:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
ReadState tr_peerMsgsImpl::can_read(tr_peerIo* io, void* vmsgs, size_t* piece)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
auto* const msgs = static_cast<tr_peerMsgsImpl*>(vmsgs);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
// https://www.bittorrent.org/beps/bep_0003.html
|
|
|
|
// Next comes an alternating stream of length prefixes and messages.
|
|
|
|
// Messages of length zero are keepalives, and ignored.
|
|
|
|
// All non-keepalive messages start with a single byte which gives their type.
|
|
|
|
//
|
|
|
|
// https://wiki.theory.org/BitTorrentSpecification
|
|
|
|
// All of the remaining messages in the protocol take the form of
|
|
|
|
// <length prefix><message ID><payload>. The length prefix is a four byte
|
|
|
|
// big-endian value. The message ID is a single decimal byte.
|
|
|
|
// The payload is message dependent.
|
|
|
|
|
|
|
|
// read <length prefix>
|
2024-03-16 00:52:09 +00:00
|
|
|
auto& current_message_len = msgs->incoming_.length; // the full message payload length. Includes the +1 for id length
|
2023-03-03 23:43:51 +00:00
|
|
|
if (!current_message_len)
|
|
|
|
{
|
|
|
|
auto message_len = uint32_t{};
|
|
|
|
if (io->read_buffer_size() < sizeof(message_len))
|
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2009-01-22 04:20:30 +00:00
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
io->read_uint32(&message_len);
|
|
|
|
|
|
|
|
// The keep-alive message is a message with zero bytes,
|
|
|
|
// specified with the length prefix set to zero.
|
|
|
|
// There is no message ID and no payload.
|
2023-10-27 04:19:24 +00:00
|
|
|
if (message_len == 0U)
|
2023-03-03 23:43:51 +00:00
|
|
|
{
|
|
|
|
logtrace(msgs, "got KeepAlive");
|
|
|
|
return READ_NOW;
|
|
|
|
}
|
2023-10-27 04:19:24 +00:00
|
|
|
|
|
|
|
current_message_len = message_len;
|
2009-01-02 23:28:57 +00:00
|
|
|
}
|
2023-03-03 23:43:51 +00:00
|
|
|
|
|
|
|
// read <message ID>
|
2024-03-16 00:52:09 +00:00
|
|
|
auto& current_message_type = msgs->incoming_.id;
|
2023-03-03 23:43:51 +00:00
|
|
|
if (!current_message_type)
|
2009-01-02 23:28:57 +00:00
|
|
|
{
|
2023-03-03 23:43:51 +00:00
|
|
|
auto message_type = uint8_t{};
|
|
|
|
if (io->read_buffer_size() < sizeof(message_type))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-03-03 23:43:51 +00:00
|
|
|
return READ_LATER;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
io->read_uint8(&message_type);
|
|
|
|
current_message_type = message_type;
|
|
|
|
}
|
2009-01-02 23:28:57 +00:00
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
// read <payload>
|
2024-03-16 00:52:09 +00:00
|
|
|
auto& current_payload = msgs->incoming_.payload;
|
2023-10-25 13:51:36 +00:00
|
|
|
auto const full_payload_len = *current_message_len - sizeof(*current_message_type);
|
2023-04-20 18:18:53 +00:00
|
|
|
auto n_left = full_payload_len - std::size(current_payload);
|
2023-10-25 13:51:36 +00:00
|
|
|
auto const [buf, n_this_pass] = current_payload.reserve_space(std::min(n_left, io->read_buffer_size()));
|
|
|
|
io->read_bytes(buf, n_this_pass);
|
|
|
|
current_payload.commit_space(n_this_pass);
|
|
|
|
n_left -= n_this_pass;
|
|
|
|
logtrace(msgs, fmt::format("read {:d} payload bytes; {:d} left to go", n_this_pass, n_left));
|
2009-01-02 23:28:57 +00:00
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
if (n_left > 0U)
|
|
|
|
{
|
|
|
|
return READ_LATER;
|
2009-01-02 23:28:57 +00:00
|
|
|
}
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2023-10-25 13:51:36 +00:00
|
|
|
// The incoming message is now complete. After processing the message
|
|
|
|
// with `process_peer_message()`, reset the peerMsgs' incoming
|
|
|
|
// field so it's ready to receive the next message.
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const [read_state, n_piece_bytes_read] = msgs->process_peer_message(*current_message_type, current_payload);
|
2023-10-25 13:51:36 +00:00
|
|
|
*piece = n_piece_bytes_read;
|
2023-04-20 18:18:53 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
current_message_len.reset();
|
2023-04-20 18:18:53 +00:00
|
|
|
current_message_type.reset();
|
2023-06-27 15:51:20 +00:00
|
|
|
current_payload.clear();
|
2023-04-20 18:18:53 +00:00
|
|
|
|
2023-03-03 23:43:51 +00:00
|
|
|
return read_state;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::got_error(tr_peerIo* /*io*/, tr_error const& /*error*/, void* vmsgs)
|
|
|
|
{
|
|
|
|
static_cast<tr_peerMsgsImpl*>(vmsgs)->publish(tr_peer_event::GotError(ENOTCONN));
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::pulse()
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const now_sec = tr_time();
|
|
|
|
auto const now_msec = tr_time_msec();
|
|
|
|
|
|
|
|
update_desired_request_count();
|
|
|
|
update_block_requests();
|
|
|
|
update_metadata_requests(now_sec);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (fill_output_buffer(now_sec, now_msec) == 0U)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-08 23:20:00 +00:00
|
|
|
}
|
2009-08-18 01:57:17 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::update_metadata_requests(time_t now) const
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (!peer_supports_metadata_xfer_)
|
2022-08-14 19:41:57 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (auto const piece = tor_.get_next_metadata_request(now); piece)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2021-10-21 17:40:36 +00:00
|
|
|
auto tmp = tr_variant{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&tmp, 3);
|
2022-02-09 02:25:19 +00:00
|
|
|
tr_variantDictAddInt(&tmp, TR_KEY_msg_type, MetadataMsgType::Request);
|
2022-08-14 19:41:57 +00:00
|
|
|
tr_variantDictAddInt(&tmp, TR_KEY_piece, *piece);
|
2024-03-16 00:52:09 +00:00
|
|
|
protocol_send_message(BtPeerMsgs::Ltep, ut_metadata_id_, tr_variant_serde::benc().to_string(tmp));
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
void tr_peerMsgsImpl::update_block_requests()
|
2009-11-08 23:20:00 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (!tor_.client_can_download())
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const n_active = tr_peerMgrCountActiveRequestsToPeer(&tor_, this);
|
|
|
|
if (n_active >= desired_request_count_)
|
2009-11-08 23:20:00 +00:00
|
|
|
{
|
2021-11-19 18:37:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
TR_ASSERT(client_is_interested());
|
|
|
|
TR_ASSERT(!client_is_choked());
|
|
|
|
|
|
|
|
auto const n_wanted = desired_request_count_ - n_active;
|
|
|
|
if (auto const requests = tr_peerMgrGetNextRequests(&tor_, this, n_wanted); !std::empty(requests))
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
request_blocks(std::data(requests), std::size(requests));
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] size_t tr_peerMsgsImpl::fill_output_buffer(time_t now_sec, uint64_t now_msec)
|
|
|
|
{
|
|
|
|
auto n_bytes_written = size_t{};
|
2009-11-08 23:20:00 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// fulfill metadata requests
|
|
|
|
for (;;)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const old_len = n_bytes_written;
|
|
|
|
n_bytes_written += add_next_metadata_piece();
|
|
|
|
if (old_len == n_bytes_written)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2009-11-08 23:20:00 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
|
|
|
|
// fulfill piece requests
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
auto const old_len = n_bytes_written;
|
|
|
|
n_bytes_written += add_next_block(now_sec, now_msec);
|
|
|
|
if (old_len == n_bytes_written)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client_sent_at_ != 0 && now_sec - client_sent_at_ > KeepaliveIntervalSecs)
|
|
|
|
{
|
|
|
|
n_bytes_written += protocol_send_keepalive();
|
|
|
|
}
|
|
|
|
|
|
|
|
return n_bytes_written;
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] size_t tr_peerMsgsImpl::add_next_metadata_piece()
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const piece = pop_next_metadata_request();
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
if (!piece.has_value()) // no pending requests
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2007-10-06 18:20:52 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto data = tor_.get_metadata_piece(*piece);
|
2024-01-03 03:04:17 +00:00
|
|
|
if (!data)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
// send a reject
|
|
|
|
auto tmp = tr_variant{};
|
|
|
|
tr_variantInitDict(&tmp, 2);
|
|
|
|
tr_variantDictAddInt(&tmp, TR_KEY_msg_type, MetadataMsgType::Reject);
|
|
|
|
tr_variantDictAddInt(&tmp, TR_KEY_piece, *piece);
|
2024-03-16 00:52:09 +00:00
|
|
|
return protocol_send_message(BtPeerMsgs::Ltep, ut_metadata_id_, tr_variant_serde::benc().to_string(tmp));
|
2008-11-28 16:00:29 +00:00
|
|
|
}
|
2023-04-15 00:31:09 +00:00
|
|
|
|
|
|
|
// send the metadata
|
|
|
|
auto tmp = tr_variant{};
|
|
|
|
tr_variantInitDict(&tmp, 3);
|
|
|
|
tr_variantDictAddInt(&tmp, TR_KEY_msg_type, MetadataMsgType::Data);
|
|
|
|
tr_variantDictAddInt(&tmp, TR_KEY_piece, *piece);
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_variantDictAddInt(&tmp, TR_KEY_total_size, tor_.info_dict_size());
|
|
|
|
return protocol_send_message(BtPeerMsgs::Ltep, ut_metadata_id_, tr_variant_serde::benc().to_string(tmp), *data);
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] size_t tr_peerMsgsImpl::add_next_block(time_t now_sec, uint64_t now_msec)
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (std::empty(peer_requested_) || io_->get_write_buffer_space(now_msec) == 0U)
|
2008-11-28 16:00:29 +00:00
|
|
|
{
|
2023-04-15 00:31:09 +00:00
|
|
|
return {};
|
2008-11-28 16:00:29 +00:00
|
|
|
}
|
2007-10-28 15:20:24 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
auto const req = peer_requested_.front();
|
|
|
|
peer_requested_.pop_front();
|
2023-04-15 00:31:09 +00:00
|
|
|
|
|
|
|
auto buf = std::array<uint8_t, tr_block_info::BlockSize>{};
|
2024-03-16 00:52:09 +00:00
|
|
|
auto ok = is_valid_request(req) && tor_.has_piece(req.index);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
if (ok)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
ok = tor_.ensure_piece_is_checked(req.index);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
if (!ok)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
tor_.error().set_local_error(fmt::format("Please Verify Local Data! Piece #{:d} is corrupt.", req.index));
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
if (ok)
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
ok = session->cache->read_block(tor_, tor_.piece_loc(req.index, req.offset), req.length, std::data(buf)) == 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
if (ok)
|
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
blocks_sent_to_peer.add(now_sec, 1);
|
2023-04-15 00:31:09 +00:00
|
|
|
auto const piece_data = std::string_view{ reinterpret_cast<char const*>(std::data(buf)), req.length };
|
2024-03-16 00:52:09 +00:00
|
|
|
return protocol_send_message(BtPeerMsgs::Piece, req.index, req.offset, piece_data);
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
2007-10-28 15:20:24 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (io_->supports_fext())
|
2008-11-28 16:00:29 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return protocol_send_reject(req);
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
2009-11-10 17:26:04 +00:00
|
|
|
|
2023-04-15 00:31:09 +00:00
|
|
|
return {};
|
|
|
|
}
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
bool tr_peerMsgsImpl::is_valid_request(peer_request const& req) const
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
int err = 0;
|
2009-06-04 14:59:17 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (req.index >= tor_.piece_count())
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
err = 1;
|
2023-04-15 00:31:09 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
else if (req.length < 1)
|
2023-04-15 00:31:09 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
err = 2;
|
2007-11-18 03:18:26 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
else if (req.offset + req.length > tor_.piece_size(req.index))
|
2007-11-18 03:18:26 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
err = 3;
|
|
|
|
}
|
|
|
|
else if (req.length > tr_block_info::BlockSize)
|
|
|
|
{
|
|
|
|
err = 4;
|
|
|
|
}
|
|
|
|
else if (tor_.piece_loc(req.index, req.offset, req.length).byte > tor_.total_size())
|
|
|
|
{
|
|
|
|
err = 5;
|
2008-11-28 16:00:29 +00:00
|
|
|
}
|
2008-02-29 03:41:50 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (err != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_logAddTraceTor(&tor_, fmt::format("index {} offset {} length {} err {}", req.index, req.offset, req.length, err));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
return err == 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
[[nodiscard]] bool tr_peerMsgsImpl::can_add_request_from_peer(peer_request const& req)
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (peer_is_choked())
|
|
|
|
{
|
|
|
|
logtrace(this, "rejecting request from choked peer");
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
if (std::size(peer_requested_) >= ReqQ)
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "rejecting request ... reqq is full");
|
|
|
|
return false;
|
2008-12-02 17:10:54 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
|
|
|
|
if (!is_valid_request(req))
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "rejecting an invalid request.");
|
|
|
|
return false;
|
2008-12-02 17:10:54 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
|
|
|
|
if (!tor_.has_piece(req.index))
|
2008-12-02 17:10:54 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
logtrace(this, "rejecting request for a piece we don't have.");
|
|
|
|
return false;
|
2008-12-02 17:10:54 +00:00
|
|
|
}
|
2024-03-16 00:52:09 +00:00
|
|
|
|
|
|
|
return true;
|
2008-12-02 17:10:54 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
size_t tr_peerMsgsImpl::max_available_reqs() const
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
if (tor_.is_done() || !tor_.has_metainfo() || client_is_choked() || !client_is_interested())
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// Get the rate limit we should use.
|
|
|
|
// TODO: this needs to consider all the other peers as well...
|
|
|
|
uint64_t const now = tr_time_msec();
|
|
|
|
auto rate = get_piece_speed(now, TR_PEER_TO_CLIENT);
|
|
|
|
if (tor_.uses_speed_limit(TR_PEER_TO_CLIENT))
|
2023-07-17 13:56:57 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
rate = std::min(rate, tor_.speed_limit(TR_PEER_TO_CLIENT));
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// honor the session limits, if enabled
|
|
|
|
if (tor_.uses_session_limits())
|
|
|
|
{
|
|
|
|
if (auto const limit = session->active_speed_limit(TR_PEER_TO_CLIENT))
|
2022-05-27 01:29:10 +00:00
|
|
|
{
|
2024-03-16 00:52:09 +00:00
|
|
|
rate = std::min(rate, *limit);
|
2022-05-27 01:29:10 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2022-05-27 01:29:10 +00:00
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
// use this desired rate to figure out how
|
|
|
|
// many requests we should send to this peer
|
|
|
|
static auto constexpr Floor = size_t{ 32 };
|
|
|
|
static size_t constexpr Seconds = RequestBufSecs;
|
|
|
|
size_t const estimated_blocks_in_period = (rate.base_quantity() * Seconds) / tr_block_info::BlockSize;
|
|
|
|
auto const ceil = reqq_.value_or(250);
|
|
|
|
|
|
|
|
return std::clamp(estimated_blocks_in_period, Floor, ceil);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2023-01-07 14:27:54 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_peerMsgs::tr_peerMsgs(
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_torrent const& tor,
|
2024-05-26 00:13:15 +00:00
|
|
|
std::shared_ptr<tr_peer_info> peer_info_in,
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_interned_string user_agent,
|
|
|
|
bool connection_is_encrypted,
|
|
|
|
bool connection_is_incoming,
|
|
|
|
bool connection_is_utp)
|
|
|
|
: tr_peer{ tor }
|
2024-05-26 00:13:15 +00:00
|
|
|
, peer_info{ std::move(peer_info_in) }
|
2023-08-01 14:56:26 +00:00
|
|
|
, user_agent_{ user_agent }
|
|
|
|
, connection_is_encrypted_{ connection_is_encrypted }
|
|
|
|
, connection_is_incoming_{ connection_is_incoming }
|
|
|
|
, connection_is_utp_{ connection_is_utp }
|
|
|
|
{
|
|
|
|
peer_info->set_connected(tr_time());
|
|
|
|
++n_peers;
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
tr_peerMsgs::~tr_peerMsgs()
|
|
|
|
{
|
2023-08-01 14:56:26 +00:00
|
|
|
peer_info->set_connected(tr_time(), false);
|
2024-03-16 00:52:09 +00:00
|
|
|
TR_ASSERT(n_peers > 0U);
|
|
|
|
--n_peers;
|
2023-01-07 14:27:54 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 00:52:09 +00:00
|
|
|
tr_peerMsgs* tr_peerMsgs::create(
|
|
|
|
tr_torrent& torrent,
|
2024-05-26 00:13:15 +00:00
|
|
|
std::shared_ptr<tr_peer_info> peer_info,
|
2023-01-07 14:27:54 +00:00
|
|
|
std::shared_ptr<tr_peerIo> io,
|
2023-04-14 21:03:08 +00:00
|
|
|
tr_interned_string user_agent,
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_peer_callback_bt callback,
|
2023-01-07 14:27:54 +00:00
|
|
|
void* callback_data)
|
|
|
|
{
|
2024-05-26 00:13:15 +00:00
|
|
|
return new tr_peerMsgsImpl{ torrent, std::move(peer_info), std::move(io), user_agent, callback, callback_data };
|
2023-01-07 14:27:54 +00:00
|
|
|
}
|