diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index 222dde39c..146fe9185 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -89,7 +89,8 @@ namespace libtorrent { namespace dht entry state() const; - void announce(sha1_hash const& ih, int listen_port, bool seed + enum flags_t { flag_seed = 1, flag_implied_port = 2 }; + void announce(sha1_hash const& ih, int listen_port, int flags , boost::function const&)> f); void dht_status(session_status& s); diff --git a/include/libtorrent/kademlia/node.hpp b/include/libtorrent/kademlia/node.hpp index ba02c82d6..751dc5a7a 100644 --- a/include/libtorrent/kademlia/node.hpp +++ b/include/libtorrent/kademlia/node.hpp @@ -232,7 +232,8 @@ public: { m_table.print_state(os); } #endif - void announce(sha1_hash const& info_hash, int listen_port, bool seed + enum flags_t { flag_seed = 1, flag_implied_port = 2 }; + void announce(sha1_hash const& info_hash, int listen_port, int flags , boost::function const&)> f); void get_item(sha1_hash const& target, boost::function f); diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 9bf3a36b5..40210ce26 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -416,10 +416,10 @@ namespace libtorrent { namespace dht #endif } - void dht_tracker::announce(sha1_hash const& ih, int listen_port, bool seed + void dht_tracker::announce(sha1_hash const& ih, int listen_port, int flags , boost::function const&)> f) { - m_dht.announce(ih, listen_port, seed, f); + m_dht.announce(ih, listen_port, flags, f); } diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index d7baa04e3..c2b2986ba 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -294,7 +294,7 @@ void node_impl::incoming(msg const& m) namespace { void announce_fun(std::vector > const& v - , node_impl& node, int listen_port, sha1_hash const& ih, bool seed) + , node_impl& node, int listen_port, sha1_hash const& ih, int flags) { #ifdef TORRENT_DHT_VERBOSE_LOGGING TORRENT_LOG(node) << "sending announce_peer [ ih: " << ih @@ -327,8 +327,8 @@ namespace a["info_hash"] = ih.to_string(); a["port"] = listen_port; a["token"] = i->second; - a["seed"] = int(seed); - // TODO: 3 if uTP is enabled, we should say "implied_port": 1 + a["seed"] = (flags & node_impl::flag_seed) ? 1 : 0; + if (flags & node_impl::flag_implied_port) a["implied_port"] = 1; node.m_rpc.invoke(e, i->first.ep(), o); } } @@ -364,7 +364,7 @@ void node_impl::add_node(udp::endpoint node) m_rpc.invoke(e, node, o); } -void node_impl::announce(sha1_hash const& info_hash, int listen_port, bool seed +void node_impl::announce(sha1_hash const& info_hash, int listen_port, int flags , boost::function const&)> f) { #ifdef TORRENT_DHT_VERBOSE_LOGGING @@ -378,13 +378,13 @@ void node_impl::announce(sha1_hash const& info_hash, int listen_port, bool seed { ta.reset(new obfuscated_get_peers(*this, info_hash, f , boost::bind(&announce_fun, _1, boost::ref(*this) - , listen_port, info_hash, seed), seed)); + , listen_port, info_hash, flags), flags & node_impl::flag_seed)); } else { ta.reset(new get_peers(*this, info_hash, f , boost::bind(&announce_fun, _1, boost::ref(*this) - , listen_port, info_hash, seed), seed)); + , listen_port, info_hash, flags), flags & node_impl::flag_seed)); } ta->start(); diff --git a/src/torrent.cpp b/src/torrent.cpp index ebba1423c..d165f5a85 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2139,8 +2139,17 @@ namespace libtorrent #endif boost::weak_ptr self(shared_from_this()); + + // if we're a seed, we tell the DHT for better scrape stats + int flags = is_seed() ? dht::dht_tracker::flag_seed : 0; + // if we allow incoming uTP connections, set the implied_port + // argument in the announce, this will make the DHT node use + // our source port in the packet as our listen port, which is + // likely more accurate when behind a NAT + if (settings().enable_incoming_utp) flags |= dht::dht_tracker::flag_implied_port; + m_ses.m_dht->announce(m_torrent_file->info_hash() - , port, is_seed() + , port, flags , boost::bind(&torrent::on_dht_announce_response_disp, self, _1)); }