2006-08-01 17:27:08 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2006, Arvid Norberg
|
|
|
|
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 <fstream>
|
|
|
|
#include <set>
|
|
|
|
#include <numeric>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
|
|
|
|
#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"
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/version.hpp"
|
2008-05-19 09:36:04 +02:00
|
|
|
#include "libtorrent/escape_string.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
using boost::ref;
|
|
|
|
using boost::lexical_cast;
|
|
|
|
using libtorrent::dht::node_impl;
|
|
|
|
using libtorrent::dht::node_id;
|
|
|
|
using libtorrent::dht::packet_t;
|
|
|
|
using libtorrent::dht::msg;
|
|
|
|
namespace messages = libtorrent::dht::messages;
|
|
|
|
using namespace libtorrent::detail;
|
|
|
|
|
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
|
|
|
|
|
|
|
struct count_peers
|
|
|
|
{
|
|
|
|
int& count;
|
|
|
|
count_peers(int& c): count(c) {}
|
|
|
|
void operator()(std::pair<libtorrent::dht::node_id
|
|
|
|
, libtorrent::dht::torrent_entry> const& t)
|
|
|
|
{
|
|
|
|
count += std::distance(t.second.peers.begin()
|
|
|
|
, t.second.peers.end());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-09-21 20:22:26 +02:00
|
|
|
template <class EndpointType>
|
2008-11-11 18:51:02 +01:00
|
|
|
void read_endpoint_list(libtorrent::lazy_entry const* n, std::vector<EndpointType>& epl)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
if (n->type() != lazy_entry::list_t) return;
|
|
|
|
for (int i = 0; i < n->list_size(); ++i)
|
|
|
|
{
|
|
|
|
lazy_entry const* e = n->list_at(i);
|
|
|
|
if (e->type() != lazy_entry::string_t) return;
|
|
|
|
if (e->string_length() < 6) continue;
|
|
|
|
char const* in = e->string_ptr();
|
|
|
|
if (e->string_length() == 6)
|
|
|
|
epl.push_back(read_v4_endpoint<EndpointType>(in));
|
2009-04-04 18:59:53 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2008-11-11 18:51:02 +01:00
|
|
|
else if (e->string_length() == 18)
|
|
|
|
epl.push_back(read_v6_endpoint<EndpointType>(in));
|
2009-04-04 18:59:53 +02:00
|
|
|
#endif
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class EndpointType>
|
|
|
|
void read_endpoint_list(libtorrent::entry const* n, std::vector<EndpointType>& epl)
|
2006-09-21 20:22:26 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2008-10-21 10:45:42 +02:00
|
|
|
if (n->type() != entry::list_t) return;
|
2006-09-21 20:22:26 +02:00
|
|
|
entry::list_type const& contacts = n->list();
|
|
|
|
for (entry::list_type::const_iterator i = contacts.begin()
|
|
|
|
, end(contacts.end()); i != end; ++i)
|
|
|
|
{
|
2008-10-21 10:45:42 +02:00
|
|
|
if (i->type() != entry::string_t) return;
|
2006-09-21 20:22:26 +02:00
|
|
|
std::string const& p = i->string();
|
|
|
|
if (p.size() < 6) continue;
|
|
|
|
std::string::const_iterator in = p.begin();
|
|
|
|
if (p.size() == 6)
|
|
|
|
epl.push_back(read_v4_endpoint<EndpointType>(in));
|
2009-04-04 18:59:53 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2006-09-21 20:22:26 +02:00
|
|
|
else if (p.size() == 18)
|
|
|
|
epl.push_back(read_v6_endpoint<EndpointType>(in));
|
2009-04-04 18:59:53 +02:00
|
|
|
#endif
|
2006-09-21 20:22:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
2007-02-25 10:42:43 +01:00
|
|
|
|
|
|
|
void intrusive_ptr_add_ref(dht_tracker const* c)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(c != 0);
|
|
|
|
TORRENT_ASSERT(c->m_refs >= 0);
|
2007-02-25 10:42:43 +01:00
|
|
|
++c->m_refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void intrusive_ptr_release(dht_tracker const* c)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(c != 0);
|
|
|
|
TORRENT_ASSERT(c->m_refs > 0);
|
2007-02-25 10:42:43 +01:00
|
|
|
if (--c->m_refs == 0)
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_DEFINE_LOG(dht_tracker)
|
|
|
|
#endif
|
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
boost::optional<node_id> extract_node_id(lazy_entry const* e)
|
|
|
|
{
|
|
|
|
if (e == 0 || e->type() != lazy_entry::dict_t) return boost::optional<node_id>();
|
|
|
|
lazy_entry const* nid = e->dict_find_string("node-id");
|
|
|
|
if (nid == 0 || nid->string_length() != 20) return boost::optional<node_id>();
|
|
|
|
return boost::optional<node_id>(node_id(nid->string_ptr()));
|
|
|
|
}
|
|
|
|
|
2008-11-11 09:33:34 +01:00
|
|
|
boost::optional<node_id> extract_node_id(entry const* e)
|
|
|
|
{
|
|
|
|
if (e == 0 || e->type() != entry::dictionary_t) return boost::optional<node_id>();
|
|
|
|
entry const* nid = e->find_key("node-id");
|
|
|
|
if (nid == 0 || nid->type() != entry::string_t || nid->string().length() != 20)
|
|
|
|
return boost::optional<node_id>();
|
|
|
|
return boost::optional<node_id>(node_id(nid->string().c_str()));
|
|
|
|
}
|
|
|
|
|
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.
|
2008-11-08 18:40:06 +01:00
|
|
|
dht_tracker::dht_tracker(libtorrent::aux::session_impl& ses, rate_limited_udp_socket& sock
|
2008-11-11 09:33:34 +01:00
|
|
|
, dht_settings const& settings, entry const* state)
|
|
|
|
: m_dht(ses, bind(&dht_tracker::send_packet, this, _1), settings, extract_node_id(state))
|
2008-09-20 19:42:25 +02:00
|
|
|
, m_ses(ses)
|
2007-12-09 05:15:24 +01:00
|
|
|
, m_sock(sock)
|
2007-05-14 19:49:36 +02:00
|
|
|
, m_last_new_key(time_now() - minutes(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)
|
|
|
|
, m_refresh_bucket(160)
|
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())
|
2008-11-10 05:16:18 +01:00
|
|
|
, m_sent_bytes(0)
|
|
|
|
, m_received_bytes(0)
|
2007-02-25 10:42:43 +01:00
|
|
|
, m_refs(0)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
using boost::bind;
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
m_counter = 0;
|
|
|
|
std::fill_n(m_replies_bytes_sent, 5, 0);
|
|
|
|
std::fill_n(m_queries_bytes_received, 5, 0);
|
|
|
|
std::fill_n(m_replies_sent, 5, 0);
|
|
|
|
std::fill_n(m_queries_received, 5, 0);
|
|
|
|
m_announces = 0;
|
|
|
|
m_failed_announces = 0;
|
|
|
|
m_total_message_input = 0;
|
2008-11-12 04:56:56 +01:00
|
|
|
m_az_message_input = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
m_ut_message_input = 0;
|
|
|
|
m_lt_message_input = 0;
|
|
|
|
m_mp_message_input = 0;
|
|
|
|
m_gr_message_input = 0;
|
2007-03-02 19:40:02 +01:00
|
|
|
m_mo_message_input = 0;
|
2006-08-06 18:36:00 +02:00
|
|
|
m_total_in_bytes = 0;
|
|
|
|
m_total_out_bytes = 0;
|
|
|
|
m_queries_out_bytes = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
// turns on and off individual components' logging
|
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
rpc_log().enable(false);
|
2008-12-23 21:04:12 +01:00
|
|
|
// node_log().enable(false);
|
2008-11-11 18:51:02 +01:00
|
|
|
traversal_log().enable(false);
|
2006-08-01 17:27:08 +02:00
|
|
|
// dht_tracker_log.enable(false);
|
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
TORRENT_LOG(dht_tracker) << "starting DHT tracker with node id: " << m_dht.nid();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::incoming_error(char const* message, lazy_entry const& e, udp::endpoint const& ep)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_LOG(dht_tracker) << "ERROR: '" << message << "' " << e;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2008-11-11 18:51:02 +01:00
|
|
|
msg reply;
|
|
|
|
reply.reply = true;
|
|
|
|
reply.message_id = messages::error;
|
|
|
|
reply.error_code = 203; // Protocol error
|
|
|
|
reply.error_msg = message;
|
|
|
|
reply.addr = ep;
|
|
|
|
reply.transaction_id = "";
|
|
|
|
send_packet(reply);
|
2008-09-02 08:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::start(entry const& bootstrap)
|
|
|
|
{
|
2006-08-01 17:27:08 +02:00
|
|
|
std::vector<udp::endpoint> initial_nodes;
|
|
|
|
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2006-08-01 17:27:08 +02:00
|
|
|
if (bootstrap.type() == entry::dictionary_t)
|
|
|
|
{
|
2009-05-07 09:01:36 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-09-21 20:22:26 +02:00
|
|
|
try
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2009-05-07 09:01:36 +02:00
|
|
|
#endif
|
2006-09-21 20:22:26 +02:00
|
|
|
if (entry const* nodes = bootstrap.find_key("nodes"))
|
|
|
|
read_endpoint_list<udp::endpoint>(nodes, initial_nodes);
|
2009-05-07 09:01:36 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-09-21 20:22:26 +02:00
|
|
|
} catch (std::exception&) {}
|
2009-05-07 09:01:36 +02:00
|
|
|
#endif
|
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);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_timer.async_wait(bind(&dht_tracker::tick, self(), _1));
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-10-21 10:45:42 +02:00
|
|
|
m_connection_timer.expires_from_now(seconds(10), ec);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_connection_timer.async_wait(
|
|
|
|
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);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_refresh_timer.async_wait(bind(&dht_tracker::refresh_timeout, self(), _1));
|
2007-03-02 22:40:18 +01:00
|
|
|
|
|
|
|
m_dht.bootstrap(initial_nodes, bind(&dht_tracker::on_bootstrap, self()));
|
2007-02-25 10:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::stop()
|
|
|
|
{
|
2008-03-24 05:38:43 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-09-05 01:22:21 +02:00
|
|
|
void dht_tracker::dht_status(session_status& s)
|
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2008-09-20 19:42:25 +02:00
|
|
|
m_dht.status(s);
|
2006-09-05 01:22:21 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 11:01:04 +01:00
|
|
|
void dht_tracker::network_stats(int& sent, int& received)
|
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
sent = m_sent_bytes;
|
|
|
|
received = m_received_bytes;
|
|
|
|
m_sent_bytes = 0;
|
|
|
|
m_received_bytes = 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
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);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_connection_timer.async_wait(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
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
if (e || m_abort) return;
|
|
|
|
|
2006-08-06 18:36:00 +02:00
|
|
|
time_duration d = m_dht.refresh_timeout();
|
2008-10-21 19:10:11 +02:00
|
|
|
error_code ec;
|
|
|
|
m_refresh_timer.expires_from_now(d, ec);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_refresh_timer.async_wait(
|
|
|
|
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
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
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);
|
2007-12-09 05:15:24 +01:00
|
|
|
m_timer.async_wait(bind(&dht_tracker::tick, self(), _1));
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-05-14 19:49:36 +02:00
|
|
|
ptime now = time_now();
|
|
|
|
if (now - m_last_new_key > minutes(key_refresh))
|
|
|
|
{
|
|
|
|
m_last_new_key = now;
|
|
|
|
m_dht.new_write_key();
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-12-23 21:04:12 +01:00
|
|
|
TORRENT_LOG(dht_tracker) << " *** new write key";
|
2007-05-14 19:49:36 +02:00
|
|
|
#endif
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
static bool first = true;
|
|
|
|
|
2008-11-12 04:56:56 +01:00
|
|
|
std::ofstream st("dht_routing_table_state.txt", std::ios_base::trunc);
|
2006-08-01 17:27:08 +02:00
|
|
|
m_dht.print_state(st);
|
|
|
|
|
|
|
|
// count torrents
|
|
|
|
int torrents = std::distance(m_dht.begin_data(), m_dht.end_data());
|
|
|
|
|
|
|
|
// count peers
|
|
|
|
int peers = 0;
|
|
|
|
std::for_each(m_dht.begin_data(), m_dht.end_data(), count_peers(peers));
|
|
|
|
|
2009-01-01 19:29:42 +01:00
|
|
|
std::ofstream pc("dht_stats.log", first ? std::ios_base::trunc : std::ios_base::app);
|
2006-08-01 17:27:08 +02:00
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
2007-04-10 09:52:58 +02:00
|
|
|
pc << "\n\n ***** starting log at " << time_now_string() << " *****\n\n"
|
2006-08-01 17:27:08 +02:00
|
|
|
<< "minute:active nodes:passive nodes"
|
2008-12-23 19:38:48 +01:00
|
|
|
":ping replies sent:ping queries recvd"
|
|
|
|
":ping replies bytes sent:ping queries bytes recvd"
|
|
|
|
":find_node replies sent:find_node queries recv"
|
2006-08-01 17:27:08 +02:00
|
|
|
":find_node replies bytes sent:find_node queries bytes recv"
|
2008-12-23 19:38:48 +01:00
|
|
|
":get_peers replies sent:get_peers queries recvd"
|
2006-08-01 17:27:08 +02:00
|
|
|
":get_peers replies bytes sent:get_peers queries bytes recv"
|
2008-12-23 19:38:48 +01:00
|
|
|
":announce_peer replies sent:announce_peer queries recvd"
|
2006-08-01 17:27:08 +02:00
|
|
|
":announce_peer replies bytes sent:announce_peer queries bytes recv"
|
2008-12-23 19:38:48 +01:00
|
|
|
":error replies sent:error queries recvd"
|
2006-08-01 17:27:08 +02:00
|
|
|
":error replies bytes sent:error queries bytes recv"
|
|
|
|
":num torrents:num peers:announces per min"
|
|
|
|
":failed announces per min:total msgs per min"
|
2008-12-23 19:38:48 +01:00
|
|
|
":az msgs per min:ut msgs per min:lt msgs per min:mp msgs per min"
|
|
|
|
":gr msgs per min:mo msgs per min:bytes in per sec:bytes out per sec"
|
2006-08-06 18:36:00 +02:00
|
|
|
":queries out bytes per sec\n\n";
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int active;
|
|
|
|
int passive;
|
|
|
|
boost::tie(active, passive) = m_dht.size();
|
|
|
|
pc << (m_counter * tick_period)
|
|
|
|
<< "\t" << active
|
|
|
|
<< "\t" << passive;
|
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
pc << "\t" << (m_replies_sent[i] / float(tick_period))
|
|
|
|
<< "\t" << (m_queries_received[i] / float(tick_period))
|
|
|
|
<< "\t" << (m_replies_bytes_sent[i] / float(tick_period*60))
|
|
|
|
<< "\t" << (m_queries_bytes_received[i] / float(tick_period*60));
|
|
|
|
|
|
|
|
pc << "\t" << torrents
|
|
|
|
<< "\t" << peers
|
|
|
|
<< "\t" << m_announces / float(tick_period)
|
|
|
|
<< "\t" << m_failed_announces / float(tick_period)
|
|
|
|
<< "\t" << (m_total_message_input / float(tick_period))
|
2008-11-12 04:56:56 +01:00
|
|
|
<< "\t" << (m_az_message_input / float(tick_period))
|
2006-08-01 17:27:08 +02:00
|
|
|
<< "\t" << (m_ut_message_input / float(tick_period))
|
|
|
|
<< "\t" << (m_lt_message_input / float(tick_period))
|
|
|
|
<< "\t" << (m_mp_message_input / float(tick_period))
|
|
|
|
<< "\t" << (m_gr_message_input / float(tick_period))
|
2007-03-02 19:40:02 +01:00
|
|
|
<< "\t" << (m_mo_message_input / float(tick_period))
|
2006-08-06 18:36:00 +02:00
|
|
|
<< "\t" << (m_total_in_bytes / float(tick_period*60))
|
|
|
|
<< "\t" << (m_total_out_bytes / float(tick_period*60))
|
|
|
|
<< "\t" << (m_queries_out_bytes / float(tick_period*60))
|
2006-08-01 17:27:08 +02:00
|
|
|
<< std::endl;
|
|
|
|
++m_counter;
|
|
|
|
std::fill_n(m_replies_bytes_sent, 5, 0);
|
|
|
|
std::fill_n(m_queries_bytes_received, 5, 0);
|
|
|
|
std::fill_n(m_replies_sent, 5, 0);
|
|
|
|
std::fill_n(m_queries_received, 5, 0);
|
|
|
|
m_announces = 0;
|
|
|
|
m_failed_announces = 0;
|
|
|
|
m_total_message_input = 0;
|
2008-11-12 04:56:56 +01:00
|
|
|
m_az_message_input = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
m_ut_message_input = 0;
|
|
|
|
m_lt_message_input = 0;
|
2006-08-06 18:36:00 +02:00
|
|
|
m_total_in_bytes = 0;
|
|
|
|
m_total_out_bytes = 0;
|
|
|
|
m_queries_out_bytes = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::announce(sha1_hash const& ih, int listen_port
|
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
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2006-08-01 17:27:08 +02:00
|
|
|
m_dht.announce(ih, listen_port, f);
|
|
|
|
}
|
|
|
|
|
2008-05-08 02:22:17 +02:00
|
|
|
|
|
|
|
void dht_tracker::on_unreachable(udp::endpoint const& ep)
|
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2008-05-08 02:22:17 +02:00
|
|
|
m_dht.unreachable(ep);
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// translate bittorrent kademlia message into the generice kademlia message
|
|
|
|
// used by the library
|
2007-12-09 05:15:24 +01:00
|
|
|
void dht_tracker::on_receive(udp::endpoint const& ep, char const* buf, int bytes_transferred)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
// account for IP and UDP overhead
|
2008-11-14 21:51:49 +01:00
|
|
|
m_received_bytes += bytes_transferred + (ep.address().is_v6() ? 48 : 28);
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2007-12-04 03:53:10 +01:00
|
|
|
node_ban_entry* match = 0;
|
|
|
|
node_ban_entry* min = m_ban_nodes;
|
|
|
|
ptime now = time_now();
|
|
|
|
for (node_ban_entry* i = m_ban_nodes; i < m_ban_nodes + num_ban_nodes; ++i)
|
|
|
|
{
|
2009-05-13 23:07:51 +02:00
|
|
|
if (i->src == ep.address())
|
2007-12-04 03:53:10 +01:00
|
|
|
{
|
|
|
|
match = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i->count < min->count) min = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match)
|
|
|
|
{
|
|
|
|
++match->count;
|
|
|
|
if (match->count >= 20)
|
|
|
|
{
|
|
|
|
if (now < match->limit)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
if (match->count == 20)
|
|
|
|
{
|
|
|
|
TORRENT_LOG(dht_tracker) << time_now_string() << " BANNING PEER [ ip: "
|
2008-05-18 07:09:11 +02:00
|
|
|
<< ep << " time: " << total_milliseconds((now - match->limit) + seconds(5)) / 1000.f
|
|
|
|
<< " count: " << match->count << " ]";
|
2007-12-04 03:53:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// we've received 20 messages in less than 5 seconds from
|
|
|
|
// this node. Ignore it until it's silent for 5 minutes
|
|
|
|
match->limit = now + minutes(5);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we got 50 messages from this peer, but it was in
|
|
|
|
// more than 5 seconds. Reset the counter and the timer
|
|
|
|
match->count = 0;
|
|
|
|
match->limit = now + seconds(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
min->count = 1;
|
|
|
|
min->limit = now + seconds(5);
|
2009-05-13 23:07:51 +02:00
|
|
|
min->src = ep.address();
|
2007-12-04 03:53:10 +01:00
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
++m_total_message_input;
|
2006-08-06 18:36:00 +02:00
|
|
|
m_total_in_bytes += bytes_transferred;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
using libtorrent::entry;
|
|
|
|
using libtorrent::bdecode;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
TORRENT_ASSERT(bytes_transferred > 0);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry e;
|
|
|
|
int ret = lazy_bdecode(buf, buf + bytes_transferred, e);
|
|
|
|
if (ret != 0)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("invalid bencoding", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
std::stringstream log_line;
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << "RECEIVED ["
|
2008-10-21 19:10:11 +02:00
|
|
|
" ip: " << ep;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
if (e.type() != lazy_entry::dict_t)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("message is not a dictionary", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-09-02 08:37:40 +02:00
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
libtorrent::dht::msg m;
|
|
|
|
m.message_id = 0;
|
|
|
|
m.addr = ep;
|
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* transaction = e.dict_find_string("t");
|
|
|
|
if (!transaction)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("missing or invalid transaction id", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
m.transaction_id = transaction->string_value();
|
2008-10-21 19:10:11 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* ver = e.dict_find_string("v");
|
|
|
|
if (!ver)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
|
|
|
log_line << " c: generic";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
std::string const& client = ver->string_value();
|
|
|
|
if (client.size() < 2)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << " c: " << client;
|
|
|
|
}
|
2008-11-12 04:56:56 +01:00
|
|
|
else if (std::equal(client.begin(), client.begin() + 2, "Az"))
|
|
|
|
{
|
|
|
|
++m_az_message_input;
|
|
|
|
log_line << " c: Azureus";
|
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
else if (std::equal(client.begin(), client.begin() + 2, "UT"))
|
|
|
|
{
|
|
|
|
++m_ut_message_input;
|
|
|
|
log_line << " c: uTorrent";
|
|
|
|
}
|
|
|
|
else if (std::equal(client.begin(), client.begin() + 2, "LT"))
|
|
|
|
{
|
|
|
|
++m_lt_message_input;
|
|
|
|
log_line << " c: libtorrent";
|
|
|
|
}
|
|
|
|
else if (std::equal(client.begin(), client.begin() + 2, "MP"))
|
|
|
|
{
|
|
|
|
++m_mp_message_input;
|
|
|
|
log_line << " c: MooPolice";
|
|
|
|
}
|
|
|
|
else if (std::equal(client.begin(), client.begin() + 2, "GR"))
|
|
|
|
{
|
|
|
|
++m_gr_message_input;
|
|
|
|
log_line << " c: GetRight";
|
|
|
|
}
|
|
|
|
else if (std::equal(client.begin(), client.begin() + 2, "MO"))
|
|
|
|
{
|
|
|
|
++m_mo_message_input;
|
|
|
|
log_line << " c: Mono Torrent";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_line << " c: " << client;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* y = e.dict_find_string("y");
|
|
|
|
if (!y || y->string_length() < 1)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("missing or invalid message type", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
char msg_type = *y->string_ptr();
|
2008-10-21 19:10:11 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
if (msg_type == 'r')
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << " t: " << to_hex(m.transaction_id);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
m.reply = true;
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* r = e.dict_find_dict("r");
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid reply dict", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* id = r->dict_find_string("id");
|
|
|
|
if (!id)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid id", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
|
|
|
if (id->string_length() != 20)
|
|
|
|
{
|
|
|
|
incoming_error("invalid node id (not 20 bytes)", e, ep);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::copy(id->string_ptr(), id->string_ptr()
|
|
|
|
+ id->string_length(), m.id.begin());
|
2008-10-21 19:10:11 +02:00
|
|
|
|
2008-12-23 21:04:12 +01:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " id: " << m.id;
|
|
|
|
#endif
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* n = r->dict_find_list("values");
|
|
|
|
if (n)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
|
|
|
m.peers.clear();
|
2008-11-11 18:51:02 +01:00
|
|
|
if (n->list_size() == 1 && n->list_at(0)->type() == lazy_entry::string_t)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2008-10-21 19:10:11 +02:00
|
|
|
// assume it's mainline format
|
2008-11-11 18:51:02 +01:00
|
|
|
char const* peers = n->list_at(0)->string_ptr();
|
|
|
|
char const* end = peers + n->list_at(0)->string_length();
|
2007-03-09 21:37:17 +01:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
while (end - peers >= 6)
|
|
|
|
m.peers.push_back(read_v4_endpoint<tcp::endpoint>(peers));
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// assume it's uTorrent/libtorrent format
|
|
|
|
read_endpoint_list<tcp::endpoint>(n, m.peers);
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
log_line << " p: " << m.peers.size();
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
m.nodes.clear();
|
2008-11-11 18:51:02 +01:00
|
|
|
n = r->dict_find_string("nodes");
|
|
|
|
if (n)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
char const* nodes = n->string_ptr();
|
|
|
|
char const* end = nodes + n->string_length();
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
while (end - nodes >= 26)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
|
|
|
node_id id;
|
2008-11-11 18:51:02 +01:00
|
|
|
std::copy(nodes, nodes + 20, id.begin());
|
|
|
|
nodes += 20;
|
2008-10-21 19:10:11 +02:00
|
|
|
m.nodes.push_back(libtorrent::dht::node_entry(
|
2008-11-11 18:51:02 +01:00
|
|
|
id, read_v4_endpoint<udp::endpoint>(nodes)));
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
log_line << " n: " << m.nodes.size();
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
n = r->dict_find_list("nodes2");
|
|
|
|
if (n)
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
for (int i = 0; i < n->list_size(); ++i)
|
2006-09-21 20:22:26 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01: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();
|
2008-10-21 19:10:11 +02:00
|
|
|
|
|
|
|
node_id id;
|
|
|
|
std::copy(in, in + 20, id.begin());
|
|
|
|
in += 20;
|
2008-11-11 18:51:02 +01:00
|
|
|
if (p->string_length() == 6 + 20)
|
2008-10-21 19:10:11 +02:00
|
|
|
m.nodes.push_back(libtorrent::dht::node_entry(
|
2008-11-11 18:51:02 +01:00
|
|
|
id, read_v4_endpoint<udp::endpoint>(in)));
|
2009-04-04 18:59:53 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2008-11-11 18:51:02 +01:00
|
|
|
else if (p->string_length() == 18 + 20)
|
2008-10-21 19:10:11 +02:00
|
|
|
m.nodes.push_back(libtorrent::dht::node_entry(
|
2008-11-11 18:51:02 +01:00
|
|
|
id, read_v6_endpoint<udp::endpoint>(in)));
|
2009-04-04 18:59:53 +02:00
|
|
|
#endif
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2006-09-21 20:22:26 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
log_line << " n2: " << m.nodes.size();
|
2006-09-21 20:22:26 +02:00
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* token = r->dict_find_string("token");
|
|
|
|
if (token)
|
|
|
|
{
|
|
|
|
m.write_token = token->string_value();
|
|
|
|
TORRENT_ASSERT(m.write_token.size() == token->string_length());
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " token: " << to_hex(m.write_token);
|
|
|
|
#endif
|
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
else if (msg_type == 'q')
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
|
|
|
m.reply = false;
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* a = e.dict_find_dict("a");
|
|
|
|
if (!a)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid argument dictionary", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* id = a->dict_find_string("id");
|
|
|
|
if (!id)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid node id", e, ep);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (id->string_length() != 20)
|
|
|
|
{
|
|
|
|
incoming_error("invalid node id (not 20 bytes)", e, ep);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::copy(id->string_ptr(), id->string_ptr()
|
|
|
|
+ id->string_length(), m.id.begin());
|
2008-10-21 19:10:11 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* q = e.dict_find_string("q");
|
|
|
|
if (!q)
|
|
|
|
{
|
|
|
|
incoming_error("invalid or missing query string", e, ep);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string request_kind = q->string_value();
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
log_line << " q: " << request_kind;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
if (request_kind == "ping")
|
|
|
|
{
|
|
|
|
m.message_id = libtorrent::dht::messages::ping;
|
|
|
|
}
|
|
|
|
else if (request_kind == "find_node")
|
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* target = a->dict_find_string("target");
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid target", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
if (target->string_length() != 20)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("invalid target (not 20 bytes)", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
std::copy(target->string_ptr(), target->string_ptr()
|
|
|
|
+ target->string_length(), m.info_hash.begin());
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
log_line << " t: " << boost::lexical_cast<std::string>(m.info_hash);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-21 19:10:11 +02:00
|
|
|
m.message_id = libtorrent::dht::messages::find_node;
|
|
|
|
}
|
|
|
|
else if (request_kind == "get_peers")
|
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* info_hash = a->dict_find_string("info_hash");
|
|
|
|
if (!info_hash)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("missing or invalid info_hash", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
if (info_hash->string_length() != 20)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("invalid info_hash (not 20 bytes)", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
std::copy(info_hash->string_ptr(), info_hash->string_ptr()
|
|
|
|
+ info_hash->string_length(), m.info_hash.begin());
|
2008-10-21 19:10:11 +02:00
|
|
|
m.message_id = libtorrent::dht::messages::get_peers;
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
log_line << " ih: " << boost::lexical_cast<std::string>(m.info_hash);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
|
|
|
else if (request_kind == "announce_peer")
|
|
|
|
{
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-10-21 19:10:11 +02:00
|
|
|
++m_announces;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2008-11-11 18:51:02 +01:00
|
|
|
lazy_entry const* info_hash = a->dict_find_string("info_hash");
|
|
|
|
if (!info_hash)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("missing or invalid info_hash", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
if (info_hash->string_length() != 20)
|
2008-11-09 01:37:03 +01:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("invalid info_hash (not 20 bytes)", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-09 01:37:03 +01:00
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
std::copy(info_hash->string_ptr(), info_hash->string_ptr()
|
|
|
|
+ info_hash->string_length(), m.info_hash.begin());
|
|
|
|
m.port = a->dict_find_int_value("port", -1);
|
|
|
|
if (m.port == -1)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid port in announce_peer message", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
|
|
|
lazy_entry const* token = a->dict_find_string("token");
|
|
|
|
if (!token)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid token in announce peer", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
|
|
|
m.write_token = token->string_value();
|
2008-10-21 19:10:11 +02:00
|
|
|
m.message_id = libtorrent::dht::messages::announce_peer;
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-12-23 21:04:12 +01:00
|
|
|
log_line << " token: " << to_hex(m.write_token);
|
2008-10-21 19:10:11 +02:00
|
|
|
log_line << " ih: " << boost::lexical_cast<std::string>(m.info_hash);
|
|
|
|
log_line << " p: " << m.port;
|
|
|
|
|
|
|
|
if (!m_dht.verify_token(m))
|
|
|
|
++m_failed_announces;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("unknown query", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
else if (msg_type == 'e')
|
2008-10-21 19:10:11 +02:00
|
|
|
{
|
|
|
|
m.message_id = messages::error;
|
2008-11-11 18:51:02 +01:00
|
|
|
m.error_code = 0;
|
|
|
|
lazy_entry const* list = e.dict_find_list("e");
|
|
|
|
if (!list)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
list = e.dict_find_string("e");
|
|
|
|
if (!list)
|
|
|
|
{
|
|
|
|
incoming_error("missing or invalid 'e' in error message", e, ep);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m.error_msg = list->string_value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (list->list_size() > 0 && list->list_at(0)->type() == lazy_entry::int_t)
|
|
|
|
m.error_code = list->list_at(0)->int_value();
|
|
|
|
if (list->list_size() > 1 && list->list_at(1)->type() == lazy_entry::string_t)
|
|
|
|
m.error_msg = list->list_at(1)->string_value();
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2008-11-11 18:51:02 +01:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-05-18 08:02:57 +02:00
|
|
|
TORRENT_LOG(dht_tracker) << log_line.str() << " ]";
|
2009-01-11 03:02:34 +01:00
|
|
|
TORRENT_LOG(dht_tracker) << "ERROR: incoming error: " << m.error_code
|
2008-11-11 18:51:02 +01:00
|
|
|
<< " " << m.error_msg;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
else
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2008-11-11 18:51:02 +01:00
|
|
|
incoming_error("unknown message", e, ep);
|
2008-10-21 19:10:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-11 18:51:02 +01:00
|
|
|
TORRENT_LOG(dht_tracker) << log_line.str() << " ]";
|
|
|
|
// TORRENT_LOG(dht_tracker) << std::string(buf, buf + bytes_transferred);
|
2008-10-21 19:10:11 +02:00
|
|
|
if (!m.reply)
|
|
|
|
{
|
|
|
|
++m_queries_received[m.message_id];
|
|
|
|
m_queries_bytes_received[m.message_id] += int(bytes_transferred);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m.message_id != messages::error);
|
|
|
|
m_dht.incoming(m);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
entry dht_tracker::state() const
|
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2006-08-01 17:27:08 +02:00
|
|
|
entry ret(entry::dictionary_t);
|
|
|
|
{
|
2006-09-01 05:06:00 +02:00
|
|
|
entry nodes(entry::list_t);
|
2006-08-01 17:27:08 +02:00
|
|
|
for (node_impl::iterator i(m_dht.begin())
|
|
|
|
, end(m_dht.end()); i != end; ++i)
|
2006-09-01 05:06:00 +02:00
|
|
|
{
|
|
|
|
std::string node;
|
|
|
|
std::back_insert_iterator<std::string> out(node);
|
2008-11-10 03:08:42 +01:00
|
|
|
write_endpoint(udp::endpoint(i->addr, i->port), out);
|
2006-09-01 05:06:00 +02:00
|
|
|
nodes.list().push_back(entry(node));
|
|
|
|
}
|
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);
|
2008-11-10 03:08:42 +01:00
|
|
|
write_endpoint(udp::endpoint(i->addr, i->port), 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-04-04 11:52:25 +02:00
|
|
|
char node_id[41];
|
|
|
|
to_hex((char*)&m_dht.nid()[0], 20, node_id);
|
|
|
|
ret["node-id"] = node_id;
|
2006-08-01 17:27:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::add_node(udp::endpoint node)
|
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2006-08-01 17:27:08 +02:00
|
|
|
m_dht.add_node(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dht_tracker::add_node(std::pair<std::string, int> const& node)
|
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2006-08-01 17:27:08 +02:00
|
|
|
udp::resolver::query q(node.first, lexical_cast<std::string>(node.second));
|
2007-12-09 05:15:24 +01:00
|
|
|
m_host_resolver.async_resolve(q,
|
|
|
|
bind(&dht_tracker::on_name_lookup, self(), _1, _2));
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
2006-09-27 19:20:18 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void dht_tracker::on_name_lookup(error_code const& e
|
2008-10-21 19:10:11 +02:00
|
|
|
, udp::resolver::iterator host)
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
if (e || host == udp::resolver::iterator()) return;
|
|
|
|
add_node(host->endpoint());
|
|
|
|
}
|
|
|
|
|
2006-09-27 19:20:18 +02:00
|
|
|
void dht_tracker::add_router_node(std::pair<std::string, int> const& node)
|
|
|
|
{
|
2008-11-02 11:01:04 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2006-09-27 19:20:18 +02:00
|
|
|
udp::resolver::query q(node.first, lexical_cast<std::string>(node.second));
|
2007-12-09 05:15:24 +01:00
|
|
|
m_host_resolver.async_resolve(q,
|
|
|
|
bind(&dht_tracker::on_router_name_lookup, self(), _1, _2));
|
2006-09-27 19:20:18 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void dht_tracker::on_router_name_lookup(error_code const& e
|
2008-10-21 19:10:11 +02:00
|
|
|
, udp::resolver::iterator host)
|
2006-09-27 19:20:18 +02:00
|
|
|
{
|
|
|
|
if (e || host == udp::resolver::iterator()) return;
|
|
|
|
m_dht.add_router_node(host->endpoint());
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void dht_tracker::on_bootstrap()
|
|
|
|
{}
|
|
|
|
|
2007-05-07 20:00:17 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void write_nodes_entry(entry& r, libtorrent::dht::msg const& m)
|
|
|
|
{
|
|
|
|
bool ipv6_nodes = false;
|
|
|
|
entry& n = r["nodes"];
|
|
|
|
std::back_insert_iterator<std::string> out(n.string());
|
|
|
|
for (msg::nodes_t::const_iterator i = m.nodes.begin()
|
|
|
|
, end(m.nodes.end()); i != end; ++i)
|
|
|
|
{
|
2008-11-10 03:08:42 +01:00
|
|
|
if (!i->addr.is_v4())
|
2007-05-07 20:00:17 +02:00
|
|
|
{
|
|
|
|
ipv6_nodes = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::copy(i->id.begin(), i->id.end(), out);
|
2008-11-10 03:08:42 +01:00
|
|
|
write_endpoint(udp::endpoint(i->addr, i->port), out);
|
2007-05-07 20:00:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ipv6_nodes)
|
|
|
|
{
|
|
|
|
entry& p = r["nodes2"];
|
|
|
|
std::string endpoint;
|
|
|
|
for (msg::nodes_t::const_iterator i = m.nodes.begin()
|
|
|
|
, end(m.nodes.end()); i != end; ++i)
|
|
|
|
{
|
2008-11-10 03:08:42 +01:00
|
|
|
if (!i->addr.is_v6()) continue;
|
2007-05-07 20:00:17 +02:00
|
|
|
endpoint.resize(18 + 20);
|
|
|
|
std::string::iterator out = endpoint.begin();
|
|
|
|
std::copy(i->id.begin(), i->id.end(), out);
|
|
|
|
out += 20;
|
2008-11-10 03:08:42 +01:00
|
|
|
write_endpoint(udp::endpoint(i->addr, i->port), out);
|
2007-05-07 20:00:17 +02:00
|
|
|
endpoint.resize(out - endpoint.begin());
|
|
|
|
p.list().push_back(entry(endpoint));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void dht_tracker::send_packet(msg const& m)
|
|
|
|
{
|
|
|
|
using libtorrent::bencode;
|
|
|
|
using libtorrent::entry;
|
2008-11-09 01:37:03 +01:00
|
|
|
int send_flags = 0;
|
2006-08-01 17:27:08 +02:00
|
|
|
entry e(entry::dictionary_t);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m.transaction_id.empty() || m.message_id == messages::error);
|
2006-08-01 17:27:08 +02:00
|
|
|
e["t"] = m.transaction_id;
|
2007-05-12 03:52:25 +02:00
|
|
|
static char const version_str[] = {'L', 'T'
|
|
|
|
, LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR};
|
|
|
|
e["v"] = std::string(version_str, version_str + 4);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-05-18 07:09:11 +02:00
|
|
|
std::stringstream log_line;
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << "SENDING [ ip: " << m.addr
|
2008-05-18 07:09:11 +02:00
|
|
|
<< " t: " << to_hex(m.transaction_id);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m.message_id == messages::error)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m.reply);
|
2006-08-01 17:27:08 +02:00
|
|
|
e["y"] = "e";
|
|
|
|
entry error_list(entry::list_t);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m.error_code > 200 && m.error_code <= 204);
|
2006-08-01 17:27:08 +02:00
|
|
|
error_list.list().push_back(entry(m.error_code));
|
|
|
|
error_list.list().push_back(entry(m.error_msg));
|
|
|
|
e["e"] = error_list;
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-05-18 07:09:11 +02:00
|
|
|
log_line << " err: " << m.error_code
|
|
|
|
<< " msg: " << m.error_msg;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (m.reply)
|
|
|
|
{
|
|
|
|
e["y"] = "r";
|
|
|
|
e["r"] = entry(entry::dictionary_t);
|
|
|
|
entry& r = e["r"];
|
|
|
|
r["id"] = std::string(m.id.begin(), m.id.end());
|
2008-11-11 18:51:02 +01:00
|
|
|
if (!m.write_token.empty())
|
|
|
|
{
|
|
|
|
r["token"] = m.write_token;
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << " token: " << to_hex(m.write_token);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
log_line << " r: " << messages::ids[m.message_id]
|
|
|
|
<< " id: " << m.id;
|
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
switch (m.message_id)
|
|
|
|
{
|
|
|
|
case messages::ping:
|
|
|
|
break;
|
|
|
|
case messages::find_node:
|
|
|
|
{
|
2007-05-07 20:00:17 +02:00
|
|
|
write_nodes_entry(r, m);
|
2006-08-01 17:27:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case messages::get_peers:
|
|
|
|
{
|
2008-12-23 21:04:12 +01:00
|
|
|
write_nodes_entry(r, m);
|
|
|
|
|
|
|
|
if (!m.peers.empty())
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
r["values"] = entry(entry::list_t);
|
|
|
|
entry& p = r["values"];
|
|
|
|
std::string endpoint;
|
|
|
|
for (msg::peers_t::const_iterator i = m.peers.begin()
|
|
|
|
, end(m.peers.end()); i != end; ++i)
|
|
|
|
{
|
2007-05-07 20:00:17 +02:00
|
|
|
endpoint.resize(18);
|
2006-08-01 17:27:08 +02:00
|
|
|
std::string::iterator out = endpoint.begin();
|
|
|
|
write_endpoint(*i, out);
|
2007-05-07 20:00:17 +02:00
|
|
|
endpoint.resize(out - endpoint.begin());
|
2006-08-01 17:27:08 +02:00
|
|
|
p.list().push_back(entry(endpoint));
|
|
|
|
}
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << " values: " << m.peers.size();
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case messages::announce_peer:
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-09 01:37:03 +01:00
|
|
|
// set bit 1 of send_flags to indicate that
|
|
|
|
// this packet should not be dropped by the
|
|
|
|
// rate limiter.
|
2006-08-01 17:27:08 +02:00
|
|
|
e["y"] = "q";
|
|
|
|
e["a"] = entry(entry::dictionary_t);
|
|
|
|
entry& a = e["a"];
|
|
|
|
a["id"] = std::string(m.id.begin(), m.id.end());
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m.message_id <= messages::error);
|
2006-08-01 17:27:08 +02:00
|
|
|
e["q"] = messages::ids[m.message_id];
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << " q: " << messages::ids[m.message_id]
|
|
|
|
<< " id: " << m.id;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (m.message_id)
|
|
|
|
{
|
|
|
|
case messages::find_node:
|
|
|
|
{
|
2008-11-09 01:37:03 +01:00
|
|
|
send_flags = 1;
|
2006-08-01 17:27:08 +02:00
|
|
|
a["target"] = std::string(m.info_hash.begin(), m.info_hash.end());
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-05-18 07:09:11 +02:00
|
|
|
log_line << " target: " << boost::lexical_cast<std::string>(m.info_hash);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case messages::get_peers:
|
|
|
|
{
|
2008-11-09 01:37:03 +01:00
|
|
|
send_flags = 1;
|
2006-08-01 17:27:08 +02:00
|
|
|
a["info_hash"] = std::string(m.info_hash.begin(), m.info_hash.end());
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-05-18 07:09:11 +02:00
|
|
|
log_line << " ih: " << boost::lexical_cast<std::string>(m.info_hash);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case messages::announce_peer:
|
2008-11-09 01:37:03 +01:00
|
|
|
send_flags = 1;
|
2008-03-23 06:10:47 +01:00
|
|
|
a["port"] = m.port;
|
2006-12-14 01:54:37 +01:00
|
|
|
a["info_hash"] = std::string(m.info_hash.begin(), m.info_hash.end());
|
2006-08-01 17:27:08 +02:00
|
|
|
a["token"] = m.write_token;
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-11 18:51:02 +01:00
|
|
|
log_line << " port: " << m.port
|
|
|
|
<< " ih: " << boost::lexical_cast<std::string>(m.info_hash)
|
|
|
|
<< " token: " << to_hex(m.write_token);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m_send_buf.clear();
|
|
|
|
bencode(std::back_inserter(m_send_buf), e);
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-11-09 01:37:03 +01:00
|
|
|
if (m_sock.send(m.addr, &m_send_buf[0], (int)m_send_buf.size(), ec, send_flags))
|
|
|
|
{
|
|
|
|
// account for IP and UDP overhead
|
2008-11-14 21:51:49 +01:00
|
|
|
m_sent_bytes += m_send_buf.size() + (m.addr.address().is_v6() ? 48 : 28);
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-11-09 01:37:03 +01:00
|
|
|
m_total_out_bytes += m_send_buf.size();
|
2006-08-06 18:36:00 +02:00
|
|
|
|
2008-11-09 01:37:03 +01:00
|
|
|
if (m.reply)
|
|
|
|
{
|
|
|
|
++m_replies_sent[m.message_id];
|
|
|
|
m_replies_bytes_sent[m.message_id] += int(m_send_buf.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_queries_out_bytes += m_send_buf.size();
|
|
|
|
}
|
2008-11-09 10:02:06 +01:00
|
|
|
#endif
|
2006-08-06 18:36:00 +02:00
|
|
|
}
|
2008-11-10 04:08:29 +01:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
2008-05-18 07:09:11 +02:00
|
|
|
TORRENT_LOG(dht_tracker) << log_line.str() << " ]";
|
2008-11-11 18:51:02 +01:00
|
|
|
// TORRENT_LOG(dht_tracker) << std::string(m_send_buf.begin(), m_send_buf.end());
|
2008-11-10 04:08:29 +01:00
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
|