diff --git a/include/libtorrent/kademlia/traversal_algorithm.hpp b/include/libtorrent/kademlia/traversal_algorithm.hpp index 7d409a77e..d65fc2b61 100644 --- a/include/libtorrent/kademlia/traversal_algorithm.hpp +++ b/include/libtorrent/kademlia/traversal_algorithm.hpp @@ -50,6 +50,7 @@ namespace libtorrent { struct dht_lookup; } namespace libtorrent { namespace dht { class node; +struct node_endpoint; // this class may not be instantiated as a stack object struct TORRENT_EXTRA_EXPORT traversal_algorithm : boost::noncopyable @@ -141,6 +142,9 @@ private: #endif }; +void look_for_nodes(char const* nodes_key, udp const& protocol + , bdecode_node const& r, std::function f); + struct traversal_observer : observer { traversal_observer( diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 151dcad7c..78c1e4b62 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -514,7 +514,7 @@ namespace libtorrent { namespace dht { int pos; error_code err; - int ret = bdecode(buf.data(), buf.data() + buf_size, m_msg, err, &pos, 10, 500); + int const ret = bdecode(buf.data(), buf.data() + buf_size, m_msg, err, &pos, 10, 500); if (ret != 0) { m_counters.inc_stats_counter(counters::dht_messages_in_dropped); @@ -538,7 +538,7 @@ namespace libtorrent { namespace dht { m_log->log_packet(dht_logger::incoming_message, buf, ep); #endif - libtorrent::dht::msg m(m_msg, ep); + libtorrent::dht::msg const m(m_msg, ep); for (auto& n : m_nodes) n.second.dht.incoming(m); return true; diff --git a/src/kademlia/find_data.cpp b/src/kademlia/find_data.cpp index aceeafc7a..3d2f9ec4a 100644 --- a/src/kademlia/find_data.cpp +++ b/src/kademlia/find_data.cpp @@ -45,7 +45,7 @@ namespace libtorrent { namespace dht { void find_data_observer::reply(msg const& m) { - bdecode_node r = m.message.dict_find_dict("r"); + bdecode_node const r = m.message.dict_find_dict("r"); if (!r) { #ifndef TORRENT_DISABLE_LOGGING diff --git a/src/kademlia/get_item.cpp b/src/kademlia/get_item.cpp index 0f122095e..97541c2ff 100644 --- a/src/kademlia/get_item.cpp +++ b/src/kademlia/get_item.cpp @@ -175,7 +175,7 @@ void get_item_observer::reply(msg const& m) signature sig; sequence_number seq{0}; - bdecode_node r = m.message.dict_find_dict("r"); + bdecode_node const r = m.message.dict_find_dict("r"); if (!r) { #ifndef TORRENT_DISABLE_LOGGING diff --git a/src/kademlia/get_peers.cpp b/src/kademlia/get_peers.cpp index 8da65a53f..7f7e51441 100644 --- a/src/kademlia/get_peers.cpp +++ b/src/kademlia/get_peers.cpp @@ -44,7 +44,7 @@ namespace libtorrent { namespace dht { void get_peers_observer::reply(msg const& m) { - bdecode_node r = m.message.dict_find_dict("r"); + bdecode_node const r = m.message.dict_find_dict("r"); if (!r) { #ifndef TORRENT_DISABLE_LOGGING diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 0d83febc4..d86306abf 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -591,23 +591,8 @@ struct ping_observer : observer #endif return; } - - // look for nodes - udp const protocol = algorithm()->get_node().protocol(); - int const protocol_size = int(detail::address_size(protocol)); - char const* nodes_key = algorithm()->get_node().protocol_nodes_key(); - bdecode_node const n = r.dict_find_string(nodes_key); - if (n) - { - char const* nodes = n.string_ptr(); - char const* end = nodes + n.string_length(); - - while (end - nodes >= 20 + protocol_size + 2) - { - node_endpoint nep = read_node_endpoint(protocol, nodes); - algorithm()->get_node().m_table.heard_about(nep.id, nep.ep); - } - } + look_for_nodes(algorithm()->get_node().protocol_nodes_key(), algorithm()->get_node().protocol(), r, + [this](node_endpoint const& nep) { algorithm()->get_node().m_table.heard_about(nep.id, nep.ep); }); } }; diff --git a/src/kademlia/traversal_algorithm.cpp b/src/kademlia/traversal_algorithm.cpp index de1b878fc..43c638ba2 100644 --- a/src/kademlia/traversal_algorithm.cpp +++ b/src/kademlia/traversal_algorithm.cpp @@ -593,6 +593,22 @@ void traversal_algorithm::status(dht_lookup& l) l.last_sent = last_sent; } +void look_for_nodes(char const* nodes_key, udp const& protocol, bdecode_node const& r, std::function f) +{ + bdecode_node const n = r.dict_find_string(nodes_key); + if (n) + { + char const* nodes = n.string_ptr(); + char const* end = nodes + n.string_length(); + int const protocol_size = int(detail::address_size(protocol)); + + while (end - nodes >= 20 + protocol_size + 2) + { + f(read_node_endpoint(protocol, nodes)); + } + } +} + void traversal_observer::reply(msg const& m) { bdecode_node const r = m.message.dict_find_dict("r"); @@ -609,13 +625,14 @@ void traversal_observer::reply(msg const& m) return; } + bdecode_node const id = r.dict_find_string("id"); + #ifndef TORRENT_DISABLE_LOGGING dht_observer* logger = get_observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { - bdecode_node const nid = r.dict_find_string("id"); char hex_id[41]; - aux::to_hex({nid.string_ptr(), 20}, hex_id); + aux::to_hex({id.string_ptr(), 20}, hex_id); logger->log(dht_logger::traversal , "[%u] RESPONSE id: %s invoke-count: %d addr: %s type: %s" , algorithm()->id(), hex_id, algorithm()->invoke_count() @@ -623,24 +640,9 @@ void traversal_observer::reply(msg const& m) } #endif - // look for nodes - udp const protocol = algorithm()->get_node().protocol(); - int const protocol_size = int(detail::address_size(protocol)); - char const* nodes_key = algorithm()->get_node().protocol_nodes_key(); - bdecode_node const n = r.dict_find_string(nodes_key); - if (n) - { - char const* nodes = n.string_ptr(); - char const* end = nodes + n.string_length(); + look_for_nodes(algorithm()->get_node().protocol_nodes_key(), algorithm()->get_node().protocol(), r, + [this](node_endpoint const& nep) { algorithm()->traverse(nep.id, nep.ep); }); - while (end - nodes >= 20 + protocol_size + 2) - { - node_endpoint nep = read_node_endpoint(protocol, nodes); - algorithm()->traverse(nep.id, nep.ep); - } - } - - bdecode_node const id = r.dict_find_string("id"); if (!id || id.string_length() != 20) { #ifndef TORRENT_DISABLE_LOGGING