2006-08-01 17:27:08 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2006-2016, 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_DHT_TRACKER
|
|
|
|
#define TORRENT_DHT_TRACKER
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2016-09-14 17:28:50 +02:00
|
|
|
#include <libtorrent/kademlia/node.hpp>
|
|
|
|
#include <libtorrent/kademlia/dos_blocker.hpp>
|
2016-09-17 15:42:04 +02:00
|
|
|
#include <libtorrent/kademlia/dht_state.hpp>
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-09-14 17:28:50 +02:00
|
|
|
#include <libtorrent/socket.hpp>
|
|
|
|
#include <libtorrent/deadline_timer.hpp>
|
|
|
|
#include <libtorrent/span.hpp>
|
2016-09-27 00:04:42 +02:00
|
|
|
#include <libtorrent/io_service.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-11 18:51:02 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
struct counters;
|
2016-09-27 00:04:42 +02:00
|
|
|
struct dht_settings;
|
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
|
|
|
|
{
|
2016-04-30 17:05:54 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT dht_tracker final
|
2015-01-17 23:06:30 +01:00
|
|
|
: udp_socket_interface
|
2016-08-29 14:31:23 +02:00
|
|
|
, std::enable_shared_from_this<dht_tracker>
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2016-09-11 07:58:48 +02:00
|
|
|
using send_fun_t = std::function<void(udp::endpoint const&
|
|
|
|
, span<char const>, error_code&, int)>;
|
2016-04-24 21:26:28 +02:00
|
|
|
|
|
|
|
dht_tracker(dht_observer* observer
|
|
|
|
, io_service& ios
|
|
|
|
, send_fun_t const& send_fun
|
2016-06-04 01:44:16 +02:00
|
|
|
, dht_settings const& settings
|
|
|
|
, counters& cnt
|
|
|
|
, dht_storage_interface& storage
|
2016-09-29 14:25:51 +02:00
|
|
|
, dht_state state);
|
2012-06-30 17:30:38 +02:00
|
|
|
virtual ~dht_tracker();
|
2008-09-02 08:37:40 +02:00
|
|
|
|
2016-09-29 14:25:51 +02:00
|
|
|
void start(find_data::nodes_callback const& f);
|
2007-02-25 10:42:43 +01:00
|
|
|
void stop();
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2016-01-09 19:28:15 +01:00
|
|
|
// tell the node to recalculate its node id based on the current
|
|
|
|
// understanding of its external address (which may have changed)
|
|
|
|
void update_node_id();
|
|
|
|
|
2016-09-09 21:02:20 +02:00
|
|
|
void add_node(udp::endpoint 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
|
|
|
|
2016-09-17 15:42:04 +02:00
|
|
|
dht_state state() const;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2014-01-20 07:35:06 +01:00
|
|
|
enum flags_t { flag_seed = 1, flag_implied_port = 2 };
|
2015-06-25 17:01:36 +02:00
|
|
|
void get_peers(sha1_hash const& ih
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(std::vector<tcp::endpoint> const&)> f);
|
2014-01-20 07:35:06 +01:00
|
|
|
void announce(sha1_hash const& ih, int listen_port, int flags
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::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
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(item const&)> 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
|
2016-07-24 00:57:04 +02:00
|
|
|
void get_item(public_key const& key
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(item const&, bool)> cb
|
2014-02-24 01:31:13 +01:00
|
|
|
, std::string salt = std::string());
|
|
|
|
|
2015-09-22 20:10:57 +02:00
|
|
|
// for immutable_item.
|
|
|
|
// the callback function will be called when put operation is done.
|
2015-11-20 03:41:56 +01:00
|
|
|
// the int parameter indicates the success numbers of put operation.
|
2015-11-22 19:00:29 +01:00
|
|
|
void put_item(entry const& data
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(int)> cb);
|
2014-02-24 01:31:13 +01:00
|
|
|
|
2015-09-22 20:10:57 +02:00
|
|
|
// for mutable_item.
|
|
|
|
// the data_cb will be called when we get authoritative mutable_item,
|
|
|
|
// the cb is same as put immutable_item.
|
2016-07-24 00:57:04 +02:00
|
|
|
void put_item(public_key const& key
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(item const&, int)> cb
|
|
|
|
, std::function<void(item&)> data_cb, std::string salt = std::string());
|
2014-02-24 01:31:13 +01:00
|
|
|
|
2014-02-17 06:56:49 +01:00
|
|
|
// send an arbitrary DHT request directly to a node
|
2016-09-14 17:28:50 +02:00
|
|
|
void direct_request(udp::endpoint const& ep, entry& e
|
2016-08-13 03:31:55 +02:00
|
|
|
, std::function<void(msg const&)> f);
|
2014-02-17 06:56:49 +01:00
|
|
|
|
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
|
2015-01-17 18:02:58 +01:00
|
|
|
void dht_status(std::vector<dht_routing_bucket>& table
|
|
|
|
, std::vector<dht_lookup>& requests);
|
2015-09-17 17:11:46 +02:00
|
|
|
void update_stats_counters(counters& c) const;
|
2015-01-04 22:31:02 +01:00
|
|
|
|
2016-09-29 05:25:08 +02:00
|
|
|
void incoming_error(error_code const& ec, udp::endpoint const& ep);
|
2016-09-11 07:58:48 +02:00
|
|
|
bool incoming_packet(udp::endpoint const& ep, span<char const> buf);
|
2007-12-09 05:15:24 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
private:
|
2015-06-06 19:49:18 +02:00
|
|
|
|
2016-08-29 14:31:23 +02:00
|
|
|
std::shared_ptr<dht_tracker> self()
|
2015-01-17 23:06:30 +01:00
|
|
|
{ return shared_from_this(); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-11-14 06:08:57 +01:00
|
|
|
void connection_timeout(node& n, error_code const& e);
|
2008-05-03 18:05:42 +02:00
|
|
|
void refresh_timeout(error_code const& e);
|
2016-01-01 15:21:07 +01:00
|
|
|
void refresh_key(error_code const& e);
|
2016-06-04 01:44:16 +02:00
|
|
|
void update_storage_node_ids();
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2012-04-30 07:39:35 +02:00
|
|
|
// implements udp_socket_interface
|
2016-04-30 17:05:54 +02:00
|
|
|
virtual bool has_quota() override;
|
2016-09-28 05:21:18 +02:00
|
|
|
virtual bool send_packet(entry& e, udp::endpoint const& addr) override;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
// this is the bdecode_node DHT messages are parsed into. It's a member
|
|
|
|
// in order to avoid having to deallocate and re-allocate it for every
|
|
|
|
// message.
|
|
|
|
bdecode_node m_msg;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
counters& m_counters;
|
2016-06-04 01:44:16 +02:00
|
|
|
dht_storage_interface& m_storage;
|
2016-09-29 14:25:51 +02:00
|
|
|
dht_state m_state; // to be used only once
|
2015-05-09 21:00:22 +02:00
|
|
|
node m_dht;
|
2015-11-14 06:08:57 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
node m_dht6;
|
|
|
|
#endif
|
2016-04-24 21:26:28 +02:00
|
|
|
send_fun_t m_send_fun;
|
2015-05-10 06:54:02 +02:00
|
|
|
dht_logger* m_log;
|
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
|
|
|
|
2016-01-01 22:42:23 +01:00
|
|
|
deadline_timer m_key_refresh_timer;
|
2006-08-06 18:36:00 +02:00
|
|
|
deadline_timer m_connection_timer;
|
2015-11-14 06:08:57 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
deadline_timer m_connection_timer6;
|
|
|
|
#endif
|
2006-08-06 18:36:00 +02:00
|
|
|
deadline_timer m_refresh_timer;
|
2006-08-01 17:27:08 +02:00
|
|
|
dht_settings const& m_settings;
|
|
|
|
|
2015-11-14 06:08:57 +01:00
|
|
|
std::map<std::string, node*> m_nodes;
|
|
|
|
|
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;
|
2016-01-17 21:09:27 +01:00
|
|
|
|
|
|
|
// state for the send rate limit
|
|
|
|
int m_send_quota;
|
|
|
|
time_point m_last_tick;
|
2006-08-01 17:27:08 +02:00
|
|
|
};
|
|
|
|
}}
|
|
|
|
|
|
|
|
#endif
|