From 60af1aa4516e701a024e76413d017f494405e1b8 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 23 Jan 2014 03:31:36 +0000 Subject: [PATCH] update peer ranks when our external IP changes --- include/libtorrent/policy.hpp | 7 ++++++- include/libtorrent/torrent.hpp | 2 ++ src/policy.cpp | 8 +++++++- src/session_impl.cpp | 6 ++++++ src/torrent.cpp | 5 +++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/policy.hpp b/include/libtorrent/policy.hpp index 42901f8ae..d015ecc5c 100644 --- a/include/libtorrent/policy.hpp +++ b/include/libtorrent/policy.hpp @@ -105,7 +105,8 @@ namespace libtorrent // calculate the priority of a peer based on its address. One of the // endpoint should be our own. The priority is symmetric, so it doesn't // matter which is which - TORRENT_EXTRA_EXPORT boost::uint32_t peer_priority(tcp::endpoint e1, tcp::endpoint e2); + TORRENT_EXTRA_EXPORT boost::uint32_t peer_priority( + tcp::endpoint e1, tcp::endpoint e2); void request_a_block(torrent& t, peer_connection& c); @@ -147,6 +148,10 @@ namespace libtorrent void set_seed(policy::peer* p, bool s); + // this clears all cached peer priorities. It's called when + // our external IP changes + void clear_peer_prio(); + #if TORRENT_USE_ASSERTS bool has_connection(const peer_connection* p); #endif diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index c7fba0c78..67c1ee9c1 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -216,6 +216,8 @@ namespace libtorrent void abort(); bool is_aborted() const { return m_abort; } + void new_external_ip(); + torrent_status::state_t state() const { return (torrent_status::state_t)m_state; } void set_state(torrent_status::state_t s); diff --git a/src/policy.cpp b/src/policy.cpp index 79113da4e..7fddd23b2 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -447,6 +447,13 @@ namespace libtorrent , m_finished(false) { TORRENT_ASSERT(t); } + void policy::clear_peer_prio() + { + for (peers_t::iterator i = m_peers.begin() + , end(m_peers.end()); i != end; ++i) + (*i)->peer_rank = 0; + } + // disconnects and removes all peers that are now filtered void policy::ip_filter_updated() { @@ -1903,7 +1910,6 @@ namespace libtorrent // TOOD: pass in both an IPv6 and IPv4 address here boost::uint32_t policy::peer::rank(external_ip const& external, int external_port) const { -//TODO 3: how do we deal with our external address changing? Pass in a force-update maybe? and keep a version number in policy if (peer_rank == 0) peer_rank = peer_priority( tcp::endpoint(external.external_address(this->address()), external_port) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index e2dff337a..813e61ef1 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -6241,6 +6241,12 @@ retry: if (m_alerts.should_post()) m_alerts.post_alert(external_ip_alert(ip)); + for (torrent_map::iterator i = m_torrents.begin() + , end(m_torrents.end()); i != end; ++i) + { + i->second->new_external_ip(); + } + // since we have a new external IP now, we need to // restart the DHT with a new node ID #ifndef TORRENT_DISABLE_DHT diff --git a/src/torrent.cpp b/src/torrent.cpp index d4d36b215..cafbc5fae 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -8594,6 +8594,11 @@ namespace libtorrent } } + void torrent::new_external_ip() + { + m_policy.clear_peer_prio(); + } + void torrent::set_state(torrent_status::state_t s) { TORRENT_ASSERT(m_ses.is_network_thread());