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"
|
|
|
|
|
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>
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
TORRENT_LOG(traversal) << "[" << this << "] failed to "
|
|
|
|
"allocate memory for observer. aborting!";
|
|
|
|
#endif
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
observer_ptr o = new_observer(ptr, addr, id);
|
|
|
|
if (id.is_all_zeros())
|
2008-03-24 03:19:47 +01:00
|
|
|
{
|
2010-11-05 20:06:50 +01:00
|
|
|
o->set_id(generate_id());
|
|
|
|
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
|
|
|
{
|
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
|
2009-10-07 22:51:02 +02:00
|
|
|
TORRENT_LOG(traversal) << "[" << this << "] 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())
|
2009-10-07 22:51:02 +02:00
|
|
|
TORRENT_LOG(traversal) << time_now_string() << "[" << this << "] 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
|
|
|
|
|
|
|
TORRENT_ASSERT(i != m_results.end());
|
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-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-11-05 20:06:50 +01:00
|
|
|
TORRENT_LOG(traversal) << " [" << this << "] first chance timeout: "
|
|
|
|
<< 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
|
|
|
|
TORRENT_LOG(traversal) << " [" << this << "] failed: "
|
|
|
|
<< o->id() << " " << o->target_ep()
|
|
|
|
<< " 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)
|
|
|
|
m_node.m_table.node_failed(o->id());
|
|
|
|
++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
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool bitwise_nand(unsigned char lhs, unsigned char rhs)
|
|
|
|
{
|
|
|
|
return (lhs & rhs) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void traversal_algorithm::add_requests()
|
|
|
|
{
|
2009-10-09 04:34:25 +02:00
|
|
|
int results_target = m_node.m_table.bucket_size();
|
|
|
|
|
|
|
|
// 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-11-05 20:06:50 +01:00
|
|
|
TORRENT_LOG(traversal) << " [" << this << "]"
|
|
|
|
<< " 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-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-11-05 20:06:50 +01:00
|
|
|
if ((*i)->flags & observer::flag_queried) continue;
|
2009-10-09 04:34:25 +02:00
|
|
|
++l.nodes_left;
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|