fix/cleanups to tr_peerMsgsImpl (#5783)

* fix: correct condition to advertise pex support in ltep handshake

1. Advertise pex support regardless of whether the peer had advertised pex support. No reason to give up an opportunity to advertise pex support just because our direct peer does not support it.
2. Check if pex is enabled in global settings as well.
This commit is contained in:
tearfur 2023-07-14 23:51:52 +08:00 committed by GitHub
parent 0fbbda90f5
commit ca4cb1a675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 26 deletions

View File

@ -625,7 +625,6 @@ public:
bool peerSupportsPex = false;
bool peerSupportsMetadataXfer = false;
bool clientSentLtepHandshake = false;
bool peerSentLtepHandshake = false;
size_t desired_request_count = 0;
@ -636,7 +635,7 @@ public:
EncryptionPreference encryption_preference = EncryptionPreference::Unknown;
size_t metadata_size_hint = 0;
int64_t metadata_size_hint = 0;
tr_torrent* const torrent;
@ -679,6 +678,7 @@ public:
private:
friend ReadResult process_peer_message(tr_peerMsgsImpl* msgs, uint8_t id, MessageReader& payload);
friend void parseLtepHandshake(tr_peerMsgsImpl* msgs, MessageReader& payload);
friend void parseUtMetadata(tr_peerMsgsImpl* msgs, MessageReader& payload_in);
tr_peer_callback const callback_;
void* const callback_data_;
@ -938,19 +938,7 @@ void sendLtepHandshake(tr_peerMsgsImpl* msgs)
bool const allow_metadata_xfer = msgs->torrent->is_public();
/* decide if we want to advertise pex support */
auto allow_pex = bool{};
if (!msgs->torrent->allows_pex())
{
allow_pex = false;
}
else if (msgs->peerSentLtepHandshake)
{
allow_pex = msgs->peerSupportsPex;
}
else
{
allow_pex = true;
}
bool const allow_pex = msgs->session->allows_pex() && msgs->torrent->allows_pex();
auto val = tr_variant{};
tr_variantInitDict(&val, 8);
@ -1045,8 +1033,6 @@ void sendLtepHandshake(tr_peerMsgsImpl* msgs)
void parseLtepHandshake(tr_peerMsgsImpl* msgs, MessageReader& payload)
{
msgs->peerSentLtepHandshake = true;
auto const handshake_sv = payload.to_string_view();
auto val = tr_variant{};
@ -1080,14 +1066,14 @@ void parseLtepHandshake(tr_peerMsgsImpl* msgs, MessageReader& payload)
if (tr_variantDictFindInt(sub, TR_KEY_ut_pex, &i))
{
msgs->peerSupportsPex = i != 0;
msgs->ut_pex_id = (uint8_t)i;
msgs->ut_pex_id = static_cast<uint8_t>(i);
logtrace(msgs, fmt::format(FMT_STRING("msgs->ut_pex is {:d}"), static_cast<int>(msgs->ut_pex_id)));
}
if (tr_variantDictFindInt(sub, TR_KEY_ut_metadata, &i))
{
msgs->peerSupportsMetadataXfer = i != 0;
msgs->ut_metadata_id = (uint8_t)i;
msgs->ut_metadata_id = static_cast<uint8_t>(i);
logtrace(msgs, fmt::format(FMT_STRING("msgs->ut_metadata_id is {:d}"), static_cast<int>(msgs->ut_metadata_id)));
}
@ -1103,7 +1089,7 @@ void parseLtepHandshake(tr_peerMsgsImpl* msgs, MessageReader& payload)
/* look for metainfo size (BEP 9) */
if (tr_variantDictFindInt(&val, TR_KEY_metadata_size, &i) && tr_torrentSetMetadataSizeHint(msgs->torrent, i))
{
msgs->metadata_size_hint = (size_t)i;
msgs->metadata_size_hint = i;
}
/* look for upload_only (BEP 21) */
@ -1182,8 +1168,8 @@ void parseUtMetadata(tr_peerMsgsImpl* msgs, MessageReader& payload_in)
/* NOOP */
}
if (msg_type == MetadataMsgType::Data && !msgs->torrent->has_metainfo() && msg_end - benc_end <= METADATA_PIECE_SIZE &&
piece * METADATA_PIECE_SIZE + (msg_end - benc_end) <= total_size)
if (msg_type == MetadataMsgType::Data && total_size == msgs->metadata_size_hint && !msgs->torrent->has_metainfo() &&
msg_end - benc_end <= METADATA_PIECE_SIZE && piece * METADATA_PIECE_SIZE + (msg_end - benc_end) <= total_size)
{
size_t const piece_len = msg_end - benc_end;
tr_torrentSetMetadataPiece(msgs->torrent, piece, benc_end, piece_len);

View File

@ -2215,7 +2215,7 @@ void addSessionField(tr_session const* s, tr_variant* d, tr_quark key)
break;
case TR_KEY_pex_enabled:
tr_variantDictAddBool(d, key, s->allowsPEX());
tr_variantDictAddBool(d, key, s->allows_pex());
break;
case TR_KEY_tcp_enabled:

View File

@ -1432,7 +1432,7 @@ bool tr_sessionIsPexEnabled(tr_session const* session)
{
TR_ASSERT(session != nullptr);
return session->allowsPEX();
return session->allows_pex();
}
bool tr_sessionIsDHTEnabled(tr_session const* session)

View File

@ -766,7 +766,7 @@ public:
return settings_.lpd_enabled;
}
[[nodiscard]] constexpr auto allowsPEX() const noexcept
[[nodiscard]] constexpr auto allows_pex() const noexcept
{
return settings_.pex_enabled;
}

View File

@ -553,7 +553,7 @@ public:
[[nodiscard]] constexpr auto allows_pex() const noexcept
{
return this->is_public() && this->session->allowsPEX();
return this->is_public() && this->session->allows_pex();
}
[[nodiscard]] constexpr auto allows_dht() const noexcept