From ed6b2ad3cf0d655d4d144df5655b444c47b23386 Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Fri, 27 Jul 2007 06:45:15 +0000 Subject: [PATCH] libtorrent sync 1417 --- libtorrent/include/libtorrent/policy.hpp | 2 ++ libtorrent/include/libtorrent/torrent.hpp | 2 ++ libtorrent/src/policy.cpp | 41 +++++++++++++++++++++++ libtorrent/src/session_impl.cpp | 26 ++------------ 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/libtorrent/include/libtorrent/policy.hpp b/libtorrent/include/libtorrent/policy.hpp index e087ccb75..6c976d047 100755 --- a/libtorrent/include/libtorrent/policy.hpp +++ b/libtorrent/include/libtorrent/policy.hpp @@ -110,6 +110,8 @@ namespace libtorrent // the peer is not interested in our pieces void not_interested(peer_connection& c); + void ip_filter_updated(); + #ifndef NDEBUG bool has_connection(const peer_connection* p); diff --git a/libtorrent/include/libtorrent/torrent.hpp b/libtorrent/include/libtorrent/torrent.hpp index bed076fe9..2eef2656b 100755 --- a/libtorrent/include/libtorrent/torrent.hpp +++ b/libtorrent/include/libtorrent/torrent.hpp @@ -175,6 +175,8 @@ namespace libtorrent boost::tuples::tuple bytes_done() const; size_type quantized_bytes_done() const; + void ip_filter_updated() { m_policy->ip_filter_updated(); } + void pause(); void resume(); bool is_paused() const { return m_paused; } diff --git a/libtorrent/src/policy.cpp b/libtorrent/src/policy.cpp index 2444deeb7..2f0d24b08 100755 --- a/libtorrent/src/policy.cpp +++ b/libtorrent/src/policy.cpp @@ -304,6 +304,47 @@ namespace libtorrent , m_available_free_upload(0) , m_last_optimistic_disconnect(min_time()) { assert(t); } + + // disconnects and removes all peers that are now filtered + void policy::ip_filter_updated() + { + aux::session_impl& ses = m_torrent->session(); + piece_picker* p = 0; + if (m_torrent->has_picker()) + p = &m_torrent->picker(); + for (std::list::iterator i = m_peers.begin() + , end(m_peers.end()); i != end;) + { + if ((ses.m_ip_filter.access(i->ip.address()) & ip_filter::blocked) == 0) + { + ++i; + continue; + } + + if (i->connection) + { + i->connection->disconnect(); + if (ses.m_alerts.should_post(alert::info)) + { + ses.m_alerts.post_alert(peer_blocked_alert(i->ip.address() + , "disconnected blocked peer")); + } + assert(i->connection == 0 + || i->connection->peer_info_struct() == 0); + } + else + { + if (ses.m_alerts.should_post(alert::info)) + { + ses.m_alerts.post_alert(peer_blocked_alert(i->ip.address() + , "blocked peer removed from peer list")); + } + } + if (p) p->clear_peer(&(*i)); + m_peers.erase(i++); + } + } + // finds the peer that has the worst download rate // and returns it. May return 0 if all peers are // choked. diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index f4323586c..8fae3bcb7 100755 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -613,29 +613,9 @@ namespace detail // Close connections whose endpoint is filtered // by the new ip-filter - for (session_impl::connection_map::iterator i - = m_connections.begin(); i != m_connections.end();) - { - tcp::endpoint sender; - try { sender = i->first->remote_endpoint(); } - catch (std::exception&) { sender = i->second->remote(); } - if (m_ip_filter.access(sender.address()) & ip_filter::blocked) - { -#if defined(TORRENT_VERBOSE_LOGGING) - (*i->second->m_logger) << "*** CONNECTION FILTERED\n"; -#endif - if (m_alerts.should_post(alert::info)) - { - m_alerts.post_alert(peer_blocked_alert(sender.address() - , "peer connection closed by IP filter")); - } - - session_impl::connection_map::iterator j = i; - ++i; - j->second->disconnect(); - } - else ++i; - } + for (torrent_map::iterator i = m_torrents.begin() + , end(m_torrents.end()); i != end; ++i) + i->second->ip_filter_updated(); } void session_impl::set_settings(session_settings const& s)