From 1af17be0462bebf716c12309a49711aa5ef21d39 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 30 Apr 2017 21:11:42 -0400 Subject: [PATCH] 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 --- include/libtorrent/aux_/session_impl.hpp | 49 ++-- .../libtorrent/aux_/session_udp_sockets.hpp | 8 +- src/session_impl.cpp | 54 ++-- src/session_udp_sockets.cpp | 6 +- test/test_listen_socket.cpp | 253 +++++++++--------- 5 files changed, 189 insertions(+), 181 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 2ab1093c2..9499628d8 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -114,9 +114,24 @@ namespace libtorrent { namespace dht { - struct dht_tracker; - class item; - } + struct dht_tracker; + 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 { @@ -164,8 +179,8 @@ namespace dht { int tcp_port_mapping[2]; int udp_port_mapping[2]; - // set to true if this is an SSL listen socket - bool ssl = false; + // indicates whether this is an SSL listen socket or not + transport ssl = transport::plaintext; // the actual sockets (TCP listen socket and UDP socket) // An entry does not necessarily have a UDP or TCP socket. One of these @@ -176,23 +191,9 @@ namespace dht { std::shared_ptr 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 { - 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) {} bool operator==(listen_endpoint_t const& o) const @@ -203,7 +204,7 @@ namespace aux { address addr; int port; std::string device; - bool ssl; + transport ssl; }; // 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_ipv4_interface() const override; - void async_accept(std::shared_ptr const& listener, bool ssl); + void async_accept(std::shared_ptr const& listener, transport ssl); void on_accept_connection(std::shared_ptr const& s - , std::weak_ptr listener, error_code const& e, bool ssl); + , std::weak_ptr listener, error_code const& e, transport ssl); void incoming_connection(std::shared_ptr const& s); @@ -1117,7 +1118,7 @@ namespace aux { void on_udp_writeable(std::weak_ptr s, error_code const& ec); void on_udp_packet(std::weak_ptr s - , bool ssl, error_code const& ec); + , transport ssl, error_code const& ec); libtorrent::utp_socket_manager m_utp_socket_manager; diff --git a/include/libtorrent/aux_/session_udp_sockets.hpp b/include/libtorrent/aux_/session_udp_sockets.hpp index 2fe2c3272..54a3b02d9 100644 --- a/include/libtorrent/aux_/session_udp_sockets.hpp +++ b/include/libtorrent/aux_/session_udp_sockets.hpp @@ -43,6 +43,8 @@ namespace libtorrent { namespace aux { struct listen_endpoint_t; struct proxy_settings; + enum class transport : std::uint8_t { plaintext, ssl }; + struct session_udp_socket : utp_socket_interface { explicit session_udp_socket(io_service& ios) @@ -61,7 +63,7 @@ namespace libtorrent { namespace aux { 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_) {} // 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; // set to true if this is an SSL socket - bool const ssl; + transport const ssl; }; // sockets used for outoing utp connections @@ -93,7 +95,7 @@ namespace libtorrent { namespace aux { private: // round-robin index into sockets // 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, 2> index = {{ {{0, 0}}, {{0, 0}} }}; }; } } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 94179b5e6..6193bb638 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1482,7 +1482,7 @@ namespace { #endif 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(); int last_op = 0; socket_type_t const sock_type @@ -1844,7 +1844,8 @@ namespace { #if !TORRENT_USE_IPV6 if (adr.is_v4()) #endif - eps.emplace_back(adr, port, std::string(), ssl); + eps.emplace_back(adr, port, std::string() + , ssl ? transport::ssl : transport::plaintext); } else { @@ -1877,7 +1878,8 @@ namespace { // (which must be of the same family as the address we're // connecting to) 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 , 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)) { @@ -2006,7 +2008,7 @@ namespace { if (!err) { socket_type_t const socket_type - = l.ssl + = l.ssl == transport::ssl ? socket_type_t::tcp_ssl : socket_type_t::tcp; @@ -2021,7 +2023,7 @@ namespace { if (!err && l.udp_sock->sock.is_open()) { socket_type_t const socket_type - = l.ssl + = l.ssl == transport::ssl ? socket_type_t::utp_ssl : socket_type_t::udp; @@ -2070,14 +2072,14 @@ namespace { // any interface if (eps.empty()) { - eps.emplace_back(address_v4(), 0, "", false); + eps.emplace_back(address_v4(), 0, "", transport::plaintext); #if TORRENT_USE_IPV6 - eps.emplace_back(address_v6(), 0, "", false); + eps.emplace_back(address_v6(), 0, "", transport::plaintext); #endif #ifdef TORRENT_USE_OPENSSL - eps.emplace_back(address_v4(), 0, "", true); + eps.emplace_back(address_v4(), 0, "", transport::ssl); #if TORRENT_USE_IPV6 - eps.emplace_back(address_v6(), 0, "", true); + eps.emplace_back(address_v6(), 0, "", transport::ssl); #endif #endif } @@ -2391,7 +2393,7 @@ namespace { // notify the utp socket manager it can start sending on the socket again struct utp_socket_manager& mgr = #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 m_utp_socket_manager; @@ -2400,7 +2402,7 @@ namespace { void session_impl::on_udp_packet(std::weak_ptr socket - , bool const ssl, error_code const& ec) + , transport const ssl, error_code const& ec) { COMPLETE_ASYNC("session_impl::on_udp_packet"); if (ec) @@ -2434,7 +2436,7 @@ namespace { struct utp_socket_manager& mgr = #ifdef TORRENT_USE_OPENSSL - ssl ? m_ssl_utp_socket_manager : + ssl == transport::ssl ? m_ssl_utp_socket_manager : #endif m_utp_socket_manager; @@ -2545,14 +2547,15 @@ namespace { , this, socket, ssl, _1)); } - void session_impl::async_accept(std::shared_ptr const& listener, bool ssl) + void session_impl::async_accept(std::shared_ptr const& listener + , transport const ssl) { TORRENT_ASSERT(!m_abort); std::shared_ptr c = std::make_shared(m_io_service); tcp::socket* str = nullptr; #ifdef TORRENT_USE_OPENSSL - if (ssl) + if (ssl == transport::ssl) { // accept connections initializing the SSL connection to // use the generic m_ssl_ctx context. However, since it has @@ -2571,7 +2574,7 @@ namespace { ADD_OUTSTANDING_ASYNC("session_impl::on_accept_connection"); #ifdef TORRENT_USE_OPENSSL - TORRENT_ASSERT(ssl == is_ssl(*c)); + TORRENT_ASSERT((ssl == transport::ssl) == is_ssl(*c)); #endif listener->async_accept(*str @@ -2581,7 +2584,7 @@ namespace { void session_impl::on_accept_connection(std::shared_ptr const& s , std::weak_ptr listen_socket, error_code const& e - , bool const ssl) + , transport const ssl) { COMPLETE_ASYNC("session_impl::on_accept_connection"); m_stats_counters.inc_stats_counter(counters::on_accept_counter); @@ -2655,14 +2658,14 @@ namespace { error_code err; m_alerts.emplace_alert(ep.address().to_string(err) , 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; } async_accept(listener, ssl); #ifdef TORRENT_USE_OPENSSL - if (ssl) + if (ssl == transport::ssl) { TORRENT_ASSERT(is_ssl(*s)); @@ -5188,14 +5191,6 @@ namespace { } #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() { @@ -5224,7 +5219,8 @@ namespace { std::vector new_ifaces; 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()); @@ -5396,7 +5392,7 @@ namespace { if (m_settings.get_bool(settings_pack::force_proxy)) return 0; 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 TORRENT_UNUSED(sock); diff --git a/src/session_udp_sockets.cpp b/src/session_udp_sockets.cpp index 94a6b7b11..df00d7c5e 100644 --- a/src/session_udp_sockets.cpp +++ b/src/session_udp_sockets.cpp @@ -68,18 +68,18 @@ namespace libtorrent { namespace aux { TORRENT_ASSERT(!sockets.empty()); utp_socket_impl* impl = nullptr; - bool ssl = false; + transport ssl = transport::plaintext; #ifdef TORRENT_USE_OPENSSL if (s.get>() != nullptr) { impl = s.get>()->next_layer().get_impl(); - ssl = true; + ssl = transport::ssl; } else #endif impl = s.get()->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; for (;;) diff --git a/test/test_listen_socket.cpp b/test/test_listen_socket.cpp index 344f894f2..7b3bb37c9 100644 --- a/test/test_listen_socket.cpp +++ b/test/test_listen_socket.cpp @@ -37,60 +37,96 @@ using namespace lt; 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.original_port, port); 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.addr, addr); 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) { - std::list sockets; - listen_socket_t s; - 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); + std::list sockets = { + sock("0.0.0.0", 6881), sock("4.4.4.4", 6881) + }; // remove the wildcard socket and replace it with a specific IP - std::vector eps; - eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", false); - eps.emplace_back(address_v4::from_string("4.4.4.5"), 6881, "", false); + std::vector eps = { + ep("4.4.4.4", 6881), ep("4.4.4.5", 6881) + }; + auto remove_iter = aux::partition_listen_sockets(eps, sockets); TEST_EQUAL(eps.size(), 1); TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 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.back(), address_v4(), 6881, "", false); - test_equal(eps.front(), address_v4::from_string("4.4.4.5"), 6881, "", false); + 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(eps.front(), address_v4::from_string("4.4.4.5"), 6881, "", tp::plaintext); } TORRENT_TEST(partition_listen_sockets_port_change) { - std::list sockets; - listen_socket_t s; - 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); + std::list sockets = { + sock("4.4.4.4", 6881), sock("4.4.4.5", 6881) + }; // change the ports - std::vector eps; - eps.emplace_back(address_v4::from_string("4.4.4.4"), 6882, "", false); - eps.emplace_back(address_v4::from_string("4.4.4.5"), 6882, "", false); + std::vector eps = { + ep("4.4.4.4", 6882), ep("4.4.4.5", 6882) + }; auto remove_iter = aux::partition_listen_sockets(eps, sockets); TEST_CHECK(sockets.begin() == remove_iter); TEST_EQUAL(eps.size(), 2); @@ -98,66 +134,58 @@ TORRENT_TEST(partition_listen_sockets_port_change) TORRENT_TEST(partition_listen_sockets_device_bound) { - std::list sockets; - listen_socket_t s; - 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); - + std::list sockets = { + sock("4.4.4.5", 6881), sock("0.0.0.0", 6881) + }; // replace the wildcard socket with a pair of device bound sockets - std::vector eps; - eps.emplace_back(address_v4::from_string("4.4.4.5"), 6881, "", false); - eps.emplace_back(address_v4::from_string("4.4.4.6"), 6881, "eth1", false); - eps.emplace_back(address_v4::from_string("4.4.4.7"), 6881, "eth1", false); + std::vector eps = { + ep("4.4.4.5", 6881) + , ep("4.4.4.6", 6881, "eth1") + , ep("4.4.4.7", 6881, "eth1") + }; + auto remove_iter = aux::partition_listen_sockets(eps, sockets); TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 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.back(), address_v4(), 6881, "", false); + 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(eps.size(), 2); } TORRENT_TEST(partition_listen_sockets_device_ip_change) { - std::list sockets; - listen_socket_t s; - s.local_endpoint = tcp::endpoint(address_v4::from_string("10.10.10.10"), 6881); - 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); + std::list sockets = { + sock("10.10.10.10", 6881, "enp3s0") + , sock("4.4.4.4", 6881, "enp3s0") + }; // change the IP of a device bound socket - std::vector eps; - eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "enp3s0", false); - eps.emplace_back(address_v4::from_string("4.4.4.5"), 6881, "enp3s0", false); + std::vector eps = { + ep("10.10.10.10", 6881, "enp3s0") + , ep("4.4.4.5", 6881, "enp3s0") + }; auto remove_iter = aux::partition_listen_sockets(eps, sockets); TEST_EQUAL(std::distance(sockets.begin(), remove_iter), 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.back(), address_v4::from_string("4.4.4.4"), 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", tp::plaintext); 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) { - std::list sockets; - listen_socket_t s; - 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); + std::list sockets = { + sock("10.10.10.10", 6883, 6881), sock("4.4.4.4", 6883, 6881) + }; // make sure all sockets are kept when the actual port is different from the original - std::vector eps; - eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "", false); - eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", false); + std::vector eps = { + ep("10.10.10.10", 6881) + , ep("4.4.4.4", 6881) + }; + auto remove_iter = aux::partition_listen_sockets(eps, sockets); TEST_CHECK(remove_iter == sockets.end()); TEST_CHECK(eps.empty()); @@ -165,20 +193,18 @@ TORRENT_TEST(partition_listen_sockets_original_port) TORRENT_TEST(partition_listen_sockets_ssl) { - std::list sockets; - listen_socket_t s; - 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); + std::list sockets = { + sock("10.10.10.10", 6881), sock("4.4.4.4", 6881) + }; // add ssl sockets - std::vector eps; - eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "", false); - eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", false); - eps.emplace_back(address_v4::from_string("10.10.10.10"), 6881, "", true); - eps.emplace_back(address_v4::from_string("4.4.4.4"), 6881, "", true); + std::vector eps = { + ep("10.10.10.10", 6881) + , ep("4.4.4.4", 6881) + , ep("10.10.10.10", 6881, tp::ssl) + , ep("4.4.4.4", 6881, tp::ssl) + }; + auto remove_iter = aux::partition_listen_sockets(eps, sockets); TEST_CHECK(remove_iter == sockets.end()); TEST_EQUAL(eps.size(), 2); @@ -186,18 +212,15 @@ TORRENT_TEST(partition_listen_sockets_ssl) TORRENT_TEST(partition_listen_sockets_op_ports) { - std::list sockets; - listen_socket_t s; - 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); + std::list sockets = { + sock("10.10.10.10", 6881, 0), sock("4.4.4.4", 6881) + }; // replace OS assigned ports with explicit ports - std::vector eps; - eps.emplace_back(address_v4::from_string("10.10.10.10"), 6882, "", false); - eps.emplace_back(address_v4::from_string("4.4.4.4"), 6882, "", false); + std::vector eps ={ + ep("10.10.10.10", 6882), + ep("4.4.4.4", 6882), + }; auto remove_iter = aux::partition_listen_sockets(eps, sockets); TEST_CHECK(remove_iter == sockets.begin()); TEST_EQUAL(eps.size(), 2); @@ -205,41 +228,27 @@ TORRENT_TEST(partition_listen_sockets_op_ports) TORRENT_TEST(expand_unspecified) { - std::vector ifs; - std::vector eps; + std::vector const ifs = { + 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; - ipi.interface_address = address::from_string("127.0.0.1"); - strcpy(ipi.name, "lo"); - ifs.push_back(ipi); - ipi.interface_address = address::from_string("192.168.1.2"); - strcpy(ipi.name, "eth0"); - ifs.push_back(ipi); - ipi.interface_address = address::from_string("24.172.48.90"); - 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); + auto v4_nossl = ep("0.0.0.0", 6881); + auto v4_ssl = ep("0.0.0.0", 6882, tp::ssl); + auto v6_unsp_nossl = ep("::", 6883); + auto v6_unsp_ssl = ep("::", 6884, tp::ssl); + auto v6_ll_nossl = ep("fe80::d250:99ff:fe0c:9b74", 6883); + auto v6_ll_ssl = ep("fe80::d250:99ff:fe0c:9b74", 6884, tp::ssl); + auto v6_g_nossl = ep("2601:646:c600:a3:d250:99ff:fe0c:9b74", 6883); + auto v6_g_ssl = ep("2601:646:c600:a3:d250:99ff:fe0c:9b74", 6884, tp::ssl); - aux::listen_endpoint_t v4_nossl(address::from_string("0.0.0.0"), 6881, std::string(), false); - aux::listen_endpoint_t v4_ssl(address::from_string("0.0.0.0"), 6882, std::string(), true); - 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); + std::vector eps = { + v4_nossl, v4_ssl, v6_unsp_nossl, v6_unsp_ssl + }; 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 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.push_back(v6_unsp_nossl);