forked from premiere/premiere-libtorrent
clean up read_endpoint_list (#1102)
This commit is contained in:
parent
219b2c36b4
commit
80ba45e1fb
|
@ -69,7 +69,7 @@ namespace libtorrent
|
|||
}
|
||||
|
||||
template<class OutIt>
|
||||
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<class InIt>
|
||||
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<class InIt>
|
||||
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<class Endpoint, class OutIt>
|
||||
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<class Endpoint, class InIt>
|
||||
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<class Endpoint, class InIt>
|
||||
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 <class EndpointType>
|
||||
void read_endpoint_list(libtorrent::bdecode_node const& n
|
||||
, std::vector<EndpointType>& epl)
|
||||
std::vector<EndpointType> read_endpoint_list(libtorrent::bdecode_node const& n)
|
||||
{
|
||||
using namespace libtorrent;
|
||||
if (n.type() != bdecode_node::list_t) return;
|
||||
std::vector<EndpointType> 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<EndpointType>(in));
|
||||
ret.push_back(read_v4_endpoint<EndpointType>(in));
|
||||
#if TORRENT_USE_IPV6
|
||||
else if (e.string_length() == 18)
|
||||
epl.push_back(read_v6_endpoint<EndpointType>(in));
|
||||
ret.push_back(read_v6_endpoint<EndpointType>(in));
|
||||
#endif
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
template <class EndpointType>
|
||||
void read_endpoint_list(libtorrent::entry const* n, std::vector<EndpointType>& 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<EndpointType>(in));
|
||||
#if TORRENT_USE_IPV6
|
||||
else if (p.size() == 18)
|
||||
epl.push_back(detail::read_v6_endpoint<EndpointType>(in));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -75,10 +75,10 @@ namespace
|
|||
#endif
|
||||
|
||||
if (bdecode_node const nodes = e.dict_find_list("nodes"))
|
||||
detail::read_endpoint_list<udp::endpoint>(nodes, ret.nodes);
|
||||
ret.nodes = detail::read_endpoint_list<udp::endpoint>(nodes);
|
||||
#if TORRENT_USE_IPV6
|
||||
if (bdecode_node const nodes = e.dict_find_list("nodes6"))
|
||||
detail::read_endpoint_list<udp::endpoint>(nodes, ret.nodes6);
|
||||
ret.nodes6 = detail::read_endpoint_list<udp::endpoint>(nodes);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -96,7 +96,7 @@ void get_peers_observer::reply(msg const& m)
|
|||
else
|
||||
{
|
||||
// assume it's uTorrent/libtorrent format
|
||||
read_endpoint_list<tcp::endpoint>(n, peer_list);
|
||||
peer_list = read_endpoint_list<tcp::endpoint>(n);
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
auto logger = get_observer();
|
||||
if (logger != nullptr && logger->should_log(dht_logger::traversal))
|
||||
|
|
|
@ -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<std::string> 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<std::string> 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<udp::endpoint>(in);
|
||||
udp::endpoint ep4 = read_v4_endpoint<udp::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<std::string> 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<std::string> 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<udp::endpoint>(in);
|
||||
udp::endpoint ep6 = read_v6_endpoint<udp::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<udp::endpoint> list;
|
||||
read_endpoint_list<udp::endpoint>(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<udp::endpoint>(&e2, list);
|
||||
std::vector<udp::endpoint> list = read_endpoint_list<udp::endpoint>(e);
|
||||
|
||||
#if TORRENT_USE_IPV6
|
||||
TEST_EQUAL(list.size(), 2);
|
||||
|
|
Loading…
Reference in New Issue