post alert on outgoing get_peers

This commit is contained in:
Arvid Norberg 2014-09-22 19:49:32 +00:00
parent 7a55069ec9
commit 8cbef3876a
5 changed files with 65 additions and 2 deletions

View File

@ -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 };
}

View File

@ -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

View File

@ -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

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/kademlia/node.hpp>
#include <libtorrent/socket_io.hpp>
#include <libtorrent/performance_counters.hpp>
#include <libtorrent/alert_types.hpp>
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);
}

View File

@ -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)
{