From 473f75e98ad5abb8f2d6a36e31eabfbdb3ce1167 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 11 Nov 2008 08:33:34 +0000 Subject: [PATCH] DHT fix where the node_id would get set after it was started and be inconsitent with rpc_manager and routing_table --- include/libtorrent/kademlia/dht_tracker.hpp | 2 +- include/libtorrent/kademlia/node.hpp | 4 ++-- src/kademlia/dht_tracker.cpp | 19 +++++++++++-------- src/kademlia/node.cpp | 5 +++-- src/session_impl.cpp | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index 7a98888c0..a35b281a1 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -74,7 +74,7 @@ namespace libtorrent { namespace dht friend void intrusive_ptr_add_ref(dht_tracker const*); friend void intrusive_ptr_release(dht_tracker const*); dht_tracker(libtorrent::aux::session_impl& ses, rate_limited_udp_socket& sock - , dht_settings const& settings); + , dht_settings const& settings, entry const* state = 0); void start(entry const& bootstrap); void stop(); diff --git a/include/libtorrent/kademlia/node.hpp b/include/libtorrent/kademlia/node.hpp index ffbe4ddc7..06d98c325 100644 --- a/include/libtorrent/kademlia/node.hpp +++ b/include/libtorrent/kademlia/node.hpp @@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "libtorrent/socket.hpp" @@ -169,7 +170,7 @@ class node_impl : boost::noncopyable typedef std::map table_t; public: node_impl(libtorrent::aux::session_impl& ses, boost::function const& f - , dht_settings const& settings); + , dht_settings const& settings, boost::optional nid); virtual ~node_impl() {} @@ -194,7 +195,6 @@ public: typedef table_t::iterator data_iterator; - void set_node_id(node_id const& nid) { m_id = nid; } node_id const& nid() const { return m_id; } boost::tuple size() const{ return m_table.size(); } diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 617fc9cee..7cc69c010 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -128,11 +128,20 @@ namespace libtorrent { namespace dht TORRENT_DEFINE_LOG(dht_tracker) #endif + boost::optional extract_node_id(entry const* e) + { + if (e == 0 || e->type() != entry::dictionary_t) return boost::optional(); + entry const* nid = e->find_key("node-id"); + if (nid == 0 || nid->type() != entry::string_t || nid->string().length() != 20) + return boost::optional(); + return boost::optional(node_id(nid->string().c_str())); + } + // class that puts the networking and the kademlia node in a single // unit and connecting them together. dht_tracker::dht_tracker(libtorrent::aux::session_impl& ses, rate_limited_udp_socket& sock - , dht_settings const& settings) - : m_dht(ses, bind(&dht_tracker::send_packet, this, _1), settings) + , dht_settings const& settings, entry const* state) + : m_dht(ses, bind(&dht_tracker::send_packet, this, _1), settings, extract_node_id(state)) , m_ses(ses) , m_sock(sock) , m_last_new_key(time_now() - minutes(key_refresh)) @@ -189,12 +198,6 @@ namespace libtorrent { namespace dht if (entry const* nodes = bootstrap.find_key("nodes")) read_endpoint_list(nodes, initial_nodes); } catch (std::exception&) {} - - entry const* nid = bootstrap.find_key("node-id"); - if (nid - && nid->type() == entry::string_t - && nid->string().length() == 40) - m_dht.set_node_id(boost::lexical_cast(nid->string())); } error_code ec; diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 2b41e1f94..a372901b3 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -94,9 +94,10 @@ void nop() {} node_impl::node_impl(libtorrent::aux::session_impl& ses , boost::function const& f - , dht_settings const& settings) + , dht_settings const& settings + , boost::optional nid) : m_settings(settings) - , m_id(generate_id()) + , m_id(nid ? *nid : generate_id()) , m_table(m_id, 8, settings) , m_rpc(bind(&node_impl::incoming_request, this, _1) , m_id, m_table, f) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index f19686168..a3616555b 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2206,7 +2206,7 @@ namespace aux { , m_dht_settings.service_port , m_dht_settings.service_port); } - m_dht = new dht::dht_tracker(*this, m_dht_socket, m_dht_settings); + m_dht = new dht::dht_tracker(*this, m_dht_socket, m_dht_settings, &startup_state); if (!m_dht_socket.is_open() || m_dht_socket.local_port() != m_dht_settings.service_port) { m_dht_socket.bind(m_dht_settings.service_port);