Avoid pass IPv6 address to current implementation of DHT while adding a node

This commit is contained in:
Alden Torres 2016-01-09 16:20:27 -05:00
parent 641b6e51f8
commit 0f442f59f3
4 changed files with 61 additions and 9 deletions

View File

@ -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.

View File

@ -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

View File

@ -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 <typename T, typename K>
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 <typename T, typename K>
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));
}

View File

@ -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<node_entry> 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