From d954ae1b18eff314121045878e5f53ec2298f2fc Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 2 Jan 2016 20:03:18 -0500 Subject: [PATCH] simplify dht routing table add --- src/kademlia/routing_table.cpp | 44 +++++++++++++--------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index c044a61cb..c14940b62 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -683,40 +683,30 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e) if (m_settings.restrict_routing_ips) { // don't allow multiple entries from IPs very close to each other + // TODO: 3 the call to compare_ip_cidr here is expensive. peel off some + // layers of abstraction here to make it quicker. Look at xoring and using _builtin_ctz() j = std::find_if(b.begin(), b.end(), boost::bind(&compare_ip_cidr, _1, e)); - if (j != b.end()) + if (j == b.end()) { - // we already have a node in this bucket with an IP very - // close to this one. We know that it's not the same, because - // it claims a different node-ID. Ignore this to avoid attacks -#ifndef TORRENT_DISABLE_LOGGING - char hex_id1[41]; - to_hex(reinterpret_cast(&e.id[0]), 20, hex_id1); - char hex_id2[41]; - to_hex(reinterpret_cast(&j->id[0]), 20, hex_id2); - m_log->log(dht_logger::routing_table, "ignoring node: %s %s existing node: %s %s" - , hex_id1, print_address(e.addr()).c_str() - , hex_id2, print_address(j->addr()).c_str()); -#endif - return failed_to_add; + j = std::find_if(rb.begin(), rb.end(), boost::bind(&compare_ip_cidr, _1, e)); + if (j == rb.end()) goto ip_ok; } - j = std::find_if(rb.begin(), rb.end(), boost::bind(&compare_ip_cidr, _1, e)); - if (j != rb.end()) - { - // same thing but for the replacement bucket + // we already have a node in this bucket with an IP very + // close to this one. We know that it's not the same, because + // it claims a different node-ID. Ignore this to avoid attacks #ifndef TORRENT_DISABLE_LOGGING - char hex_id1[41]; - to_hex(reinterpret_cast(&e.id[0]), 20, hex_id1); - char hex_id2[41]; - to_hex(reinterpret_cast(&j->id[0]), 20, hex_id2); - m_log->log(dht_logger::routing_table, "ignoring (replacement) node: %s %s existing node: %s %s" - , hex_id1, print_address(e.addr()).c_str() - , hex_id2, print_address(j->addr()).c_str()); + char hex_id1[41]; + to_hex(e.id.data(), 20, hex_id1); + char hex_id2[41]; + to_hex(j->id.data(), 20, hex_id2); + m_log->log(dht_logger::routing_table, "ignoring node: %s %s existing node: %s %s" + , hex_id1, print_address(e.addr()).c_str() + , hex_id2, print_address(j->addr()).c_str()); #endif - return failed_to_add; - } + return failed_to_add; } +ip_ok: // can we split the bucket? // only nodes that haven't failed can split the bucket, and we can only