peer_plugin refactor (#1002)

refactor of ut_pex peer storage
This commit is contained in:
Alden Torres 2016-08-15 21:05:39 -04:00 committed by Arvid Norberg
parent f51e782905
commit 9b0bc2ed5f
15 changed files with 70 additions and 104 deletions

View File

@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_ADDRESS_HPP_INCLUDED
#define TORRENT_ADDRESS_HPP_INCLUDED
#include <boost/version.hpp>
#include "libtorrent/config.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
@ -77,4 +76,3 @@ namespace libtorrent
}
#endif

View File

@ -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<torrent_plugin> new_torrent(torrent_handle const& t, void* user) override
{ return m_f(t, user); }

View File

@ -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<std::pair<address_v4::bytes_type, std::uint16_t>>;
peers4_t m_peers;
#if TORRENT_USE_IPV6
using peers6_t = std::vector<std::pair<address_v6::bytes_type, std::uint16_t>>;
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_peer_store> 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<ut_pex_peer_store> m_ut_pex;
char m_reserved_bits[8];
#endif

View File

@ -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&) {}

View File

@ -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<torrent_plugin> create_ut_pex_plugin(torrent_handle const&, void*);
bool was_introduced_by(peer_plugin const* pp, tcp::endpoint const& ep);
}
#endif // TORRENT_DISABLE_EXTENSIONS

View File

@ -331,7 +331,6 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_EXTENSIONS
void add_extension(boost::shared_ptr<peer_plugin>);
peer_plugin const* find_plugin(char const* type);
#endif
// this function is called once the torrent associated

View File

@ -60,7 +60,6 @@ struct TORRENT_EXPORT peer_connection_handle
int type() const;
void add_extension(boost::shared_ptr<peer_plugin>);
peer_plugin const* find_plugin(char const* type);
bool is_seed() const;

View File

@ -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)

View File

@ -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()

View File

@ -59,18 +59,6 @@ void peer_connection_handle::add_extension(boost::shared_ptr<peer_plugin> ext)
#endif
}
peer_plugin const* peer_connection_handle::find_plugin(char const* type)
{
#ifndef TORRENT_DISABLE_EXTENSIONS
boost::shared_ptr<peer_connection> 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<peer_connection> pc = native_handle();

View File

@ -332,9 +332,9 @@ namespace libtorrent
if (empty) return {};
using wrapper = session_impl::session_plugin_wrapper;
return {
boost::make_shared<wrapper>(wrapper(create_ut_pex_plugin)),
boost::make_shared<wrapper>(wrapper(create_ut_metadata_plugin)),
boost::make_shared<wrapper>(wrapper(create_smart_ban_plugin))
boost::make_shared<wrapper>(create_ut_pex_plugin),
boost::make_shared<wrapper>(create_ut_metadata_plugin),
boost::make_shared<wrapper>(create_smart_ban_plugin)
};
#else
TORRENT_UNUSED(empty);

View File

@ -776,8 +776,7 @@ namespace aux {
TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(ext);
add_ses_extension(boost::make_shared<session_plugin_wrapper>(
session_plugin_wrapper(ext)));
add_ses_extension(boost::make_shared<session_plugin_wrapper>(ext));
}
void session_impl::add_ses_extension(boost::shared_ptr<plugin> ext)

View File

@ -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<bt_peer_connection*>(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);

View File

@ -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
{

View File

@ -53,12 +53,6 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_DISABLE_EXTENSIONS
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/shared_ptr.hpp>
#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)
@ -259,8 +253,6 @@ namespace libtorrent { namespace
}
}
char const* type() const override { return "ut_pex"; }
void add_handshake(entry& h) override
{
entry& messages = h["m"];
@ -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<std::pair<address_v4::bytes_type, std::uint16_t>> peers4_t;
peers4_t m_peers;
#if TORRENT_USE_IPV6
typedef std::vector<std::pair<address_v6::bytes_type, std::uint16_t>> 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<peer_plugin>();
return boost::shared_ptr<peer_plugin>(new ut_pex_peer_plugin(m_torrent
, *pc.native_handle(), *this));
bt_peer_connection* c = static_cast<bt_peer_connection*>(pc.native_handle().get());
auto p = boost::make_shared<ut_pex_peer_plugin>(m_torrent, *c, *this);
c->set_ut_pex(p);
return p;
}
} }
@ -682,30 +663,7 @@ namespace libtorrent
{
return boost::shared_ptr<torrent_plugin>();
}
return boost::shared_ptr<torrent_plugin>(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<ut_pex_peer_plugin const*>(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<ut_pex_plugin>(*t);
}
}