use enum class for ssl bool in listen socket (#1960)

use enum class for ssl bool in listen socket. use std::array instead of C array. make test_listen_socket a bit more compact
This commit is contained in:
Arvid Norberg 2017-04-30 21:11:42 -04:00 committed by GitHub
parent 749d0da875
commit 1af17be046
5 changed files with 189 additions and 181 deletions

View File

@ -116,7 +116,22 @@ namespace dht {
struct dht_tracker; struct dht_tracker;
class item; class item;
}
}
namespace aux {
struct session_impl;
struct session_settings;
#ifndef TORRENT_DISABLE_LOGGING
struct tracker_logger;
#endif
#ifndef TORRENT_DISABLE_DHT
TORRENT_EXTRA_EXPORT dht_settings read_dht_settings(bdecode_node const& e);
TORRENT_EXTRA_EXPORT entry save_dht_settings(dht_settings const& settings);
#endif
struct listen_socket_t final : aux::session_listen_socket struct listen_socket_t final : aux::session_listen_socket
{ {
@ -164,8 +179,8 @@ namespace dht {
int tcp_port_mapping[2]; int tcp_port_mapping[2];
int udp_port_mapping[2]; int udp_port_mapping[2];
// set to true if this is an SSL listen socket // indicates whether this is an SSL listen socket or not
bool ssl = false; transport ssl = transport::plaintext;
// the actual sockets (TCP listen socket and UDP socket) // the actual sockets (TCP listen socket and UDP socket)
// An entry does not necessarily have a UDP or TCP socket. One of these // An entry does not necessarily have a UDP or TCP socket. One of these
@ -176,23 +191,9 @@ namespace dht {
std::shared_ptr<aux::session_udp_socket> udp_sock; std::shared_ptr<aux::session_udp_socket> udp_sock;
}; };
namespace aux {
struct session_impl;
struct session_settings;
#ifndef TORRENT_DISABLE_LOGGING
struct tracker_logger;
#endif
#ifndef TORRENT_DISABLE_DHT
TORRENT_EXTRA_EXPORT dht_settings read_dht_settings(bdecode_node const& e);
TORRENT_EXTRA_EXPORT entry save_dht_settings(dht_settings const& settings);
#endif
struct TORRENT_EXTRA_EXPORT listen_endpoint_t struct TORRENT_EXTRA_EXPORT listen_endpoint_t
{ {
listen_endpoint_t(address adr, int p, std::string dev, bool s) listen_endpoint_t(address adr, int p, std::string dev, transport s)
: addr(adr), port(p), device(dev), ssl(s) {} : addr(adr), port(p), device(dev), ssl(s) {}
bool operator==(listen_endpoint_t const& o) const bool operator==(listen_endpoint_t const& o) const
@ -203,7 +204,7 @@ namespace aux {
address addr; address addr;
int port; int port;
std::string device; std::string device;
bool ssl; transport ssl;
}; };
// partitions sockets based on whether they match one of the given endpoints // partitions sockets based on whether they match one of the given endpoints
@ -315,9 +316,9 @@ namespace aux {
tcp::endpoint get_ipv6_interface() const override; tcp::endpoint get_ipv6_interface() const override;
tcp::endpoint get_ipv4_interface() const override; tcp::endpoint get_ipv4_interface() const override;
void async_accept(std::shared_ptr<tcp::acceptor> const& listener, bool ssl); void async_accept(std::shared_ptr<tcp::acceptor> const& listener, transport ssl);
void on_accept_connection(std::shared_ptr<socket_type> const& s void on_accept_connection(std::shared_ptr<socket_type> const& s
, std::weak_ptr<tcp::acceptor> listener, error_code const& e, bool ssl); , std::weak_ptr<tcp::acceptor> listener, error_code const& e, transport ssl);
void incoming_connection(std::shared_ptr<socket_type> const& s); void incoming_connection(std::shared_ptr<socket_type> const& s);
@ -1117,7 +1118,7 @@ namespace aux {
void on_udp_writeable(std::weak_ptr<session_udp_socket> s, error_code const& ec); void on_udp_writeable(std::weak_ptr<session_udp_socket> s, error_code const& ec);
void on_udp_packet(std::weak_ptr<session_udp_socket> s void on_udp_packet(std::weak_ptr<session_udp_socket> s
, bool ssl, error_code const& ec); , transport ssl, error_code const& ec);
libtorrent::utp_socket_manager m_utp_socket_manager; libtorrent::utp_socket_manager m_utp_socket_manager;

View File

@ -43,6 +43,8 @@ namespace libtorrent { namespace aux {
struct listen_endpoint_t; struct listen_endpoint_t;
struct proxy_settings; struct proxy_settings;
enum class transport : std::uint8_t { plaintext, ssl };
struct session_udp_socket : utp_socket_interface struct session_udp_socket : utp_socket_interface
{ {
explicit session_udp_socket(io_service& ios) explicit session_udp_socket(io_service& ios)
@ -61,7 +63,7 @@ namespace libtorrent { namespace aux {
struct outgoing_udp_socket final : session_udp_socket struct outgoing_udp_socket final : session_udp_socket
{ {
outgoing_udp_socket(io_service& ios, std::string const& dev, bool ssl_) outgoing_udp_socket(io_service& ios, std::string const& dev, transport ssl_)
: session_udp_socket(ios), device(dev), ssl(ssl_) {} : session_udp_socket(ios), device(dev), ssl(ssl_) {}
// the name of the device the socket is bound to, may be empty // the name of the device the socket is bound to, may be empty
@ -69,7 +71,7 @@ namespace libtorrent { namespace aux {
std::string const device; std::string const device;
// set to true if this is an SSL socket // set to true if this is an SSL socket
bool const ssl; transport const ssl;
}; };
// sockets used for outoing utp connections // sockets used for outoing utp connections
@ -93,7 +95,7 @@ namespace libtorrent { namespace aux {
private: private:
// round-robin index into sockets // round-robin index into sockets
// one dimention for IPv4/IPv6 and a second for SSL/non-SSL // one dimention for IPv4/IPv6 and a second for SSL/non-SSL
mutable std::uint8_t index[2][2] = { {0, 0}, {0, 0} }; mutable std::array<std::array<std::uint8_t, 2>, 2> index = {{ {{0, 0}}, {{0, 0}} }};
}; };
} } } }

View File

@ -1482,7 +1482,7 @@ namespace {
#endif #endif
listen_socket_t ret; listen_socket_t ret;
ret.ssl = (flags & open_ssl_socket) != 0; ret.ssl = (flags & open_ssl_socket) != 0 ? transport::ssl : transport::plaintext;
ret.original_port = bind_ep.port(); ret.original_port = bind_ep.port();
int last_op = 0; int last_op = 0;
socket_type_t const sock_type socket_type_t const sock_type
@ -1844,7 +1844,8 @@ namespace {
#if !TORRENT_USE_IPV6 #if !TORRENT_USE_IPV6
if (adr.is_v4()) if (adr.is_v4())
#endif #endif
eps.emplace_back(adr, port, std::string(), ssl); eps.emplace_back(adr, port, std::string()
, ssl ? transport::ssl : transport::plaintext);
} }
else else
{ {
@ -1877,7 +1878,8 @@ namespace {
// (which must be of the same family as the address we're // (which must be of the same family as the address we're
// connecting to) // connecting to)
if (device != ipface.name) continue; if (device != ipface.name) continue;
eps.emplace_back(ipface.interface_address, port, device, ssl); eps.emplace_back(ipface.interface_address, port, device
, ssl ? transport::ssl : transport::plaintext);
} }
} }
} }
@ -1971,7 +1973,7 @@ namespace {
{ {
listen_socket_t const s = setup_listener(ep.device listen_socket_t const 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 ? 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))
{ {
@ -2006,7 +2008,7 @@ namespace {
if (!err) if (!err)
{ {
socket_type_t const socket_type socket_type_t const socket_type
= l.ssl = l.ssl == transport::ssl
? socket_type_t::tcp_ssl ? socket_type_t::tcp_ssl
: socket_type_t::tcp; : socket_type_t::tcp;
@ -2021,7 +2023,7 @@ namespace {
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 = l.ssl == transport::ssl
? socket_type_t::utp_ssl ? socket_type_t::utp_ssl
: socket_type_t::udp; : socket_type_t::udp;
@ -2070,14 +2072,14 @@ namespace {
// any interface // any interface
if (eps.empty()) if (eps.empty())
{ {
eps.emplace_back(address_v4(), 0, "", false); eps.emplace_back(address_v4(), 0, "", transport::plaintext);
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
eps.emplace_back(address_v6(), 0, "", false); eps.emplace_back(address_v6(), 0, "", transport::plaintext);
#endif #endif
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
eps.emplace_back(address_v4(), 0, "", true); eps.emplace_back(address_v4(), 0, "", transport::ssl);
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
eps.emplace_back(address_v6(), 0, "", true); eps.emplace_back(address_v6(), 0, "", transport::ssl);
#endif #endif
#endif #endif
} }
@ -2391,7 +2393,7 @@ namespace {
// 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) ? 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;
@ -2400,7 +2402,7 @@ namespace {
void session_impl::on_udp_packet(std::weak_ptr<session_udp_socket> socket void session_impl::on_udp_packet(std::weak_ptr<session_udp_socket> socket
, bool const ssl, error_code const& ec) , transport const ssl, error_code const& ec)
{ {
COMPLETE_ASYNC("session_impl::on_udp_packet"); COMPLETE_ASYNC("session_impl::on_udp_packet");
if (ec) if (ec)
@ -2434,7 +2436,7 @@ namespace {
struct utp_socket_manager& mgr = struct utp_socket_manager& mgr =
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
ssl ? m_ssl_utp_socket_manager : ssl == transport::ssl ? m_ssl_utp_socket_manager :
#endif #endif
m_utp_socket_manager; m_utp_socket_manager;
@ -2545,14 +2547,15 @@ namespace {
, this, socket, ssl, _1)); , this, socket, ssl, _1));
} }
void session_impl::async_accept(std::shared_ptr<tcp::acceptor> const& listener, bool ssl) void session_impl::async_accept(std::shared_ptr<tcp::acceptor> const& listener
, transport const ssl)
{ {
TORRENT_ASSERT(!m_abort); TORRENT_ASSERT(!m_abort);
std::shared_ptr<socket_type> c = std::make_shared<socket_type>(m_io_service); std::shared_ptr<socket_type> c = std::make_shared<socket_type>(m_io_service);
tcp::socket* str = nullptr; tcp::socket* str = nullptr;
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
if (ssl) if (ssl == transport::ssl)
{ {
// accept connections initializing the SSL connection to // accept connections initializing the SSL connection to
// use the generic m_ssl_ctx context. However, since it has // use the generic m_ssl_ctx context. However, since it has
@ -2571,7 +2574,7 @@ namespace {
ADD_OUTSTANDING_ASYNC("session_impl::on_accept_connection"); ADD_OUTSTANDING_ASYNC("session_impl::on_accept_connection");
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
TORRENT_ASSERT(ssl == is_ssl(*c)); TORRENT_ASSERT((ssl == transport::ssl) == is_ssl(*c));
#endif #endif
listener->async_accept(*str listener->async_accept(*str
@ -2581,7 +2584,7 @@ namespace {
void session_impl::on_accept_connection(std::shared_ptr<socket_type> const& s void session_impl::on_accept_connection(std::shared_ptr<socket_type> const& s
, std::weak_ptr<tcp::acceptor> listen_socket, error_code const& e , std::weak_ptr<tcp::acceptor> listen_socket, error_code const& e
, bool const ssl) , transport const ssl)
{ {
COMPLETE_ASYNC("session_impl::on_accept_connection"); COMPLETE_ASYNC("session_impl::on_accept_connection");
m_stats_counters.inc_stats_counter(counters::on_accept_counter); m_stats_counters.inc_stats_counter(counters::on_accept_counter);
@ -2655,14 +2658,14 @@ namespace {
error_code err; error_code err;
m_alerts.emplace_alert<listen_failed_alert>(ep.address().to_string(err) m_alerts.emplace_alert<listen_failed_alert>(ep.address().to_string(err)
, ep, listen_failed_alert::accept, e , ep, listen_failed_alert::accept, e
, ssl ? socket_type_t::tcp_ssl : socket_type_t::tcp); , ssl == transport::ssl ? socket_type_t::tcp_ssl : socket_type_t::tcp);
} }
return; return;
} }
async_accept(listener, ssl); async_accept(listener, ssl);
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
if (ssl) if (ssl == transport::ssl)
{ {
TORRENT_ASSERT(is_ssl(*s)); TORRENT_ASSERT(is_ssl(*s));
@ -5188,14 +5191,6 @@ namespace {
} }
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
namespace {
listen_interface_t set_ssl_flag(listen_interface_t in)
{
in.ssl = true;
return in;
}
}
void session_impl::update_ssl_listen() void session_impl::update_ssl_listen()
{ {
@ -5224,7 +5219,8 @@ namespace {
std::vector<listen_interface_t> new_ifaces; std::vector<listen_interface_t> new_ifaces;
std::transform(current_ifaces.begin(), current_ifaces.end() std::transform(current_ifaces.begin(), current_ifaces.end()
, std::back_inserter(new_ifaces), &set_ssl_flag); , std::back_inserter(new_ifaces), [](listen_interface_t in)
{ in.ssl = true; return in; });
current_ifaces.insert(current_ifaces.end(), new_ifaces.begin(), new_ifaces.end()); current_ifaces.insert(current_ifaces.end(), new_ifaces.begin(), new_ifaces.end());
@ -5396,7 +5392,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) 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);

View File

@ -68,18 +68,18 @@ namespace libtorrent { namespace aux {
TORRENT_ASSERT(!sockets.empty()); TORRENT_ASSERT(!sockets.empty());
utp_socket_impl* impl = nullptr; utp_socket_impl* impl = nullptr;
bool ssl = false; transport ssl = transport::plaintext;
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
if (s.get<ssl_stream<utp_stream>>() != nullptr) if (s.get<ssl_stream<utp_stream>>() != nullptr)
{ {
impl = s.get<ssl_stream<utp_stream>>()->next_layer().get_impl(); impl = s.get<ssl_stream<utp_stream>>()->next_layer().get_impl();
ssl = true; ssl = transport::ssl;
} }
else else
#endif #endif
impl = s.get<utp_stream>()->get_impl(); impl = s.get<utp_stream>()->get_impl();
auto& idx = index[remote_address.is_v4() ? 0 : 1][ssl ? 1 : 0]; auto& idx = index[remote_address.is_v4() ? 0 : 1][ssl == transport::ssl ? 1 : 0];
auto const index_begin = idx; auto const index_begin = idx;
for (;;) for (;;)

View File

@ -37,60 +37,96 @@ using namespace lt;
namespace namespace
{ {
void test_equal(listen_socket_t const& s, address addr, int port, std::string dev, bool ssl) using tp = aux::transport;
void test_equal(aux::listen_socket_t const& s, address addr, int port
, std::string dev, tp ssl)
{ {
TEST_EQUAL(s.ssl, ssl); TEST_CHECK(s.ssl == ssl);
TEST_EQUAL(s.local_endpoint.address(), addr); TEST_EQUAL(s.local_endpoint.address(), addr);
TEST_EQUAL(s.original_port, port); TEST_EQUAL(s.original_port, port);
TEST_EQUAL(s.device, dev); TEST_EQUAL(s.device, dev);
} }
void test_equal(aux::listen_endpoint_t const& e1, address addr, int port, std::string dev, bool ssl) void test_equal(aux::listen_endpoint_t const& e1, address addr, int port
, std::string dev, tp ssl)
{ {
TEST_EQUAL(e1.ssl, ssl); TEST_CHECK(e1.ssl == ssl);
TEST_EQUAL(e1.port, port); TEST_EQUAL(e1.port, port);
TEST_EQUAL(e1.addr, addr); TEST_EQUAL(e1.addr, addr);
TEST_EQUAL(e1.device, dev); TEST_EQUAL(e1.device, dev);
} }
}
ip_interface ifc(char const* ip, char const* device)
{
ip_interface ipi;
ipi.interface_address = address::from_string(ip);
strncpy(ipi.name, device, sizeof(ipi.name));
return ipi;
}
aux::listen_endpoint_t ep(char const* ip, int port
, tp ssl = tp::plaintext
, std::string device = {})
{
return aux::listen_endpoint_t(address::from_string(ip), port, device, ssl);
}
aux::listen_endpoint_t ep(char const* ip, int port
, std::string device
, tp ssl = tp::plaintext)
{
return aux::listen_endpoint_t(address::from_string(ip), port, device, ssl);
}
aux::listen_socket_t sock(char const* ip, int const port
, int const original_port, char const* device = "")
{
aux::listen_socket_t s;
s.local_endpoint = tcp::endpoint(address::from_string(ip), port);
s.original_port = original_port;
s.device = device;
return s;
}
aux::listen_socket_t sock(char const* ip, int const port, char const* dev)
{ return sock(ip, port, port, dev); }
aux::listen_socket_t sock(char const* ip, int const port)
{ return sock(ip, port, port); }
} // anonymous namespace
TORRENT_TEST(partition_listen_sockets_wildcard2specific) TORRENT_TEST(partition_listen_sockets_wildcard2specific)
{ {
std::list<listen_socket_t> sockets; std::list<aux::listen_socket_t> sockets = {
listen_socket_t s; sock("0.0.0.0", 6881), sock("4.4.4.4", 6881)
s.local_endpoint = tcp::endpoint(tcp::v4(), 6881); };
s.original_port = 6881;
sockets.push_back(s);
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.4"), 6881);
sockets.push_back(s);
// remove the wildcard socket and replace it with a specific IP // remove the wildcard socket and replace it with a specific IP
std::vector<aux::listen_endpoint_t> eps; std::vector<aux::listen_endpoint_t> eps = {
eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", false); ep("4.4.4.4", 6881), ep("4.4.4.5", 6881)
eps.emplace_back(address_v4::from_string("4.4.4.5"), 6881, "", false); };
auto remove_iter = aux::partition_listen_sockets(eps, sockets); auto remove_iter = aux::partition_listen_sockets(eps, sockets);
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, "", false); test_equal(sockets.front(), address_v4::from_string("4.4.4.4"), 6881, "", tp::plaintext);
test_equal(sockets.back(), address_v4(), 6881, "", false); test_equal(sockets.back(), address_v4(), 6881, "", tp::plaintext);
test_equal(eps.front(), address_v4::from_string("4.4.4.5"), 6881, "", false); 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<listen_socket_t> sockets; std::list<aux::listen_socket_t> sockets = {
listen_socket_t s; sock("4.4.4.4", 6881), sock("4.4.4.5", 6881)
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.4"), 6881); };
s.original_port = 6881;
sockets.push_back(s);
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.5"), 6881);
sockets.push_back(s);
// change the ports // change the ports
std::vector<aux::listen_endpoint_t> eps; std::vector<aux::listen_endpoint_t> eps = {
eps.emplace_back(address_v4::from_string("4.4.4.4"), 6882, "", false); ep("4.4.4.4", 6882), ep("4.4.4.5", 6882)
eps.emplace_back(address_v4::from_string("4.4.4.5"), 6882, "", false); };
auto remove_iter = aux::partition_listen_sockets(eps, sockets); auto remove_iter = aux::partition_listen_sockets(eps, sockets);
TEST_CHECK(sockets.begin() == remove_iter); TEST_CHECK(sockets.begin() == remove_iter);
TEST_EQUAL(eps.size(), 2); TEST_EQUAL(eps.size(), 2);
@ -98,66 +134,58 @@ TORRENT_TEST(partition_listen_sockets_port_change)
TORRENT_TEST(partition_listen_sockets_device_bound) TORRENT_TEST(partition_listen_sockets_device_bound)
{ {
std::list<listen_socket_t> sockets; std::list<aux::listen_socket_t> sockets = {
listen_socket_t s; sock("4.4.4.5", 6881), sock("0.0.0.0", 6881)
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.5"), 6881); };
s.original_port = 6881;
sockets.push_back(s);
s.local_endpoint = tcp::endpoint(tcp::v4(), 6881);
sockets.push_back(s);
// replace the wildcard socket with a pair of device bound sockets // replace the wildcard socket with a pair of device bound sockets
std::vector<aux::listen_endpoint_t> eps; std::vector<aux::listen_endpoint_t> eps = {
eps.emplace_back(address_v4::from_string("4.4.4.5"), 6881, "", false); ep("4.4.4.5", 6881)
eps.emplace_back(address_v4::from_string("4.4.4.6"), 6881, "eth1", false); , ep("4.4.4.6", 6881, "eth1")
eps.emplace_back(address_v4::from_string("4.4.4.7"), 6881, "eth1", false); , ep("4.4.4.7", 6881, "eth1")
};
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, "", false); test_equal(sockets.front(), address_v4::from_string("4.4.4.5"), 6881, "", tp::plaintext);
test_equal(sockets.back(), address_v4(), 6881, "", false); 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<listen_socket_t> sockets; std::list<aux::listen_socket_t> sockets = {
listen_socket_t s; sock("10.10.10.10", 6881, "enp3s0")
s.local_endpoint = tcp::endpoint(address_v4::from_string("10.10.10.10"), 6881); , sock("4.4.4.4", 6881, "enp3s0")
s.device = "enp3s0"; };
s.original_port = 6881;
sockets.push_back(s);
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.4"), 6881);
sockets.push_back(s);
// change the IP of a device bound socket // change the IP of a device bound socket
std::vector<aux::listen_endpoint_t> eps; std::vector<aux::listen_endpoint_t> eps = {
eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "enp3s0", false); ep("10.10.10.10", 6881, "enp3s0")
eps.emplace_back(address_v4::from_string("4.4.4.5"), 6881, "enp3s0", false); , ep("4.4.4.5", 6881, "enp3s0")
};
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", false); 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", false); 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", false); 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<listen_socket_t> sockets; std::list<aux::listen_socket_t> sockets = {
listen_socket_t s; sock("10.10.10.10", 6883, 6881), sock("4.4.4.4", 6883, 6881)
s.local_endpoint = tcp::endpoint(address_v4::from_string("10.10.10.10"), 6883); };
s.original_port = 6881;
sockets.push_back(s);
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.4"), 6883);
sockets.push_back(s);
// make sure all sockets are kept when the actual port is different from the original // make sure all sockets are kept when the actual port is different from the original
std::vector<aux::listen_endpoint_t> eps; std::vector<aux::listen_endpoint_t> eps = {
eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "", false); ep("10.10.10.10", 6881)
eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", false); , ep("4.4.4.4", 6881)
};
auto remove_iter = aux::partition_listen_sockets(eps, sockets); auto remove_iter = aux::partition_listen_sockets(eps, sockets);
TEST_CHECK(remove_iter == sockets.end()); TEST_CHECK(remove_iter == sockets.end());
TEST_CHECK(eps.empty()); TEST_CHECK(eps.empty());
@ -165,20 +193,18 @@ TORRENT_TEST(partition_listen_sockets_original_port)
TORRENT_TEST(partition_listen_sockets_ssl) TORRENT_TEST(partition_listen_sockets_ssl)
{ {
std::list<listen_socket_t> sockets; std::list<aux::listen_socket_t> sockets = {
listen_socket_t s; sock("10.10.10.10", 6881), sock("4.4.4.4", 6881)
s.local_endpoint = tcp::endpoint(address_v4::from_string("10.10.10.10"), 6881); };
s.original_port = 6881;
sockets.push_back(s);
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.4"), 6881);
sockets.push_back(s);
// add ssl sockets // add ssl sockets
std::vector<aux::listen_endpoint_t> eps; std::vector<aux::listen_endpoint_t> eps = {
eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "", false); ep("10.10.10.10", 6881)
eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", false); , ep("4.4.4.4", 6881)
eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "", true); , ep("10.10.10.10", 6881, tp::ssl)
eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", true); , ep("4.4.4.4", 6881, tp::ssl)
};
auto remove_iter = aux::partition_listen_sockets(eps, sockets); auto remove_iter = aux::partition_listen_sockets(eps, sockets);
TEST_CHECK(remove_iter == sockets.end()); TEST_CHECK(remove_iter == sockets.end());
TEST_EQUAL(eps.size(), 2); TEST_EQUAL(eps.size(), 2);
@ -186,18 +212,15 @@ TORRENT_TEST(partition_listen_sockets_ssl)
TORRENT_TEST(partition_listen_sockets_op_ports) TORRENT_TEST(partition_listen_sockets_op_ports)
{ {
std::list<listen_socket_t> sockets; std::list<aux::listen_socket_t> sockets = {
listen_socket_t s; sock("10.10.10.10", 6881, 0), sock("4.4.4.4", 6881)
s.local_endpoint = tcp::endpoint(address_v4::from_string("10.10.10.10"), 6881); };
s.original_port = 0;
sockets.push_back(s);
s.local_endpoint = tcp::endpoint(address_v4::from_string("4.4.4.4"), 6881);
sockets.push_back(s);
// replace OS assigned ports with explicit ports // replace OS assigned ports with explicit ports
std::vector<aux::listen_endpoint_t> eps; std::vector<aux::listen_endpoint_t> eps ={
eps.emplace_back(address_v4::from_string("10.10.10.10"), 6882, "", false); ep("10.10.10.10", 6882),
eps.emplace_back(address_v4::from_string("4.4.4.4"), 6882, "", false); ep("4.4.4.4", 6882),
};
auto remove_iter = aux::partition_listen_sockets(eps, sockets); auto remove_iter = aux::partition_listen_sockets(eps, sockets);
TEST_CHECK(remove_iter == sockets.begin()); TEST_CHECK(remove_iter == sockets.begin());
TEST_EQUAL(eps.size(), 2); TEST_EQUAL(eps.size(), 2);
@ -205,41 +228,27 @@ TORRENT_TEST(partition_listen_sockets_op_ports)
TORRENT_TEST(expand_unspecified) TORRENT_TEST(expand_unspecified)
{ {
std::vector<ip_interface> ifs; std::vector<ip_interface> const ifs = {
std::vector<aux::listen_endpoint_t> eps; ifc("127.0.0.1", "lo")
, ifc("192.168.1.2", "eth0")
, ifc("24.172.48.90", "eth1")
, ifc("::1", "lo")
, ifc("fe80::d250:99ff:fe0c:9b74", "eth0")
, ifc( "2601:646:c600:a3:d250:99ff:fe0c:9b74", "eth0")
};
ip_interface ipi; auto v4_nossl = ep("0.0.0.0", 6881);
ipi.interface_address = address::from_string("127.0.0.1"); auto v4_ssl = ep("0.0.0.0", 6882, tp::ssl);
strcpy(ipi.name, "lo"); auto v6_unsp_nossl = ep("::", 6883);
ifs.push_back(ipi); auto v6_unsp_ssl = ep("::", 6884, tp::ssl);
ipi.interface_address = address::from_string("192.168.1.2"); auto v6_ll_nossl = ep("fe80::d250:99ff:fe0c:9b74", 6883);
strcpy(ipi.name, "eth0"); auto v6_ll_ssl = ep("fe80::d250:99ff:fe0c:9b74", 6884, tp::ssl);
ifs.push_back(ipi); auto v6_g_nossl = ep("2601:646:c600:a3:d250:99ff:fe0c:9b74", 6883);
ipi.interface_address = address::from_string("24.172.48.90"); auto v6_g_ssl = ep("2601:646:c600:a3:d250:99ff:fe0c:9b74", 6884, tp::ssl);
strcpy(ipi.name, "eth1");
ifs.push_back(ipi);
ipi.interface_address = address::from_string("::1");
strcpy(ipi.name, "lo");
ifs.push_back(ipi);
ipi.interface_address = address::from_string("fe80::d250:99ff:fe0c:9b74");
strcpy(ipi.name, "eth0");
ifs.push_back(ipi);
ipi.interface_address = address::from_string("2601:646:c600:a3:d250:99ff:fe0c:9b74");
ifs.push_back(ipi);
aux::listen_endpoint_t v4_nossl(address::from_string("0.0.0.0"), 6881, std::string(), false); std::vector<aux::listen_endpoint_t> eps = {
aux::listen_endpoint_t v4_ssl(address::from_string("0.0.0.0"), 6882, std::string(), true); v4_nossl, v4_ssl, v6_unsp_nossl, v6_unsp_ssl
aux::listen_endpoint_t v6_unsp_nossl(address::from_string("::"), 6883, std::string(), false); };
aux::listen_endpoint_t v6_unsp_ssl(address::from_string("::"), 6884, std::string(), true);
aux::listen_endpoint_t v6_ll_nossl(address::from_string("fe80::d250:99ff:fe0c:9b74"), 6883, std::string(), false);
aux::listen_endpoint_t v6_ll_ssl(address::from_string("fe80::d250:99ff:fe0c:9b74"), 6884, std::string(), true);
aux::listen_endpoint_t v6_g_nossl(address::from_string("2601:646:c600:a3:d250:99ff:fe0c:9b74"), 6883, std::string(), false);
aux::listen_endpoint_t v6_g_ssl(address::from_string("2601:646:c600:a3:d250:99ff:fe0c:9b74"), 6884, std::string(), true);
eps.push_back(v4_nossl);
eps.push_back(v4_ssl);
eps.push_back(v6_unsp_nossl);
eps.push_back(v6_unsp_ssl);
aux::expand_unspecified_address(ifs, eps); aux::expand_unspecified_address(ifs, eps);
@ -254,7 +263,7 @@ TORRENT_TEST(expand_unspecified)
TEST_CHECK(std::count(eps.begin(), eps.end(), v6_unsp_ssl) == 0); TEST_CHECK(std::count(eps.begin(), eps.end(), v6_unsp_ssl) == 0);
// test that a user configured endpoint is not duplicated // test that a user configured endpoint is not duplicated
aux::listen_endpoint_t v6_g_nossl_dev(address::from_string("2601:646:c600:a3:d250:99ff:fe0c:9b74"), 6883, "eth0", false); auto v6_g_nossl_dev = ep("2601:646:c600:a3:d250:99ff:fe0c:9b74", 6883, "eth0");
eps.clear(); eps.clear();
eps.push_back(v6_unsp_nossl); eps.push_back(v6_unsp_nossl);