remove the DHT's last dependency on session_impl

This commit is contained in:
Arvid Norberg 2015-05-09 18:06:02 +00:00
parent b4a6a14dfb
commit 6557910c7f
8 changed files with 72 additions and 36 deletions

View File

@ -569,15 +569,16 @@ namespace libtorrent
// implements dht_observer // implements dht_observer
virtual void set_external_address(address const& ip virtual void set_external_address(address const& ip
, address const& source); , address const& source) TORRENT_OVERRIDE;
virtual void get_peers(sha1_hash const& ih); virtual address external_address() TORRENT_OVERRIDE;
virtual void announce(sha1_hash const& ih, address const& addr, int port); virtual void get_peers(sha1_hash const& ih) TORRENT_OVERRIDE;
virtual void announce(sha1_hash const& ih, address const& addr, int port) TORRENT_OVERRIDE;
virtual void outgoing_get_peers(sha1_hash const& target virtual void outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep); , sha1_hash const& sent_target, udp::endpoint const& ep) TORRENT_OVERRIDE;
void set_external_address(address const& ip void set_external_address(address const& ip
, int source_type, address const& source); , int source_type, address const& source);
virtual external_ip const& external_address() const; virtual external_ip const& external_address() const TORRENT_OVERRIDE;
// used when posting synchronous function // used when posting synchronous function
// calls to session_impl and torrent objects // calls to session_impl and torrent objects

View File

