diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index e24ec6e40..f4ae1e3f6 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -403,7 +403,7 @@ namespace libtorrent public: // constructs the default_storage based on the give file_storage (fs). // ``mapped`` is an optional argument (it may be NULL). If non-NULL it - // represents the file mappsing that have been made to the torrent before + // represents the file mapping that have been made to the torrent before // adding it. That's where files are supposed to be saved and looked for // on disk. ``save_path`` is the root save folder for this torrent. // ``file_pool`` is the cache of file handles that the storage will use. diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index ecdd6c745..82c7579f1 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -291,6 +291,8 @@ namespace libtorrent { namespace dht } if (size <= 20 || *buf != 'd' || buf[size-1] != 'e') return false; + // remove this line/check once the DHT supports IPv6 + if (!ep.address().is_v4()) return false; m_counters.inc_stats_counter(counters::dht_bytes_in, size); // account for IP and UDP overhead diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index d1c958806..0f0c96471 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -47,6 +47,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alert_types.hpp" // for dht_routing_bucket #include "libtorrent/socket_io.hpp" // for print_endpoint #include "libtorrent/invariant_check.hpp" +#include "libtorrent/address.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" @@ -68,13 +69,24 @@ size_t hash_value(libtorrent::address_v4::bytes_type ip) namespace libtorrent { namespace dht { - -template -void erase_one(T& container, K const& key) +namespace { - typename T::iterator i = container.find(key); - TORRENT_ASSERT(i != container.end()); - container.erase(i); + template + void erase_one(T& container, K const& key) + { + typename T::iterator i = container.find(key); + TORRENT_ASSERT(i != container.end()); + container.erase(i); + } + + bool verify_node_address(dht_settings const& settings + , node_id const& id, address const& addr) + { + // only when the node_id pass the verification, add it to routing table. + if (settings.enforce_node_id && !verify_id(id, addr)) return false; + + return true; + } } routing_table::routing_table(node_id const& id, int bucket_size @@ -1125,8 +1137,7 @@ void routing_table::add_router_node(udp::endpoint router) // pinged == false) void routing_table::heard_about(node_id const& id, udp::endpoint const& ep) { - // only when the node_id pass the verification, add it to routing table. - if (m_settings.enforce_node_id && !verify_id(id, ep.address())) return; + if (!verify_node_address(m_settings, id, ep.address())) return; add_node(node_entry(id, ep)); } @@ -1137,6 +1148,7 @@ void routing_table::heard_about(node_id const& id, udp::endpoint const& ep) // id) bool routing_table::node_seen(node_id const& id, udp::endpoint ep, int rtt) { + if (!verify_node_address(m_settings, id, ep.address())) return false; return add_node(node_entry(id, ep, rtt, true)); } diff --git a/test/test_dht.cpp b/test/test_dht.cpp index e6f8f1ecb..85f71c08a 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -2558,5 +2558,43 @@ TORRENT_TEST(node_id_bucket_distribution) expected /= 2; } } + +TORRENT_TEST(dht_verify_node_address) +{ + obs observer; + // initial setup taken from dht test above + dht_settings s; + s.extended_routing_table = false; + node_id id = to_hash("3123456789abcdef01232456789abcdef0123456"); + const int bucket_size = 10; + dht::routing_table table(id, bucket_size, s, &observer); + std::vector nodes; + TEST_EQUAL(table.size().get<0>(), 0); + + node_id tmp = id; + node_id diff = to_hash("15764f7459456a9453f8719b09547c11d5f34061"); + + add_and_replace(tmp, diff); + table.node_seen(tmp, udp::endpoint(address::from_string("4.4.4.4"), 4), 10); + table.find_node(id, nodes, 0, 10); + TEST_EQUAL(table.size().get<0>(), 1); + TEST_EQUAL(nodes.size(), 1); + + // incorrect data, wrong id + table.node_seen(to_hash("0123456789abcdef01232456789abcdef0123456") + , udp::endpoint(address::from_string("4.4.4.4"), 4), 10); + table.find_node(id, nodes, 0, 10); + + TEST_EQUAL(table.size().get<0>(), 1); + TEST_EQUAL(nodes.size(), 1); + + // incorrect data, wrong IP + table.node_seen(tmp + , udp::endpoint(address::from_string("4.4.4.6"), 4), 10); + table.find_node(id, nodes, 0, 10); + + TEST_EQUAL(table.size().get<0>(), 1); + TEST_EQUAL(nodes.size(), 1); +} #endif