Skip to content

Commit

Permalink
add asserts around protocol_v2 peer flag
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Sep 26, 2020
1 parent 1c8b2af commit 81b4e66
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/bt_peer_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ namespace {

// this is a v1 peer in a hybrid torrent
// indicate that we support upgrading to v2
if (!peer_info_struct()->protocol_v2 && t->torrent_file().info_hashes().has_v2())
if (!peer_info_struct()->protocol_v2 && t->info_hash().has_v2())
{
*(ptr + 7) |= 0x10;
}
Expand Down Expand Up @@ -2901,7 +2901,7 @@ namespace {
{
if (!t)
{
attach_to_torrent(ti->torrent_file().info_hashes());
attach_to_torrent(ti->info_hash());
if (is_disconnecting()) return;
TORRENT_ASSERT(!is_disconnecting());

Expand Down Expand Up @@ -3430,8 +3430,8 @@ namespace {
// the client isn't attempting to use a protocol version the torrent
// doesn't support
if (std::equal(recv_buffer.begin() + 8, recv_buffer.begin() + 28
, t->torrent_file().info_hashes().get(protocol_version::V2).data())
&& t->torrent_file().info_hashes().has_v2())
, t->info_hash().get(protocol_version::V2).data())
&& t->info_hash().has_v2())
{
peer_info_struct()->protocol_v2 = true;
}
Expand Down
26 changes: 19 additions & 7 deletions src/peer_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ namespace libtorrent {
+ static_cast<std::uint8_t>(socket_type_idx(m_socket)));
std::shared_ptr<torrent> t = m_torrent.lock();

// the protocol_v2 flag should not be set for non-v2 torrents
TORRENT_ASSERT(!t || t->info_hash().has_v2() || !m_peer_info->protocol_v2);

if (m_connected)
m_counters.inc_stats_counter(counters::num_peers_connected);
else if (m_connecting)
Expand Down Expand Up @@ -1116,8 +1119,13 @@ namespace libtorrent {
sha1_hash peer_connection::associated_info_hash() const
{
std::shared_ptr<torrent> t = associated_torrent().lock();
return t->torrent_file().info_hashes().get(
peer_info_struct()->protocol_v2 ? protocol_version::V2 : protocol_version::V1);
TORRENT_ASSERT(t);
auto const& ih = t->info_hash();
// if protocol_v2 is set on the peer, this better be a v2 torrent,
// otherwise something isn't right
TORRENT_ASSERT(ih.has_v2() || !peer_info_struct()->protocol_v2);
return ih.get((ih.has_v2() && peer_info_struct()->protocol_v2)
? protocol_version::V2 : protocol_version::V1);
}

void peer_connection::received_bytes(int const bytes_payload, int const bytes_protocol)
Expand Down Expand Up @@ -1269,7 +1277,7 @@ namespace libtorrent {
{
peer_log(peer_log_alert::info, "ATTACH"
, "Delay loaded torrent: %s:"
, aux::to_hex(t->torrent_file().info_hashes().get_best()).c_str());
, aux::to_hex(t->info_hash().get_best()).c_str());
}
#endif
}
Expand Down Expand Up @@ -5255,10 +5263,10 @@ namespace libtorrent {
// this means we're in seed mode and we haven't yet
// verified this piece (r.piece)
disk_job_flags_t flags;
if (t->torrent_file().info_hashes().has_v1())
if (t->info_hash().has_v1())
flags |= disk_interface::v1_hash;
aux::vector<sha256_hash> hashes;
if (t->torrent_file().info_hashes().has_v2())
if (t->info_hash().has_v2())
hashes.resize(t->torrent_file().orig_files().blocks_in_piece2(r.piece));

span<sha256_hash> v2_hashes(hashes);
Expand Down Expand Up @@ -5359,13 +5367,13 @@ namespace libtorrent {

// we're using the piece hashes here, we need the torrent to be loaded
if (!m_settings.get_bool(settings_pack::disable_hash_checks)
&& t->torrent_file().info_hashes().has_v1())
&& t->info_hash().has_v1())
{
hash_failed[protocol_version::V1] = piece_hash != t->torrent_file().hash_for_piece(piece);
}

if (!m_settings.get_bool(settings_pack::disable_hash_checks)
&& t->torrent_file().info_hashes().has_v2())
&& t->info_hash().has_v2())
{
hash_failed[protocol_version::V2] = false;

Expand Down Expand Up @@ -6575,6 +6583,10 @@ namespace libtorrent {
return;
}

auto const& ih = t->info_hash();
if (peer_info_struct() && peer_info_struct()->protocol_v2)
TORRENT_ASSERT(ih.has_v2());

if (t->ready_for_connections() && m_initialized)
TORRENT_ASSERT(t->torrent_file().num_pieces() == int(m_have_piece.size()));

Expand Down

0 comments on commit 81b4e66

Please sign in to comment.