@ -42,6 +42,7 @@ namespace libtorrent { namespace dht
{ {
virtual void set_external_address(address const& addr virtual void set_external_address(address const& addr
, address const& source) = 0; , address const& source) = 0;
virtual address external_address() = 0;
virtual void get_peers(sha1_hash const& ih) = 0; virtual void get_peers(sha1_hash const& ih) = 0;
virtual void outgoing_get_peers(sha1_hash const& target virtual void outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep) = 0; , sha1_hash const& sent_target, udp::endpoint const& ep) = 0;

View File

@ -76,7 +76,7 @@ namespace libtorrent { namespace dht
, udp_socket_observer , udp_socket_observer
, boost::enable_shared_from_this<dht_tracker> , boost::enable_shared_from_this<dht_tracker>
{ {
dht_tracker(libtorrent::aux::session_impl& ses, rate_limited_udp_socket& sock dht_tracker(dht_observer* observer, rate_limited_udp_socket& sock
, dht_settings const& settings, counters& cnt, entry const* state = 0); , dht_settings const& settings, counters& cnt, entry const* state = 0);
virtual ~dht_tracker(); virtual ~dht_tracker();

View File

@ -206,7 +206,7 @@ typedef std::map<node_id, dht_mutable_item> dht_mutable_table_t;
public: public:
node_impl(udp_socket_interface* sock node_impl(udp_socket_interface* sock
, libtorrent::dht_settings const& settings, node_id nid, address const& external_address , libtorrent::dht_settings const& settings, node_id nid
, dht_observer* observer, counters& cnt); , dht_observer* observer, counters& cnt);
virtual ~node_impl() {} virtual ~node_impl() {}

View File

@ -41,13 +41,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/kademlia/dht_tracker.hpp" #include "libtorrent/kademlia/dht_tracker.hpp"
#include "libtorrent/kademlia/msg.hpp" #include "libtorrent/kademlia/msg.hpp"
// TODO: it would be nice to not have a dependency on session here
#include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/socket.hpp" #include "libtorrent/socket.hpp"
#include "libtorrent/socket_io.hpp" #include "libtorrent/socket_io.hpp"
#include "libtorrent/bencode.hpp" #include "libtorrent/bencode.hpp"
#include "libtorrent/io.hpp" #include "libtorrent/io.hpp"
#include "libtorrent/version.hpp" #include "libtorrent/version.hpp"
#include "libtorrent/time.hpp"
#include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/performance_counters.hpp" // for counters
using boost::ref; using boost::ref;
@ -139,16 +138,15 @@ namespace libtorrent { namespace dht
// class that puts the networking and the kademlia node in a single // class that puts the networking and the kademlia node in a single
// unit and connecting them together. // unit and connecting them together.
// TODO: 3 don't pass in session here. pass in dht_observer and maybe add dht_tracker::dht_tracker(dht_observer* observer
// a way to ask it for the external IP. use bdecode_node instead of entry
dht_tracker::dht_tracker(libtorrent::aux::session_impl& ses
, rate_limited_udp_socket& sock , rate_limited_udp_socket& sock
, dht_settings const& settings, counters& cnt, entry const* state) , dht_settings const& settings
, counters& cnt
, entry const* state)
: m_counters(cnt) : m_counters(cnt)
, m_dht(this, settings, extract_node_id(state) , m_dht(this, settings, extract_node_id(state), observer, cnt)
, ses.external_address().external_address(address_v4()), &ses, cnt)
, m_sock(sock) , m_sock(sock)
, m_last_new_key(aux::time_now() - minutes(int(key_refresh))) , m_last_new_key(clock_type::now() - minutes(int(key_refresh)))
, m_timer(sock.get_io_service()) , m_timer(sock.get_io_service())
, m_connection_timer(sock.get_io_service()) , m_connection_timer(sock.get_io_service())
, m_refresh_timer(sock.get_io_service()) , m_refresh_timer(sock.get_io_service())
@ -257,7 +255,7 @@ namespace libtorrent { namespace dht
m_timer.expires_from_now(minutes(tick_period), ec); m_timer.expires_from_now(minutes(tick_period), ec);
m_timer.async_wait(boost::bind(&dht_tracker::tick, self(), _1)); m_timer.async_wait(boost::bind(&dht_tracker::tick, self(), _1));
time_point now = aux::time_now(); time_point now = clock_type::now();
if (now - minutes(int(key_refresh)) > m_last_new_key) if (now - minutes(int(key_refresh)) > m_last_new_key)
{ {
m_last_new_key = now; m_last_new_key = now;
@ -399,7 +397,7 @@ namespace libtorrent { namespace dht
return true; return true;
} }
if (!m_blocker.incoming(ep.address(), aux::time_now())) if (!m_blocker.incoming(ep.address(), clock_type::now()))
return true; return true;
using libtorrent::entry; using libtorrent::entry;

View File

@ -96,14 +96,24 @@ void purge_peers(std::set<peer_entry>& peers)
void nop() {} void nop() {}
node_id calculate_node_id(node_id const& nid, dht_observer* observer)
{
address external_address;
if (observer) external_address = observer->external_address();
if (nid == (node_id::min)() || !verify_id(nid, external_address))
return generate_id(external_address);
return nid;
}
} // anonymous namespace } // anonymous namespace
node_impl::node_impl(udp_socket_interface* sock node_impl::node_impl(udp_socket_interface* sock
, dht_settings const& settings, node_id nid, address const& external_address , dht_settings const& settings, node_id nid
, dht_observer* observer , dht_observer* observer
, struct counters& cnt) , struct counters& cnt)
: m_settings(settings) : m_settings(settings)
, m_id(nid == (node_id::min)() || !verify_id(nid, external_address) ? generate_id(external_address) : nid) , m_id(calculate_node_id(nid, observer))
, m_table(m_id, 8, settings) , m_table(m_id, 8, settings)
, m_rpc(m_id, m_table, sock) , m_rpc(m_id, m_table, sock)
, m_observer(observer) , m_observer(observer)

View File

@ -818,6 +818,9 @@ namespace aux {
settings = e->dict_find_dict("dht state"); settings = e->dict_find_dict("dht state");
if (settings) if (settings)
{ {
// TODO: 2 if the DHT is enabled, it should probably be restarted here.
// maybe it should even be deferred to not be started until the client
// has had a chance to pass in the dht state
m_dht_state = settings; m_dht_state = settings;
} }
#endif #endif
@ -5391,13 +5394,12 @@ retry:
} }
} }
// TODO: 3 use bdecode_node instead of entry.
void session_impl::start_dht(entry const& startup_state) void session_impl::start_dht(entry const& startup_state)
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
stop_dht(); stop_dht();
m_dht = boost::make_shared<dht::dht_tracker>(boost::ref(*this) m_dht = boost::make_shared<dht::dht_tracker>((dht_observer*)this
, boost::ref(m_udp_socket), boost::cref(m_dht_settings) , boost::ref(m_udp_socket), boost::cref(m_dht_settings)
, boost::ref(m_stats_counters), &startup_state); , boost::ref(m_stats_counters), &startup_state);
@ -6429,31 +6431,38 @@ retry:
m_upnp.reset(); m_upnp.reset();
} }
external_ip const& session_impl::external_address() const external_ip const& session_impl::external_address() const TORRENT_OVERRIDE
{ return m_external_ip; } {
return m_external_ip;
}
// this is the DHT observer version. DHT is the implied source // this is the DHT observer version. DHT is the implied source
void session_impl::set_external_address(address const& ip void session_impl::set_external_address(address const& ip
, address const& source) , address const& source) TORRENT_OVERRIDE
{ {
set_external_address(ip, source_dht, source); set_external_address(ip, source_dht, source);
} }
void session_impl::get_peers(sha1_hash const& ih) address session_impl::external_address() TORRENT_OVERRIDE
{
return m_external_ip.external_address(address_v4());
}
void session_impl::get_peers(sha1_hash const& ih) TORRENT_OVERRIDE
{ {
if (!m_alerts.should_post<dht_get_peers_alert>()) return; if (!m_alerts.should_post<dht_get_peers_alert>()) return;
m_alerts.emplace_alert<dht_get_peers_alert>(ih); m_alerts.emplace_alert<dht_get_peers_alert>(ih);
} }
void session_impl::announce(sha1_hash const& ih, address const& addr void session_impl::announce(sha1_hash const& ih, address const& addr
, int port) , int port) TORRENT_OVERRIDE
{ {
if (!m_alerts.should_post<dht_announce_alert>()) return; if (!m_alerts.should_post<dht_announce_alert>()) return;
m_alerts.emplace_alert<dht_announce_alert>(addr, port, ih); m_alerts.emplace_alert<dht_announce_alert>(addr, port, ih);
} }
void session_impl::outgoing_get_peers(sha1_hash const& target void session_impl::outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep) , sha1_hash const& sent_target, udp::endpoint const& ep) TORRENT_OVERRIDE
{ {
if (!m_alerts.should_post<dht_outgoing_get_peers_alert>()) return; if (!m_alerts.should_post<dht_outgoing_get_peers_alert>()) return;
m_alerts.emplace_alert<dht_outgoing_get_peers_alert>(target, sent_target, ep); m_alerts.emplace_alert<dht_outgoing_get_peers_alert>(target, sent_target, ep);

View File

@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/kademlia/node_id.hpp" #include "libtorrent/kademlia/node_id.hpp"
#include "libtorrent/kademlia/routing_table.hpp" #include "libtorrent/kademlia/routing_table.hpp"
#include "libtorrent/kademlia/item.hpp" #include "libtorrent/kademlia/item.hpp"
#include "libtorrent/kademlia/dht_observer.hpp"
#include "libtorrent/ed25519.hpp" #include "libtorrent/ed25519.hpp"
#include <numeric> #include <numeric>
@ -418,6 +419,22 @@ bool get_item_cb(dht::item& i)
return false; return false;
} }
struct obs : dht::dht_observer
{
virtual void set_external_address(address const& addr
, address const& source) TORRENT_OVERRIDE
{}
virtual address external_address() TORRENT_OVERRIDE
{
return address_v4::from_string("236.0.0.1");
}
virtual void get_peers(sha1_hash const& ih) TORRENT_OVERRIDE {}
virtual void outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep) TORRENT_OVERRIDE {}
virtual void announce(sha1_hash const& ih, address const& addr, int port) TORRENT_OVERRIDE {}
};
// TODO: test obfuscated_get_peers // TODO: test obfuscated_get_peers
int test_main() int test_main()
{ {
@ -425,10 +442,10 @@ int test_main()
sett.max_torrents = 4; sett.max_torrents = 4;
sett.max_dht_items = 4; sett.max_dht_items = 4;
sett.enforce_node_id = false; sett.enforce_node_id = false;
address ext = address::from_string("236.0.0.1");
mock_socket s; mock_socket s;
obs observer;
counters cnt; counters cnt;
dht::node_impl node(&s, sett, node_id(0), ext, 0, cnt); dht::node_impl node(&s, sett, node_id(0), &observer, cnt);
// DHT should be running on port 48199 now // DHT should be running on port 48199 now
bdecode_node response; bdecode_node response;
@ -1445,7 +1462,7 @@ int test_main()
g_sent_packets.clear(); g_sent_packets.clear();
do do
{ {
dht::node_impl node(&s, sett, node_id::min(), ext, 0, cnt); dht::node_impl node(&s, sett, (node_id::min)(), &observer, cnt);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234); udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
std::vector<udp::endpoint> nodesv; std::vector<udp::endpoint> nodesv;
@ -1517,7 +1534,7 @@ int test_main()
do do
{ {
dht::node_id target = to_hash("1234876923549721020394873245098347598635"); dht::node_id target = to_hash("1234876923549721020394873245098347598635");
dht::node_impl node(&s, sett, node_id::min(), ext, 0, cnt); dht::node_impl node(&s, sett, (node_id::min)(), &observer, cnt);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234); udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node); node.m_table.add_node(initial_node);
@ -1610,7 +1627,7 @@ int test_main()
g_sent_packets.clear(); g_sent_packets.clear();
do do
{ {
dht::node_impl node(&s, sett, node_id::min(), ext, 0, cnt); dht::node_impl node(&s, sett, (node_id::min)(), &observer, cnt);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234); udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node); node.m_table.add_node(initial_node);
@ -1656,7 +1673,7 @@ int test_main()
g_sent_packets.clear(); g_sent_packets.clear();
do do
{ {
dht::node_impl node(&s, sett, node_id::min(), ext, 0, cnt); dht::node_impl node(&s, sett, (node_id::min)(), &observer, cnt);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234); udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node); node.m_table.add_node(initial_node);
@ -1732,7 +1749,7 @@ int test_main()
g_sent_packets.clear(); g_sent_packets.clear();
do do
{ {
dht::node_impl node(&s, sett, node_id::min(), ext, 0, cnt); dht::node_impl node(&s, sett, (node_id::min)(), &observer, cnt);
enum { num_test_nodes = 2 }; enum { num_test_nodes = 2 };
node_entry nodes[num_test_nodes] = node_entry nodes[num_test_nodes] =
{ node_entry(generate_next(), udp::endpoint(address_v4::from_string("4.4.4.4"), 1234)) { node_entry(generate_next(), udp::endpoint(address_v4::from_string("4.4.4.4"), 1234))
@ -1814,7 +1831,7 @@ int test_main()
g_sent_packets.clear(); g_sent_packets.clear();
do do
{ {
dht::node_impl node(&s, sett, node_id::min(), ext, 0, cnt); dht::node_impl node(&s, sett, (node_id::min)(), &observer, cnt);
enum { num_test_nodes = 2 }; enum { num_test_nodes = 2 };
node_entry nodes[num_test_nodes] = node_entry nodes[num_test_nodes] =
{ node_entry(generate_next(), udp::endpoint(address_v4::from_string("4.4.4.4"), 1234)) { node_entry(generate_next(), udp::endpoint(address_v4::from_string("4.4.4.4"), 1234))