dht: announce with per-interface listen port

This commit is contained in:
Steven Siloti 2018-07-08 14:45:29 -07:00 committed by Arvid Norberg
parent 965ca9a565
commit 961cf62cad
9 changed files with 41 additions and 11 deletions

View File

@ -593,6 +593,8 @@ namespace aux {
std::uint16_t ssl_listen_port() const override; std::uint16_t ssl_listen_port() const override;
std::uint16_t ssl_listen_port(listen_socket_t* sock) const; std::uint16_t ssl_listen_port(listen_socket_t* sock) const;
int get_listen_port(transport ssl, aux::listen_socket_handle const& s) override;
std::uint32_t get_tracker_key(address const& iface) const; std::uint32_t get_tracker_key(address const& iface) const;
void for_each_listen_socket(std::function<void(aux::listen_socket_handle const&)> f) override void for_each_listen_socket(std::function<void(aux::listen_socket_handle const&)> f) override

View File

@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/address.hpp" #include "libtorrent/address.hpp"
#include "libtorrent/string_view.hpp" #include "libtorrent/string_view.hpp"
#include "libtorrent/kademlia/msg.hpp" #include "libtorrent/kademlia/msg.hpp"
#include "libtorrent/aux_/session_udp_sockets.hpp" // for transport
namespace libtorrent { namespace libtorrent {
@ -78,6 +79,7 @@ namespace dht {
{ {
virtual void set_external_address(aux::listen_socket_handle const& iface virtual void set_external_address(aux::listen_socket_handle const& iface
, address const& addr, address const& source) = 0; , address const& addr, address const& source) = 0;
virtual int get_listen_port(aux::transport ssl, aux::listen_socket_handle const& s) = 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

@ -96,7 +96,7 @@ namespace libtorrent { namespace dht {
dht_state state() const; dht_state state() const;
enum flags_t { flag_seed = 1, flag_implied_port = 2 }; enum flags_t { flag_seed = 1, flag_implied_port = 2, flag_ssl_torrent = 4 };
void get_peers(sha1_hash const& ih void get_peers(sha1_hash const& ih
, std::function<void(std::vector<tcp::endpoint> const&)> f); , std::function<void(std::vector<tcp::endpoint> const&)> f);
void announce(sha1_hash const& ih, int listen_port, int flags void announce(sha1_hash const& ih, int listen_port, int flags

View File

@ -132,7 +132,7 @@ public:
int data_size() const { return int(m_storage.num_torrents()); } int data_size() const { return int(m_storage.num_torrents()); }
#endif #endif
enum flags_t { flag_seed = 1, flag_implied_port = 2 }; enum flags_t { flag_seed = 1, flag_implied_port = 2, flag_ssl_torrent = 4 };
void get_peers(sha1_hash const& info_hash void get_peers(sha1_hash const& info_hash
, std::function<void(std::vector<tcp::endpoint> const&)> dcallback , std::function<void(std::vector<tcp::endpoint> const&)> dcallback
, std::function<void(std::vector<std::pair<node_entry, std::string>> const&)> ncallback , std::function<void(std::vector<std::pair<node_entry, std::string>> const&)> ncallback

View File

@ -60,6 +60,8 @@ struct obs : dht::dht_observer
void set_external_address(lt::aux::listen_socket_handle const&, address const& /* addr */ void set_external_address(lt::aux::listen_socket_handle const&, address const& /* addr */
, address const& /* source */) override , address const& /* source */) override
{} {}
int get_listen_port(lt::aux::transport, lt::aux::listen_socket_handle const& s) override
{ return s.get()->udp_external_port; }
void get_peers(sha1_hash const&) override {} void get_peers(sha1_hash const&) override {}
void outgoing_get_peers(sha1_hash const& /* target */ void outgoing_get_peers(sha1_hash const& /* target */
, sha1_hash const& /* sent_target */, udp::endpoint const& /* ep */) override {} , sha1_hash const& /* sent_target */, udp::endpoint const& /* ep */) override {}

View File

@ -441,7 +441,7 @@ void node::get_peers(sha1_hash const& info_hash
ta->start(); ta->start();
} }
void node::announce(sha1_hash const& info_hash, int const listen_port, int const flags void node::announce(sha1_hash const& info_hash, int listen_port, int const flags
, std::function<void(std::vector<tcp::endpoint> const&)> f) , std::function<void(std::vector<tcp::endpoint> const&)> f)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -452,6 +452,13 @@ void node::announce(sha1_hash const& info_hash, int const listen_port, int const
} }
#endif #endif
if (listen_port == 0)
{
listen_port = m_observer->get_listen_port(
flags & node::flag_ssl_torrent ? aux::transport::ssl : aux::transport::plaintext
, m_sock);
}
get_peers(info_hash, std::move(f) get_peers(info_hash, std::move(f)
, std::bind(&announce_fun, _1, std::ref(*this) , std::bind(&announce_fun, _1, std::ref(*this)
, listen_port, info_hash, flags), flags & node::flag_seed); , listen_port, info_hash, flags), flags & node::flag_seed);

View File

@ -5438,6 +5438,24 @@ namespace aux {
return 0; return 0;
} }
int session_impl::get_listen_port(transport const ssl, aux::listen_socket_handle const& s)
{
auto socket = s.get();
if (socket->ssl != ssl)
{
auto alt_socket = std::find_if(m_listen_sockets.begin(), m_listen_sockets.end()
, [&](std::shared_ptr<listen_socket_t> const& e)
{
return e->ssl == ssl
&& e->external_address.external_address()
== socket->external_address.external_address();
});
if (alt_socket != m_listen_sockets.end())
socket = alt_socket->get();
}
return socket->udp_external_port;
}
void session_impl::announce_lsd(sha1_hash const& ih, int port, bool broadcast) void session_impl::announce_lsd(sha1_hash const& ih, int port, bool broadcast)
{ {
// use internal listen port for local peers // use internal listen port for local peers

View File

@ -2586,12 +2586,6 @@ bool is_downloading_state(int const st)
TORRENT_ASSERT(!m_paused); TORRENT_ASSERT(!m_paused);
#ifdef TORRENT_USE_OPENSSL
int port = is_ssl_torrent() ? m_ses.ssl_listen_port() : m_ses.listen_port();
#else
int port = m_ses.listen_port();
#endif
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
debug_log("START DHT announce"); debug_log("START DHT announce");
m_dht_start_time = aux::time_now(); m_dht_start_time = aux::time_now();
@ -2606,9 +2600,11 @@ bool is_downloading_state(int const st)
if (settings().get_bool(settings_pack::enable_incoming_utp)) if (settings().get_bool(settings_pack::enable_incoming_utp))
flags |= dht::dht_tracker::flag_implied_port; flags |= dht::dht_tracker::flag_implied_port;
if (is_ssl_torrent())
flags |= dht::dht_tracker::flag_ssl_torrent;
std::weak_ptr<torrent> self(shared_from_this()); std::weak_ptr<torrent> self(shared_from_this());
m_ses.dht()->announce(m_torrent_file->info_hash() m_ses.dht()->announce(m_torrent_file->info_hash(), 0, flags
, port, flags
, std::bind(&torrent::on_dht_announce_response_disp, self, _1)); , std::bind(&torrent::on_dht_announce_response_disp, self, _1));
} }

View File

@ -527,6 +527,9 @@ struct obs : dht::dht_observer
, aux::session_interface::source_dht, rand_v4()); , aux::session_interface::source_dht, rand_v4());
} }
int get_listen_port(aux::transport, aux::listen_socket_handle const& s) override
{ return s.get()->udp_external_port; }
void get_peers(sha1_hash const&) override {} void get_peers(sha1_hash const&) override {}
void outgoing_get_peers(sha1_hash const& /*target*/ void outgoing_get_peers(sha1_hash const& /*target*/
, sha1_hash const& /*sent_target*/, udp::endpoint const&) override {} , sha1_hash const& /*sent_target*/, udp::endpoint const&) override {}