From 8cbef3876afda554de034d49f711429e9bf9b401 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 22 Sep 2014 19:49:32 +0000 Subject: [PATCH] post alert on outgoing get_peers --- include/libtorrent/alert_types.hpp | 30 +++++++++++++++++++++++++++- include/libtorrent/kademlia/node.hpp | 3 +++ src/alert.cpp | 17 ++++++++++++++++ src/kademlia/get_peers.cpp | 9 ++++++++- src/kademlia/node.cpp | 8 ++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index d808f4613..b5d55353c 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -2140,9 +2140,37 @@ namespace libtorrent error_code error; }; + // This alert is generated we send a get_peers request + // It belongs to the ``dht_notification`` category. + struct TORRENT_EXPORT dht_outgoing_get_peers_alert: alert + { + // internal + dht_outgoing_get_peers_alert(sha1_hash const& ih, sha1_hash const& obfih + , udp::endpoint ep) + : info_hash(ih) + , obfuscated_info_hash(obfih) + , ip(ep) + {} + + TORRENT_DEFINE_ALERT(dht_outgoing_get_peers_alert, 78); + + const static int static_category = alert::dht_notification; + virtual std::string message() const; + + // the info_hash of the torrent we're looking for peers for. + sha1_hash info_hash; + + // if this was an obfuscated lookup, this is the info-hash target + // actually sent to the node. + sha1_hash obfuscated_info_hash; + + // the endpoint we're sending this query to + udp::endpoint ip; + }; + #undef TORRENT_DEFINE_ALERT - enum { num_alert_types = 74 }; + enum { num_alert_types = 79 }; } diff --git a/include/libtorrent/kademlia/node.hpp b/include/libtorrent/kademlia/node.hpp index 9f85e9a29..abe1669c6 100644 --- a/include/libtorrent/kademlia/node.hpp +++ b/include/libtorrent/kademlia/node.hpp @@ -59,6 +59,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { class alert_manager; struct alert_dispatcher; + class alert; struct counters; } @@ -280,6 +281,8 @@ public: libtorrent::dht_settings const& settings() const { return m_settings; } counters& stats_counters() const { return m_counters; } + void post_alert(alert* a); + protected: void lookup_peers(sha1_hash const& info_hash, int prefix, entry& reply diff --git a/src/alert.cpp b/src/alert.cpp index b06ba0f7c..317aeee1a 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -685,5 +685,22 @@ namespace libtorrent { return msg; } + std::string dht_outgoing_get_peers_alert::message() const + { + char msg[600]; + char obf[70]; + obf[0] = '\0'; + if (obfuscated_info_hash != info_hash) + { + snprintf(obf, sizeof(obf), " [obfuscated: %s]" + , to_hex(obfuscated_info_hash.to_string()).c_str()); + } + snprintf(msg, sizeof(msg), "outgoing dht get_peers : %s%s -> %s" + , to_hex(info_hash.to_string()).c_str() + , obf + , print_endpoint(ip).c_str()); + return msg; + } + } // namespace libtorrent diff --git a/src/kademlia/get_peers.cpp b/src/kademlia/get_peers.cpp index 9e7821a50..0905b34f4 100644 --- a/src/kademlia/get_peers.cpp +++ b/src/kademlia/get_peers.cpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include namespace libtorrent { namespace dht { @@ -143,6 +144,9 @@ bool get_peers::invoke(observer_ptr o) a["info_hash"] = m_target.to_string(); if (m_noseeds) a["noseed"] = 1; + m_node.post_alert(new dht_outgoing_get_peers_alert(m_target, m_target + , o->target_ep())); + m_node.stats_counters().inc_stats_counter(counters::dht_get_peers_out); return m_node.m_rpc.invoke(e, o->target_ep(), o); @@ -203,7 +207,7 @@ bool obfuscated_get_peers::invoke(observer_ptr o) // when we get close to the target zone in the DHT // start using the correct info-hash, in order to // start receiving peers - if (shared_prefix > m_node.m_table.depth() - 10) + if (shared_prefix > m_node.m_table.depth() - 4) { m_obfuscated = false; // clear the queried bits on all successful nodes in @@ -240,6 +244,9 @@ bool obfuscated_get_peers::invoke(observer_ptr o) obfuscated_target^= m_target; a["target"] = obfuscated_target.to_string(); + m_node.post_alert(new dht_outgoing_get_peers_alert(m_target + , obfuscated_target, o->target_ep())); + return m_node.m_rpc.invoke(e, o->target_ep(), o); } diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 864afd012..7b9c63f3f 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -115,6 +115,14 @@ node_impl::node_impl(alert_dispatcher* alert_disp m_secret[1] = random(); } +void node_impl::post_alert(alert* a) +{ + if (!m_post_alert) + delete a; + else + m_post_alert->post_alert(a); +} + bool node_impl::verify_token(std::string const& token, char const* info_hash , udp::endpoint const& addr) {