From df8a57efb7d51069b9a40eae80a9c8c7ccb300aa Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Sun, 10 Sep 2017 01:25:04 -0700 Subject: [PATCH] select which DHT port to report based on the connection's local endpoint (#2316) --- include/libtorrent/aux_/session_impl.hpp | 9 +-------- include/libtorrent/aux_/session_interface.hpp | 2 +- src/bt_peer_connection.cpp | 2 +- src/session_impl.cpp | 16 ++++++++++++++++ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 5f52f794e..b04a13f89 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -600,14 +600,7 @@ namespace aux { #ifndef TORRENT_DISABLE_DHT bool is_dht_running() const { return (m_dht.get() != nullptr); } - int external_udp_port() const override - { - for (auto const& s : m_listen_sockets) - { - if (s->udp_sock) return s->udp_external_port; - } - return -1; - } + int external_udp_port(address const& local_address) const override; #endif #if TORRENT_USE_I2P diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index 8755f529b..52533ec2e 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -300,7 +300,7 @@ namespace libtorrent { namespace aux { virtual bool announce_dht() const = 0; virtual void add_dht_node(udp::endpoint const& n) = 0; virtual bool has_dht() const = 0; - virtual int external_udp_port() const = 0; + virtual int external_udp_port(address const& local_address) const = 0; virtual dht::dht_tracker* dht() = 0; virtual void prioritize_dht(std::weak_ptr t) = 0; #endif diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 5d8e3d202..cdeedcd90 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -333,7 +333,7 @@ namespace { #ifndef TORRENT_DISABLE_DHT if (m_supports_dht_port && m_ses.has_dht()) { - int const port = m_ses.external_udp_port(); + int const port = m_ses.external_udp_port(local_endpoint().address()); if (port >= 0) write_dht_port(port); } #endif diff --git a/src/session_impl.cpp b/src/session_impl.cpp index b1b73a9e6..7e53626d6 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2151,6 +2151,22 @@ namespace { #endif } +#ifndef TORRENT_DISABLE_DHT + int session_impl::external_udp_port(address const& local_address) const + { + auto ls = std::find_if(m_listen_sockets.begin(), m_listen_sockets.end() + , [&](std::shared_ptr const& e) + { + return e->local_endpoint.address() == local_address; + }); + + if (ls != m_listen_sockets.end()) + return (*ls)->udp_external_port; + else + return -1; + } +#endif + #if TORRENT_USE_I2P proxy_settings session_impl::i2p_proxy() const