back-ported observer for set_external_ip for DHT

This commit is contained in:
Arvid Norberg 2012-04-30 06:30:35 +00:00
parent 918dce1341
commit cec9a16cc1
8 changed files with 22 additions and 17 deletions

View File

@ -133,6 +133,7 @@ nobase_include_HEADERS = \
extensions/ut_pex.hpp \
\
kademlia/dht_tracker.hpp \
kademlia/dht_observer.hpp \
kademlia/find_data.hpp \
kademlia/logging.hpp \
kademlia/msg.hpp \

View File

@ -86,6 +86,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/bloom_filter.hpp"
#include "libtorrent/rss.hpp"
#include "libtorrent/alert_dispatcher.hpp"
#include "libtorrent/kademlia/dht_observer.hpp"
#if TORRENT_COMPLETE_TYPES_REQUIRED
#include "libtorrent/peer_connection.hpp"
@ -184,6 +185,7 @@ namespace libtorrent
// thread started to run the main downloader loop
struct TORRENT_EXTRA_EXPORT session_impl
: alert_dispatcher
, dht::dht_observer
, boost::noncopyable
, initialize_timer
, boost::enable_shared_from_this<session_impl>
@ -487,7 +489,8 @@ namespace libtorrent
source_router = 8
};
void set_external_address(address const& ip
// implements dht_observer
virtual void set_external_address(address const& ip
, int source_type, address const& source);
address const& external_address() const { return m_external_address; }

View File

@ -187,11 +187,9 @@ typedef std::map<node_id, dht_immutable_item> dht_immutable_table_t;
typedef std::map<rsa_key, dht_mutable_item> dht_mutable_table_t;
public:
typedef boost::function3<void, address, int, address> external_ip_fun;
node_impl(alert_dispatcher* alert_disp, udp_socket_interface* sock
, dht_settings const& settings, node_id nid, address const& external_address
, external_ip_fun ext_ip);
, dht_observer* observer);
virtual ~node_impl() {}

View File

@ -66,6 +66,7 @@ struct null_observer : public observer
};
class routing_table;
struct dht_observer;
class TORRENT_EXTRA_EXPORT rpc_manager
{
@ -74,7 +75,7 @@ public:
rpc_manager(node_id const& our_id
, routing_table& table, udp_socket_interface* sock
, external_ip_fun ext_ip);
, dht_observer* observer);
~rpc_manager();
void unreachable(udp::endpoint const& ep);
@ -117,7 +118,7 @@ private:
node_id m_random_number;
int m_allocated_observers;
bool m_destructing;
external_ip_fun m_ext_ip;
dht_observer* m_observer;
};
} } // namespace libtorrent::dht

View File

@ -203,8 +203,7 @@ namespace libtorrent { namespace dht
dht_tracker::dht_tracker(libtorrent::aux::session_impl& ses, rate_limited_udp_socket& sock
, dht_settings const& settings, entry const* state)
: m_dht(&ses, this, settings, extract_node_id(state)
, ses.external_address()
, boost::bind(&aux::session_impl::set_external_address, &ses, _1, _2, _3))
, ses.external_address(), &ses)
, m_sock(sock)
, m_last_new_key(time_now() - minutes(key_refresh))
, m_timer(sock.get_io_service())

View File

@ -91,11 +91,11 @@ void nop() {}
node_impl::node_impl(alert_dispatcher* alert_disp
, udp_socket_interface* sock
, dht_settings const& settings, node_id nid, address const& external_address
, external_ip_fun ext_ip)
, dht_observer* observer)
: m_settings(settings)
, m_id(nid == (node_id::min)() || !verify_id(nid, external_address) ? generate_id(external_address) : nid)
, m_table(m_id, 8, settings)
, m_rpc(m_id, m_table, sock, ext_ip)
, m_rpc(m_id, m_table, sock, observer)
, m_last_tracker_tick(time_now())
, m_post_alert(alert_disp)
, m_sock(sock)

View File

@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/kademlia/refresh.hpp>
#include <libtorrent/kademlia/node.hpp>
#include <libtorrent/kademlia/observer.hpp>
#include <libtorrent/kademlia/dht_observer.hpp>
#include <libtorrent/hasher.hpp>
#include <libtorrent/time.hpp>
#include <time.h> // time()
@ -161,7 +162,7 @@ enum { observer_size = max3<
rpc_manager::rpc_manager(node_id const& our_id
, routing_table& table, udp_socket_interface* sock
, external_ip_fun ext_ip)
, dht_observer* observer)
: m_pool_allocator(observer_size, 10)
, m_sock(sock)
, m_our_id(our_id)
@ -170,7 +171,7 @@ rpc_manager::rpc_manager(node_id const& our_id
, m_random_number(generate_random_id())
, m_allocated_observers(0)
, m_destructing(false)
, m_ext_ip(ext_ip)
, m_observer(observer)
{
std::srand(time(0));
@ -342,7 +343,9 @@ bool rpc_manager::incoming(msg const& m, node_id* id)
// this node claims we use the wrong node-ID!
address_v4::bytes_type b;
memcpy(&b[0], ext_ip->string_ptr(), 4);
m_ext_ip(address_v4(b), aux::session_impl::source_dht, m.addr.address());
if (m_observer)
m_observer->set_external_address(address_v4(b)
, aux::session_impl::source_dht, m.addr.address());
}
#if TORRENT_USE_IPV6
else if (ext_ip && ext_ip->string_length() == 16)
@ -350,7 +353,9 @@ bool rpc_manager::incoming(msg const& m, node_id* id)
// this node claims we use the wrong node-ID!
address_v6::bytes_type b;
memcpy(&b[0], ext_ip->string_ptr(), 16);
m_ext_ip(address_v6(b), aux::session_impl::source_dht, m.addr.address());
if (m_observer)
m_observer->set_external_address(address_v6(b)
, aux::session_impl::source_dht, m.addr.address());
}
#endif

View File

@ -260,8 +260,6 @@ void announce_immutable_items(node_impl& node, udp::endpoint const* eps
// TEST_CHECK(items_num.find(3) != items_num.end());
}
void nop(address, int, address) {}
struct print_alert : alert_dispatcher
{
virtual bool post_alert(alert* a)
@ -280,7 +278,7 @@ int test_main()
address ext = address::from_string("236.0.0.1");
mock_socket s;
print_alert ad;
dht::node_impl node(&ad, &s, sett, node_id(0), ext, boost::bind(nop, _1, _2, _3));
dht::node_impl node(&ad, &s, sett, node_id(0), ext, 0);
// DHT should be running on port 48199 now
lazy_entry response;