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/find_data.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>
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <libtorrent/io.hpp>
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <libtorrent/socket.hpp>
|
2009-09-20 02:23:36 +02:00
|
|
|
#include <libtorrent/socket_io.hpp>
|
|
|
|
#include <vector>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2009-10-09 04:34:25 +02:00
|
|
|
TORRENT_DECLARE_LOG(traversal);
|
2009-09-20 02:23:36 +02:00
|
|
|
#endif
|
|
|
|
|
2009-10-19 04:43:50 +02:00
|
|
|
using detail::read_endpoint_list;
|
2009-09-20 02:23:36 +02:00
|
|
|
using detail::read_v4_endpoint;
|
2009-10-19 04:43:50 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2009-09-20 02:23:36 +02:00
|
|
|
using detail::read_v6_endpoint;
|
2009-10-19 04:43:50 +02:00
|
|
|
#endif
|
2009-09-20 02:23:36 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void find_data_observer::reply(msg const& m)
|
|
|
|
{
|
2009-09-20 02:23:36 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
std::stringstream log_line;
|
2009-10-07 22:51:02 +02:00
|
|
|
log_line << "[" << m_algorithm.get() << "] incoming get_peer response [ ";
|
2009-09-20 02:23:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
lazy_entry const* r = m.message.dict_find_dict("r");
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2009-10-09 04:34:25 +02:00
|
|
|
TORRENT_LOG(traversal) << "[" << m_algorithm.get() << "] missing response dict";
|
2009-09-20 02:23:36 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_entry const* id = r->dict_find_string("id");
|
|
|
|
if (!id || id->string_length() != 20)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2009-10-09 04:34:25 +02:00
|
|
|
TORRENT_LOG(traversal) << "[" << m_algorithm.get() << "] invalid id in response";
|
2009-09-20 02:23:36 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_entry const* token = r->dict_find_string("token");
|
|
|
|
if (token)
|
|
|
|
{
|
2009-10-07 22:51:02 +02:00
|
|
|
static_cast<find_data*>(m_algorithm.get())->got_write_token(
|
|
|
|
node_id(id->string_ptr()), token->string_value());
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " token: " << to_hex(token->string_value());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for peers
|
|
|
|
lazy_entry const* n = r->dict_find_list("values");
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
std::vector<tcp::endpoint> peer_list;
|
|
|
|
if (n->list_size() == 1 && n->list_at(0)->type() == lazy_entry::string_t)
|
|
|
|
{
|
|
|
|
// assume it's mainline format
|
|
|
|
char const* peers = n->list_at(0)->string_ptr();
|
|
|
|
char const* end = peers + n->list_at(0)->string_length();
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " p: " << ((end - peers) / 6);
|
|
|
|
#endif
|
|
|
|
while (end - peers >= 6)
|
|
|
|
peer_list.push_back(read_v4_endpoint<tcp::endpoint>(peers));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// assume it's uTorrent/libtorrent format
|
|
|
|
read_endpoint_list<tcp::endpoint>(n, peer_list);
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " p: " << n->list_size();
|
|
|
|
#endif
|
|
|
|
}
|
2009-10-07 22:51:02 +02:00
|
|
|
static_cast<find_data*>(m_algorithm.get())->got_peers(peer_list);
|
2009-09-20 02:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// look for nodes
|
|
|
|
n = r->dict_find_string("nodes");
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
std::vector<node_entry> node_list;
|
|
|
|
char const* nodes = n->string_ptr();
|
|
|
|
char const* end = nodes + n->string_length();
|
2008-12-23 21:04:12 +01:00
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " nodes: " << ((end - nodes) / 26);
|
|
|
|
#endif
|
|
|
|
while (end - nodes >= 26)
|
|
|
|
{
|
|
|
|
node_id id;
|
|
|
|
std::copy(nodes, nodes + 20, id.begin());
|
|
|
|
nodes += 20;
|
|
|
|
m_algorithm->traverse(id, read_v4_endpoint<udp::endpoint>(nodes));
|
|
|
|
}
|
|
|
|
}
|
2008-12-23 21:04:12 +01:00
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
n = r->dict_find_list("nodes2");
|
|
|
|
if (n)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2009-09-20 02:23:36 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " nodes2: " << n->list_size();
|
|
|
|
#endif
|
|
|
|
for (int i = 0; i < n->list_size(); ++i)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2009-09-20 02:23:36 +02:00
|
|
|
lazy_entry const* p = n->list_at(0);
|
|
|
|
if (p->type() != lazy_entry::string_t) continue;
|
|
|
|
if (p->string_length() < 6 + 20) continue;
|
|
|
|
char const* in = p->string_ptr();
|
|
|
|
|
|
|
|
node_id id;
|
|
|
|
std::copy(in, in + 20, id.begin());
|
|
|
|
in += 20;
|
|
|
|
if (p->string_length() == 6 + 20)
|
|
|
|
m_algorithm->traverse(id, read_v4_endpoint<udp::endpoint>(in));
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
else if (p->string_length() == 18 + 20)
|
|
|
|
m_algorithm->traverse(id, read_v6_endpoint<udp::endpoint>(in));
|
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-20 02:23:36 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " ]";
|
2009-10-09 04:34:25 +02:00
|
|
|
TORRENT_LOG(traversal) << log_line.str();
|
2009-09-20 02:23:36 +02:00
|
|
|
#endif
|
2009-10-07 22:51:02 +02:00
|
|
|
done();
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
find_data::find_data(
|
2008-09-20 19:42:25 +02:00
|
|
|
node_impl& node
|
|
|
|
, node_id target
|
2008-12-23 21:04:12 +01:00
|
|
|
, data_callback const& dcallback
|
|
|
|
, nodes_callback const& ncallback)
|
2009-09-20 02:23:36 +02:00
|
|
|
: traversal_algorithm(node, target)
|
2008-12-23 21:04:12 +01:00
|
|
|
, m_data_callback(dcallback)
|
|
|
|
, m_nodes_callback(ncallback)
|
|
|
|
, m_target(target)
|
2006-08-01 17:27:08 +02:00
|
|
|
, m_done(false)
|
2009-10-07 22:51:02 +02:00
|
|
|
, m_got_peers(false)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2009-09-20 02:23:36 +02:00
|
|
|
for (routing_table::const_iterator i = node.m_table.begin()
|
|
|
|
, end(node.m_table.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
add_entry(i->id, i->ep(), result::initial);
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2009-10-09 04:34:25 +02:00
|
|
|
bool find_data::invoke(udp::endpoint addr)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
if (m_done)
|
|
|
|
{
|
|
|
|
m_invoke_count = -1;
|
2009-10-07 22:51:02 +02:00
|
|
|
return false;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
TORRENT_ASSERT(m_node.m_rpc.allocation_size() >= sizeof(find_data_observer));
|
2009-05-14 00:18:41 +02:00
|
|
|
void* ptr = m_node.m_rpc.allocator().malloc();
|
|
|
|
if (ptr == 0)
|
|
|
|
{
|
2009-10-09 04:34:25 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_LOG(traversal) << "[" << this << "] failed to "
|
|
|
|
"allocate memory for observer. aborting!";
|
|
|
|
#endif
|
2009-05-14 00:18:41 +02:00
|
|
|
done();
|
2009-10-07 22:51:02 +02:00
|
|
|
return false;
|
2009-05-14 00:18:41 +02:00
|
|
|
}
|
|
|
|
m_node.m_rpc.allocator().set_next_size(10);
|
2009-10-09 04:34:25 +02:00
|
|
|
observer_ptr o(new (ptr) find_data_observer(this));
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-02-09 22:04:24 +01:00
|
|
|
o->m_in_constructor = false;
|
|
|
|
#endif
|
2009-09-20 02:23:36 +02:00
|
|
|
entry e;
|
|
|
|
e["y"] = "q";
|
|
|
|
e["q"] = "get_peers";
|
|
|
|
entry& a = e["a"];
|
2009-10-09 04:34:25 +02:00
|
|
|
a["info_hash"] = m_target.to_string();
|
2009-10-07 22:51:02 +02:00
|
|
|
return m_node.m_rpc.invoke(e, addr, o);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
void find_data::got_peers(std::vector<tcp::endpoint> const& peers)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2009-10-07 22:51:02 +02:00
|
|
|
if (!peers.empty()) m_got_peers = true;
|
2009-09-20 02:23:36 +02:00
|
|
|
m_data_callback(peers);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void find_data::done()
|
|
|
|
{
|
|
|
|
if (m_invoke_count != 0) return;
|
2008-12-23 21:04:12 +01:00
|
|
|
|
2009-10-07 22:51:02 +02:00
|
|
|
m_done = true;
|
|
|
|
|
2008-12-23 21:04:12 +01:00
|
|
|
std::vector<std::pair<node_entry, std::string> > results;
|
|
|
|
int num_results = m_node.m_table.bucket_size();
|
|
|
|
for (std::vector<result>::iterator i = m_results.begin()
|
|
|
|
, end(m_results.end()); i != end && num_results > 0; ++i)
|
|
|
|
{
|
|
|
|
if (i->flags & result::no_id) continue;
|
|
|
|
if ((i->flags & result::queried) == 0) continue;
|
|
|
|
std::map<node_id, std::string>::iterator j = m_write_tokens.find(i->id);
|
|
|
|
if (j == m_write_tokens.end()) continue;
|
2009-10-07 22:51:02 +02:00
|
|
|
results.push_back(std::make_pair(node_entry(i->id, i->endpoint()), j->second));
|
2008-12-23 21:04:12 +01:00
|
|
|
--num_results;
|
|
|
|
}
|
2009-10-07 22:51:02 +02:00
|
|
|
m_nodes_callback(results, m_got_peers);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|