convert m_listen_sockets to a vector of shared_ptr

This commit is contained in:
Steven Siloti 2017-07-12 19:37:50 -07:00
parent 0f30815d8f
commit e0dcd34352
3 changed files with 153 additions and 160 deletions

View File

@ -133,9 +133,9 @@ namespace aux {
TORRENT_EXTRA_EXPORT entry save_dht_settings(dht_settings const& settings); TORRENT_EXTRA_EXPORT entry save_dht_settings(dht_settings const& settings);
#endif #endif
struct listen_socket_impl struct listen_socket_t final : aux::session_listen_socket
{ {
listen_socket_impl() listen_socket_t()
{ {
tcp_port_mapping[0] = -1; tcp_port_mapping[0] = -1;
tcp_port_mapping[1] = -1; tcp_port_mapping[1] = -1;
@ -143,6 +143,20 @@ namespace aux {
udp_port_mapping[1] = -1; udp_port_mapping[1] = -1;
} }
listen_socket_t(listen_socket_t const&) = delete;
listen_socket_t(listen_socket_t&&) = delete;
listen_socket_t& operator=(listen_socket_t const&) = delete;
listen_socket_t& operator=(listen_socket_t&&) = delete;
address get_external_address() override
{ return external_address.external_address(); }
tcp::endpoint get_local_endpoint() override
{ return local_endpoint; }
bool is_ssl() override
{ return ssl == transport::ssl; }
// this may be empty but can be set // this may be empty but can be set
// to the WAN IP address of a NAT router // to the WAN IP address of a NAT router
ip_voter external_address; ip_voter external_address;
@ -185,27 +199,6 @@ namespace aux {
std::shared_ptr<aux::session_udp_socket> udp_sock; std::shared_ptr<aux::session_udp_socket> udp_sock;
}; };
struct listen_socket_t final : listen_socket_impl, aux::session_listen_socket
{
listen_socket_t(listen_socket_t const&) = delete;
listen_socket_t(listen_socket_t&&) = delete;
listen_socket_t& operator=(listen_socket_t const&) = delete;
listen_socket_t& operator=(listen_socket_t&&) = delete;
address get_external_address() override
{ return external_address.external_address(); }
tcp::endpoint get_local_endpoint() override
{ return local_endpoint; }
bool is_ssl() override
{ return ssl == transport::ssl; }
listen_socket_t(listen_socket_impl const& i) // NOLINT
: listen_socket_impl(i)
{}
};
struct TORRENT_EXTRA_EXPORT listen_endpoint_t struct TORRENT_EXTRA_EXPORT listen_endpoint_t
{ {
listen_endpoint_t(address adr, int p, std::string dev, transport s) listen_endpoint_t(address adr, int p, std::string dev, transport s)
@ -226,10 +219,10 @@ namespace aux {
// all matched sockets are ordered before unmatched sockets // all matched sockets are ordered before unmatched sockets
// matched endpoints are removed from the vector // matched endpoints are removed from the vector
// returns an iterator to the first unmatched socket // returns an iterator to the first unmatched socket
TORRENT_EXTRA_EXPORT std::list<listen_socket_t>::iterator TORRENT_EXTRA_EXPORT std::vector<std::shared_ptr<aux::listen_socket_t>>::iterator
partition_listen_sockets( partition_listen_sockets(
std::vector<listen_endpoint_t>& eps std::vector<listen_endpoint_t>& eps
, std::list<listen_socket_t>& sockets); , std::vector<std::shared_ptr<aux::listen_socket_t>>& sockets);
// expand [::] to all IPv6 interfaces for BEP 45 compliance // expand [::] to all IPv6 interfaces for BEP 45 compliance
TORRENT_EXTRA_EXPORT void expand_unspecified_address( TORRENT_EXTRA_EXPORT void expand_unspecified_address(
@ -595,7 +588,7 @@ namespace aux {
{ {
for (auto& s : m_listen_sockets) for (auto& s : m_listen_sockets)
{ {
f(&s); f(s.get());
} }
} }
@ -623,7 +616,7 @@ namespace aux {
{ {
for (auto const& s : m_listen_sockets) for (auto const& s : m_listen_sockets)
{ {
if (s.udp_sock) return s.udp_external_port; if (s->udp_sock) return s->udp_external_port;
} }
return -1; return -1;
} }
@ -948,7 +941,7 @@ namespace aux {
// since we might be listening on multiple interfaces // since we might be listening on multiple interfaces
// we might need more than one listen socket // we might need more than one listen socket
std::list<listen_socket_t> m_listen_sockets; std::vector<std::shared_ptr<listen_socket_t>> m_listen_sockets;
outgoing_sockets m_outgoing_sockets; outgoing_sockets m_outgoing_sockets;
@ -971,7 +964,7 @@ namespace aux {
open_ssl_socket = 0x10 open_ssl_socket = 0x10
}; };
listen_socket_impl setup_listener(std::string const& device std::shared_ptr<listen_socket_t> setup_listener(std::string const& device
, tcp::endpoint bind_ep, int flags, error_code& ec); , tcp::endpoint bind_ep, int flags, error_code& ec);
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT

View File

@ -260,20 +260,20 @@ namespace aux {
} }
#endif // TORRENT_DISABLE_DHT #endif // TORRENT_DISABLE_DHT
std::list<listen_socket_t>::iterator partition_listen_sockets( std::vector<std::shared_ptr<listen_socket_t>>::iterator partition_listen_sockets(
std::vector<listen_endpoint_t>& eps std::vector<listen_endpoint_t>& eps
, std::list<listen_socket_t>& sockets) , std::vector<std::shared_ptr<listen_socket_t>>& sockets)
{ {
return std::partition(sockets.begin(), sockets.end() return std::partition(sockets.begin(), sockets.end()
, [&eps](listen_socket_t const& sock) , [&eps](std::shared_ptr<listen_socket_t> const& sock)
{ {
auto match = std::find_if(eps.begin(), eps.end() auto match = std::find_if(eps.begin(), eps.end()
, [&sock](listen_endpoint_t const& ep) , [&sock](listen_endpoint_t const& ep)
{ {
return ep.ssl == sock.ssl return ep.ssl == sock->ssl
&& ep.port == sock.original_port && ep.port == sock->original_port
&& ep.device == sock.device && ep.device == sock->device
&& ep.addr == sock.local_endpoint.address(); && ep.addr == sock->local_endpoint.address();
}); });
if (match != eps.end()) if (match != eps.end())
@ -1009,17 +1009,17 @@ namespace aux {
// close the listen sockets // close the listen sockets
for (auto const& l : m_listen_sockets) for (auto const& l : m_listen_sockets)
{ {
if (l.sock) if (l->sock)
{ {
l.sock->close(ec); l->sock->close(ec);
TORRENT_ASSERT(!ec); TORRENT_ASSERT(!ec);
} }
// TODO: 3 closing the udp sockets here means that // TODO: 3 closing the udp sockets here means that
// the uTP connections cannot be closed gracefully // the uTP connections cannot be closed gracefully
if (l.udp_sock) if (l->udp_sock)
{ {
l.udp_sock->sock.close(); l->udp_sock->sock.close();
} }
} }
@ -1224,12 +1224,12 @@ namespace {
{ {
for (auto& ls : m_listen_sockets) for (auto& ls : m_listen_sockets)
{ {
req.listen_port = listen_port(&ls); req.listen_port = listen_port(ls.get());
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
// SSL torrents use the SSL listen port // SSL torrents use the SSL listen port
if (use_ssl) req.listen_port = ssl_listen_port(&ls); if (use_ssl) req.listen_port = ssl_listen_port(&ls);
#endif #endif
req.outgoing_socket = &ls; req.outgoing_socket = ls.get();
m_tracker_manager.queue_request(get_io_service(), req, c); m_tracker_manager.queue_request(get_io_service(), req, c);
} }
} }
@ -1451,8 +1451,8 @@ namespace {
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
for (auto const& i : m_listen_sockets) for (auto const& i : m_listen_sockets)
{ {
if (!i.local_endpoint.address().is_v6()) continue; if (!i->local_endpoint.address().is_v6()) continue;
return tcp::endpoint(i.local_endpoint.address(), std::uint16_t(i.tcp_external_port)); return tcp::endpoint(i->local_endpoint.address(), std::uint16_t(i->tcp_external_port));
} }
#endif #endif
return tcp::endpoint(); return tcp::endpoint();
@ -1462,15 +1462,15 @@ namespace {
{ {
for (auto const& i : m_listen_sockets) for (auto const& i : m_listen_sockets)
{ {
if (!i.local_endpoint.address().is_v4()) continue; if (!i->local_endpoint.address().is_v4()) continue;
return tcp::endpoint(i.local_endpoint.address(), std::uint16_t(i.tcp_external_port)); return tcp::endpoint(i->local_endpoint.address(), std::uint16_t(i->tcp_external_port));
} }
return tcp::endpoint(); return tcp::endpoint();
} }
enum { listen_no_system_port = 0x02 }; enum { listen_no_system_port = 0x02 };
listen_socket_impl session_impl::setup_listener(std::string const& device std::shared_ptr<listen_socket_t> session_impl::setup_listener(std::string const& device
, tcp::endpoint bind_ep, int flags, error_code& ec) , tcp::endpoint bind_ep, int flags, error_code& ec)
{ {
int retries = m_settings.get_int(settings_pack::max_retry_port_bind); int retries = m_settings.get_int(settings_pack::max_retry_port_bind);
@ -1483,9 +1483,9 @@ namespace {
} }
#endif #endif
listen_socket_impl ret; auto ret = std::make_shared<listen_socket_t>();
ret.ssl = (flags & open_ssl_socket) != 0 ? transport::ssl : transport::plaintext; ret->ssl = (flags & open_ssl_socket) != 0 ? transport::ssl : transport::plaintext;
ret.original_port = bind_ep.port(); ret->original_port = bind_ep.port();
operation_t last_op = operation_t::unknown; operation_t last_op = operation_t::unknown;
socket_type_t const sock_type socket_type_t const sock_type
= (flags & open_ssl_socket) = (flags & open_ssl_socket)
@ -1498,8 +1498,8 @@ namespace {
// separate function. At least most of it // separate function. At least most of it
if (!m_settings.get_bool(settings_pack::force_proxy)) if (!m_settings.get_bool(settings_pack::force_proxy))
{ {
ret.sock = std::make_shared<tcp::acceptor>(m_io_service); ret->sock = std::make_shared<tcp::acceptor>(m_io_service);
ret.sock->open(bind_ep.protocol(), ec); ret->sock->open(bind_ep.protocol(), ec);
last_op = operation_t::sock_open; last_op = operation_t::sock_open;
if (ec) if (ec)
{ {
@ -1521,7 +1521,7 @@ namespace {
{ {
// this is best-effort. ignore errors // this is best-effort. ignore errors
error_code err; error_code err;
ret.sock->set_option(exclusive_address_use(true), err); ret->sock->set_option(exclusive_address_use(true), err);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (err && should_log()) if (err && should_log())
{ {
@ -1535,7 +1535,7 @@ namespace {
{ {
// this is best-effort. ignore errors // this is best-effort. ignore errors
error_code err; error_code err;
ret.sock->set_option(tcp::acceptor::reuse_address(true), err); ret->sock->set_option(tcp::acceptor::reuse_address(true), err);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (err && should_log()) if (err && should_log())
{ {
@ -1550,7 +1550,7 @@ namespace {
if (bind_ep.address().is_v6()) if (bind_ep.address().is_v6())
{ {
error_code err; // ignore errors here error_code err; // ignore errors here
ret.sock->set_option(boost::asio::ip::v6_only(true), err); ret->sock->set_option(boost::asio::ip::v6_only(true), err);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (err && should_log()) if (err && should_log())
{ {
@ -1561,7 +1561,7 @@ namespace {
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
// enable Teredo on windows // enable Teredo on windows
ret.sock->set_option(v6_protection_level(PROTECTION_LEVEL_UNRESTRICTED), err); ret->sock->set_option(v6_protection_level(PROTECTION_LEVEL_UNRESTRICTED), err);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (err && should_log()) if (err && should_log())
{ {
@ -1578,7 +1578,7 @@ namespace {
// we have an actual device we're interested in listening on, if we // we have an actual device we're interested in listening on, if we
// have SO_BINDTODEVICE functionality, use it now. // have SO_BINDTODEVICE functionality, use it now.
#if TORRENT_HAS_BINDTODEVICE #if TORRENT_HAS_BINDTODEVICE
ret.sock->set_option(bind_to_device(device.c_str()), ec); ret->sock->set_option(bind_to_device(device.c_str()), ec);
if (ec) if (ec)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -1600,7 +1600,7 @@ namespace {
#endif #endif
} }
ret.sock->bind(bind_ep, ec); ret->sock->bind(bind_ep, ec);
last_op = operation_t::sock_bind; last_op = operation_t::sock_bind;
while (ec == error_code(error::address_in_use) && retries > 0) while (ec == error_code(error::address_in_use) && retries > 0)
@ -1620,7 +1620,7 @@ namespace {
ec.clear(); ec.clear();
--retries; --retries;
bind_ep.port(bind_ep.port() + 1); bind_ep.port(bind_ep.port() + 1);
ret.sock->bind(bind_ep, ec); ret->sock->bind(bind_ep, ec);
} }
if (ec == error_code(error::address_in_use) if (ec == error_code(error::address_in_use)
@ -1630,7 +1630,7 @@ namespace {
// instead of giving up, try let the OS pick a port // instead of giving up, try let the OS pick a port
bind_ep.port(0); bind_ep.port(0);
ec.clear(); ec.clear();
ret.sock->bind(bind_ep, ec); ret->sock->bind(bind_ep, ec);
last_op = operation_t::sock_bind; last_op = operation_t::sock_bind;
} }
@ -1653,11 +1653,11 @@ namespace {
m_alerts.emplace_alert<listen_failed_alert>(device, bind_ep m_alerts.emplace_alert<listen_failed_alert>(device, bind_ep
, last_op, ec, sock_type); , last_op, ec, sock_type);
} }
ret.sock.reset(); ret->sock.reset();
return ret; return ret;
} }
ret.local_endpoint = ret.sock->local_endpoint(ec); ret->local_endpoint = ret->sock->local_endpoint(ec);
ret.device = device; ret->device = device;
last_op = operation_t::getname; last_op = operation_t::getname;
if (ec) if (ec)
{ {
@ -1675,11 +1675,11 @@ namespace {
} }
return ret; return ret;
} }
ret.tcp_external_port = ret.local_endpoint.port(); ret->tcp_external_port = ret->local_endpoint.port();
TORRENT_ASSERT(ret.tcp_external_port == bind_ep.port() TORRENT_ASSERT(ret->tcp_external_port == bind_ep.port()
|| bind_ep.port() == 0); || bind_ep.port() == 0);
ret.sock->listen(m_settings.get_int(settings_pack::listen_queue_size), ec); ret->sock->listen(m_settings.get_int(settings_pack::listen_queue_size), ec);
last_op = operation_t::sock_listen; last_op = operation_t::sock_listen;
if (ec) if (ec)
@ -1706,8 +1706,8 @@ namespace {
: socket_type_t::udp; : socket_type_t::udp;
udp::endpoint const udp_bind_ep(bind_ep.address(), bind_ep.port()); udp::endpoint const udp_bind_ep(bind_ep.address(), bind_ep.port());
ret.udp_sock = std::make_shared<session_udp_socket>(m_io_service); ret->udp_sock = std::make_shared<session_udp_socket>(m_io_service);
ret.udp_sock->sock.open(udp_bind_ep.protocol(), ec); ret->udp_sock->sock.open(udp_bind_ep.protocol(), ec);
if (ec) if (ec)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -1729,7 +1729,7 @@ namespace {
#if TORRENT_HAS_BINDTODEVICE #if TORRENT_HAS_BINDTODEVICE
if (!device.empty()) if (!device.empty())
{ {
ret.udp_sock->sock.set_option(bind_to_device(device.c_str()), ec); ret->udp_sock->sock.set_option(bind_to_device(device.c_str()), ec);
if (ec) if (ec)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -1750,7 +1750,7 @@ namespace {
} }
} }
#endif #endif
ret.udp_sock->sock.bind(udp_bind_ep, ec); ret->udp_sock->sock.bind(udp_bind_ep, ec);
last_op = operation_t::sock_bind; last_op = operation_t::sock_bind;
if (ec) if (ec)
@ -1769,34 +1769,34 @@ namespace {
return ret; return ret;
} }
ret.udp_external_port = ret.udp_sock->sock.local_port(); ret->udp_external_port = ret->udp_sock->sock.local_port();
error_code err; error_code err;
set_socket_buffer_size(ret.udp_sock->sock, m_settings, err); set_socket_buffer_size(ret->udp_sock->sock, m_settings, err);
if (err) if (err)
{ {
if (m_alerts.should_post<udp_error_alert>()) if (m_alerts.should_post<udp_error_alert>())
m_alerts.emplace_alert<udp_error_alert>(ret.udp_sock->sock.local_endpoint(ec), err); m_alerts.emplace_alert<udp_error_alert>(ret->udp_sock->sock.local_endpoint(ec), err);
} }
ret.udp_sock->sock.set_force_proxy(m_settings.get_bool(settings_pack::force_proxy)); ret->udp_sock->sock.set_force_proxy(m_settings.get_bool(settings_pack::force_proxy));
// this call is necessary here because, unless the settings actually // this call is necessary here because, unless the settings actually
// change after the session is up and listening, at no other point // change after the session is up and listening, at no other point
// set_proxy_settings is called with the correct proxy configuration, // set_proxy_settings is called with the correct proxy configuration,
// internally, this method handle the SOCKS5's connection logic // internally, this method handle the SOCKS5's connection logic
ret.udp_sock->sock.set_proxy_settings(proxy()); ret->udp_sock->sock.set_proxy_settings(proxy());
// TODO: 2 use a handler allocator here // TODO: 2 use a handler allocator here
ADD_OUTSTANDING_ASYNC("session_impl::on_udp_packet"); ADD_OUTSTANDING_ASYNC("session_impl::on_udp_packet");
ret.udp_sock->sock.async_read(std::bind(&session_impl::on_udp_packet ret->udp_sock->sock.async_read(std::bind(&session_impl::on_udp_packet
, this, ret.udp_sock, ret.ssl, _1)); , this, ret->udp_sock, ret->ssl, _1));
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (should_log()) if (should_log())
{ {
session_log(" listening on: %s TCP port: %d UDP port: %d" session_log(" listening on: %s TCP port: %d UDP port: %d"
, bind_ep.address().to_string().c_str() , bind_ep.address().to_string().c_str()
, ret.tcp_external_port, ret.udp_external_port); , ret->tcp_external_port, ret->udp_external_port);
} }
#endif #endif
return ret; return ret;
@ -1954,19 +1954,19 @@ namespace {
{ {
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
if (m_dht) if (m_dht)
m_dht->delete_socket(&*remove_iter); m_dht->delete_socket(remove_iter->get());
#endif #endif
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (should_log()) if (should_log())
{ {
session_log("closing listen socket for %s on device \"%s\"" session_log("closing listen socket for %s on device \"%s\""
, print_endpoint(remove_iter->local_endpoint).c_str() , print_endpoint((*remove_iter)->local_endpoint).c_str()
, remove_iter->device.c_str()); , (*remove_iter)->device.c_str());
} }
#endif #endif
if (remove_iter->sock) remove_iter->sock->close(ec); if ((*remove_iter)->sock) (*remove_iter)->sock->close(ec);
if (remove_iter->udp_sock) remove_iter->udp_sock->sock.close(); if ((*remove_iter)->udp_sock) (*remove_iter)->udp_sock->sock.close();
remove_iter = m_listen_sockets.erase(remove_iter); remove_iter = m_listen_sockets.erase(remove_iter);
} }
@ -1974,17 +1974,17 @@ namespace {
// an existing socket // an existing socket
for (auto const& ep : eps) for (auto const& ep : eps)
{ {
listen_socket_impl const s = setup_listener(ep.device std::shared_ptr<listen_socket_t> s = setup_listener(ep.device
, tcp::endpoint(ep.addr, std::uint16_t(ep.port)) , tcp::endpoint(ep.addr, std::uint16_t(ep.port))
, flags | (ep.ssl == transport::ssl ? open_ssl_socket : 0), ec); , flags | (ep.ssl == transport::ssl ? open_ssl_socket : 0), ec);
if (!ec && (s.sock || s.udp_sock)) if (!ec && (s->sock || s->udp_sock))
{ {
m_listen_sockets.emplace_back(s); m_listen_sockets.emplace_back(s);
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
if (m_dht) if (m_dht)
m_dht->new_socket(&m_listen_sockets.back()); m_dht->new_socket(m_listen_sockets.back().get());
#endif #endif
} }
} }
@ -2005,13 +2005,13 @@ namespace {
for (auto const& l : m_listen_sockets) for (auto const& l : m_listen_sockets)
{ {
error_code err; error_code err;
if (l.sock) if (l->sock)
{ {
tcp::endpoint const tcp_ep = l.sock->local_endpoint(err); tcp::endpoint const tcp_ep = l->sock->local_endpoint(err);
if (!err) if (!err)
{ {
socket_type_t const socket_type socket_type_t const socket_type
= l.ssl == transport::ssl = l->ssl == transport::ssl
? socket_type_t::tcp_ssl ? socket_type_t::tcp_ssl
: socket_type_t::tcp; : socket_type_t::tcp;
@ -2020,13 +2020,13 @@ namespace {
} }
} }
if (l.udp_sock) if (l->udp_sock)
{ {
udp::endpoint const udp_ep = l.udp_sock->sock.local_endpoint(err); udp::endpoint const udp_ep = l->udp_sock->sock.local_endpoint(err);
if (!err && l.udp_sock->sock.is_open()) if (!err && l->udp_sock->sock.is_open())
{ {
socket_type_t const socket_type socket_type_t const socket_type
= l.ssl == transport::ssl = l->ssl == transport::ssl
? socket_type_t::utp_ssl ? socket_type_t::utp_ssl
: socket_type_t::udp; : socket_type_t::udp;
@ -2047,8 +2047,8 @@ namespace {
// initiate accepting on the listen sockets // initiate accepting on the listen sockets
for (auto& s : m_listen_sockets) for (auto& s : m_listen_sockets)
{ {
if (s.sock) async_accept(s.sock, s.ssl); if (s->sock) async_accept(s->sock, s->ssl);
remap_ports(remap_natpmp_and_upnp, s); remap_ports(remap_natpmp_and_upnp, *s);
} }
#if TORRENT_USE_I2P #if TORRENT_USE_I2P
@ -2393,15 +2393,15 @@ namespace {
s->write_blocked = false; s->write_blocked = false;
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
std::list<listen_socket_t>::iterator i = std::find_if( auto i = std::find_if(
m_listen_sockets.begin(), m_listen_sockets.end() m_listen_sockets.begin(), m_listen_sockets.end()
, [&s] (listen_socket_t const& ls) { return ls.udp_sock == s; }); , [&s] (std::shared_ptr<listen_socket_t> const& ls) { return ls->udp_sock == s; });
#endif #endif
// notify the utp socket manager it can start sending on the socket again // notify the utp socket manager it can start sending on the socket again
struct utp_socket_manager& mgr = struct utp_socket_manager& mgr =
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
(i != m_listen_sockets.end() && i->ssl == transport::ssl) ? m_ssl_utp_socket_manager : (i != m_listen_sockets.end() && (*i)->ssl == transport::ssl) ? m_ssl_utp_socket_manager :
#endif #endif
m_utp_socket_manager; m_utp_socket_manager;
@ -5298,7 +5298,7 @@ namespace {
void session_impl::update_proxy() void session_impl::update_proxy()
{ {
for (auto& i : m_listen_sockets) for (auto& i : m_listen_sockets)
i.udp_sock->sock.set_proxy_settings(proxy()); i->udp_sock->sock.set_proxy_settings(proxy());
m_outgoing_sockets.update_proxy(proxy()); m_outgoing_sockets.update_proxy(proxy());
} }
@ -5384,7 +5384,7 @@ namespace {
if (m_settings.get_bool(settings_pack::force_proxy)) return 0; if (m_settings.get_bool(settings_pack::force_proxy)) return 0;
if (m_listen_sockets.empty()) return 0; if (m_listen_sockets.empty()) return 0;
if (sock) return std::uint16_t(sock->tcp_external_port); if (sock) return std::uint16_t(sock->tcp_external_port);
return std::uint16_t(m_listen_sockets.front().tcp_external_port); return std::uint16_t(m_listen_sockets.front()->tcp_external_port);
} }
// TODO: 2 this function should be removed and users need to deal with the // TODO: 2 this function should be removed and users need to deal with the
@ -5405,7 +5405,7 @@ namespace {
if (m_settings.get_bool(settings_pack::force_proxy)) return 0; if (m_settings.get_bool(settings_pack::force_proxy)) return 0;
for (auto const& s : m_listen_sockets) for (auto const& s : m_listen_sockets)
{ {
if (s.ssl == transport::ssl) return std::uint16_t(s.tcp_external_port); if (s->ssl == transport::ssl) return std::uint16_t(s->tcp_external_port);
} }
#else #else
TORRENT_UNUSED(sock); TORRENT_UNUSED(sock);
@ -5445,14 +5445,14 @@ namespace {
} }
namespace { namespace {
bool find_tcp_port_mapping(int transport, int mapping, listen_socket_t const& ls) bool find_tcp_port_mapping(int transport, int mapping, std::shared_ptr<listen_socket_t> const& ls)
{ {
return ls.tcp_port_mapping[transport] == mapping; return ls->tcp_port_mapping[transport] == mapping;
} }
bool find_udp_port_mapping(int transport, int mapping, listen_socket_t const& ls) bool find_udp_port_mapping(int transport, int mapping, std::shared_ptr<listen_socket_t> const& ls)
{ {
return ls.udp_port_mapping[transport] == mapping; return ls->udp_port_mapping[transport] == mapping;
} }
} }
@ -5476,7 +5476,7 @@ namespace {
// look through our listen sockets to see if this mapping is for one of // look through our listen sockets to see if this mapping is for one of
// them (it could also be a user mapping) // them (it could also be a user mapping)
std::list<listen_socket_t>::iterator ls auto ls
= std::find_if(m_listen_sockets.begin(), m_listen_sockets.end() = std::find_if(m_listen_sockets.begin(), m_listen_sockets.end()
, std::bind(find_tcp_port_mapping, map_transport, mapping, _1)); , std::bind(find_tcp_port_mapping, map_transport, mapping, _1));
@ -5494,11 +5494,11 @@ namespace {
{ {
// TODO: 1 report the proper address of the router as the source IP of // TODO: 1 report the proper address of the router as the source IP of
// this vote of our external address, instead of the empty address // this vote of our external address, instead of the empty address
ls->external_address.cast_vote(ip, source_router, address()); (*ls)->external_address.cast_vote(ip, source_router, address());
} }
if (tcp) ls->tcp_external_port = port; if (tcp) (*ls)->tcp_external_port = port;
else ls->udp_external_port = port; else (*ls)->udp_external_port = port;
} }
if (!ec && m_alerts.should_post<portmap_alert>()) if (!ec && m_alerts.should_post<portmap_alert>())
@ -5690,7 +5690,7 @@ namespace {
, std::move(m_dht_state)); , std::move(m_dht_state));
for (auto& s : m_listen_sockets) for (auto& s : m_listen_sockets)
m_dht->new_socket(&s); m_dht->new_socket(s.get());
for (auto const& n : m_dht_router_nodes) for (auto const& n : m_dht_router_nodes)
{ {
@ -6124,32 +6124,32 @@ namespace {
int const tos = m_settings.get_int(settings_pack::peer_tos); int const tos = m_settings.get_int(settings_pack::peer_tos);
for (auto const& l : m_listen_sockets) for (auto const& l : m_listen_sockets)
{ {
if (l.sock) if (l->sock)
{ {
error_code ec; error_code ec;
set_tos(*l.sock, tos, ec); set_tos(*l->sock, tos, ec);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (should_log()) if (should_log())
{ {
session_log(">>> SET_TOS [ tcp (%s %d) tos: %x e: %s ]" session_log(">>> SET_TOS [ tcp (%s %d) tos: %x e: %s ]"
, l.sock->local_endpoint().address().to_string().c_str() , l->sock->local_endpoint().address().to_string().c_str()
, l.sock->local_endpoint().port(), tos, ec.message().c_str()); , l->sock->local_endpoint().port(), tos, ec.message().c_str());
} }
#endif #endif
} }
if (l.udp_sock) if (l->udp_sock)
{ {
error_code ec; error_code ec;
set_tos(l.udp_sock->sock, tos, ec); set_tos(l->udp_sock->sock, tos, ec);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (should_log()) if (should_log())
{ {
session_log(">>> SET_TOS [ udp (%s %d) tos: %x e: %s ]" session_log(">>> SET_TOS [ udp (%s %d) tos: %x e: %s ]"
, l.udp_sock->sock.local_endpoint().address().to_string().c_str() , l->udp_sock->sock.local_endpoint().address().to_string().c_str()
, l.udp_sock->sock.local_port() , l->udp_sock->sock.local_port()
, tos, ec.message().c_str()); , tos, ec.message().c_str());
} }
#endif #endif
@ -6297,25 +6297,25 @@ namespace {
for (auto const& l : m_listen_sockets) for (auto const& l : m_listen_sockets)
{ {
error_code ec; error_code ec;
set_socket_buffer_size(l.udp_sock->sock, m_settings, ec); set_socket_buffer_size(l->udp_sock->sock, m_settings, ec);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (ec && should_log()) if (ec && should_log())
{ {
error_code err; error_code err;
session_log("socket buffer size [ udp %s %d]: (%d) %s" session_log("socket buffer size [ udp %s %d]: (%d) %s"
, l.udp_sock->sock.local_endpoint().address().to_string(err).c_str() , l->udp_sock->sock.local_endpoint().address().to_string(err).c_str()
, l.udp_sock->sock.local_port(), ec.value(), ec.message().c_str()); , l->udp_sock->sock.local_port(), ec.value(), ec.message().c_str());
} }
#endif #endif
ec.clear(); ec.clear();
set_socket_buffer_size(*l.sock, m_settings, ec); set_socket_buffer_size(*l->sock, m_settings, ec);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (ec && should_log()) if (ec && should_log())
{ {
error_code err; error_code err;
session_log("socket buffer size [ udp %s %d]: (%d) %s" session_log("socket buffer size [ udp %s %d]: (%d) %s"
, l.sock->local_endpoint().address().to_string(err).c_str() , l->sock->local_endpoint().address().to_string(err).c_str()
, l.sock->local_endpoint().port(), ec.value(), ec.message().c_str()); , l->sock->local_endpoint().port(), ec.value(), ec.message().c_str());
} }
#endif #endif
} }
@ -6369,14 +6369,14 @@ namespace {
{ {
for (auto& i : m_listen_sockets) for (auto& i : m_listen_sockets)
{ {
i.udp_sock->sock.set_force_proxy(m_settings.get_bool(settings_pack::force_proxy)); i->udp_sock->sock.set_force_proxy(m_settings.get_bool(settings_pack::force_proxy));
// close the TCP listen sockets // close the TCP listen sockets
if (i.sock) if (i->sock)
{ {
error_code ec; error_code ec;
i.sock->close(ec); i->sock->close(ec);
i.sock.reset(); i->sock.reset();
} }
} }
@ -6606,7 +6606,7 @@ namespace {
for (auto& s : m_listen_sockets) for (auto& s : m_listen_sockets)
{ {
remap_ports(remap_natpmp, s); remap_ports(remap_natpmp, *s);
} }
return m_natpmp.get(); return m_natpmp.get();
} }
@ -6629,7 +6629,7 @@ namespace {
for (auto& s : m_listen_sockets) for (auto& s : m_listen_sockets)
{ {
remap_ports(remap_upnp, s); remap_ports(remap_upnp, *s);
} }
return m_upnp.get(); return m_upnp.get();
} }
@ -6665,8 +6665,8 @@ namespace {
m_natpmp->close(); m_natpmp->close();
for (auto& s : m_listen_sockets) for (auto& s : m_listen_sockets)
{ {
s.tcp_port_mapping[0] = -1; s->tcp_port_mapping[0] = -1;
s.udp_port_mapping[0] = -1; s->udp_port_mapping[0] = -1;
} }
m_natpmp.reset(); m_natpmp.reset();
@ -6679,8 +6679,8 @@ namespace {
m_upnp->close(); m_upnp->close();
for (auto& s : m_listen_sockets) for (auto& s : m_listen_sockets)
{ {
s.tcp_port_mapping[1] = -1; s->tcp_port_mapping[1] = -1;
s.udp_port_mapping[1] = -1; s->udp_port_mapping[1] = -1;
} }
m_upnp.reset(); m_upnp.reset();
} }
@ -6692,10 +6692,10 @@ namespace {
// take the first IP we find which matches each category // take the first IP we find which matches each category
for (auto const& i : m_listen_sockets) for (auto const& i : m_listen_sockets)
{ {
address external_addr = i.external_address.external_address(); address external_addr = i->external_address.external_address();
if (ips[0][external_addr.is_v6()] == address()) if (ips[0][external_addr.is_v6()] == address())
ips[0][external_addr.is_v6()] = external_addr; ips[0][external_addr.is_v6()] = external_addr;
address local_addr = i.local_endpoint.address(); address local_addr = i->local_endpoint.address();
if (ips[is_local(local_addr)][local_addr.is_v6()] == address()) if (ips[is_local(local_addr)][local_addr.is_v6()] == address())
ips[is_local(local_addr)][local_addr.is_v6()] = local_addr; ips[is_local(local_addr)][local_addr.is_v6()] = local_addr;
} }
@ -6812,10 +6812,10 @@ namespace {
// TODO: remove this function once all callers are updated to specify a listen socket // TODO: remove this function once all callers are updated to specify a listen socket
for (auto& i : m_listen_sockets) for (auto& i : m_listen_sockets)
{ {
if (i.local_endpoint.address().is_v4() != ip.is_v4()) if (i->local_endpoint.address().is_v4() != ip.is_v4())
continue; continue;
set_external_address(i, ip, source_type, source); set_external_address(*i, ip, source_type, source);
break; break;
} }
} }
@ -6825,10 +6825,10 @@ namespace {
, int const source_type, address const& source) , int const source_type, address const& source)
{ {
auto sock = std::find_if(m_listen_sockets.begin(), m_listen_sockets.end() auto sock = std::find_if(m_listen_sockets.begin(), m_listen_sockets.end()
, [&](listen_socket_t const& v) { return v.local_endpoint == local_endpoint; }); , [&](std::shared_ptr<listen_socket_t> const& v) { return v->local_endpoint == local_endpoint; });
if (sock != m_listen_sockets.end()) if (sock != m_listen_sockets.end())
set_external_address(*sock, ip, source_type, source); set_external_address(**sock, ip, source_type, source);
} }
void session_impl::set_external_address(listen_socket_t& sock void session_impl::set_external_address(listen_socket_t& sock

View File

@ -79,27 +79,27 @@ namespace
return aux::listen_endpoint_t(address::from_string(ip), port, device, ssl); return aux::listen_endpoint_t(address::from_string(ip), port, device, ssl);
} }
aux::listen_socket_impl sock(char const* ip, int const port std::shared_ptr<aux::listen_socket_t> sock(char const* ip, int const port
, int const original_port, char const* device = "") , int const original_port, char const* device = "")
{ {
aux::listen_socket_impl s; auto s = std::make_shared<aux::listen_socket_t>();
s.local_endpoint = tcp::endpoint(address::from_string(ip), port); s->local_endpoint = tcp::endpoint(address::from_string(ip), port);
s.original_port = original_port; s->original_port = original_port;
s.device = device; s->device = device;
return s; return s;
} }
aux::listen_socket_impl sock(char const* ip, int const port, char const* dev) std::shared_ptr<aux::listen_socket_t> sock(char const* ip, int const port, char const* dev)
{ return sock(ip, port, port, dev); } { return sock(ip, port, port, dev); }
aux::listen_socket_impl sock(char const* ip, int const port) std::shared_ptr<aux::listen_socket_t> sock(char const* ip, int const port)
{ return sock(ip, port, port); } { return sock(ip, port, port); }
} // anonymous namespace } // anonymous namespace
TORRENT_TEST(partition_listen_sockets_wildcard2specific) TORRENT_TEST(partition_listen_sockets_wildcard2specific)
{ {
std::list<aux::listen_socket_t> sockets = { std::vector<std::shared_ptr<aux::listen_socket_t>> sockets = {
sock("0.0.0.0", 6881), sock("4.4.4.4", 6881) sock("0.0.0.0", 6881), sock("4.4.4.4", 6881)
}; };
@ -112,14 +112,14 @@ TORRENT_TEST(partition_listen_sockets_wildcard2specific)
TEST_EQUAL(eps.size(), 1); TEST_EQUAL(eps.size(), 1);
TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 1); TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 1);
TEST_EQUAL(std::distance(remove_iter, sockets.end()), 1); TEST_EQUAL(std::distance(remove_iter, sockets.end()), 1);
test_equal(sockets.front(), address_v4::from_string("4.4.4.4"), 6881, "", tp::plaintext); test_equal(*sockets.front(), address_v4::from_string("4.4.4.4"), 6881, "", tp::plaintext);
test_equal(sockets.back(), address_v4(), 6881, "", tp::plaintext); test_equal(*sockets.back(), address_v4(), 6881, "", tp::plaintext);
test_equal(eps.front(), address_v4::from_string("4.4.4.5"), 6881, "", tp::plaintext); test_equal(eps.front(), address_v4::from_string("4.4.4.5"), 6881, "", tp::plaintext);
} }
TORRENT_TEST(partition_listen_sockets_port_change) TORRENT_TEST(partition_listen_sockets_port_change)
{ {
std::list<aux::listen_socket_t> sockets = { std::vector<std::shared_ptr<aux::listen_socket_t>> sockets = {
sock("4.4.4.4", 6881), sock("4.4.4.5", 6881) sock("4.4.4.4", 6881), sock("4.4.4.5", 6881)
}; };
@ -134,7 +134,7 @@ TORRENT_TEST(partition_listen_sockets_port_change)
TORRENT_TEST(partition_listen_sockets_device_bound) TORRENT_TEST(partition_listen_sockets_device_bound)
{ {
std::list<aux::listen_socket_t> sockets = { std::vector<std::shared_ptr<aux::listen_socket_t>> sockets = {
sock("4.4.4.5", 6881), sock("0.0.0.0", 6881) sock("4.4.4.5", 6881), sock("0.0.0.0", 6881)
}; };
@ -148,14 +148,14 @@ TORRENT_TEST(partition_listen_sockets_device_bound)
auto remove_iter = aux::partition_listen_sockets(eps, sockets); auto remove_iter = aux::partition_listen_sockets(eps, sockets);
TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 1); TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 1);
TEST_EQUAL(std::distance(remove_iter, sockets.end()), 1); TEST_EQUAL(std::distance(remove_iter, sockets.end()), 1);
test_equal(sockets.front(), address_v4::from_string("4.4.4.5"), 6881, "", tp::plaintext); test_equal(*sockets.front(), address_v4::from_string("4.4.4.5"), 6881, "", tp::plaintext);
test_equal(sockets.back(), address_v4(), 6881, "", tp::plaintext); test_equal(*sockets.back(), address_v4(), 6881, "", tp::plaintext);
TEST_EQUAL(eps.size(), 2); TEST_EQUAL(eps.size(), 2);
} }
TORRENT_TEST(partition_listen_sockets_device_ip_change) TORRENT_TEST(partition_listen_sockets_device_ip_change)
{ {
std::list<aux::listen_socket_t> sockets = { std::vector<std::shared_ptr<aux::listen_socket_t>> sockets = {
sock("10.10.10.10", 6881, "enp3s0") sock("10.10.10.10", 6881, "enp3s0")
, sock("4.4.4.4", 6881, "enp3s0") , sock("4.4.4.4", 6881, "enp3s0")
}; };
@ -168,15 +168,15 @@ TORRENT_TEST(partition_listen_sockets_device_ip_change)
auto remove_iter = aux::partition_listen_sockets(eps, sockets); auto remove_iter = aux::partition_listen_sockets(eps, sockets);
TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 1); TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 1);
TEST_EQUAL(std::distance(remove_iter, sockets.end()), 1); TEST_EQUAL(std::distance(remove_iter, sockets.end()), 1);
test_equal(sockets.front(), address_v4::from_string("10.10.10.10"), 6881, "enp3s0", tp::plaintext); test_equal(*sockets.front(), address_v4::from_string("10.10.10.10"), 6881, "enp3s0", tp::plaintext);
test_equal(sockets.back(), address_v4::from_string("4.4.4.4"), 6881, "enp3s0", tp::plaintext); test_equal(*sockets.back(), address_v4::from_string("4.4.4.4"), 6881, "enp3s0", tp::plaintext);
TEST_EQUAL(eps.size(), 1); TEST_EQUAL(eps.size(), 1);
test_equal(eps.front(), address_v4::from_string("4.4.4.5"), 6881, "enp3s0", tp::plaintext); test_equal(eps.front(), address_v4::from_string("4.4.4.5"), 6881, "enp3s0", tp::plaintext);
} }
TORRENT_TEST(partition_listen_sockets_original_port) TORRENT_TEST(partition_listen_sockets_original_port)
{ {
std::list<aux::listen_socket_t> sockets = { std::vector<std::shared_ptr<aux::listen_socket_t>> sockets = {
sock("10.10.10.10", 6883, 6881), sock("4.4.4.4", 6883, 6881) sock("10.10.10.10", 6883, 6881), sock("4.4.4.4", 6883, 6881)
}; };
@ -193,7 +193,7 @@ TORRENT_TEST(partition_listen_sockets_original_port)
TORRENT_TEST(partition_listen_sockets_ssl) TORRENT_TEST(partition_listen_sockets_ssl)
{ {
std::list<aux::listen_socket_t> sockets = { std::vector<std::shared_ptr<aux::listen_socket_t>> sockets = {
sock("10.10.10.10", 6881), sock("4.4.4.4", 6881) sock("10.10.10.10", 6881), sock("4.4.4.4", 6881)
}; };
@ -212,7 +212,7 @@ TORRENT_TEST(partition_listen_sockets_ssl)
TORRENT_TEST(partition_listen_sockets_op_ports) TORRENT_TEST(partition_listen_sockets_op_ports)
{ {
std::list<aux::listen_socket_t> sockets = { std::vector<std::shared_ptr<aux::listen_socket_t>> sockets = {
sock("10.10.10.10", 6881, 0), sock("4.4.4.4", 6881) sock("10.10.10.10", 6881, 0), sock("4.4.4.4", 6881)
}; };