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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NODE_HPP
|
|
|
|
#define NODE_HPP
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2009-12-02 18:46:25 +01:00
|
|
|
#include <libtorrent/config.hpp>
|
2015-09-08 22:12:54 +02:00
|
|
|
#include <libtorrent/kademlia/dht_storage.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <libtorrent/kademlia/routing_table.hpp>
|
|
|
|
#include <libtorrent/kademlia/rpc_manager.hpp>
|
|
|
|
#include <libtorrent/kademlia/node_id.hpp>
|
2007-05-23 10:45:12 +02:00
|
|
|
#include <libtorrent/kademlia/msg.hpp>
|
2009-09-20 02:23:36 +02:00
|
|
|
#include <libtorrent/kademlia/find_data.hpp>
|
2015-09-22 20:10:57 +02:00
|
|
|
#include <libtorrent/kademlia/put_data.hpp>
|
2013-12-27 05:28:25 +01:00
|
|
|
#include <libtorrent/kademlia/item.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <libtorrent/io.hpp>
|
2015-10-25 15:55:20 +01:00
|
|
|
#include <libtorrent/socket.hpp> // for udp::endpoint
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <libtorrent/session_settings.hpp>
|
2007-09-01 06:08:39 +02:00
|
|
|
#include <libtorrent/assert.hpp>
|
2009-10-20 04:49:56 +02:00
|
|
|
#include <libtorrent/thread.hpp>
|
2011-01-19 06:57:44 +01:00
|
|
|
#include <libtorrent/bloom_filter.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
namespace libtorrent {
|
2011-02-21 06:24:41 +01:00
|
|
|
class alert_manager;
|
2012-04-30 07:39:35 +02:00
|
|
|
struct alert_dispatcher;
|
2014-09-22 21:49:32 +02:00
|
|
|
class alert;
|
2014-07-06 21:18:00 +02:00
|
|
|
struct counters;
|
2015-01-17 18:02:58 +01:00
|
|
|
struct dht_routing_bucket;
|
2008-09-20 19:42:25 +02:00
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
2009-12-02 18:46:25 +01:00
|
|
|
struct traversal_algorithm;
|
2013-10-14 01:04:40 +02:00
|
|
|
struct dht_observer;
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2015-04-21 02:23:00 +02:00
|
|
|
void TORRENT_EXTRA_EXPORT write_nodes_entry(entry& r, nodes_t const& nodes);
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
struct null_type {};
|
|
|
|
|
2007-05-23 10:45:12 +02:00
|
|
|
class announce_observer : public observer
|
|
|
|
{
|
|
|
|
public:
|
2010-11-05 20:06:50 +01:00
|
|
|
announce_observer(boost::intrusive_ptr<traversal_algorithm> const& algo
|
|
|
|
, udp::endpoint const& ep, node_id const& id)
|
|
|
|
: observer(algo, ep, id)
|
2007-05-23 10:45:12 +02:00
|
|
|
{}
|
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
void reply(msg const&) { flags |= flag_done; }
|
2007-05-23 10:45:12 +02:00
|
|
|
};
|
|
|
|
|
2012-04-30 07:39:35 +02:00
|
|
|
struct udp_socket_interface
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual bool has_quota() = 0;
|
2012-04-30 07:39:35 +02:00
|
|
|
virtual bool send_packet(entry& e, udp::endpoint const& addr, int flags) = 0;
|
2015-04-19 08:28:21 +02:00
|
|
|
protected:
|
|
|
|
~udp_socket_interface() {}
|
2012-04-30 07:39:35 +02:00
|
|
|
};
|
|
|
|
|
2015-05-09 21:00:22 +02:00
|
|
|
class TORRENT_EXTRA_EXPORT node : boost::noncopyable
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-05-09 21:00:22 +02:00
|
|
|
node(udp_socket_interface* sock
|
2015-05-09 20:06:02 +02:00
|
|
|
, libtorrent::dht_settings const& settings, node_id nid
|
2015-09-27 01:00:36 +02:00
|
|
|
, dht_observer* observer, counters& cnt
|
|
|
|
, dht_storage_constructor_type storage_constructor = dht_default_storage_constructor);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-09-08 22:12:54 +02:00
|
|
|
virtual ~node();
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
void tick();
|
2006-08-01 17:27:08 +02:00
|
|
|
void bootstrap(std::vector<udp::endpoint> const& nodes
|
2009-09-20 02:23:36 +02:00
|
|
|
, find_data::nodes_callback const& f);
|
2006-09-27 19:20:18 +02:00
|
|
|
void add_router_node(udp::endpoint router);
|
2015-05-23 03:38:47 +02:00
|
|
|
|
2008-05-08 02:22:17 +02:00
|
|
|
void unreachable(udp::endpoint const& ep);
|
2006-08-01 17:27:08 +02:00
|
|
|
void incoming(msg const& m);
|
|
|
|
|
2015-09-08 22:12:54 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
int num_torrents() const { return m_storage->num_torrents(); }
|
|
|
|
int num_peers() const { return m_storage->num_peers(); }
|
|
|
|
#endif
|
2009-09-27 05:38:41 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
int bucket_size(int bucket);
|
|
|
|
|
|
|
|
node_id const& nid() const { return m_id; }
|
2008-09-02 08:37:40 +02:00
|
|
|
|
2014-11-08 17:58:18 +01:00
|
|
|
boost::tuple<int, int, int> size() const { return m_table.size(); }
|
2014-12-03 05:32:50 +01:00
|
|
|
boost::int64_t num_global_nodes() const
|
2007-05-12 03:52:25 +02:00
|
|
|
{ return m_table.num_global_nodes(); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-09-08 22:12:54 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
int data_size() const { return int(m_storage->num_torrents()); }
|
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-05-26 22:09:19 +02:00
|
|
|
#if defined TORRENT_DEBUG
|
2006-08-01 17:27:08 +02:00
|
|
|
void print_state(std::ostream& os) const
|
|
|
|
{ m_table.print_state(os); }
|
2007-05-12 20:24:14 +02:00
|
|
|
#endif
|
|
|
|
|
2014-01-20 07:35:06 +01:00
|
|
|
enum flags_t { flag_seed = 1, flag_implied_port = 2 };
|
2015-06-23 18:24:30 +02:00
|
|
|
void get_peers(sha1_hash const& info_hash
|
|
|
|
, boost::function<void(std::vector<tcp::endpoint> const&)> dcallback
|
|
|
|
, boost::function<void(std::vector<std::pair<node_entry, std::string> > const&)> ncallback
|
|
|
|
, bool noseeds);
|
2014-01-20 07:35:06 +01:00
|
|
|
void announce(sha1_hash const& info_hash, 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
|
|
|
|
2015-10-25 15:55:20 +01:00
|
|
|
void direct_request(udp::endpoint ep, entry& e
|
2014-02-17 06:56:49 +01:00
|
|
|
, boost::function<void(msg const&)> f);
|
|
|
|
|
2015-11-22 19:00:29 +01:00
|
|
|
void get_item(sha1_hash const& target, boost::function<void(item const&)> f);
|
|
|
|
void get_item(char const* pk, std::string const& salt, boost::function<void(item const&, bool)> f);
|
2015-09-22 20:10:57 +02:00
|
|
|
|
2015-11-22 19:00:29 +01:00
|
|
|
void put_item(sha1_hash const& target, entry const& data, boost::function<void(int)> f);
|
2015-09-22 20:10:57 +02:00
|
|
|
void put_item(char const* pk, std::string const& salt
|
2015-11-22 19:00:29 +01:00
|
|
|
, boost::function<void(item const&, int)> f
|
2015-09-22 20:10:57 +02:00
|
|
|
, boost::function<void(item&)> data_cb);
|
2013-12-27 05:28:25 +01:00
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
bool verify_token(std::string const& token, char const* info_hash
|
2015-08-06 08:30:06 +02:00
|
|
|
, udp::endpoint const& addr) const;
|
2009-09-20 02:23:36 +02:00
|
|
|
|
|
|
|
std::string generate_token(udp::endpoint const& addr, char const* info_hash);
|
2015-05-26 22:09:19 +02:00
|
|
|
|
2006-08-06 18:36:00 +02:00
|
|
|
// the returned time is the delay until connection_timeout()
|
|
|
|
// should be called again the next time
|
2007-04-05 00:27:36 +02:00
|
|
|
time_duration connection_timeout();
|
2006-08-06 18:36:00 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// generates a new secret number used to generate write tokens
|
|
|
|
void new_write_key();
|
|
|
|
|
|
|
|
// pings the given node, and adds it to
|
|
|
|
// the routing table if it respons and if the
|
|
|
|
// bucket is not full.
|
|
|
|
void add_node(udp::endpoint node);
|
|
|
|
|
|
|
|
void replacement_cache(bucket_t& nodes) const
|
|
|
|
{ m_table.replacement_cache(nodes); }
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
int branch_factor() const { return m_settings.search_branching; }
|
|
|
|
|
|
|
|
void add_traversal_algorithm(traversal_algorithm* a)
|
2008-10-09 05:32:57 +02:00
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
m_running_requests.insert(a);
|
|
|
|
}
|
2008-09-20 19:42:25 +02:00
|
|
|
|
|
|
|
void remove_traversal_algorithm(traversal_algorithm* a)
|
2008-10-09 05:32:57 +02:00
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
m_running_requests.erase(a);
|
|
|
|
}
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
void 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
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2008-09-20 19:42:25 +02:00
|
|
|
void status(libtorrent::session_status& s);
|
2015-01-04 22:31:02 +01:00
|
|
|
#endif
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2013-10-14 03:03:43 +02:00
|
|
|
libtorrent::dht_settings const& settings() const { return m_settings; }
|
2014-07-06 21:18:00 +02:00
|
|
|
counters& stats_counters() const { return m_counters; }
|
2011-01-08 09:54:51 +01:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
dht_observer* observer() const { return m_observer; }
|
2016-01-01 15:21:07 +01:00
|
|
|
private:
|
2011-05-23 02:45:36 +02:00
|
|
|
|
2014-11-02 10:41:29 +01:00
|
|
|
void send_single_refresh(udp::endpoint const& ep, int bucket
|
|
|
|
, node_id const& id = node_id());
|
2014-10-12 06:18:34 +02:00
|
|
|
void lookup_peers(sha1_hash const& info_hash, entry& reply
|
2011-05-23 02:45:36 +02:00
|
|
|
, bool noseed, bool scrape) const;
|
2009-09-27 05:38:41 +02:00
|
|
|
bool lookup_torrents(sha1_hash const& target, entry& reply
|
|
|
|
, char* tags) const;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2013-10-14 03:03:43 +02:00
|
|
|
libtorrent::dht_settings const& m_settings;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
typedef libtorrent::mutex mutex_t;
|
2008-10-09 05:32:57 +02:00
|
|
|
mutex_t m_mutex;
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
// this list must be destructed after the rpc manager
|
|
|
|
// since it might have references to it
|
|
|
|
std::set<traversal_algorithm*> m_running_requests;
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
void incoming_request(msg const& h, entry& e);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
node_id m_id;
|
2008-09-20 19:42:25 +02:00
|
|
|
|
|
|
|
public:
|
2006-08-01 17:27:08 +02:00
|
|
|
routing_table m_table;
|
|
|
|
rpc_manager m_rpc;
|
2008-09-20 19:42:25 +02:00
|
|
|
|
|
|
|
private:
|
2013-10-14 01:04:40 +02:00
|
|
|
dht_observer* m_observer;
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point m_last_tracker_tick;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2014-11-01 23:47:56 +01:00
|
|
|
// the last time we issued a bootstrap or a refresh on our own ID, to expand
|
|
|
|
// the routing table buckets close to us.
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point m_last_self_refresh;
|
2014-11-01 23:47:56 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// secret random numbers used to create write tokens
|
|
|
|
int m_secret[2];
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2012-04-30 07:39:35 +02:00
|
|
|
udp_socket_interface* m_sock;
|
2014-07-06 21:18:00 +02:00
|
|
|
counters& m_counters;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-09-08 22:12:54 +02:00
|
|
|
boost::scoped_ptr<dht_storage_interface> m_storage;
|
|
|
|
};
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|
|
|
|
#endif // NODE_HPP
|