diff --git a/libtransmission/handshake.cc b/libtransmission/handshake.cc index cd6c9e6fe..dd2ebcf7f 100644 --- a/libtransmission/handshake.cc +++ b/libtransmission/handshake.cc @@ -290,14 +290,14 @@ ReadState tr_handshake::read_handshake(tr_peerIo* peer_io) auto hash = tr_sha1_digest_t{}; peer_io->read_bytes(std::data(hash), std::size(hash)); + if (auto const& info = mediator_->torrent(hash); !info || !info->is_running) + { + tr_logAddTraceHand(this, "peer is trying to connect to us for a torrent we aren't running."); + return done(false); + } + if (is_incoming() && peer_io->torrent_hash() == tr_sha1_digest_t{}) // incoming plain handshake { - if (!mediator_->torrent(hash)) - { - tr_logAddTraceHand(this, "peer is trying to connect to us for a torrent we don't have."); - return done(false); - } - peer_io->set_torrent_hash(hash); } else // outgoing, or incoming MSE handshake @@ -458,14 +458,14 @@ ReadState tr_handshake::read_crypto_provide(tr_peerIo* peer_io) obfuscated_hash[i] = x_or[i] ^ req3[i]; } - if (auto const info = mediator_->torrent_from_obfuscated(obfuscated_hash); info) + if (auto const info = mediator_->torrent_from_obfuscated(obfuscated_hash); info && info->is_running) { tr_logAddTraceHand(this, fmt::format("got INCOMING connection's MSE handshake for torrent [{}]", info->id)); peer_io->set_torrent_hash(info->info_hash); } else { - tr_logAddTraceHand(this, "can't find that torrent..."); + tr_logAddTraceHand(this, "we are not running that torrent..."); return done(false); } @@ -714,7 +714,7 @@ bool tr_handshake::build_handshake_message(tr_peerIo* io, libtransmission::Buffe TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "build_handshake_message requires an info_hash"); auto const info = mediator_->torrent(info_hash); - if (!info) + if (!info || !info->is_running) { return false; } diff --git a/libtransmission/handshake.h b/libtransmission/handshake.h index 82ab864fa..ea4274f09 100644 --- a/libtransmission/handshake.h +++ b/libtransmission/handshake.h @@ -64,6 +64,7 @@ public: tr_peer_id_t client_peer_id; tr_torrent_id_t id; bool is_done; + bool is_running; }; virtual ~Mediator() = default; diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index 33803f25f..fe956141e 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -75,12 +75,13 @@ private: return {}; } - auto info = TorrentInfo{}; - info.info_hash = tor->info_hash(); - info.client_peer_id = tor->peer_id(); - info.id = tor->id(); - info.is_done = tor->is_done(); - return info; + return TorrentInfo{ + tor->info_hash(), // info_hash + tor->peer_id(), // client_peer_id + tor->id(), // id + tor->is_done(), // is_done + tor->is_running() // is_running + }; } public: @@ -1289,7 +1290,12 @@ void create_bit_torrent_peer( info->destroy_handshake(); } - if (!result.is_connected || swarm == nullptr || !swarm->is_running) + if (swarm == nullptr || !swarm->is_running) + { + return false; + } + + if (!result.is_connected) { if (info && !info->is_connected()) { diff --git a/tests/libtransmission/handshake-test.cc b/tests/libtransmission/handshake-test.cc index 2a1603928..ec5fcd530 100644 --- a/tests/libtransmission/handshake-test.cc +++ b/tests/libtransmission/handshake-test.cc @@ -155,11 +155,13 @@ public: tr_handshake::Mediator::TorrentInfo const TorrentWeAreSeeding{ tr_sha1::digest("abcde"sv), tr_peerIdInit(), tr_torrent_id_t{ 100 }, - true /*is_done*/ }; + true /*is_done*/, + true /*is_running*/ }; tr_handshake::Mediator::TorrentInfo const UbuntuTorrent{ *tr_sha1_from_string("2c6b6858d61da9543d4231a71db4b1c9264b0685"sv), tr_peerIdInit(), tr_torrent_id_t{ 101 }, - false /*is_done*/ }; + false /*is_done*/, + true /*is_running*/ }; auto createIncomingIo(tr_session* session) {