2006-08-01 17:27:08 +02:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2006-2014, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
|
|
|
|
#ifndef TORRENT_DHT_TRACKER
|
|
|
|
#define TORRENT_DHT_TRACKER
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <set>
|
|
|
|
#include <numeric>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
2007-02-25 10:42:43 +01:00
|
|
|
#include <boost/intrusive_ptr.hpp>
|
|
|
|
#include <boost/detail/atomic_count.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"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/kademlia/dos_blocker.hpp"
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#include "libtorrent/session_settings.hpp"
|
2007-12-09 05:15:24 +01:00
|
|
|
#include "libtorrent/udp_socket.hpp"
|
2008-05-03 18:05:42 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
2009-10-20 04:49:56 +02:00
|
|
|
#include "libtorrent/thread.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/deadline_timer.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
namespace aux { struct session_impl; }
|
|
|
|
struct lazy_entry;
|
2014-07-06 21:18:00 +02:00
|
|
|
struct counters;
|
2015-01-04 22:31:02 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
struct session_status;
|
|
|
|
#endif
|
2008-11-11 18:51:02 +01:00
|
|
|
}
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_DECLARE_LOG(dht_tracker);
|
|
|
|
#endif
|
|
|
|
|
2007-02-25 10:42:43 +01:00
|
|
|
struct dht_tracker;
|
|
|
|
|
2012-03-20 04:53:07 +01:00
|
|
|
TORRENT_EXTRA_EXPORT void intrusive_ptr_add_ref(dht_tracker const*);
|
|
|
|
TORRENT_EXTRA_EXPORT void intrusive_ptr_release(dht_tracker const*);
|
2007-02-25 10:42:43 +01:00
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
struct dht_tracker : udp_socket_interface, udp_socket_observer
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2007-02-25 10:42:43 +01:00
|
|
|
friend void intrusive_ptr_add_ref(dht_tracker const*);
|
|
|
|
friend void intrusive_ptr_release(dht_tracker const*);
|
2012-04-30 07:39:35 +02:00
|
|
|
|
2008-11-08 18:40:06 +01:00
|
|
|
dht_tracker(libtorrent::aux::session_impl& ses, rate_limited_udp_socket& sock
|
2014-07-06 21:18:00 +02:00
|
|
|
, dht_settings const& settings, counters& cnt, entry const* state = 0);
|
2012-06-30 17:30:38 +02:00
|
|
|
virtual ~dht_tracker();
|
2008-09-02 08:37:40 +02:00
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
void start(entry const& bootstrap
|
|
|
|
, find_data::nodes_callback const& f);
|
2007-02-25 10:42:43 +01:00
|
|
|
void stop();
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
void add_node(udp::endpoint node);
|
|
|
|
void add_node(std::pair<std::string, int> const& node);
|
2010-02-14 08:46:57 +01:00
|
|
|
void add_router_node(udp::endpoint const& node);
|
2006-09-27 19:20:18 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
entry state() const;
|
|
|
|
|
2014-01-20 07:35:06 +01:00
|
|
|
enum flags_t { flag_seed = 1, flag_implied_port = 2 };
|
|
|
|
void 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-02-24 01:31:13 +01:00
|
|
|
void get_item(sha1_hash const& target
|
|
|
|
, boost::function<void(item const&)> cb);
|
|
|
|
|
|
|
|
// key is a 32-byte binary string, the public key to look up.
|
|
|
|
// the salt is optional
|
|
|
|
void get_item(char const* key
|
|
|
|
, boost::function<void(item const&)> cb
|
|
|
|
, std::string salt = std::string());
|
|
|
|
|
|
|
|
void put_item(entry data
|
|
|
|
, boost::function<void()> cb);
|
|
|
|
|
|
|
|
void put_item(char const* key
|
|
|
|
, boost::function<void(item&)> cb, std::string salt = std::string());
|
|
|
|
|
2015-01-04 22:31:02 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2006-09-05 01:22:21 +02:00
|
|
|
void dht_status(session_status& s);
|
2015-01-04 22:31:02 +01:00
|
|
|
#endif
|
|
|
|
|
2008-11-02 11:01:04 +01:00
|
|
|
void network_stats(int& sent, int& received);
|
2006-09-05 01:22:21 +02:00
|
|
|
|
2007-12-09 05:15:24 +01:00
|
|
|
// translate bittorrent kademlia message into the generic kademlia message
|
|
|
|
// used by the library
|
2012-06-22 06:21:20 +02:00
|
|
|
virtual bool incoming_packet(error_code const& ec
|
|
|
|
, udp::endpoint const&, char const* buf, int size);
|
2007-12-09 05:15:24 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
private:
|
2007-02-25 10:42:43 +01:00
|
|
|
|
|
|
|
boost::intrusive_ptr<dht_tracker> self()
|
|
|
|
{ return boost::intrusive_ptr<dht_tracker>(this); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_name_lookup(error_code const& e
|
2006-08-01 17:27:08 +02:00
|
|
|
, udp::resolver::iterator host);
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_router_name_lookup(error_code const& e
|
2006-09-27 19:20:18 +02:00
|
|
|
, udp::resolver::iterator host);
|
2008-05-03 18:05:42 +02:00
|
|
|
void connection_timeout(error_code const& e);
|
|
|
|
void refresh_timeout(error_code const& e);
|
|
|
|
void tick(error_code const& e);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2012-04-30 07:39:35 +02:00
|
|
|
// implements udp_socket_interface
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual bool has_quota();
|
2013-12-21 10:22:10 +01:00
|
|
|
virtual bool send_packet(libtorrent::entry& e, udp::endpoint const& addr
|
|
|
|
, int send_flags);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
counters& m_counters;
|
2006-08-01 17:27:08 +02:00
|
|
|
node_impl m_dht;
|
2008-11-08 18:40:06 +01:00
|
|
|
rate_limited_udp_socket& m_sock;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
std::vector<char> m_send_buf;
|
2014-07-06 21:18:00 +02:00
|
|
|
dos_blocker m_blocker;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-05-14 19:49:36 +02:00
|
|
|
ptime m_last_new_key;
|
2006-08-01 17:27:08 +02:00
|
|
|
deadline_timer m_timer;
|
2006-08-06 18:36:00 +02:00
|
|
|
deadline_timer m_connection_timer;
|
|
|
|
deadline_timer m_refresh_timer;
|
2006-08-01 17:27:08 +02:00
|
|
|
dht_settings const& m_settings;
|
|
|
|
int m_refresh_bucket;
|
|
|
|
|
2008-03-24 05:38:43 +01:00
|
|
|
bool m_abort;
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// used to resolve hostnames for nodes
|
|
|
|
udp::resolver m_host_resolver;
|
2007-12-04 03:53:10 +01:00
|
|
|
|
2008-11-02 11:01:04 +01:00
|
|
|
// sent and received bytes since queried last time
|
|
|
|
int m_sent_bytes;
|
|
|
|
int m_received_bytes;
|
|
|
|
|
2007-02-25 10:42:43 +01:00
|
|
|
// reference counter for intrusive_ptr
|
|
|
|
mutable boost::detail::atomic_count m_refs;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
int m_replies_sent[5];
|
|
|
|
int m_queries_received[5];
|
|
|
|
int m_replies_bytes_sent[5];
|
|
|
|
int m_queries_bytes_received[5];
|
|
|
|
int m_counter;
|
|
|
|
|
|
|
|
int m_total_message_input;
|
2006-08-06 18:36:00 +02:00
|
|
|
int m_total_in_bytes;
|
|
|
|
int m_total_out_bytes;
|
|
|
|
|
|
|
|
int m_queries_out_bytes;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|