From dea237d2ed2eba1130f14046ae7c5a3cbf277058 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Wed, 7 Aug 2013 17:40:16 -0300 Subject: [PATCH] implement dht_get_data_alert to detect when we are neighbor of a certain request --- libtorrent/include/libtorrent/alert_types.hpp | 17 ++++++++++++++ libtorrent/src/alert.cpp | 7 ++++++ libtorrent/src/kademlia/node.cpp | 22 ++++++++++++++++++ src/twister.cpp | 23 ++++++++++++++++--- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/libtorrent/include/libtorrent/alert_types.hpp b/libtorrent/include/libtorrent/alert_types.hpp index 54af2836..38165b0b 100644 --- a/libtorrent/include/libtorrent/alert_types.hpp +++ b/libtorrent/include/libtorrent/alert_types.hpp @@ -1163,6 +1163,23 @@ namespace libtorrent entry::list_type const m_lst; }; + struct TORRENT_EXPORT dht_get_data_alert: alert + { + dht_get_data_alert(entry const& target, bool possiblyNeighbor, bool hasData) + : m_target(target), m_possiblyNeighbor(possiblyNeighbor), m_hasData(hasData) + {} + + TORRENT_DEFINE_ALERT(dht_get_data_alert); + + const static int static_category = alert::dht_notification; + virtual std::string message() const; + + entry m_target; + bool m_possiblyNeighbor; + bool m_hasData; + }; + + struct TORRENT_EXPORT stats_alert: torrent_alert { stats_alert(torrent_handle const& h, int interval diff --git a/libtorrent/src/alert.cpp b/libtorrent/src/alert.cpp index 2647cccb..6aa99f2d 100644 --- a/libtorrent/src/alert.cpp +++ b/libtorrent/src/alert.cpp @@ -358,6 +358,13 @@ namespace libtorrent { return msg; } + std::string dht_get_data_alert::message() const + { + char msg[200]; + snprintf(msg, sizeof(msg), "request of getData received (m_possiblyNeighbor=%d)", m_possiblyNeighbor); + return msg; + } + stats_alert::stats_alert(torrent_handle const& h, int in , stat const& s) : torrent_alert(h) diff --git a/libtorrent/src/kademlia/node.cpp b/libtorrent/src/kademlia/node.cpp index a7059a93..d2d6b4a6 100644 --- a/libtorrent/src/kademlia/node.cpp +++ b/libtorrent/src/kademlia/node.cpp @@ -1332,9 +1332,11 @@ void node_impl::incoming_request(msg const& m, entry& e) m_table.find_node(target, n, 0); write_nodes_entry(reply, n); + bool hasData = false; dht_storage_table_t::iterator i = m_storage_table.find(target); if (i != m_storage_table.end()) { + hasData = true; reply["values"] = entry::list_type(); entry::list_type &values = reply["values"].list(); @@ -1349,6 +1351,26 @@ void node_impl::incoming_request(msg const& m, entry& e) values.push_back(v); } } + + // check distance between target, nodes and our own id + // n is sorted from closer(begin) to more distant (end) + bool possiblyNeighbor = false; + if( n.size() < m_table.bucket_size() ) { + possiblyNeighbor = true; + } else { + node_id dFarther = distance(n.back().id, target); + node_id dOwn = distance(nid(), target); + if( dOwn < dFarther ) + possiblyNeighbor = true; + } + + if (m_post_alert) + { + entry eTarget; + eTarget = *msg_keys[mk_target]; + alert* a = new dht_get_data_alert(eTarget,possiblyNeighbor,hasData); + if (!m_post_alert->post_alert(a)) delete a; + } } else { diff --git a/src/twister.cpp b/src/twister.cpp index f357c949..d1da9089 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -170,6 +170,10 @@ void ThreadWaitExtIP() , listen_port, listen_port+1, ec.message().c_str()); } + dht_settings dhts; + dhts.restrict_routing_ips = false; + dhts.restrict_search_ips = false; + ses->set_dht_settings(dhts); ses->start_dht(); //ses->set_settings(settings); @@ -182,12 +186,12 @@ void ThreadMaintainDHTNodes() RenameThread("maintain-dht-nodes"); while(1) { - MilliSleep(5000); + MilliSleep(15000); if( ses ) { session_status ss = ses->status(); - if( ss.dht_nodes == 0 && vNodes.size() ) { - printf("ThreadMaintainDHTNodes: no dht_nodes, trying to add some...\n"); + if( ss.dht_nodes < (int)vNodes.size() ) { + printf("ThreadMaintainDHTNodes: too few dht_nodes, trying to add some...\n"); LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) { BOOST_FOREACH(CAddress const &knownAddr, pnode->setAddrKnown) { @@ -253,6 +257,19 @@ void ThreadSessionAlerts() continue; } + dht_get_data_alert const* gd = alert_cast(*i); + if (gd) + { + if( gd->m_possiblyNeighbor ) { + printf("possiblyNeighbor of [%s,%s,%s]\n", + gd->m_target.find_key("n")->string().c_str(), + gd->m_target.find_key("r")->string().c_str(), + gd->m_target.find_key("t")->string().c_str()); + } + continue; + } + + /* save_resume_data_alert const* rd = alert_cast(*i); if (rd) {