2006-08-01 17:27:08 +02:00
|
|
|
/*
|
|
|
|
|
2015-06-03 07:18:48 +02:00
|
|
|
Copyright (c) 2006-2015, Arvid Norberg
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <numeric>
|
2015-05-10 20:38:10 +02:00
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include "libtorrent/kademlia/node.hpp"
|
|
|
|
#include "libtorrent/kademlia/node_id.hpp"
|
|
|
|
#include "libtorrent/kademlia/traversal_algorithm.hpp"
|
|
|
|
#include "libtorrent/kademlia/dht_tracker.hpp"
|
2008-11-11 18:51:02 +01:00
|
|
|
#include "libtorrent/kademlia/msg.hpp"
|
2015-05-10 06:54:02 +02:00
|
|
|
#include "libtorrent/kademlia/dht_observer.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include "libtorrent/socket.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/version.hpp"
|
2015-05-09 20:06:02 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/performance_counters.hpp" // for counters
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-05-10 20:38:10 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/function/function0.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
|
|
|
|
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
using boost::ref;
|
2015-05-09 21:00:22 +02:00
|
|
|
using libtorrent::dht::node;
|
2006-08-01 17:27:08 +02:00
|
|
|
using libtorrent::dht::node_id;
|
|
|
|
using libtorrent::dht::packet_t;
|
|
|
|
using libtorrent::dht::msg;
|
2015-07-04 04:33:17 +02:00
|
|
|
using libtorrent::detail::write_endpoint;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-05-14 19:49:36 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
key_refresh = 5 // generate a new write token key every 5 minutes
|
|
|
|
};
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
namespace
|
|
|
|
{
|
2006-08-06 18:36:00 +02:00
|
|
|
const int tick_period = 1; // minutes
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
2009-09-25 18:32:02 +02:00
|
|
|
void incoming_error(entry& e, char const* msg);
|
|
|
|
|
2015-04-20 06:52:49 +02:00
|
|
|
namespace {
|
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
node_id extract_node_id(entry const* e)
|
2008-11-11 09:33:34 +01:00
|
|
|
{
|
2009-11-23 09:38:50 +01:00
|
|
|
if (e == 0 || e->type() != entry::dictionary_t) return (node_id::min)();
|
2008-11-11 09:33:34 +01:00
|
|
|
entry const* nid = e->find_key("node-id");
|
|
|
|
if (nid == 0 || nid->type() != entry::string_t || nid->string().length() != 20)
|
2009-11-23 09:38:50 +01:00
|
|
|
return (node_id::min)();
|
2015-08-22 22:01:53 +02:00
|
|
|
return node_id(nid->string().c_str());
|
2008-11-11 09:33:34 +01:00
|
|
|
}
|
|
|
|
|
2015-04-20 06:52:49 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// class that puts the networking and the kademlia node in a single
|
|
|
|
// unit and connecting them together.
|
2015-05-09 20:06:02 +02:00
|
|
|
dht_tracker::dht_tracker(dht_observer* observer
|
2015-03-12 06:20:12 +01:00
|
|
|
, rate_limited_udp_socket& sock
|
2015-05-09 20:06:02 +02:00
|
|
|
, dht_settings const& settings
|
|
|
|
, counters& cnt
|
2015-09-27 01:00:36 +02:00
|
|
|
, dht_storage_constructor_type storage_constructor
|
2015-05-09 20:06:02 +02:00
|
|
|
, entry const* state)
|
2014-07-06 21:18:00 +02:00
|
|
|
: m_counters(cnt)
|
2015-09-27 01:00:36 +02:00
|
|
|
, m_dht(this, settings, extract_node_id(state), observer, cnt, storage_constructor)
|
2007-12-09 05:15:24 +01:00
|
|
|
, m_sock(sock)
|
2015-05-10 06:54:02 +02:00
|
|
|
, m_log(observer)
|
2015-05-09 20:06:02 +02:00
|
|
|
, m_last_new_key(clock_type::now() - minutes(int(key_refresh)))
|
2007-12-09 05:15:24 +01:00
|
|
|
, m_timer(sock.get_io_service())
|
|
|
|
, m_connection_timer(sock.get_io_service())
|
|
|
|
, m_refresh_timer(sock.get_io_service())
|
2006-08-01 17:27:08 +02:00
|
|
|
, m_settings(settings)
|
2008-03-24 05:38:43 +01:00
|
|
|
, m_abort(false)
|
2007-12-09 05:15:24 +01:00
|
|
|
, m_host_resolver(sock.get_io_service())
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-10 06:54:02 +02:00
|
|
|
m_log->log(dht_logger::tracker, "starting DHT tracker with node id: %s"
|
|
|
|
, to_hex(m_dht.nid().to_string()).c_str());
|
2008-11-11 18:51:02 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-30 17:30:38 +02:00
|
|
|
dht_tracker::~dht_tracker() {}
|
|
|
|
|
2011-11-28 12:11:29 +01:00
|
|
|
// defined in node.cpp
|
|
|
|
extern void nop();
|
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
void dht_tracker::start(entry const& bootstrap
|
|
|
|
, find_data::nodes_callback const& f)
|
2008-09-02 08:37:40 +02:00
|
|
|
{
|
2006-08-01 17:27:08 +02:00
|
|
|
std::vector<udp::endpoint> initial_nodes;
|
|
|
|
|
|
|
|
if (bootstrap.type() == entry::dictionary_t)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
if (entry const* nodes = bootstrap.find_key("nodes"))
|
|
|
|
read_endpoint_list<udp::endpoint>(nodes, initial_nodes);
|
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
|
|
|
m_timer.expires_from_now(seconds(1), ec);
|
2010-04-30 21:08:16 +02:00
|
|
|
m_timer.async_wait(boost::bind(&dht_tracker::tick, self(), _1));
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2013-01-14 03:42:44 +01:00
|
|
|
m_connection_timer.expires_from_now(seconds(1), ec);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_connection_timer.async_wait(
|
2010-04-30 21:08:16 +02:00
|
|
|
boost::bind(&dht_tracker::connection_timeout, self(), _1));
|
2006-08-06 18:36:00 +02:00
|
|
|
|
2008-10-21 10:45:42 +02:00
|
|
|
m_refresh_timer.expires_from_now(seconds(5), ec);
|
2010-04-30 21:08:16 +02:00
|
|
|
m_refresh_timer.async_wait(boost::bind(&dht_tracker::refresh_timeout, self(), _1));
|
2014-02-28 05:02:48 +01:00
|
|
|
m_dht.bootstrap(initial_nodes, f);
|
2007-02-25 10:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::stop()
|
|
|
|
{
|
2008-03-24 05:38:43 +01:00
|
|
|
m_abort = true;
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
|
|
|
m_timer.cancel(ec);
|
|
|
|
m_connection_timer.cancel(ec);
|
|
|
|
m_refresh_timer.cancel(ec);
|
2007-11-11 20:09:29 +01:00
|
|
|
m_host_resolver.cancel();
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 22:31:02 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2006-09-05 01:22:21 +02:00
|
|
|
void dht_tracker::dht_status(session_status& s)
|
|
|
|
{
|
2008-09-20 19:42:25 +02:00
|
|
|
m_dht.status(s);
|
2006-09-05 01:22:21 +02:00
|
|
|
}
|
2015-01-04 22:31:02 +01:00
|
|
|
#endif
|
2006-09-05 01:22:21 +02:00
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
void dht_tracker::dht_status(std::vector<dht_routing_bucket>& table
|
|
|
|
, std::vector<dht_lookup>& requests)
|
|
|
|
{
|
|
|
|
m_dht.status(table, requests);
|
|
|
|
}
|
|
|
|
|
2015-09-17 17:11:46 +02:00
|
|
|
void dht_tracker::update_stats_counters(counters& c) const
|
|
|
|
{
|
|
|
|
m_dht.update_stats_counters(c);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void dht_tracker::connection_timeout(error_code const& e)
|
2006-08-06 18:36:00 +02:00
|
|
|
{
|
2008-03-24 05:38:43 +01:00
|
|
|
if (e || m_abort) return;
|
|
|
|
|
2006-08-06 18:36:00 +02:00
|
|
|
time_duration d = m_dht.connection_timeout();
|
2008-10-21 19:10:11 +02:00
|
|
|
error_code ec;
|
|
|
|
m_connection_timer.expires_from_now(d, ec);
|
2010-04-30 21:08:16 +02:00
|
|
|
m_connection_timer.async_wait(boost::bind(&dht_tracker::connection_timeout, self(), _1));
|
2006-08-06 18:36:00 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void dht_tracker::refresh_timeout(error_code const& e)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2008-03-24 05:38:43 +01:00
|
|
|
if (e || m_abort) return;
|
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
m_dht.tick();
|
2015-01-02 00:24:21 +01:00
|
|
|
|
|
|
|
// periodically update the DOS blocker's settings from the dht_settings
|
|
|
|
m_blocker.set_block_timer(m_settings.block_timeout);
|
|
|
|
m_blocker.set_rate_limit(m_settings.block_ratelimit);
|
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
error_code ec;
|
2010-01-03 12:08:39 +01:00
|
|
|
m_refresh_timer.expires_from_now(seconds(5), ec);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_refresh_timer.async_wait(
|
2010-04-30 21:08:16 +02:00
|
|
|
boost::bind(&dht_tracker::refresh_timeout, self(), _1));
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void dht_tracker::tick(error_code const& e)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2008-03-24 05:38:43 +01:00
|
|
|
if (e || m_abort) return;
|
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
error_code ec;
|
|
|
|
m_timer.expires_from_now(minutes(tick_period), ec);
|
2010-04-30 21:08:16 +02:00
|
|
|
m_timer.async_wait(boost::bind(&dht_tracker::tick, self(), _1));
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-05-09 20:06:02 +02:00
|
|
|
time_point now = clock_type::now();
|
2014-07-06 21:18:00 +02:00
|
|
|
if (now - minutes(int(key_refresh)) > m_last_new_key)
|
2007-05-14 19:49:36 +02:00
|
|
|
{
|
|
|
|
m_last_new_key = now;
|
|
|
|
m_dht.new_write_key();
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-10 06:54:02 +02:00
|
|
|
m_log->log(dht_logger::tracker, "*** new write key***");
|
2007-05-14 19:49:36 +02:00
|
|
|
#endif
|
|
|
|
}
|
2015-05-08 07:21:26 +02:00
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM
|
2015-05-08 07:21:26 +02:00
|
|
|
std::ofstream st("dht_routing_table_state.txt", std::ios_base::trunc);
|
|
|
|
m_dht.print_state(st);
|
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2015-06-25 17:01:36 +02:00
|
|
|
void dht_tracker::get_peers(sha1_hash const& ih
|
|
|
|
, boost::function<void(std::vector<tcp::endpoint> const&)> f)
|
|
|
|
{
|
|
|
|
m_dht.get_peers(ih, f, NULL, false);
|
|
|
|
}
|
|
|
|
|
2014-01-20 07:35:06 +01:00
|
|
|
void dht_tracker::announce(sha1_hash const& ih, int listen_port, int flags
|
2008-12-23 21:04:12 +01:00
|
|
|
, boost::function<void(std::vector<tcp::endpoint> const&)> f)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2014-01-20 07:35:06 +01:00
|
|
|
m_dht.announce(ih, listen_port, flags, f);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-24 01:31:13 +01:00
|
|
|
void dht_tracker::get_item(sha1_hash const& target
|
|
|
|
, boost::function<void(item const&)> cb)
|
|
|
|
{
|
2015-09-22 20:10:57 +02:00
|
|
|
m_dht.get_item(target, cb);
|
2014-02-24 01:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// key is a 32-byte binary string, the public key to look up.
|
|
|
|
// the salt is optional
|
|
|
|
void dht_tracker::get_item(char const* key
|
2015-08-08 05:37:36 +02:00
|
|
|
, boost::function<void(item const&, bool)> cb
|
2014-02-24 01:31:13 +01:00
|
|
|
, std::string salt)
|
|
|
|
{
|
2015-09-22 20:10:57 +02:00
|
|
|
m_dht.get_item(key, salt, cb);
|
2014-02-24 01:31:13 +01:00
|
|
|
}
|
|
|
|
|
2015-11-22 19:00:29 +01:00
|
|
|
void dht_tracker::put_item(entry const& data
|
2015-09-22 20:10:57 +02:00
|
|
|
, boost::function<void(int)> cb)
|
2014-02-24 01:31:13 +01:00
|
|
|
{
|
|
|
|
std::string flat_data;
|
|
|
|
bencode(std::back_inserter(flat_data), data);
|
|
|
|
sha1_hash target = item_target_id(
|
2014-03-03 00:35:35 +01:00
|
|
|
std::pair<char const*, int>(flat_data.c_str(), flat_data.size()));
|
2014-02-24 01:31:13 +01:00
|
|
|
|
2015-09-22 20:10:57 +02:00
|
|
|
m_dht.put_item(target, data, cb);
|
2014-02-24 01:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::put_item(char const* key
|
2015-11-22 19:00:29 +01:00
|
|
|
, boost::function<void(item const&, int)> cb
|
2015-09-22 20:10:57 +02:00
|
|
|
, boost::function<void(item&)> data_cb, std::string salt)
|
2014-02-24 01:31:13 +01:00
|
|
|
{
|
2015-09-22 20:10:57 +02:00
|
|
|
m_dht.put_item(key, salt, cb, data_cb);
|
2014-02-24 01:31:13 +01:00
|
|
|
}
|
2008-05-08 02:22:17 +02:00
|
|
|
|
2015-08-12 06:49:09 +02:00
|
|
|
void dht_tracker::direct_request(udp::endpoint ep, entry& e
|
2014-02-17 06:56:49 +01:00
|
|
|
, boost::function<void(msg const&)> f)
|
|
|
|
{
|
|
|
|
m_dht.direct_request(ep, e, f);
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// translate bittorrent kademlia message into the generice kademlia message
|
|
|
|
// used by the library
|
2012-06-22 06:21:20 +02:00
|
|
|
bool dht_tracker::incoming_packet(error_code const& ec
|
|
|
|
, udp::endpoint const& ep, char const* buf, int size)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2012-06-22 06:21:20 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
if (ec == boost::asio::error::connection_refused
|
|
|
|
|| ec == boost::asio::error::connection_reset
|
|
|
|
|| ec == boost::asio::error::connection_aborted
|
2012-09-25 20:57:50 +02:00
|
|
|
#ifdef WIN32
|
2014-07-06 21:18:00 +02:00
|
|
|
|| ec == error_code(ERROR_HOST_UNREACHABLE, system_category())
|
|
|
|
|| ec == error_code(ERROR_PORT_UNREACHABLE, system_category())
|
|
|
|
|| ec == error_code(ERROR_CONNECTION_REFUSED, system_category())
|
|
|
|
|| ec == error_code(ERROR_CONNECTION_ABORTED, system_category())
|
2012-09-25 20:57:50 +02:00
|
|
|
#endif
|
|
|
|
)
|
2012-06-22 06:21:20 +02:00
|
|
|
{
|
|
|
|
m_dht.unreachable(ep);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size <= 20 || *buf != 'd' || buf[size-1] != 'e') return false;
|
|
|
|
|
2015-01-17 23:06:30 +01:00
|
|
|
m_counters.inc_stats_counter(counters::dht_bytes_in, size);
|
2008-11-02 11:01:04 +01:00
|
|
|
// account for IP and UDP overhead
|
2015-01-17 23:06:30 +01:00
|
|
|
m_counters.inc_stats_counter(counters::recv_ip_overhead_bytes
|
|
|
|
, ep.address().is_v6() ? 48 : 28);
|
|
|
|
m_counters.inc_stats_counter(counters::dht_messages_in);
|
|
|
|
|
2014-01-19 08:44:16 +01:00
|
|
|
if (m_settings.ignore_dark_internet && ep.address().is_v4())
|
|
|
|
{
|
|
|
|
address_v4::bytes_type b = ep.address().to_v4().to_bytes();
|
|
|
|
|
|
|
|
// these are class A networks not available to the public
|
|
|
|
// if we receive messages from here, that seems suspicious
|
|
|
|
boost::uint8_t class_a[] = { 3, 6, 7, 9, 11, 19, 21, 22, 25
|
2015-07-15 05:15:50 +02:00
|
|
|
, 26, 28, 29, 30, 33, 34, 48, 51, 56 };
|
2014-01-19 08:44:16 +01:00
|
|
|
|
|
|
|
int num = sizeof(class_a)/sizeof(class_a[0]);
|
|
|
|
if (std::find(class_a, class_a + num, b[0]) != class_a + num)
|
|
|
|
return true;
|
|
|
|
}
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2015-05-10 06:54:02 +02:00
|
|
|
if (!m_blocker.incoming(ep.address(), clock_type::now(), m_log))
|
2014-07-06 21:18:00 +02:00
|
|
|
return true;
|
2007-12-04 03:53:10 +01:00
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
using libtorrent::entry;
|
|
|
|
using libtorrent::bdecode;
|
2015-05-28 22:36:22 +02:00
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
TORRENT_ASSERT(size > 0);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2010-10-28 06:01:59 +02:00
|
|
|
int pos;
|
2012-06-22 06:21:20 +02:00
|
|
|
error_code err;
|
2015-03-12 06:20:12 +01:00
|
|
|
int ret = bdecode(buf, buf + size, m_msg, err, &pos, 10, 500);
|
2008-11-11 18:51:02 +01:00
|
|
|
if (ret != 0)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-28 22:36:22 +02:00
|
|
|
m_log->log_packet(dht_logger::incoming_message, buf, size, ep);
|
2009-09-20 17:21:31 +02:00
|
|
|
#endif
|
2012-06-22 06:21:20 +02:00
|
|
|
return false;
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
if (m_msg.type() != bdecode_node::dict_t)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-28 22:36:22 +02:00
|
|
|
m_log->log_packet(dht_logger::incoming_message, buf, size, ep);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2015-05-30 06:37:12 +02:00
|
|
|
// it's not a good idea to send a response to an invalid messages
|
2012-06-22 06:21:20 +02:00
|
|
|
return false;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-28 22:36:22 +02:00
|
|
|
m_log->log_packet(dht_logger::incoming_message, buf
|
|
|
|
, size, ep);
|
2015-05-16 21:29:49 +02:00
|
|
|
#endif
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
libtorrent::dht::msg m(m_msg, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
m_dht.incoming(m);
|
2012-06-22 06:21:20 +02:00
|
|
|
return true;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 06:52:49 +02:00
|
|
|
namespace {
|
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
void add_node_fun(void* userdata, node_entry const& e)
|
|
|
|
{
|
2015-08-11 02:03:24 +02:00
|
|
|
entry* n = static_cast<entry*>(userdata);
|
2010-01-03 12:08:39 +01:00
|
|
|
std::string node;
|
|
|
|
std::back_insert_iterator<std::string> out(node);
|
2015-07-04 04:33:17 +02:00
|
|
|
write_endpoint(e.ep(), out);
|
2010-01-03 12:08:39 +01:00
|
|
|
n->list().push_back(entry(node));
|
|
|
|
}
|
2015-04-20 06:52:49 +02:00
|
|
|
|
|
|
|
} // anonymous namespace
|
2015-05-28 22:36:22 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
entry dht_tracker::state() const
|
|
|
|
{
|
|
|
|
entry ret(entry::dictionary_t);
|
|
|
|
{
|
2006-09-01 05:06:00 +02:00
|
|
|
entry nodes(entry::list_t);
|
2010-01-03 12:08:39 +01:00
|
|
|
m_dht.m_table.for_each_node(&add_node_fun, &add_node_fun, &nodes);
|
2006-08-01 17:27:08 +02:00
|
|
|
bucket_t cache;
|
|
|
|
m_dht.replacement_cache(cache);
|
|
|
|
for (bucket_t::iterator i(cache.begin())
|
|
|
|
, end(cache.end()); i != end; ++i)
|
2006-09-01 05:06:00 +02:00
|
|
|
{
|
|
|
|
std::string node;
|
|
|
|
std::back_insert_iterator<std::string> out(node);
|
2015-07-04 04:33:17 +02:00
|
|
|
write_endpoint(i->ep(), out);
|
2006-09-01 05:06:00 +02:00
|
|
|
nodes.list().push_back(entry(node));
|
|
|
|
}
|
|
|
|
if (!nodes.list().empty())
|
2006-08-01 17:27:08 +02:00
|
|
|
ret["nodes"] = nodes;
|
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
|
2009-11-10 18:01:05 +01:00
|
|
|
ret["node-id"] = m_dht.nid().to_string();
|
2006-08-01 17:27:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::add_node(udp::endpoint node)
|
|
|
|
{
|
|
|
|
m_dht.add_node(node);
|
|
|
|
}
|
|
|
|
|
2010-02-14 08:46:57 +01:00
|
|
|
void dht_tracker::add_router_node(udp::endpoint const& node)
|
2006-09-27 19:20:18 +02:00
|
|
|
{
|
2010-02-14 08:46:57 +01:00
|
|
|
m_dht.add_router_node(node);
|
2006-09-27 19:20:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool dht_tracker::has_quota()
|
|
|
|
{
|
|
|
|
return m_sock.has_quota();
|
|
|
|
}
|
|
|
|
|
2011-01-23 03:02:04 +01:00
|
|
|
bool dht_tracker::send_packet(libtorrent::entry& e, udp::endpoint const& addr, int send_flags)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
using libtorrent::bencode;
|
|
|
|
using libtorrent::entry;
|
2008-12-23 21:04:12 +01:00
|
|
|
|
2011-01-23 03:02:04 +01:00
|
|
|
static char const version_str[] = {'L', 'T'
|
|
|
|
, LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR};
|
|
|
|
e["v"] = std::string(version_str, version_str + 4);
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
m_send_buf.clear();
|
|
|
|
bencode(std::back_inserter(m_send_buf), e);
|
2010-10-28 06:01:59 +02:00
|
|
|
error_code ec;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-08-31 19:48:14 +02:00
|
|
|
bool ret = m_sock.send(addr, &m_send_buf[0], int(m_send_buf.size()), ec, send_flags);
|
|
|
|
if (!ret || ec)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2015-08-31 19:48:14 +02:00
|
|
|
m_counters.inc_stats_counter(counters::dht_messages_out_dropped);
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-28 22:36:22 +02:00
|
|
|
m_log->log_packet(dht_logger::outgoing_message, &m_send_buf[0]
|
|
|
|
, m_send_buf.size(), addr);
|
2008-11-09 10:02:06 +01:00
|
|
|
#endif
|
2015-08-31 19:48:14 +02:00
|
|
|
return false;
|
2006-08-06 18:36:00 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-08-31 19:48:14 +02:00
|
|
|
m_counters.inc_stats_counter(counters::dht_bytes_out, m_send_buf.size());
|
|
|
|
// account for IP and UDP overhead
|
|
|
|
m_counters.inc_stats_counter(counters::sent_ip_overhead_bytes
|
|
|
|
, addr.address().is_v6() ? 48 : 28);
|
|
|
|
m_counters.inc_stats_counter(counters::dht_messages_out);
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-08-31 19:48:14 +02:00
|
|
|
m_log->log_packet(dht_logger::outgoing_message, &m_send_buf[0]
|
|
|
|
, m_send_buf.size(), addr);
|
2008-11-10 04:08:29 +01:00
|
|
|
#endif
|
2015-08-31 19:48:14 +02:00
|
|
|
return true;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
|