diff --git a/include/libtorrent/socket_io.hpp b/include/libtorrent/socket_io.hpp index 6441b06ae..111cd5fdf 100644 --- a/include/libtorrent/socket_io.hpp +++ b/include/libtorrent/socket_io.hpp @@ -69,7 +69,7 @@ namespace libtorrent } template - void write_address(address const& a, OutIt& out) + void write_address(address const& a, OutIt&& out) { #if TORRENT_USE_IPV6 if (a.is_v4()) @@ -87,15 +87,15 @@ namespace libtorrent } template - address read_v4_address(InIt& in) + address read_v4_address(InIt&& in) { - unsigned long ip = read_uint32(in); + std::uint32_t const ip = read_uint32(in); return address_v4(ip); } #if TORRENT_USE_IPV6 template - address read_v6_address(InIt& in) + address read_v6_address(InIt&& in) { address_v6::bytes_type bytes; for (auto& b : bytes) @@ -105,14 +105,14 @@ namespace libtorrent #endif template - void write_endpoint(Endpoint const& e, OutIt& out) + void write_endpoint(Endpoint const& e, OutIt&& out) { write_address(e.address(), out); write_uint16(e.port(), out); } template - Endpoint read_v4_endpoint(InIt& in) + Endpoint read_v4_endpoint(InIt&& in) { address addr = read_v4_address(in); int port = read_uint16(in); @@ -121,57 +121,34 @@ namespace libtorrent #if TORRENT_USE_IPV6 template - Endpoint read_v6_endpoint(InIt& in) + Endpoint read_v6_endpoint(InIt&& in) { address addr = read_v6_address(in); int port = read_uint16(in); return Endpoint(addr, port); } #endif - template - void read_endpoint_list(libtorrent::bdecode_node const& n - , std::vector& epl) + std::vector read_endpoint_list(libtorrent::bdecode_node const& n) { - using namespace libtorrent; - if (n.type() != bdecode_node::list_t) return; + std::vector ret; + if (n.type() != bdecode_node::list_t) return ret; for (int i = 0; i < n.list_size(); ++i) { bdecode_node e = n.list_at(i); - if (e.type() != bdecode_node::string_t) return; + if (e.type() != bdecode_node::string_t) return ret; if (e.string_length() < 6) continue; char const* in = e.string_ptr(); if (e.string_length() == 6) - epl.push_back(read_v4_endpoint(in)); + ret.push_back(read_v4_endpoint(in)); #if TORRENT_USE_IPV6 else if (e.string_length() == 18) - epl.push_back(read_v6_endpoint(in)); + ret.push_back(read_v6_endpoint(in)); #endif } + return ret; } - } - - template - void read_endpoint_list(libtorrent::entry const* n, std::vector& epl) - { - using namespace libtorrent; - if (n->type() != entry::list_t) return; - entry::list_type const& contacts = n->list(); - for (entry::list_type::const_iterator i = contacts.begin() - , end(contacts.end()); i != end; ++i) - { - if (i->type() != entry::string_t) return; - std::string const& p = i->string(); - if (p.size() < 6) continue; - std::string::const_iterator in = p.begin(); - if (p.size() == 6) - epl.push_back(detail::read_v4_endpoint(in)); -#if TORRENT_USE_IPV6 - else if (p.size() == 18) - epl.push_back(detail::read_v6_endpoint(in)); -#endif - } - } + } // namespace detail } diff --git a/src/kademlia/dht_state.cpp b/src/kademlia/dht_state.cpp index 4c6046afd..6fc15d53d 100644 --- a/src/kademlia/dht_state.cpp +++ b/src/kademlia/dht_state.cpp @@ -75,10 +75,10 @@ namespace #endif if (bdecode_node const nodes = e.dict_find_list("nodes")) - detail::read_endpoint_list(nodes, ret.nodes); + ret.nodes = detail::read_endpoint_list(nodes); #if TORRENT_USE_IPV6 if (bdecode_node const nodes = e.dict_find_list("nodes6")) - detail::read_endpoint_list(nodes, ret.nodes6); + ret.nodes6 = detail::read_endpoint_list(nodes); #endif return ret; diff --git a/src/kademlia/get_peers.cpp b/src/kademlia/get_peers.cpp index 3b1dfbc71..7c533b66b 100644 --- a/src/kademlia/get_peers.cpp +++ b/src/kademlia/get_peers.cpp @@ -96,7 +96,7 @@ void get_peers_observer::reply(msg const& m) else { // assume it's uTorrent/libtorrent format - read_endpoint_list(n, peer_list); + peer_list = read_endpoint_list(n); #ifndef TORRENT_DISABLE_LOGGING auto logger = get_observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) diff --git a/test/test_socket_io.cpp b/test/test_socket_io.cpp index e333a74aa..4f325cf10 100644 --- a/test/test_socket_io.cpp +++ b/test/test_socket_io.cpp @@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. using namespace libtorrent; using namespace libtorrent::detail; -TORRENT_TEST(socket_io) +TORRENT_TEST(address_to_bytes) { // test address_to_bytes TEST_EQUAL(address_to_bytes(address_v4::from_string("10.11.12.13")), "\x0a\x0b\x0c\x0d"); @@ -49,61 +49,51 @@ TORRENT_TEST(socket_io) // test endpoint_to_bytes TEST_EQUAL(endpoint_to_bytes(udp::endpoint(address_v4::from_string("10.11.12.13"), 8080)), "\x0a\x0b\x0c\x0d\x1f\x90"); TEST_EQUAL(endpoint_to_bytes(udp::endpoint(address_v4::from_string("16.5.127.1"), 12345)), "\x10\x05\x7f\x01\x30\x39"); +} +TORRENT_TEST(read_v4_address) +{ std::string buf; - std::back_insert_iterator out1(buf); - write_address(address_v4::from_string("16.5.128.1"), out1); + write_address(address_v4::from_string("16.5.128.1"), std::back_inserter(buf)); TEST_EQUAL(buf, "\x10\x05\x80\x01"); - std::string::iterator in = buf.begin(); - address addr4 = read_v4_address(in); + address addr4 = read_v4_address(buf.begin()); TEST_EQUAL(addr4, address_v4::from_string("16.5.128.1")); buf.clear(); - std::back_insert_iterator out2(buf); - write_endpoint(udp::endpoint(address_v4::from_string("16.5.128.1"), 1337), out2); + write_endpoint(udp::endpoint(address_v4::from_string("16.5.128.1"), 1337) + , std::back_inserter(buf)); TEST_EQUAL(buf, "\x10\x05\x80\x01\x05\x39"); - in = buf.begin(); - udp::endpoint ep4 = read_v4_endpoint(in); + udp::endpoint ep4 = read_v4_endpoint(buf.begin()); TEST_EQUAL(ep4, udp::endpoint(address_v4::from_string("16.5.128.1"), 1337)); +} #if TORRENT_USE_IPV6 - buf.clear(); - std::back_insert_iterator out3(buf); - write_address(address_v6::from_string("1000::ffff"), out3); +TORRENT_TEST(read_v6_endpoint) +{ + std::string buf; + write_address(address_v6::from_string("1000::ffff"), std::back_inserter(buf)); TEST_CHECK(std::equal(buf.begin(), buf.end(), "\x10\0\0\0\0\0\0\0\0\0\0\0\0\0\xff\xff")); - in = buf.begin(); - address addr6 = read_v6_address(in); + address addr6 = read_v6_address(buf.begin()); TEST_EQUAL(addr6, address_v6::from_string("1000::ffff")); buf.clear(); - std::back_insert_iterator out4(buf); - write_endpoint(udp::endpoint(address_v6::from_string("1000::ffff"), 1337), out4); + write_endpoint(udp::endpoint(address_v6::from_string("1000::ffff"), 1337) + , std::back_inserter(buf)); TEST_CHECK(std::equal(buf.begin(), buf.end(), "\x10\0\0\0\0\0\0\0\0\0\0\0\0\0\xff\xff\x05\x39")); TEST_EQUAL(buf.size(), 18); - in = buf.begin(); - udp::endpoint ep6 = read_v6_endpoint(in); + udp::endpoint ep6 = read_v6_endpoint(buf.begin()); TEST_EQUAL(ep6, udp::endpoint(address_v6::from_string("1000::ffff"), 1337)); +} #endif +TORRENT_TEST(read_endpoint_list) +{ char const eplist[] = "l6:\x10\x05\x80\x01\x05\x39" "18:\x10\0\0\0\0\0\0\0\0\0\0\0\0\0\xff\xff\x05\x39" "e"; bdecode_node e; error_code ec; bdecode(eplist, eplist + sizeof(eplist)-1, e, ec); TEST_CHECK(!ec); - std::vector list; - read_endpoint_list(e, list); - -#if TORRENT_USE_IPV6 - TEST_EQUAL(list.size(), 2); - TEST_EQUAL(list[1], udp::endpoint(address_v6::from_string("1000::ffff"), 1337)); -#else - TEST_EQUAL(list.size(), 1); -#endif - TEST_EQUAL(list[0], udp::endpoint(address_v4::from_string("16.5.128.1"), 1337)); - - entry e2 = bdecode(eplist, eplist + sizeof(eplist)-1); - list.clear(); - read_endpoint_list(&e2, list); + std::vector list = read_endpoint_list(e); #if TORRENT_USE_IPV6 TEST_EQUAL(list.size(), 2);