From 9b0bc2ed5fc1518dfc504a92f3cba0c8bc7ce8a1 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Mon, 15 Aug 2016 21:05:39 -0400 Subject: [PATCH] peer_plugin refactor (#1002) refactor of ut_pex peer storage --- include/libtorrent/address.hpp | 2 - include/libtorrent/aux_/session_impl.hpp | 1 - include/libtorrent/bt_peer_connection.hpp | 28 ++++++++ include/libtorrent/extensions.hpp | 4 -- include/libtorrent/extensions/ut_pex.hpp | 4 +- include/libtorrent/peer_connection.hpp | 1 - include/libtorrent/peer_connection_handle.hpp | 1 - src/bt_peer_connection.cpp | 22 ++++++ src/peer_connection.cpp | 10 --- src/peer_connection_handle.cpp | 12 ---- src/session.cpp | 6 +- src/session_impl.cpp | 3 +- src/torrent.cpp | 8 +-- src/ut_metadata.cpp | 2 - src/ut_pex.cpp | 70 ++++--------------- 15 files changed, 70 insertions(+), 104 deletions(-) diff --git a/include/libtorrent/address.hpp b/include/libtorrent/address.hpp index 1085cb29d..02461dc2e 100644 --- a/include/libtorrent/address.hpp +++ b/include/libtorrent/address.hpp @@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_ADDRESS_HPP_INCLUDED #define TORRENT_ADDRESS_HPP_INCLUDED -#include #include "libtorrent/config.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" @@ -77,4 +76,3 @@ namespace libtorrent } #endif - diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 48f6b42d3..85523ce84 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -222,7 +222,6 @@ namespace libtorrent struct session_plugin_wrapper : plugin { explicit session_plugin_wrapper(ext_function_t const& f) : m_f(f) {} - explicit session_plugin_wrapper(session_plugin_wrapper const& p) : m_f(p.m_f) {} boost::shared_ptr new_torrent(torrent_handle const& t, void* user) override { return m_f(t, user); } diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index 46a604b46..d2f912b7d 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -66,6 +66,28 @@ namespace libtorrent { class torrent; +#ifndef TORRENT_DISABLE_EXTENSIONS + struct TORRENT_EXTRA_EXPORT ut_pex_peer_store + { + // stores all peers this peer is connected to. These lists + // are updated with each pex message and are limited in size + // to protect against malicious clients. These lists are also + // used for looking up which peer a peer that supports holepunch + // came from. + // these are vectors to save memory and keep the items close + // together for performance. Inserting and removing is relatively + // cheap since the lists' size is limited + using peers4_t = std::vector>; + peers4_t m_peers; +#if TORRENT_USE_IPV6 + using peers6_t = std::vector>; + peers6_t m_peers6; +#endif + + bool was_introduced_by(tcp::endpoint const& ep); + }; +#endif + class TORRENT_EXTRA_EXPORT bt_peer_connection : public peer_connection { @@ -170,6 +192,10 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS bool supports_holepunch() const { return m_holepunch_id != 0; } + void set_ut_pex(boost::shared_ptr ut_pex) + { m_ut_pex = ut_pex; } + bool was_introduced_by(tcp::endpoint const& ep) const + { return m_ut_pex && m_ut_pex->was_introduced_by(ep); } #endif bool support_extensions() const { return m_supports_extensions; } @@ -431,6 +457,8 @@ private: // 0 if not supported std::uint8_t m_share_mode_id; + boost::shared_ptr m_ut_pex; + char m_reserved_bits[8]; #endif diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 7d7cedd43..8f9ccd077 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -374,10 +374,6 @@ namespace libtorrent // hidden virtual ~peer_plugin() {} - // This function is expected to return the name of - // the plugin. - virtual char const* type() const { return ""; } - // can add entries to the extension handshake // this is not called for web seeds virtual void add_handshake(entry&) {} diff --git a/include/libtorrent/extensions/ut_pex.hpp b/include/libtorrent/extensions/ut_pex.hpp index 97ca3e246..cd0136efc 100644 --- a/include/libtorrent/extensions/ut_pex.hpp +++ b/include/libtorrent/extensions/ut_pex.hpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/socket.hpp" // for endpoint +#include "libtorrent/address.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" @@ -47,7 +48,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { struct torrent_plugin; - struct peer_plugin; struct torrent_handle; // constructor function for the ut_pex extension. The ut_pex @@ -59,8 +59,6 @@ namespace libtorrent // This can either be passed in the add_torrent_params::extensions field, or // via torrent_handle::add_extension(). TORRENT_EXPORT boost::shared_ptr create_ut_pex_plugin(torrent_handle const&, void*); - - bool was_introduced_by(peer_plugin const* pp, tcp::endpoint const& ep); } #endif // TORRENT_DISABLE_EXTENSIONS diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 8bc5e9cb4..448258c65 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -331,7 +331,6 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS void add_extension(boost::shared_ptr); - peer_plugin const* find_plugin(char const* type); #endif // this function is called once the torrent associated diff --git a/include/libtorrent/peer_connection_handle.hpp b/include/libtorrent/peer_connection_handle.hpp index a2b8d5c1a..8bd7c6452 100644 --- a/include/libtorrent/peer_connection_handle.hpp +++ b/include/libtorrent/peer_connection_handle.hpp @@ -60,7 +60,6 @@ struct TORRENT_EXPORT peer_connection_handle int type() const; void add_extension(boost::shared_ptr); - peer_plugin const* find_plugin(char const* type); bool is_seed() const; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index c5aa378da..a87b0a989 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -131,6 +131,28 @@ namespace libtorrent } // anonymous namespace #endif +#ifndef TORRENT_DISABLE_EXTENSIONS + bool ut_pex_peer_store::was_introduced_by(tcp::endpoint const &ep) + { +#if TORRENT_USE_IPV6 + if (ep.address().is_v4()) + { +#endif + peers4_t::value_type v(ep.address().to_v4().to_bytes(), ep.port()); + auto i = std::lower_bound(m_peers.begin(), m_peers.end(), v); + return i != m_peers.end() && *i == v; +#if TORRENT_USE_IPV6 + } + else + { + peers6_t::value_type v(ep.address().to_v6().to_bytes(), ep.port()); + auto i = std::lower_bound(m_peers6.begin(), m_peers6.end(), v); + return i != m_peers6.end() && *i == v; + } +#endif + } +#endif // TORRENT_DISABLE_EXTENSIONS + bt_peer_connection::bt_peer_connection(peer_connection_args const& pack , peer_id const& pid) : peer_connection(pack) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 60a2652a1..60d6b89f8 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -528,16 +528,6 @@ namespace libtorrent TORRENT_ASSERT(is_single_thread()); m_extensions.push_back(ext); } - - peer_plugin const* peer_connection::find_plugin(char const* type) - { - TORRENT_ASSERT(is_single_thread()); - for (auto p : m_extensions) - { - if (std::strcmp(p->type(), type) == 0) return p.get(); - } - return nullptr; - } #endif void peer_connection::send_allowed_set() diff --git a/src/peer_connection_handle.cpp b/src/peer_connection_handle.cpp index 065bb0b8f..a258cee35 100644 --- a/src/peer_connection_handle.cpp +++ b/src/peer_connection_handle.cpp @@ -59,18 +59,6 @@ void peer_connection_handle::add_extension(boost::shared_ptr ext) #endif } -peer_plugin const* peer_connection_handle::find_plugin(char const* type) -{ -#ifndef TORRENT_DISABLE_EXTENSIONS - boost::shared_ptr pc = native_handle(); - TORRENT_ASSERT(pc); - return pc->find_plugin(type); -#else - TORRENT_UNUSED(type); - return nullptr; -#endif -} - bool peer_connection_handle::is_seed() const { boost::shared_ptr pc = native_handle(); diff --git a/src/session.cpp b/src/session.cpp index 9e3ac34b5..4e3b71804 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -332,9 +332,9 @@ namespace libtorrent if (empty) return {}; using wrapper = session_impl::session_plugin_wrapper; return { - boost::make_shared(wrapper(create_ut_pex_plugin)), - boost::make_shared(wrapper(create_ut_metadata_plugin)), - boost::make_shared(wrapper(create_smart_ban_plugin)) + boost::make_shared(create_ut_pex_plugin), + boost::make_shared(create_ut_metadata_plugin), + boost::make_shared(create_smart_ban_plugin) }; #else TORRENT_UNUSED(empty); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 79bc38c3f..87732605a 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -776,8 +776,7 @@ namespace aux { TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(ext); - add_ses_extension(boost::make_shared( - session_plugin_wrapper(ext))); + add_ses_extension(boost::make_shared(ext)); } void session_impl::add_ses_extension(boost::shared_ptr ext) diff --git a/src/torrent.cpp b/src/torrent.cpp index 3b3c14e45..2ac7a04b3 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -104,10 +104,6 @@ POSSIBILITY OF SUCH DAMAGE. // TODO: factor out cache_status to its own header #include "libtorrent/disk_io_thread.hpp" // for cache_status -#ifndef TORRENT_DISABLE_EXTENSIONS -#include "libtorrent/extensions/ut_pex.hpp" // for was_introduced_by -#endif - #ifndef TORRENT_DISABLE_LOGGING #include "libtorrent/aux_/session_impl.hpp" // for tracker_logger #endif @@ -2160,9 +2156,7 @@ namespace libtorrent if (pe->type() != peer_connection::bittorrent_connection) continue; bt_peer_connection* p = static_cast(pe); if (!p->supports_holepunch()) continue; - peer_plugin const* pp = p->find_plugin("ut_pex"); - if (!pp) continue; - if (was_introduced_by(pp, ep)) return p; + if (p->was_introduced_by(ep)) return p; } #else TORRENT_UNUSED(ep); diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 10101c703..11dc8e716 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -217,8 +217,6 @@ namespace libtorrent { namespace , m_tp(tp) {} - char const* type() const override { return "ut_metadata"; } - // can add entries to the extension handshake void add_handshake(entry& h) override { diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index a6e2bc6d1..fde34b0ef 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -53,12 +53,6 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_DISABLE_EXTENSIONS -#include "libtorrent/aux_/disable_warnings_push.hpp" - -#include - -#include "libtorrent/aux_/disable_warnings_pop.hpp" - namespace libtorrent { namespace { const char extension_name[] = "ut_pex"; @@ -242,7 +236,7 @@ namespace libtorrent { namespace }; struct ut_pex_peer_plugin final - : peer_plugin + : ut_pex_peer_store, peer_plugin { ut_pex_peer_plugin(torrent& t, peer_connection& pc, ut_pex_plugin& tp) : m_torrent(t) @@ -252,15 +246,13 @@ namespace libtorrent { namespace , m_message_index(0) , m_first_time(true) { - const int num_pex_timers = sizeof(m_last_pex)/sizeof(m_last_pex[0]); + const int num_pex_timers = sizeof(m_last_pex) / sizeof(m_last_pex[0]); for (int i = 0; i < num_pex_timers; ++i) { - m_last_pex[i]= min_time(); + m_last_pex[i] = min_time(); } } - char const* type() const override { return "ut_pex"; } - void add_handshake(entry& h) override { entry& messages = h["m"]; @@ -302,10 +294,10 @@ namespace libtorrent { namespace return true; } - int const num_pex_timers = sizeof(m_last_pex)/sizeof(m_last_pex[0]); - for (int i = 0; i < num_pex_timers-1; ++i) - m_last_pex[i] = m_last_pex[i+1]; - m_last_pex[num_pex_timers-1] = now; + int const num_pex_timers = sizeof(m_last_pex) / sizeof(m_last_pex[0]); + for (int i = 0; i < num_pex_timers - 1; ++i) + m_last_pex[i] = m_last_pex[i + 1]; + m_last_pex[num_pex_timers - 1] = now; bdecode_node pex_msg; error_code ec; @@ -321,7 +313,7 @@ namespace libtorrent { namespace #ifndef TORRENT_DISABLE_LOGGING int num_dropped = 0; int num_added = 0; - if (p) num_dropped += p.string_length()/6; + if (p) num_dropped += p.string_length() / 6; #endif if (p) { @@ -626,20 +618,7 @@ namespace libtorrent { namespace torrent& m_torrent; peer_connection& m_pc; ut_pex_plugin& m_tp; - // stores all peers this peer is connected to. These lists - // are updated with each pex message and are limited in size - // to protect against malicious clients. These lists are also - // used for looking up which peer a peer that supports holepunch - // came from. - // these are vectors to save memory and keep the items close - // together for performance. Inserting and removing is relatively - // cheap since the lists' size is limited - typedef std::vector> peers4_t; - peers4_t m_peers; -#if TORRENT_USE_IPV6 - typedef std::vector> peers6_t; - peers6_t m_peers6; -#endif + // the last pex messages we received // [0] is the oldest one. There is a problem with // rate limited connections, because we may sit @@ -667,8 +646,10 @@ namespace libtorrent { namespace if (pc.type() != peer_connection::bittorrent_connection) return boost::shared_ptr(); - return boost::shared_ptr(new ut_pex_peer_plugin(m_torrent - , *pc.native_handle(), *this)); + bt_peer_connection* c = static_cast(pc.native_handle().get()); + auto p = boost::make_shared(m_torrent, *c, *this); + c->set_ut_pex(p); + return p; } } } @@ -682,30 +663,7 @@ namespace libtorrent { return boost::shared_ptr(); } - return boost::shared_ptr(new ut_pex_plugin(*t)); - } - - bool was_introduced_by(peer_plugin const* pp, tcp::endpoint const& ep) - { - ut_pex_peer_plugin const* p = static_cast(pp); -#if TORRENT_USE_IPV6 - if (ep.address().is_v4()) - { -#endif - ut_pex_peer_plugin::peers4_t::value_type v(ep.address().to_v4().to_bytes(), ep.port()); - ut_pex_peer_plugin::peers4_t::const_iterator i - = std::lower_bound(p->m_peers.begin(), p->m_peers.end(), v); - return i != p->m_peers.end() && *i == v; -#if TORRENT_USE_IPV6 - } - else - { - ut_pex_peer_plugin::peers6_t::value_type v(ep.address().to_v6().to_bytes(), ep.port()); - ut_pex_peer_plugin::peers6_t::const_iterator i - = std::lower_bound(p->m_peers6.begin(), p->m_peers6.end(), v); - return i != p->m_peers6.end() && *i == v; - } -#endif + return boost::make_shared(*t); } }