use asio::protcol instead of an enum/bool

This commit is contained in:
Steven Siloti 2016-02-11 19:56:52 -08:00
parent d3ecc4c906
commit 2d9a6b76f1
13 changed files with 97 additions and 77 deletions

View File

@ -61,8 +61,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
enum address_type { ipv4, ipv6, num_address_type };
#if defined TORRENT_BUILD_SIMULATOR
typedef sim::asio::ip::address address;
typedef sim::asio::ip::address_v4 address_v4;

View File

@ -604,7 +604,7 @@ namespace libtorrent
// implements dht_observer
virtual void set_external_address(address const& ip
, address const& source) TORRENT_OVERRIDE;
virtual address external_address(address_type at) TORRENT_OVERRIDE;
virtual address external_address(udp proto) TORRENT_OVERRIDE;
virtual void get_peers(sha1_hash const& ih) TORRENT_OVERRIDE;
virtual void announce(sha1_hash const& ih, address const& addr, int port) TORRENT_OVERRIDE;
virtual void outgoing_get_peers(sha1_hash const& target

View File

@ -68,7 +68,7 @@ namespace libtorrent { namespace dht
{
virtual void set_external_address(address const& addr
, address const& source) = 0;
virtual address external_address(address_type at) = 0;
virtual address external_address(udp proto) = 0;
virtual void get_peers(sha1_hash const& ih) = 0;
virtual void outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep) = 0;

View File

@ -73,9 +73,6 @@ namespace libtorrent { namespace dht
struct traversal_algorithm;
struct dht_observer;
extern char const* address_type_names[num_address_type];
extern char const* address_type_keys[num_address_type];
void TORRENT_EXTRA_EXPORT write_nodes_entry(entry& r, nodes_t const& nodes);
struct null_type {};
@ -102,7 +99,7 @@ protected:
class TORRENT_EXTRA_EXPORT node : boost::noncopyable
{
public:
node(address_type at, udp_socket_interface* sock
node(udp proto, udp_socket_interface* sock
, libtorrent::dht_settings const& settings, node_id nid
, dht_observer* observer, counters& cnt
, std::map<std::string, node*> const& nodes
@ -209,18 +206,18 @@ public:
dht_observer* observer() const { return m_observer; }
address_type native_address_type() { return m_address_type; }
char const* native_address_name() { return address_type_names[m_address_type]; }
char const* native_nodes_key() { return address_type_keys[m_address_type]; }
udp protocol() { return m_protocol.protocol; }
char const* protocol_family_name() { return m_protocol.family_name; }
char const* protocol_nodes_key() { return m_protocol.nodes_key; }
bool native_address(udp::endpoint ep) const
{ return native_address(ep.address()); }
{ return ep.protocol().family() == m_protocol.protocol.family(); }
bool native_address(tcp::endpoint ep) const
{ return native_address(ep.address()); }
{ return ep.protocol().family() == m_protocol.protocol.family(); }
bool native_address(address addr) const
{
return (addr.is_v4() && m_address_type == ipv4)
|| (addr.is_v6() && m_address_type == ipv6);
return (addr.is_v4() && m_protocol.protocol == m_protocol.protocol.v4())
|| (addr.is_v6() && m_protocol.protocol == m_protocol.protocol.v6());
}
private:
@ -254,9 +251,18 @@ public:
std::map<std::string, node*> const& m_nodes;
private:
struct protocol_descriptor
{
udp protocol;
char const* family_name;
char const* nodes_key;
};
static protocol_descriptor const& map_protocol_to_descriptor(udp protocol);
dht_observer* m_observer;
address_type m_address_type;
protocol_descriptor const& m_protocol;
time_point m_last_tracker_tick;

View File

@ -132,7 +132,7 @@ public:
// Perhaps replacement nodes should be in a separate vector.
typedef std::vector<routing_table_node> table_t;
routing_table(node_id const& id, address_type at
routing_table(node_id const& id, udp proto
, int bucket_size
, dht_settings const& settings
, dht_logger* log);
@ -246,10 +246,13 @@ public:
bool native_address(address addr) const
{
return (addr.is_v4() && m_address_type == ipv4)
|| (addr.is_v6() && m_address_type == ipv6);
return (addr.is_v4() && m_protocol == udp::v4())
|| (addr.is_v6() && m_protocol == udp::v6());
}
bool native_endpoint(udp::endpoint ep) const
{ return ep.protocol() == m_protocol; }
private:
#ifndef TORRENT_DISABLE_LOGGING
@ -277,7 +280,7 @@ private:
table_t m_buckets;
node_id m_id; // our own node id
address_type m_address_type; // address type to be stored
udp m_protocol; // protocol this table is for
// the last seen depth (i.e. levels in the routing table)
// it's mutable because it's updated by depth(), which is const

View File

@ -86,12 +86,12 @@ struct dht_node final : lt::dht::udp_socket_interface
: m_io_service(sim, (flags & dht_network::bind_ipv6) ? addr6_from_int(idx) : addr_from_int(idx))
#if LIBSIMULATOR_USE_MOVE
, m_socket(m_io_service)
, m_dht((flags & dht_network::bind_ipv6) ? ipv6 : ipv4
, m_dht((flags & dht_network::bind_ipv6) ? udp::v6() : udp::v4()
, this, sett, id_from_addr(m_io_service.get_ips().front())
, nullptr, cnt, std::map<std::string, lt::dht::node*>())
#else
, m_socket(new asio::ip::udp::socket(m_io_service))
, m_dht(new lt::dht::node((flags & dht_network::bind_ipv6) ? ipv6 : ipv4
, m_dht(new lt::dht::node((flags & dht_network::bind_ipv6) ? udp::v6() : udp::v4()
, this, sett, id_from_addr(m_io_service.get_ips().front())
, nullptr, cnt, std::map<std::string, lt::dht::node*>()))
#endif
@ -122,7 +122,7 @@ struct dht_node final : lt::dht::udp_socket_interface
// reserving space in the vector before emplacing any nodes).
dht_node(dht_node&& n) noexcept
: m_socket(std::move(n.m_socket))
, m_dht(n.m_ipv6 ? ipv6 : ipv4, this, n.m_dht.settings(), n.m_dht.nid()
, m_dht(n.m_ipv6 ? udp::v6() : udp::v4(), this, n.m_dht.settings(), n.m_dht.nid()
, n.m_dht.observer(), n.m_dht.stats_counters()
, std::map<std::string, lt::dht::node*>())
{

View File

@ -97,10 +97,10 @@ namespace libtorrent { namespace dht
, dht_storage_constructor_type storage_constructor
, entry const& state)
: m_counters(cnt)
, m_dht(ipv4, this, settings, extract_node_id(state, "node-id")
, m_dht(udp::v4(), this, settings, extract_node_id(state, "node-id")
, observer, cnt, m_nodes, storage_constructor)
#if TORRENT_USE_IPV6
, m_dht6(ipv6, this, settings, extract_node_id(state, "node-id6")
, m_dht6(udp::v6(), this, settings, extract_node_id(state, "node-id6")
, observer, cnt, m_nodes, storage_constructor)
#endif
, m_send_fun(send_fun)
@ -120,9 +120,9 @@ namespace libtorrent { namespace dht
m_blocker.set_block_timer(m_settings.block_timeout);
m_blocker.set_rate_limit(m_settings.block_ratelimit);
m_nodes.insert(std::make_pair(m_dht.native_address_name(), &m_dht));
m_nodes.insert(std::make_pair(m_dht.protocol_family_name(), &m_dht));
#if TORRENT_USE_IPV6
m_nodes.insert(std::make_pair(m_dht6.native_address_name(), &m_dht6));
m_nodes.insert(std::make_pair(m_dht6.protocol_family_name(), &m_dht6));
#endif
#ifndef TORRENT_DISABLE_LOGGING
@ -226,7 +226,7 @@ namespace libtorrent { namespace dht
time_duration d = n.connection_timeout();
error_code ec;
#if TORRENT_USE_IPV6
deadline_timer& timer = n.native_address_type() == ipv4 ? m_connection_timer : m_connection_timer6;
deadline_timer& timer = n.protocol() == udp::v4() ? m_connection_timer : m_connection_timer6;
#else
deadline_timer& timer = m_connection_timer;
#endif

View File

@ -69,17 +69,14 @@ namespace libtorrent { namespace dht
using detail::write_endpoint;
char const* address_type_names[num_address_type] = { "n4", "n6" };
char const* address_type_keys[num_address_type] = { "nodes", "nodes6" };
namespace {
void nop() {}
node_id calculate_node_id(node_id const& nid, dht_observer* observer, address_type at)
node_id calculate_node_id(node_id const& nid, dht_observer* observer, udp protocol)
{
address external_address;
if (observer) external_address = observer->external_address(at);
if (observer) external_address = observer->external_address(protocol);
// if we don't have an observer, don't pretend that external_address is valid
// generating an ID based on 0.0.0.0 would be terrible. random is better
@ -96,19 +93,19 @@ node_id calculate_node_id(node_id const& nid, dht_observer* observer, address_ty
} // anonymous namespace
node::node(address_type at, udp_socket_interface* sock
node::node(udp proto, udp_socket_interface* sock
, dht_settings const& settings, node_id nid
, dht_observer* observer
, struct counters& cnt
, std::map<std::string, node*> const& nodes
, dht_storage_constructor_type storage_constructor)
: m_settings(settings)
, m_id(calculate_node_id(nid, observer, at))
, m_table(m_id, at, 8, settings, observer)
, m_id(calculate_node_id(nid, observer, proto))
, m_table(m_id, proto, 8, settings, observer)
, m_rpc(m_id, m_settings, m_table, sock, observer)
, m_nodes(nodes)
, m_observer(observer)
, m_address_type(at)
, m_protocol(map_protocol_to_descriptor(proto))
, m_last_tracker_tick(aux::time_now())
, m_last_self_refresh(min_time())
, m_sock(sock)
@ -132,7 +129,7 @@ void node::update_node_id()
// it's possible that our external address hasn't actually changed. If our
// current ID is still valid, don't do anything.
if (verify_id(m_id, m_observer->external_address(m_address_type)))
if (verify_id(m_id, m_observer->external_address(protocol())))
return;
#ifndef TORRENT_DISABLE_LOGGING
@ -140,7 +137,7 @@ void node::update_node_id()
, "updating node ID (because external IP address changed)");
#endif
m_id = generate_id(m_observer->external_address(m_address_type));
m_id = generate_id(m_observer->external_address(protocol()));
m_table.update_node_id(m_id);
}
@ -610,9 +607,9 @@ struct ping_observer : observer
// look for nodes
#if TORRENT_USE_IPV6
address_type at = algorithm()->get_node().native_address_type();
udp protocol = algorithm()->get_node().protocol();
#endif
char const* nodes_key = algorithm()->get_node().native_nodes_key();
char const* nodes_key = algorithm()->get_node().protocol_nodes_key();
bdecode_node n = r.dict_find_string(nodes_key);
if (n)
{
@ -626,7 +623,7 @@ struct ping_observer : observer
nodes += 20;
udp::endpoint ep;
#if TORRENT_USE_IPV6
if (at == ipv6)
if (protocol == udp::v6())
ep = detail::read_v6_endpoint<udp::endpoint>(nodes);
else
#endif
@ -1201,7 +1198,7 @@ void node::write_nodes_entries(sha1_hash const& info_hash
{
nodes_t n;
m_table.find_node(info_hash, n, 0);
write_nodes_entry(r[native_nodes_key()], n);
write_nodes_entry(r[protocol_nodes_key()], n);
return;
}
@ -1221,8 +1218,24 @@ void node::write_nodes_entries(sha1_hash const& info_hash
continue;
nodes_t n;
wanted_node->second->m_table.find_node(info_hash, n, 0);
write_nodes_entry(r[wanted_node->second->native_nodes_key()], n);
write_nodes_entry(r[wanted_node->second->protocol_nodes_key()], n);
}
}
node::protocol_descriptor const& node::map_protocol_to_descriptor(udp protocol)
{
static protocol_descriptor descriptors[] =
{ {udp::v4(), "n4", "nodes"}
, {udp::v6(), "n6", "nodes6"} };
for (int i = 0; i < sizeof(descriptors) / sizeof(protocol_descriptor); ++i)
{
if (descriptors[i].protocol == protocol)
return descriptors[i];
}
TORRENT_ASSERT(false);
throw std::out_of_range("unknown protocol");
}
} } // namespace libtorrent::dht

View File

@ -124,7 +124,7 @@ void ip_set::erase(address addr)
erase_one(m_ip4s, addr.to_v4().to_bytes());
}
routing_table::routing_table(node_id const& id, address_type at, int bucket_size
routing_table::routing_table(node_id const& id, udp proto, int bucket_size
, dht_settings const& settings
, dht_logger* log)
:
@ -133,7 +133,7 @@ routing_table::routing_table(node_id const& id, address_type at, int bucket_size
#endif
m_settings(settings)
, m_id(id)
, m_address_type(at)
, m_protocol(proto)
, m_depth(0)
, m_last_self_refresh(min_time())
, m_bucket_size(bucket_size)
@ -611,7 +611,7 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e)
#endif
// don't add if the address isn't the right type
if (!native_address(e.addr()))
if (!native_endpoint(e.ep()))
return failed_to_add;
// if we already have this (IP,port), don't do anything

View File

@ -287,7 +287,7 @@ bool rpc_manager::incoming(msg const& m, node_id* id)
if (!o)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_table.native_address(m.addr.address()))
if (m_table.native_endpoint(m.addr))
{
m_log->log(dht_logger::rpc_manager, "reply with unknown transaction id size: %d from %s"
, int(transaction_id.size()), print_endpoint(m.addr).c_str());
@ -470,7 +470,7 @@ bool rpc_manager::invoke(entry& e, udp::endpoint target_addr
node& n = o->algorithm()->get_node();
if (!n.native_address(o->target_addr()))
{
a["want"].list().push_back(entry(n.native_address_name()));
a["want"].list().push_back(entry(n.protocol_family_name()));
}
o->set_target(target_addr);

View File

@ -617,9 +617,9 @@ void traversal_observer::reply(msg const& m)
// look for nodes
#if TORRENT_USE_IPV6
address_type at = algorithm()->get_node().native_address_type();
udp protocol = algorithm()->get_node().protocol();
#endif
char const* nodes_key = algorithm()->get_node().native_nodes_key();
char const* nodes_key = algorithm()->get_node().protocol_nodes_key();
bdecode_node n = r.dict_find_string(nodes_key);
if (n)
{
@ -633,7 +633,7 @@ void traversal_observer::reply(msg const& m)
nodes += 20;
udp::endpoint ep;
#if TORRENT_USE_IPV6
if (at == ipv6)
if (protocol == udp::v6())
ep = read_v6_endpoint<udp::endpoint>(nodes);
else
#endif

View File

@ -6779,15 +6779,15 @@ namespace aux {
set_external_address(ip, source_dht, source);
}
address session_impl::external_address(address_type at)
address session_impl::external_address(udp proto)
{
#if !TORRENT_USE_IPV6
TORRENT_UNUSED(at);
TORRENT_UNUSED(proto);
#endif
address addr;
#if TORRENT_USE_IPV6
if (at == ipv6)
if (proto == udp::v6())
addr = address_v6();
else
#endif

View File

@ -479,7 +479,7 @@ struct obs : dht::dht_observer
, address const& source) TORRENT_OVERRIDE
{}
virtual address external_address(address_type at) TORRENT_OVERRIDE
virtual address external_address(udp proto) TORRENT_OVERRIDE
{
return address_v4::from_string("236.0.0.1");
}
@ -523,8 +523,8 @@ void do_test_dht(address(&rand_addr)())
counters cnt;
udp::endpoint source(rand_addr(), 20);
std::map<std::string, node*> nodes;
dht::node node(source.protocol() == udp::v4() ? ipv4 : ipv6
, &s, sett, node_id(0), &observer, cnt, nodes);
dht::node node(source.protocol(), &s, sett
, node_id(0), &observer, cnt, nodes);
// DHT should be running on port 48199 now
bdecode_node response;
@ -1265,7 +1265,7 @@ void do_test_dht(address(&rand_addr)())
// s.restrict_routing_ips = false;
node_id id = to_hash("3123456789abcdef01232456789abcdef0123456");
const int bucket_size = 10;
dht::routing_table table(id, source.protocol() == udp::v4() ? ipv4 : ipv6, bucket_size, s, &observer);
dht::routing_table table(id, source.protocol(), bucket_size, s, &observer);
std::vector<node_entry> nodes;
TEST_EQUAL(table.size().get<0>(), 0);
@ -1515,7 +1515,7 @@ void do_test_dht(address(&rand_addr)())
g_sent_packets.clear();
do
{
dht::node node(ipv4, &s, sett, (node_id::min)(), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
std::vector<udp::endpoint> nodesv;
@ -1587,7 +1587,7 @@ void do_test_dht(address(&rand_addr)())
do
{
dht::node_id target = to_hash("1234876923549721020394873245098347598635");
dht::node node(ipv4, &s, sett, (node_id::min)(), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node);
@ -1682,7 +1682,7 @@ void do_test_dht(address(&rand_addr)())
g_sent_packets.clear();
do
{
dht::node node(ipv4, &s, sett, (node_id::min)(), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node);
@ -1728,7 +1728,7 @@ void do_test_dht(address(&rand_addr)())
g_sent_packets.clear();
do
{
dht::node node(ipv4, &s, sett, (node_id::min)(), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes);
udp::endpoint initial_node(address_v4::from_string("4.4.4.4"), 1234);
node.m_table.add_node(initial_node);
@ -1815,7 +1815,7 @@ void do_test_dht(address(&rand_addr)())
// set the branching factor to k to make this a little easier
int old_branching = sett.search_branching;
sett.search_branching = 8;
dht::node node(ipv4, &s, sett, (node_id::min)(), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes);
enum { num_test_nodes = 8 };
node_entry nodes[num_test_nodes] =
{ node_entry(items[0].target, udp::endpoint(address_v4::from_string("1.1.1.1"), 1231))
@ -1915,7 +1915,7 @@ void do_test_dht(address(&rand_addr)())
// set the branching factor to k to make this a little easier
int old_branching = sett.search_branching;
sett.search_branching = 8;
dht::node node(ipv4, &s, sett, (node_id::min)(), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes);
enum { num_test_nodes = 8 };
node_entry nodes[num_test_nodes] =
{ node_entry(items[0].target, udp::endpoint(address_v4::from_string("1.1.1.1"), 1231))
@ -2017,7 +2017,7 @@ void do_test_dht(address(&rand_addr)())
// set the branching factor to k to make this a little easier
int old_branching = sett.search_branching;
sett.search_branching = 8;
dht::node node(ipv4, &s, sett, (node_id::min)(), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, (node_id::min)(), &observer, cnt, nodes);
sha1_hash target = hasher(public_key, item_pk_len).final();
enum { num_test_nodes = 9 }; // we need K + 1 nodes to create the failing sequence
node_entry nodes[num_test_nodes] =
@ -2113,8 +2113,8 @@ TORRENT_TEST(dht_dual_stack)
obs observer;
counters cnt;
std::map<std::string, node*> nodes;
dht::node node4(ipv4, &s, sett, node_id(0), &observer, cnt, nodes);
dht::node node6(ipv6, &s, sett, node_id(0), &observer, cnt, nodes);
dht::node node4(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes);
dht::node node6(udp::v6(), &s, sett, node_id(0), &observer, cnt, nodes);
nodes.insert(std::make_pair("n4", &node4));
nodes.insert(std::make_pair("n6", &node6));
@ -2419,7 +2419,7 @@ TORRENT_TEST(routing_table_uniform)
node_id id = to_hash("1234876923549721020394873245098347598635");
node_id diff = to_hash("15764f7459456a9453f8719b09547c11d5f34061");
routing_table tbl(id, ipv4, 8, sett, &observer);
routing_table tbl(id, udp::v4(), 8, sett, &observer);
// insert 256 nodes evenly distributed across the ID space.
// we expect to fill the top 5 buckets
@ -2462,7 +2462,7 @@ TORRENT_TEST(routing_table_balance)
sett.extended_routing_table = false;
node_id id = to_hash("1234876923549721020394873245098347598635");
routing_table tbl(id, ipv4, 8, sett, &observer);
routing_table tbl(id, udp::v4(), 8, sett, &observer);
// insert nodes in the routing table that will force it to split
// and make sure we don't end up with a table completely out of balance
@ -2494,7 +2494,7 @@ TORRENT_TEST(routing_table_extended)
for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i);
std::random_shuffle(node_id_prefix.begin(), node_id_prefix.end());
routing_table tbl(id, ipv4, 8, sett, &observer);
routing_table tbl(id, udp::v4(), 8, sett, &observer);
for (int i = 0; i < 256; ++i)
{
add_and_replace(id, diff);
@ -2527,7 +2527,7 @@ TORRENT_TEST(routing_table_set_id)
node_id_prefix.reserve(256);
for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i);
std::random_shuffle(node_id_prefix.begin(), node_id_prefix.end());
routing_table tbl(id, ipv4, 8, sett, &observer);
routing_table tbl(id, udp::v4(), 8, sett, &observer);
for (int i = 0; i < 256; ++i)
{
id[0] = node_id_prefix[i];
@ -2573,7 +2573,7 @@ TORRENT_TEST(read_only_node)
counters cnt;
std::map<std::string, node*> nodes;
dht::node node(ipv4, &s, sett, node_id(0), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes);
udp::endpoint source(address::from_string("10.0.0.1"), 20);
bdecode_node response;
msg_args args;
@ -2661,7 +2661,7 @@ TORRENT_TEST(invalid_error_msg)
counters cnt;
std::map<std::string, node*> nodes;
dht::node node(ipv4, &s, sett, node_id(0), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes);
udp::endpoint source(address::from_string("10.0.0.1"), 20);
entry e;
@ -2699,9 +2699,9 @@ TORRENT_TEST(rpc_invalid_error_msg)
counters cnt;
std::map<std::string, node*> nodes;
dht::routing_table table(node_id(), ipv4, 8, sett, &observer);
dht::routing_table table(node_id(), udp::v4(), 8, sett, &observer);
dht::rpc_manager rpc(node_id(), sett, table, &s, &observer);
dht::node node(ipv4, &s, sett, node_id(0), &observer, cnt, nodes);
dht::node node(udp::v4(), &s, sett, node_id(0), &observer, cnt, nodes);
udp::endpoint source(address::from_string("10.0.0.1"), 20);
@ -2788,7 +2788,7 @@ TORRENT_TEST(dht_verify_node_address)
s.extended_routing_table = false;
node_id id = to_hash("3123456789abcdef01232456789abcdef0123456");
const int bucket_size = 10;
dht::routing_table table(id, ipv4, bucket_size, s, &observer);
dht::routing_table table(id, udp::v4(), bucket_size, s, &observer);
std::vector<node_entry> nodes;
TEST_EQUAL(table.size().get<0>(), 0);