From 356d2506bd9879964c4018e2a31c6cafeab7a27f Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Sat, 29 Aug 2015 19:49:47 -0700 Subject: [PATCH] extend dht_observer to support IPv6 external addresses --- include/libtorrent/address.hpp | 2 ++ include/libtorrent/aux_/session_impl.hpp | 2 +- include/libtorrent/kademlia/dht_observer.hpp | 2 +- src/kademlia/node.cpp | 8 ++++---- src/session_impl.cpp | 7 +++++-- test/test_dht.cpp | 2 +- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/libtorrent/address.hpp b/include/libtorrent/address.hpp index 0682991b5..0efad18ee 100644 --- a/include/libtorrent/address.hpp +++ b/include/libtorrent/address.hpp @@ -61,6 +61,8 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { + enum address_type { ipv4, ipv6, num_address_type }; + #if defined TORRENT_BUILD_SIMULATOR typedef sim::asio::ip::address address; typedef sim::asio::ip::address_v4 address_v4; diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index e3d59c483..8b037d98e 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -604,7 +604,7 @@ namespace libtorrent // implements dht_observer virtual void set_external_address(address const& ip , address const& source) TORRENT_OVERRIDE; - virtual address external_address() TORRENT_OVERRIDE; + virtual address external_address(address_type at) TORRENT_OVERRIDE; 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 diff --git a/include/libtorrent/kademlia/dht_observer.hpp b/include/libtorrent/kademlia/dht_observer.hpp index 9ea84eee7..773cbfc1c 100644 --- a/include/libtorrent/kademlia/dht_observer.hpp +++ b/include/libtorrent/kademlia/dht_observer.hpp @@ -68,7 +68,7 @@ namespace libtorrent { namespace dht { virtual void set_external_address(address const& addr , address const& source) = 0; - virtual address external_address() = 0; + virtual address external_address(address_type at) = 0; virtual void get_peers(sha1_hash const& ih) = 0; virtual void outgoing_get_peers(sha1_hash const& target , sha1_hash const& sent_target, udp::endpoint const& ep) = 0; diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index debd81c9d..1dca84d28 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -73,14 +73,14 @@ namespace { void nop() {} -node_id calculate_node_id(node_id const& nid, dht_observer* observer) +node_id calculate_node_id(node_id const& nid, dht_observer* observer, address_type at) { address external_address; - if (observer) external_address = observer->external_address(); + if (observer) external_address = observer->external_address(at); // if we don't have an observer, don't pretend that external_address is valid // generating an ID based on 0.0.0.0 would be terrible. random is better - if (!observer || external_address == address()) + if (!observer || external_address.is_unspecified()) { return generate_random_id(); } @@ -99,7 +99,7 @@ node::node(udp_socket_interface* sock , struct counters& cnt , dht_storage_constructor_type storage_constructor) : m_settings(settings) - , m_id(calculate_node_id(nid, observer)) + , m_id(calculate_node_id(nid, observer, ipv4)) , m_table(m_id, 8, settings, observer) , m_rpc(m_id, m_settings, m_table, sock, observer) , m_observer(observer) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 219eaca01..9662bcf48 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -6779,9 +6779,12 @@ namespace aux { set_external_address(ip, source_dht, source); } - address session_impl::external_address() + address session_impl::external_address(address_type at) { - return m_external_ip.external_address(address_v4()); + if (at == ipv4) + return m_external_ip.external_address(address_v4()); + else + return m_external_ip.external_address(address_v6()); } void session_impl::get_peers(sha1_hash const& ih) diff --git a/test/test_dht.cpp b/test/test_dht.cpp index d95a8e4e8..8643af567 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -476,7 +476,7 @@ struct obs : dht::dht_observer , address const& source) TORRENT_OVERRIDE {} - virtual address external_address() TORRENT_OVERRIDE + virtual address external_address(address_type at) TORRENT_OVERRIDE { return address_v4::from_string("236.0.0.1"); }