2006-08-01 17:27:08 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2006, Arvid Norberg & Daniel Wallin
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
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>
|
|
|
|
#include <libtorrent/session_status.hpp>
|
2011-01-08 09:54:51 +01:00
|
|
|
#include "libtorrent/broadcast_socket.hpp" // for cidr_distance
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_DEFINE_LOG(traversal)
|
|
|
|
#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));
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
o->m_in_constructor = false;
|
|
|
|
#endif
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2010-12-12 21:36:42 +01:00
|
|
|
traversal_algorithm::traversal_algorithm(
|
|
|
|
node_impl& node
|
|
|
|
, node_id target)
|
|
|
|
: m_ref_count(0)
|
|
|
|
, m_node(node)
|
|
|
|
, m_target(target)
|
|
|
|
, m_invoke_count(0)
|
|
|
|
, m_branch_factor(3)
|
|
|
|
, m_responses(0)
|
|
|
|
, m_timeouts(0)
|
|
|
|
, m_num_target_nodes(m_node.m_table.bucket_size() * 2)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_LOG(traversal) << " [" << this << "] new traversal process. Target: " << target;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-26 04:12:46 +01:00
|
|
|
// returns true of lhs and rhs are too close to each other to appear
|
|
|
|
// in the same DHT search under different node IDs
|
2011-01-08 09:54:51 +01:00
|
|
|
bool compare_ip_cidr(observer_ptr const& lhs, observer_ptr const& rhs)
|
|
|
|
{
|
2011-01-26 04:12:46 +01:00
|
|
|
if (lhs->target_addr().is_v4() != rhs->target_addr().is_v4())
|
|
|
|
return false;
|
2011-01-08 09:54:51 +01:00
|
|
|
// the number of bits in the IPs that may match. If
|
|
|
|
// more bits that this matches, something suspicious is
|
|
|
|
// going on and we shouldn't add the second one to our
|
|
|
|
// routing table
|
|
|
|
int cutoff = rhs->target_addr().is_v4() ? 4 : 64;
|
|
|
|
int dist = cidr_distance(lhs->target_addr(), rhs->target_addr());
|
|
|
|
return dist <= cutoff;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2010-12-12 04:17:08 +01:00
|
|
|
TORRENT_LOG(traversal) << "[" << this << ":" << name()
|
|
|
|
<< "] failed to allocate memory for observer. aborting!";
|
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;
|
|
|
|
|
|
|
|
std::vector<observer_ptr>::iterator i = std::lower_bound(
|
2006-08-01 17:27:08 +02:00
|
|
|
m_results.begin()
|
|
|
|
, m_results.end()
|
2010-11-05 20:06:50 +01:00
|
|
|
, o
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(
|
2006-08-01 17:27:08 +02:00
|
|
|
compare_ref
|
2010-11-05 20:06:50 +01:00
|
|
|
, boost::bind(&observer::id, _1)
|
|
|
|
, boost::bind(&observer::id, _2)
|
2006-08-01 17:27:08 +02:00
|
|
|
, m_target
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
if (i == m_results.end() || (*i)->id() != id)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2011-01-08 09:54:51 +01:00
|
|
|
if (m_node.settings().restrict_search_ips)
|
|
|
|
{
|
|
|
|
// don't allow multiple entries from IPs very close to each other
|
|
|
|
std::vector<observer_ptr>::iterator j = std::find_if(
|
|
|
|
m_results.begin(), m_results.end(), boost::bind(&compare_ip_cidr, _1, o));
|
|
|
|
|
|
|
|
if (j != m_results.end())
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_LOG(traversal) << "ignoring DHT search entry: " << o->id()
|
|
|
|
<< " " << o->target_addr()
|
|
|
|
<< " existing node: "
|
|
|
|
<< (*j)->id() << " " << (*j)->target_addr();
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(std::find_if(m_results.begin(), m_results.end()
|
2010-11-05 20:06:50 +01:00
|
|
|
, boost::bind(&observer::id, _1) == id) == m_results.end());
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2010-12-12 04:17:08 +01:00
|
|
|
TORRENT_LOG(traversal) << "[" << this << ":" << name()
|
|
|
|
<< "] adding result: " << id << " " << addr;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2010-11-05 20:06:50 +01:00
|
|
|
i = m_results.insert(i, o);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2010-11-05 20:06:50 +01:00
|
|
|
|
|
|
|
if (m_results.size() > 100) 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
|
|
|
|
if (m_results.empty()) add_router_entries();
|
|
|
|
init();
|
2009-10-07 22:51:02 +02:00
|
|
|
add_requests();
|
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
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr)
|
|
|
|
{
|
2008-03-29 15:52:18 +01:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-05-08 01:25:47 +02:00
|
|
|
if (id.is_all_zeros())
|
2010-12-12 04:17:08 +01:00
|
|
|
TORRENT_LOG(traversal) << time_now_string() << "[" << this << ":" << name()
|
|
|
|
<< "] WARNING: node returned a list which included a node with id 0";
|
2008-03-29 15:52:18 +01:00
|
|
|
#endif
|
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
|
|
|
{
|
2010-11-05 20:06:50 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
std::vector<observer_ptr>::iterator i = std::find(
|
|
|
|
m_results.begin(), m_results.end(), o);
|
2009-09-29 19:06:08 +02:00
|
|
|
|
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)
|
|
|
|
--m_branch_factor;
|
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;
|
2006-08-02 00:23:05 +02:00
|
|
|
--m_invoke_count;
|
2009-09-29 19:06:08 +02:00
|
|
|
TORRENT_ASSERT(m_invoke_count >= 0);
|
2006-08-01 17:27:08 +02:00
|
|
|
add_requests();
|
|
|
|
if (m_invoke_count == 0) done();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-09-29 19:06:08 +02:00
|
|
|
TORRENT_ASSERT(m_invoke_count >= 0);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
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;
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2010-12-12 04:17:08 +01:00
|
|
|
TORRENT_LOG(traversal) << " [" << this << ":" << name()
|
|
|
|
<< "] first chance timeout: "
|
2010-11-05 20:06:50 +01:00
|
|
|
<< o->id() << " " << o->target_ep()
|
|
|
|
<< " branch-factor: " << m_branch_factor
|
|
|
|
<< " invoke-count: " << m_invoke_count;
|
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;
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2010-12-12 04:17:08 +01:00
|
|
|
TORRENT_LOG(traversal) << " [" << this << ":" << name()
|
|
|
|
<< "] failed: " << o->id() << " " << o->target_ep()
|
2010-11-05 20:06:50 +01:00
|
|
|
<< " branch-factor: " << m_branch_factor
|
|
|
|
<< " invoke-count: " << m_invoke_count;
|
|
|
|
#endif
|
|
|
|
// don't tell the routing table about
|
|
|
|
// node ids that we just generated ourself
|
|
|
|
if ((o->flags & observer::flag_no_id) == 0)
|
2011-01-17 08:49:44 +01:00
|
|
|
m_node.m_table.node_failed(o->id(), o->target_ep());
|
2010-11-05 20:06:50 +01:00
|
|
|
++m_timeouts;
|
|
|
|
--m_invoke_count;
|
|
|
|
TORRENT_ASSERT(m_invoke_count >= 0);
|
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;
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
add_requests();
|
|
|
|
if (m_invoke_count == 0) done();
|
|
|
|
}
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
void traversal_algorithm::done()
|
|
|
|
{
|
|
|
|
// delete all our references to the observer objects so
|
|
|
|
// they will in turn release the traversal algorithm
|
|
|
|
m_results.clear();
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void traversal_algorithm::add_requests()
|
|
|
|
{
|
2010-12-12 21:36:42 +01:00
|
|
|
int results_target = m_num_target_nodes;
|
2009-10-09 04:34:25 +02:00
|
|
|
|
|
|
|
// Find the first node that hasn't already been queried.
|
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
|
|
|
|
&& results_target > 0 && m_invoke_count < m_branch_factor; ++i)
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
2010-11-05 20:06:50 +01:00
|
|
|
if ((*i)->flags & observer::flag_alive) --results_target;
|
|
|
|
if ((*i)->flags & observer::flag_queried) continue;
|
2009-10-09 04:34:25 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2010-12-12 04:17:08 +01:00
|
|
|
TORRENT_LOG(traversal) << " [" << this << ":" << name() << "]"
|
2010-11-05 20:06:50 +01:00
|
|
|
<< " nodes-left: " << (m_results.end() - i)
|
|
|
|
<< " invoke-count: " << m_invoke_count
|
|
|
|
<< " branch-factor: " << m_branch_factor;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
if (invoke(*i))
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
2009-10-07 22:51:02 +02:00
|
|
|
TORRENT_ASSERT(m_invoke_count >= 0);
|
2007-01-29 08:39:33 +01:00
|
|
|
++m_invoke_count;
|
2010-11-05 20:06:50 +01:00
|
|
|
(*i)->flags |= observer::flag_queried;
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
void traversal_algorithm::add_router_entries()
|
|
|
|
{
|
2010-02-14 08:46:57 +01:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_LOG(traversal) << " using router nodes to initiate traversal algorithm. "
|
|
|
|
<< std::distance(m_node.m_table.router_begin(), m_node.m_table.router_end()) << " routers";
|
|
|
|
#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()
|
|
|
|
{
|
2010-02-20 17:37:50 +01:00
|
|
|
// update the last activity of this bucket
|
|
|
|
m_node.m_table.touch_bucket(m_target);
|
2008-09-20 19:42:25 +02:00
|
|
|
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;
|
|
|
|
ptime now = 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)
|
|
|
|
{
|
|
|
|
last_sent = (std::min)(last_sent, total_seconds(now - o.sent()));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|