2006-08-01 17:27:08 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2006-2016, Arvid Norberg & Daniel Wallin
|
2006-08-01 17:27:08 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
#include "libtorrent/time.hpp" // for total_seconds
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <libtorrent/kademlia/traversal_algorithm.hpp>
|
|
|
|
#include <libtorrent/kademlia/routing_table.hpp>
|
|
|
|
#include <libtorrent/kademlia/rpc_manager.hpp>
|
2008-09-20 19:42:25 +02:00
|
|
|
#include <libtorrent/kademlia/node.hpp>
|
2015-05-10 06:54:02 +02:00
|
|
|
#include <libtorrent/kademlia/dht_observer.hpp> // for dht_logger
|
2008-09-20 19:42:25 +02:00
|
|
|
#include <libtorrent/session_status.hpp>
|
2013-12-15 00:25:38 +01:00
|
|
|
#include <libtorrent/socket_io.hpp> // for read_*_endpoint
|
2015-01-18 05:40:01 +01:00
|
|
|
#include <libtorrent/alert_types.hpp> // for dht_lookup
|
2015-09-18 06:23:45 +02:00
|
|
|
#include <libtorrent/aux_/time.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2016-05-23 14:15:39 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-06-04 16:01:43 +02:00
|
|
|
#include <libtorrent/hex.hpp> // to_hex
|
2016-05-23 14:15:39 +02:00
|
|
|
#endif
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace std::placeholders;
|
2014-05-25 07:10:30 +02:00
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
using detail::read_v4_endpoint;
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
using detail::read_v6_endpoint;
|
|
|
|
#endif
|
|
|
|
|
2014-03-03 06:09:53 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2014-05-25 02:13:17 +02:00
|
|
|
template <class It, class Cmp>
|
|
|
|
bool is_sorted(It b, It e, Cmp cmp)
|
|
|
|
{
|
|
|
|
if (b == e) return true;
|
|
|
|
|
|
|
|
typename std::iterator_traits<It>::value_type v = *b;
|
|
|
|
++b;
|
|
|
|
while (b != e)
|
|
|
|
{
|
|
|
|
if (cmp(*b, v)) return false;
|
|
|
|
v = *b;
|
|
|
|
++b;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-03 06:09:53 +01:00
|
|
|
#endif
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
observer_ptr traversal_algorithm::new_observer(void* ptr
|
|
|
|
, udp::endpoint const& ep, node_id const& id)
|
|
|
|
{
|
|
|
|
observer_ptr o(new (ptr) null_observer(boost::intrusive_ptr<traversal_algorithm>(this), ep, id));
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2010-11-05 20:06:50 +01:00
|
|
|
o->m_in_constructor = false;
|
|
|
|
#endif
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2010-12-12 21:36:42 +01:00
|
|
|
traversal_algorithm::traversal_algorithm(
|
2015-05-09 21:00:22 +02:00
|
|
|
node& dht_node
|
2010-12-12 21:36:42 +01:00
|
|
|
, node_id target)
|
2015-05-09 21:00:22 +02:00
|
|
|
: m_node(dht_node)
|
2010-12-12 21:36:42 +01:00
|
|
|
, m_target(target)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_ref_count(0)
|
2010-12-12 21:36:42 +01:00
|
|
|
, m_invoke_count(0)
|
|
|
|
, m_branch_factor(3)
|
|
|
|
, m_responses(0)
|
|
|
|
, m_timeouts(0)
|
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
|
|
|
char hex_target[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(reinterpret_cast<char const*>(&target[0]), 20, hex_target);
|
2015-05-19 02:48:04 +02:00
|
|
|
get_node().observer()->log(dht_logger::traversal, "[%p] NEW target: %s k: %d"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), hex_target, int(m_node.m_table.bucket_size()));
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2010-12-12 21:36:42 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-03-03 06:09:53 +01:00
|
|
|
void traversal_algorithm::resort_results()
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
std::sort(m_results.begin(), m_results.end()
|
|
|
|
, [this](observer_ptr const& lhs, observer_ptr const& rhs)
|
|
|
|
{ return compare_ref(lhs->id(), rhs->id(), m_target); });
|
2014-03-03 06:09:53 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsigned char flags)
|
|
|
|
{
|
2010-11-05 20:06:50 +01:00
|
|
|
TORRENT_ASSERT(m_node.m_rpc.allocation_size() >= sizeof(find_data_observer));
|
2010-11-06 08:12:57 +01:00
|
|
|
void* ptr = m_node.m_rpc.allocate_observer();
|
2010-11-05 20:06:50 +01:00
|
|
|
if (ptr == 0)
|
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
|
|
|
get_node().observer()->log(dht_logger::traversal, "[%p] failed to allocate memory or observer. aborting!"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this));
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2010-11-05 20:06:50 +01:00
|
|
|
#endif
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
observer_ptr o = new_observer(ptr, addr, id);
|
|
|
|
if (id.is_all_zeros())
|
2008-03-24 03:19:47 +01:00
|
|
|
{
|
2011-05-26 19:04:53 +02:00
|
|
|
o->set_id(generate_random_id());
|
2010-11-05 20:06:50 +01:00
|
|
|
o->flags |= observer::flag_no_id;
|
2008-03-24 03:19:47 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
o->flags |= flags;
|
|
|
|
|
2014-05-25 07:10:30 +02:00
|
|
|
TORRENT_ASSERT(libtorrent::dht::is_sorted(m_results.begin(), m_results.end()
|
2016-05-25 06:31:52 +02:00
|
|
|
, [this](observer_ptr const& lhs, observer_ptr const& rhs)
|
|
|
|
{ return compare_ref(lhs->id(), rhs->id(), m_target); }));
|
2014-03-03 06:09:53 +01:00
|
|
|
|
2015-08-06 08:30:06 +02:00
|
|
|
std::vector<observer_ptr>::iterator iter = std::lower_bound(
|
2016-05-25 06:31:52 +02:00
|
|
|
m_results.begin(), m_results.end(), o
|
|
|
|
, [this](observer_ptr const& lhs, observer_ptr const& rhs)
|
|
|
|
{ return compare_ref(lhs->id(), rhs->id(), m_target); });
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-08-06 08:30:06 +02:00
|
|
|
if (iter == m_results.end() || (*iter)->id() != id)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2013-11-23 12:06:35 +01:00
|
|
|
if (m_node.settings().restrict_search_ips
|
|
|
|
&& !(flags & observer::flag_initial))
|
2011-01-08 09:54:51 +01:00
|
|
|
{
|
2015-11-14 06:08:57 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (o->target_addr().is_v6())
|
2011-01-08 09:54:51 +01:00
|
|
|
{
|
2015-11-14 06:08:57 +01:00
|
|
|
address_v6::bytes_type addr_bytes = o->target_addr().to_v6().to_bytes();
|
|
|
|
address_v6::bytes_type::const_iterator prefix_it = addr_bytes.begin();
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t prefix6 = detail::read_uint64(prefix_it);
|
2015-11-14 06:08:57 +01:00
|
|
|
|
|
|
|
if (m_peer6_prefixes.insert(prefix6).second)
|
|
|
|
goto add_result;
|
|
|
|
}
|
|
|
|
else
|
2011-01-08 09:54:51 +01:00
|
|
|
#endif
|
2015-11-14 06:08:57 +01:00
|
|
|
{
|
|
|
|
// mask the lower octet
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t prefix4 = o->target_addr().to_v4().to_ulong();
|
2015-11-14 06:08:57 +01:00
|
|
|
prefix4 &= 0xffffff00;
|
|
|
|
|
|
|
|
if (m_peer4_prefixes.insert(prefix4).second)
|
|
|
|
goto add_result;
|
2011-01-08 09:54:51 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-11-14 06:08:57 +01:00
|
|
|
// we already have a node in this search 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
|
|
|
|
if (get_node().observer())
|
|
|
|
{
|
|
|
|
char hex_id[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
|
2015-11-14 06:08:57 +01:00
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] traversal DUPLICATE node. id: %s addr: %s type: %s"
|
|
|
|
, static_cast<void*>(this), hex_id, print_address(o->target_addr()).c_str(), name());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return;
|
2011-01-08 09:54:51 +01:00
|
|
|
}
|
2013-01-20 08:54:54 +01:00
|
|
|
|
2015-11-14 06:08:57 +01:00
|
|
|
add_result:
|
|
|
|
|
2015-08-06 08:30:06 +02:00
|
|
|
TORRENT_ASSERT((o->flags & observer::flag_no_id)
|
2016-05-25 06:31:52 +02:00
|
|
|
|| std::none_of(m_results.begin(), m_results.end()
|
|
|
|
, [&id](observer_ptr const& ob) { return ob->id() == id; }));
|
2015-05-19 02:48:04 +02:00
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
2015-05-10 06:54:02 +02:00
|
|
|
char hex_id[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(reinterpret_cast<char const*>(&id[0]), 20, hex_id);
|
2015-05-10 06:54:02 +02:00
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] ADD id: %s addr: %s distance: %d invoke-count: %d type: %s"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), hex_id, print_endpoint(addr).c_str()
|
2015-05-10 06:54:02 +02:00
|
|
|
, distance_exp(m_target, id), m_invoke_count, name());
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2015-08-06 08:30:06 +02:00
|
|
|
iter = m_results.insert(iter, o);
|
2014-03-03 06:09:53 +01:00
|
|
|
|
2014-05-25 07:28:15 +02:00
|
|
|
TORRENT_ASSERT(libtorrent::dht::is_sorted(m_results.begin(), m_results.end()
|
2016-05-25 06:31:52 +02:00
|
|
|
, [this](observer_ptr const& lhs, observer_ptr const& rhs)
|
|
|
|
{ return compare_ref(lhs->id(), rhs->id(), m_target); }));
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2010-11-05 20:06:50 +01:00
|
|
|
|
2012-06-25 00:53:15 +02:00
|
|
|
if (m_results.size() > 100)
|
|
|
|
{
|
2014-07-05 01:40:31 +02:00
|
|
|
for (int i = 100; i < int(m_results.size()); ++i)
|
2015-11-17 06:05:32 +01:00
|
|
|
{
|
|
|
|
if ((m_results[i]->flags & (observer::flag_queried | observer::flag_failed | observer::flag_alive))
|
|
|
|
== observer::flag_queried)
|
|
|
|
{
|
|
|
|
// set the done flag on any outstanding queries to prevent them from
|
|
|
|
// calling finished() or failed()
|
|
|
|
m_results[i]->flags |= observer::flag_done;
|
|
|
|
TORRENT_ASSERT(m_invoke_count > 0);
|
|
|
|
--m_invoke_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TORRENT_USE_ASSERTS
|
2012-06-25 00:53:15 +02:00
|
|
|
m_results[i]->m_was_abandoned = true;
|
|
|
|
#endif
|
2015-11-17 06:05:32 +01:00
|
|
|
}
|
2012-06-25 00:53:15 +02:00
|
|
|
m_results.resize(100);
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
void traversal_algorithm::start()
|
|
|
|
{
|
|
|
|
// in case the routing table is empty, use the
|
|
|
|
// router nodes in the table
|
2015-03-22 05:34:11 +01:00
|
|
|
if (m_results.size() < 3) add_router_entries();
|
2009-09-20 02:23:36 +02:00
|
|
|
init();
|
2013-01-22 03:04:58 +01:00
|
|
|
bool is_done = add_requests();
|
|
|
|
if (is_done) done();
|
2009-09-20 02:23:36 +02:00
|
|
|
}
|
|
|
|
|
2010-11-06 08:12:57 +01:00
|
|
|
void* traversal_algorithm::allocate_observer()
|
2007-05-23 10:45:12 +02:00
|
|
|
{
|
2010-11-06 08:12:57 +01:00
|
|
|
return m_node.m_rpc.allocate_observer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void traversal_algorithm::free_observer(void* ptr)
|
|
|
|
{
|
|
|
|
m_node.m_rpc.free_observer(ptr);
|
2007-05-23 10:45:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 09:08:02 +02:00
|
|
|
char const* traversal_algorithm::name() const
|
|
|
|
{
|
|
|
|
return "traversal_algorithm";
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr)
|
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (id.is_all_zeros() && get_node().observer())
|
2013-01-20 08:54:54 +01:00
|
|
|
{
|
2015-05-10 06:54:02 +02:00
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] WARNING node returned a list which included a node with id 0"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this));
|
2013-01-20 08:54:54 +01:00
|
|
|
}
|
2008-03-29 15:52:18 +01:00
|
|
|
#endif
|
2014-11-02 10:41:29 +01:00
|
|
|
|
|
|
|
// let the routing table know this node may exist
|
|
|
|
m_node.m_table.heard_about(id, addr);
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
add_entry(id, addr, 0);
|
|
|
|
}
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
void traversal_algorithm::finished(observer_ptr o)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2016-06-11 06:48:13 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2016-05-25 06:31:52 +02:00
|
|
|
auto i = std::find(m_results.begin(), m_results.end(), o);
|
2010-11-06 18:01:21 +01:00
|
|
|
TORRENT_ASSERT(i != m_results.end() || m_results.size() == 100);
|
2010-11-05 20:06:50 +01:00
|
|
|
#endif
|
2009-09-29 19:06:08 +02:00
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
// if this flag is set, it means we increased the
|
|
|
|
// branch factor for it, and we should restore it
|
|
|
|
if (o->flags & observer::flag_short_timeout)
|
2013-01-20 20:51:57 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_branch_factor > 0);
|
2010-11-05 20:06:50 +01:00
|
|
|
--m_branch_factor;
|
2013-01-20 20:51:57 +01:00
|
|
|
}
|
2009-09-29 19:06:08 +02:00
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
TORRENT_ASSERT(o->flags & observer::flag_queried);
|
2010-11-05 20:06:50 +01:00
|
|
|
o->flags |= observer::flag_alive;
|
2009-10-09 04:34:25 +02:00
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
++m_responses;
|
2015-04-26 02:51:44 +02:00
|
|
|
TORRENT_ASSERT(m_invoke_count > 0);
|
2006-08-02 00:23:05 +02:00
|
|
|
--m_invoke_count;
|
2013-01-20 08:54:54 +01:00
|
|
|
bool is_done = add_requests();
|
|
|
|
if (is_done) done();
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
// prevent request means that the total number of requests has
|
|
|
|
// overflown. This query failed because it was the oldest one.
|
|
|
|
// So, if this is true, don't make another request
|
2010-11-05 20:06:50 +01:00
|
|
|
void traversal_algorithm::failed(observer_ptr o, int flags)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2014-11-02 10:41:29 +01:00
|
|
|
// don't tell the routing table about
|
|
|
|
// node ids that we just generated ourself
|
|
|
|
if ((o->flags & observer::flag_no_id) == 0)
|
|
|
|
m_node.m_table.node_failed(o->id(), o->target_ep());
|
|
|
|
|
2009-10-07 22:51:02 +02:00
|
|
|
if (m_results.empty()) return;
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
TORRENT_ASSERT(o->flags & observer::flag_queried);
|
|
|
|
if (flags & short_timeout)
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
2010-11-05 20:06:50 +01:00
|
|
|
// short timeout means that it has been more than
|
|
|
|
// two seconds since we sent the request, and that
|
|
|
|
// we'll most likely not get a response. But, in case
|
|
|
|
// we do get a late response, keep the handler
|
|
|
|
// around for some more, but open up the slot
|
|
|
|
// by increasing the branch factor
|
|
|
|
if ((o->flags & observer::flag_short_timeout) == 0)
|
|
|
|
++m_branch_factor;
|
|
|
|
o->flags |= observer::flag_short_timeout;
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
2015-05-10 06:54:02 +02:00
|
|
|
char hex_id[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
|
2015-05-10 06:54:02 +02:00
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] 1ST_TIMEOUT id: %s distance: %d addr: %s branch-factor: %d "
|
|
|
|
"invoke-count: %d type: %s"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), hex_id, distance_exp(m_target, o->id())
|
2015-05-10 06:54:02 +02:00
|
|
|
, print_address(o->target_addr()).c_str(), m_branch_factor
|
|
|
|
, m_invoke_count, name());
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2010-11-05 20:06:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
o->flags |= observer::flag_failed;
|
|
|
|
// if this flag is set, it means we increased the
|
|
|
|
// branch factor for it, and we should restore it
|
|
|
|
if (o->flags & observer::flag_short_timeout)
|
|
|
|
--m_branch_factor;
|
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
2015-05-10 06:54:02 +02:00
|
|
|
char hex_id[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
|
2015-05-10 06:54:02 +02:00
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] TIMEOUT id: %s distance: %d addr: %s branch-factor: %d "
|
|
|
|
"invoke-count: %d type: %s"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), hex_id, distance_exp(m_target, o->id())
|
2015-05-10 06:54:02 +02:00
|
|
|
, print_address(o->target_addr()).c_str(), m_branch_factor
|
|
|
|
, m_invoke_count, name());
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2010-11-05 20:06:50 +01:00
|
|
|
#endif
|
2014-11-02 10:41:29 +01:00
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
++m_timeouts;
|
2015-04-26 02:51:44 +02:00
|
|
|
TORRENT_ASSERT(m_invoke_count > 0);
|
2010-11-05 20:06:50 +01:00
|
|
|
--m_invoke_count;
|
2009-09-29 19:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & prevent_request)
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
|
|
|
--m_branch_factor;
|
|
|
|
if (m_branch_factor <= 0) m_branch_factor = 1;
|
|
|
|
}
|
2013-01-20 08:54:54 +01:00
|
|
|
bool is_done = add_requests();
|
|
|
|
if (is_done) done();
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
void traversal_algorithm::done()
|
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2014-07-06 21:18:00 +02:00
|
|
|
int results_target = m_node.m_table.bucket_size();
|
2013-01-20 08:54:54 +01:00
|
|
|
int closest_target = 160;
|
2015-11-08 06:41:53 +01:00
|
|
|
#endif
|
2013-01-20 08:54:54 +01:00
|
|
|
|
|
|
|
for (std::vector<observer_ptr>::iterator i = m_results.begin()
|
2015-11-08 06:41:53 +01:00
|
|
|
, end(m_results.end()); i != end; ++i)
|
2013-01-20 08:54:54 +01:00
|
|
|
{
|
|
|
|
boost::intrusive_ptr<observer> o = *i;
|
2015-11-15 03:30:42 +01:00
|
|
|
if ((o->flags & (observer::flag_queried | observer::flag_failed)) == observer::flag_queried)
|
2015-11-08 06:41:53 +01:00
|
|
|
{
|
|
|
|
// set the done flag on any outstanding queries to prevent them from
|
|
|
|
// calling finished() or failed() after we've already declared the traversal
|
|
|
|
// done
|
|
|
|
o->flags |= observer::flag_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
if (results_target > 0 && (o->flags & observer::flag_alive) && get_node().observer())
|
2013-01-20 08:54:54 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(o->flags & observer::flag_queried);
|
2015-05-10 06:54:02 +02:00
|
|
|
char hex_id[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
|
2015-05-10 06:54:02 +02:00
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] id: %s distance: %d addr: %s"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), hex_id, closest_target
|
2015-05-10 06:54:02 +02:00
|
|
|
, print_endpoint(o->target_ep()).c_str());
|
|
|
|
|
2013-01-20 08:54:54 +01:00
|
|
|
--results_target;
|
|
|
|
int dist = distance_exp(m_target, o->id());
|
|
|
|
if (dist < closest_target) closest_target = dist;
|
|
|
|
}
|
2015-11-08 06:41:53 +01:00
|
|
|
#endif
|
2013-01-20 08:54:54 +01:00
|
|
|
}
|
|
|
|
|
2015-11-08 06:41:53 +01:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] COMPLETED distance: %d type: %s"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), closest_target, name());
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2013-01-20 08:54:54 +01:00
|
|
|
#endif
|
2015-11-08 06:41:53 +01:00
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
// delete all our references to the observer objects so
|
|
|
|
// they will in turn release the traversal algorithm
|
|
|
|
m_results.clear();
|
2015-11-08 06:41:53 +01:00
|
|
|
m_invoke_count = 0;
|
2010-11-05 20:06:50 +01:00
|
|
|
}
|
|
|
|
|
2013-01-20 08:54:54 +01:00
|
|
|
bool traversal_algorithm::add_requests()
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
int results_target = m_node.m_table.bucket_size();
|
2009-10-09 04:34:25 +02:00
|
|
|
|
2013-01-20 08:54:54 +01:00
|
|
|
// this only counts outstanding requests at the top of the
|
|
|
|
// target list. This is <= m_invoke count. m_invoke_count
|
|
|
|
// is the total number of outstanding requests, including
|
|
|
|
// old ones that may be waiting on nodes much farther behind
|
|
|
|
// the current point we've reached in the search.
|
|
|
|
int outstanding = 0;
|
|
|
|
|
|
|
|
// if we're doing aggressive lookups, we keep branch-factor
|
|
|
|
// outstanding requests _at the tops_ of the result list. Otherwise
|
|
|
|
// we just keep any branch-factor outstanding requests
|
|
|
|
bool agg = m_node.settings().aggressive_lookups;
|
|
|
|
|
2009-10-09 04:34:25 +02:00
|
|
|
// Find the first node that hasn't already been queried.
|
2013-01-20 08:54:54 +01:00
|
|
|
// and make sure that the 'm_branch_factor' top nodes
|
|
|
|
// stay queried at all times (obviously ignoring failed nodes)
|
|
|
|
// and without surpassing the 'result_target' nodes (i.e. k=8)
|
|
|
|
// this is a slight variation of the original paper which instead
|
|
|
|
// limits the number of outstanding requests, this limits the
|
|
|
|
// number of good outstanding requests. It will use more traffic,
|
|
|
|
// but is intended to speed up lookups
|
2010-11-05 20:06:50 +01:00
|
|
|
for (std::vector<observer_ptr>::iterator i = m_results.begin()
|
2009-10-09 04:34:25 +02:00
|
|
|
, end(m_results.end()); i != end
|
2013-01-20 08:54:54 +01:00
|
|
|
&& results_target > 0
|
|
|
|
&& (agg ? outstanding < m_branch_factor
|
|
|
|
: m_invoke_count < m_branch_factor);
|
|
|
|
++i)
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
2013-01-20 08:54:54 +01:00
|
|
|
observer* o = i->get();
|
|
|
|
if (o->flags & observer::flag_alive)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(o->flags & observer::flag_queried);
|
|
|
|
--results_target;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (o->flags & observer::flag_queried)
|
|
|
|
{
|
|
|
|
// if it's queried, not alive and not failed, it
|
|
|
|
// must be currently in flight
|
|
|
|
if ((o->flags & observer::flag_failed) == 0)
|
|
|
|
++outstanding;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2009-10-09 04:34:25 +02:00
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
|
|
|
char hex_id[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
|
2015-05-19 02:48:04 +02:00
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] INVOKE nodes-left: %d top-invoke-count: %d "
|
|
|
|
"invoke-count: %d branch-factor: %d "
|
|
|
|
"distance: %d id: %s addr: %s type: %s"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), int(m_results.end() - i), outstanding, int(m_invoke_count)
|
2015-05-19 02:48:04 +02:00
|
|
|
, int(m_branch_factor), distance_exp(m_target, o->id()), hex_id
|
|
|
|
, print_address(o->target_addr()).c_str(), name());
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2013-01-22 03:04:58 +01:00
|
|
|
o->flags |= observer::flag_queried;
|
2010-11-05 20:06:50 +01:00
|
|
|
if (invoke(*i))
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
TORRENT_ASSERT(m_invoke_count < (std::numeric_limits<std::uint16_t>::max)());
|
2007-01-29 08:39:33 +01:00
|
|
|
++m_invoke_count;
|
2013-01-20 08:54:54 +01:00
|
|
|
++outstanding;
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
2013-01-22 03:04:58 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
o->flags |= observer::flag_failed;
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2013-01-20 08:54:54 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is the completion condition. If we found m_node.m_table.bucket_size()
|
2013-01-20 08:54:54 +01:00
|
|
|
// (i.e. k=8) completed results, without finding any still
|
|
|
|
// outstanding requests, we're done.
|
|
|
|
// also, if invoke count is 0, it means we didn't even find 'k'
|
|
|
|
// working nodes, we still have to terminate though.
|
|
|
|
return (results_target == 0 && outstanding == 0) || m_invoke_count == 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
void traversal_algorithm::add_router_entries()
|
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-19 02:48:04 +02:00
|
|
|
if (get_node().observer())
|
|
|
|
{
|
|
|
|
get_node().observer()->log(dht_logger::traversal
|
|
|
|
, "[%p] using router nodes to initiate traversal algorithm %d routers"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(this), int(std::distance(m_node.m_table.router_begin(), m_node.m_table.router_end())));
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2010-02-14 08:46:57 +01:00
|
|
|
#endif
|
2008-09-20 19:42:25 +02:00
|
|
|
for (routing_table::router_iterator i = m_node.m_table.router_begin()
|
|
|
|
, end(m_node.m_table.router_end()); i != end; ++i)
|
|
|
|
{
|
2010-11-05 20:06:50 +01:00
|
|
|
add_entry(node_id(0), *i, observer::flag_initial);
|
2008-09-20 19:42:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void traversal_algorithm::init()
|
|
|
|
{
|
|
|
|
m_branch_factor = m_node.branch_factor();
|
|
|
|
m_node.add_traversal_algorithm(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
traversal_algorithm::~traversal_algorithm()
|
|
|
|
{
|
|
|
|
m_node.remove_traversal_algorithm(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void traversal_algorithm::status(dht_lookup& l)
|
|
|
|
{
|
|
|
|
l.timeouts = m_timeouts;
|
|
|
|
l.responses = m_responses;
|
|
|
|
l.outstanding_requests = m_invoke_count;
|
|
|
|
l.branch_factor = m_branch_factor;
|
|
|
|
l.type = name();
|
2009-10-09 04:34:25 +02:00
|
|
|
l.nodes_left = 0;
|
2010-12-12 04:17:08 +01:00
|
|
|
l.first_timeout = 0;
|
|
|
|
|
|
|
|
int last_sent = INT_MAX;
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point now = aux::time_now();
|
2010-11-05 20:06:50 +01:00
|
|
|
for (std::vector<observer_ptr>::iterator i = m_results.begin()
|
2009-10-09 04:34:25 +02:00
|
|
|
, end(m_results.end()); i != end; ++i)
|
|
|
|
{
|
2010-12-12 04:17:08 +01:00
|
|
|
observer& o = **i;
|
|
|
|
if (o.flags & observer::flag_queried)
|
|
|
|
{
|
2013-11-02 04:35:45 +01:00
|
|
|
last_sent = (std::min)(last_sent, int(total_seconds(now - o.sent())));
|
2010-12-12 04:17:08 +01:00
|
|
|
if (o.has_short_timeout()) ++l.first_timeout;
|
|
|
|
continue;
|
|
|
|
}
|
2009-10-09 04:34:25 +02:00
|
|
|
++l.nodes_left;
|
|
|
|
}
|
2010-12-12 04:17:08 +01:00
|
|
|
l.last_sent = last_sent;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2013-12-15 00:25:38 +01:00
|
|
|
void traversal_observer::reply(msg const& m)
|
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node r = m.message.dict_find_dict("r");
|
2013-12-15 00:25:38 +01:00
|
|
|
if (!r)
|
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-22 04:42:26 +02:00
|
|
|
if (get_observer())
|
2015-05-19 02:48:04 +02:00
|
|
|
{
|
2015-05-22 04:42:26 +02:00
|
|
|
get_observer()->log(dht_logger::traversal
|
2015-05-19 02:48:04 +02:00
|
|
|
, "[%p] missing response dict"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(algorithm()));
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2013-12-15 00:25:38 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-22 04:42:26 +02:00
|
|
|
if (get_observer())
|
2015-05-19 02:48:04 +02:00
|
|
|
{
|
|
|
|
bdecode_node nid = r.dict_find_string("id");
|
|
|
|
char hex_id[41];
|
2016-06-04 16:01:43 +02:00
|
|
|
aux::to_hex(nid.string_ptr(), 20, hex_id);
|
2015-05-22 04:42:26 +02:00
|
|
|
get_observer()->log(dht_logger::traversal
|
2015-05-19 02:48:04 +02:00
|
|
|
, "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(algorithm()), hex_id, algorithm()->invoke_count()
|
2015-05-22 04:42:26 +02:00
|
|
|
, print_endpoint(target_ep()).c_str(), algorithm()->name());
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2014-10-01 11:14:49 +02:00
|
|
|
#endif
|
2015-11-14 06:08:57 +01:00
|
|
|
|
2013-12-15 00:25:38 +01:00
|
|
|
// look for nodes
|
2015-11-14 06:08:57 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
2016-02-12 04:56:52 +01:00
|
|
|
udp protocol = algorithm()->get_node().protocol();
|
2015-11-14 06:08:57 +01:00
|
|
|
#endif
|
2016-02-12 04:56:52 +01:00
|
|
|
char const* nodes_key = algorithm()->get_node().protocol_nodes_key();
|
2015-11-14 06:08:57 +01:00
|
|
|
bdecode_node n = r.dict_find_string(nodes_key);
|
2013-12-15 00:25:38 +01:00
|
|
|
if (n)
|
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
char const* nodes = n.string_ptr();
|
|
|
|
char const* end = nodes + n.string_length();
|
2013-12-15 00:25:38 +01:00
|
|
|
|
|
|
|
while (end - nodes >= 26)
|
|
|
|
{
|
|
|
|
node_id id;
|
|
|
|
std::copy(nodes, nodes + 20, id.begin());
|
|
|
|
nodes += 20;
|
2015-11-14 06:08:57 +01:00
|
|
|
udp::endpoint ep;
|
|
|
|
#if TORRENT_USE_IPV6
|
2016-02-12 04:56:52 +01:00
|
|
|
if (protocol == udp::v6())
|
2015-11-14 06:08:57 +01:00
|
|
|
ep = read_v6_endpoint<udp::endpoint>(nodes);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
ep = read_v4_endpoint<udp::endpoint>(nodes);
|
|
|
|
algorithm()->traverse(id, ep);
|
2013-12-15 00:25:38 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-03 06:09:53 +01:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node id = r.dict_find_string("id");
|
|
|
|
if (!id || id.string_length() != 20)
|
2014-03-03 06:09:53 +01:00
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-22 04:42:26 +02:00
|
|
|
if (get_observer())
|
2015-05-19 02:48:04 +02:00
|
|
|
{
|
2015-05-22 04:42:26 +02:00
|
|
|
get_observer()->log(dht_logger::traversal, "[%p] invalid id in response"
|
2015-08-20 01:33:20 +02:00
|
|
|
, static_cast<void*>(algorithm()));
|
2015-05-19 02:48:04 +02:00
|
|
|
}
|
2014-03-03 06:09:53 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case we didn't know the id of this peer when we sent the message to
|
|
|
|
// it. For instance if it's a bootstrap node.
|
2015-03-12 06:20:12 +01:00
|
|
|
set_id(node_id(id.string_ptr()));
|
2013-12-15 00:25:38 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|