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 ROUTING_TABLE_HPP
|
|
|
|
#define ROUTING_TABLE_HPP
|
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <vector>
|
2015-04-18 04:33:39 +02:00
|
|
|
#include <set>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-04-18 04:33:39 +02:00
|
|
|
#include <boost/cstdint.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
#include <boost/utility.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
2016-05-01 05:10:47 +02:00
|
|
|
#include <array>
|
2014-11-02 10:41:29 +01:00
|
|
|
#include <boost/noncopyable.hpp>
|
2015-04-18 04:33:39 +02:00
|
|
|
#include <boost/unordered_set.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <libtorrent/kademlia/node_id.hpp>
|
|
|
|
#include <libtorrent/kademlia/node_entry.hpp>
|
|
|
|
#include <libtorrent/session_settings.hpp>
|
2007-09-10 08:12:41 +02:00
|
|
|
#include <libtorrent/assert.hpp>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <libtorrent/time.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2009-01-23 11:36:07 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2015-01-17 18:02:58 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2009-01-23 11:36:07 +01:00
|
|
|
struct session_status;
|
2015-01-04 22:31:02 +01:00
|
|
|
#endif
|
2015-01-17 18:02:58 +01:00
|
|
|
struct dht_routing_bucket;
|
|
|
|
}
|
2009-01-23 11:36:07 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
2015-05-10 06:54:02 +02:00
|
|
|
struct dht_logger;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-05-08 10:49:07 +02:00
|
|
|
typedef std::vector<node_entry> bucket_t;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
struct routing_table_node
|
|
|
|
{
|
|
|
|
bucket_t replacements;
|
|
|
|
bucket_t live_nodes;
|
|
|
|
};
|
|
|
|
|
2015-08-17 01:21:10 +02:00
|
|
|
struct ip_set
|
|
|
|
{
|
|
|
|
void insert(address addr);
|
|
|
|
size_t count(address addr);
|
|
|
|
void erase(address addr);
|
|
|
|
|
2015-11-14 06:08:57 +01:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_ip4s.clear();
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
m_ip6s.clear();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-17 01:21:10 +02:00
|
|
|
bool operator==(ip_set const& rh)
|
|
|
|
{
|
2015-11-14 06:08:57 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
2015-08-17 01:21:10 +02:00
|
|
|
return m_ip4s == rh.m_ip4s && m_ip6s == rh.m_ip6s;
|
2015-11-14 06:08:57 +01:00
|
|
|
#else
|
|
|
|
return m_ip4s == rh.m_ip4s;
|
|
|
|
#endif
|
2015-08-17 01:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// these must be multisets because there can be multiple routing table
|
|
|
|
// entries for a single IP when restrict_routing_ips is set to false
|
|
|
|
boost::unordered_multiset<address_v4::bytes_type> m_ip4s;
|
2015-11-14 06:08:57 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
2015-08-17 01:21:10 +02:00
|
|
|
boost::unordered_multiset<address_v6::bytes_type> m_ip6s;
|
2015-11-14 06:08:57 +01:00
|
|
|
#endif
|
2015-08-17 01:21:10 +02:00
|
|
|
};
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// differences in the implementation from the description in
|
|
|
|
// the paper:
|
|
|
|
//
|
|
|
|
// * Nodes are not marked as being stale, they keep a counter
|
|
|
|
// that tells how many times in a row they have failed. When
|
|
|
|
// a new node is to be inserted, the node that has failed
|
|
|
|
// the most times is replaced. If none of the nodes in the
|
|
|
|
// bucket has failed, then it is put in the replacement
|
|
|
|
// cache (just like in the paper).
|
|
|
|
|
2016-01-12 05:47:27 +01:00
|
|
|
namespace impl
|
|
|
|
{
|
|
|
|
template <typename F>
|
|
|
|
inline void forwarder(void* userdata, node_entry const& node)
|
|
|
|
{
|
|
|
|
F* f = reinterpret_cast<F*>(userdata);
|
|
|
|
(*f)(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 05:22:25 +02:00
|
|
|
TORRENT_EXTRA_EXPORT bool compare_ip_cidr(address const& lhs, address const& rhs);
|
|
|
|
|
2014-11-02 10:41:29 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT routing_table : boost::noncopyable
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
public:
|
2016-01-01 15:21:07 +01:00
|
|
|
// TODO: 3 to improve memory locality and scanning performance, turn the
|
|
|
|
// routing table into a single vector with boundaries for the nodes instead.
|
|
|
|
// Perhaps replacement nodes should be in a separate vector.
|
2014-01-17 05:49:04 +01:00
|
|
|
typedef std::vector<routing_table_node> table_t;
|
|
|
|
|
2016-02-12 04:56:52 +01:00
|
|
|
routing_table(node_id const& id, udp proto
|
2015-11-14 06:08:57 +01:00
|
|
|
, int bucket_size
|
2015-05-10 06:54:02 +02:00
|
|
|
, dht_settings const& settings
|
|
|
|
, dht_logger* log);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-01-04 22:31:02 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2009-01-23 11:36:07 +01:00
|
|
|
void status(session_status& s) const;
|
2015-01-04 22:31:02 +01:00
|
|
|
#endif
|
2009-01-23 11:36:07 +01:00
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
void status(std::vector<dht_routing_bucket>& s) const;
|
|
|
|
|
2011-01-17 08:49:44 +01:00
|
|
|
void node_failed(node_id const& id, udp::endpoint const& ep);
|
2015-11-29 08:06:36 +01:00
|
|
|
|
2006-09-27 19:20:18 +02:00
|
|
|
// adds an endpoint that will never be added to
|
|
|
|
// the routing table
|
|
|
|
void add_router_node(udp::endpoint router);
|
|
|
|
|
|
|
|
// iterates over the router nodes added
|
|
|
|
typedef std::set<udp::endpoint>::const_iterator router_iterator;
|
|
|
|
router_iterator router_begin() const { return m_router_nodes.begin(); }
|
|
|
|
router_iterator router_end() const { return m_router_nodes.end(); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2014-11-08 17:58:18 +01:00
|
|
|
enum add_node_status_t {
|
|
|
|
failed_to_add = 0,
|
|
|
|
node_added,
|
|
|
|
need_bucket_split
|
|
|
|
};
|
|
|
|
add_node_status_t add_node_impl(node_entry e);
|
|
|
|
|
2012-09-22 20:15:29 +02:00
|
|
|
bool add_node(node_entry e);
|
2010-01-03 12:08:39 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// this function is called every time the node sees
|
|
|
|
// a sign of a node being alive. This node will either
|
|
|
|
// be inserted in the k-buckets or be moved to the top
|
|
|
|
// of its bucket.
|
2012-09-22 20:15:29 +02:00
|
|
|
bool node_seen(node_id const& id, udp::endpoint ep, int rtt);
|
2010-01-03 12:08:39 +01:00
|
|
|
|
|
|
|
// this may add a node to the routing table and mark it as
|
|
|
|
// not pinged. If the bucket the node falls into is full,
|
|
|
|
// the node will be ignored.
|
|
|
|
void heard_about(node_id const& id, udp::endpoint const& ep);
|
2015-08-02 05:57:11 +02:00
|
|
|
|
2016-01-09 19:28:15 +01:00
|
|
|
// change our node ID. This can be expensive since nodes must be moved around
|
|
|
|
// and potentially dropped
|
|
|
|
void update_node_id(node_id id);
|
|
|
|
|
2014-11-02 10:41:29 +01:00
|
|
|
node_entry const* next_refresh();
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-10 03:08:42 +01:00
|
|
|
enum
|
|
|
|
{
|
2014-11-01 23:47:56 +01:00
|
|
|
// nodes that have not been pinged are considered failed by this flag
|
2010-01-03 12:08:39 +01:00
|
|
|
include_failed = 1
|
2008-11-10 03:08:42 +01:00
|
|
|
};
|
2014-11-01 23:47:56 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// fills the vector with the count nodes from our buckets that
|
|
|
|
// are nearest to the given id.
|
|
|
|
void find_node(node_id const& id, std::vector<node_entry>& l
|
2008-11-10 03:08:42 +01:00
|
|
|
, int options, int count = 0);
|
2014-01-17 05:49:04 +01:00
|
|
|
void remove_node(node_entry* n
|
|
|
|
, table_t::iterator bucket) ;
|
2015-08-02 05:57:11 +02:00
|
|
|
|
2012-09-22 23:40:16 +02:00
|
|
|
int bucket_size(int bucket) const
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
int num_buckets = int(m_buckets.size());
|
2011-01-17 08:49:44 +01:00
|
|
|
if (num_buckets == 0) return 0;
|
2015-09-10 23:16:11 +02:00
|
|
|
if (bucket >= num_buckets) bucket = num_buckets - 1;
|
2012-09-22 23:40:16 +02:00
|
|
|
table_t::const_iterator i = m_buckets.begin();
|
2010-01-03 12:08:39 +01:00
|
|
|
std::advance(i, bucket);
|
2015-08-02 05:57:11 +02:00
|
|
|
return int(i->live_nodes.size());
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
2016-01-12 05:47:27 +01:00
|
|
|
template <typename F>
|
|
|
|
void for_each_node(F f)
|
|
|
|
{
|
|
|
|
for_each_node(&impl::forwarder<F>, &impl::forwarder<F>, reinterpret_cast<void*>(&f));
|
|
|
|
}
|
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
void for_each_node(void (*)(void*, node_entry const&)
|
|
|
|
, void (*)(void*, node_entry const&), void* userdata) const;
|
|
|
|
|
|
|
|
int bucket_size() const { return m_bucket_size; }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2014-11-08 17:58:18 +01:00
|
|
|
// returns the number of nodes in the main buckets, number of nodes in the
|
|
|
|
// replacement buckets and the number of nodes in the main buckets that have
|
|
|
|
// been pinged and confirmed up
|
|
|
|
boost::tuple<int, int, int> size() const;
|
|
|
|
|
2014-12-03 05:32:50 +01:00
|
|
|
boost::int64_t num_global_nodes() const;
|
2013-10-27 00:59:55 +02:00
|
|
|
|
|
|
|
// the number of bits down we have full buckets
|
|
|
|
// i.e. essentially the number of full buckets
|
|
|
|
// we have
|
|
|
|
int depth() const;
|
2015-05-26 22:09:19 +02:00
|
|
|
|
2016-04-25 23:22:09 +02:00
|
|
|
int num_active_buckets() const { return int(m_buckets.size()); }
|
2015-05-26 22:09:19 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void replacement_cache(bucket_t& nodes) const;
|
2010-01-03 12:08:39 +01:00
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#if defined TORRENT_DEBUG
|
2006-08-01 17:27:08 +02:00
|
|
|
// used for debug and monitoring purposes. This will print out
|
|
|
|
// the state of the routing table to the given stream
|
|
|
|
void print_state(std::ostream& os) const;
|
2007-05-12 03:52:25 +02:00
|
|
|
#endif
|
|
|
|
|
2012-09-22 23:40:16 +02:00
|
|
|
int bucket_limit(int bucket) const;
|
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2013-12-20 05:54:52 +01:00
|
|
|
void check_invariant() const;
|
|
|
|
#endif
|
|
|
|
|
2015-09-11 22:43:21 +02:00
|
|
|
bool is_full(int bucket) const;
|
|
|
|
|
2015-11-14 06:08:57 +01:00
|
|
|
bool native_address(address addr) const
|
|
|
|
{
|
2016-02-12 04:56:52 +01:00
|
|
|
return (addr.is_v4() && m_protocol == udp::v4())
|
|
|
|
|| (addr.is_v6() && m_protocol == udp::v6());
|
2015-11-14 06:08:57 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 04:56:52 +01:00
|
|
|
bool native_endpoint(udp::endpoint ep) const
|
|
|
|
{ return ep.protocol() == m_protocol; }
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
private:
|
|
|
|
|
2015-11-29 08:06:36 +01:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-10 06:54:02 +02:00
|
|
|
dht_logger* m_log;
|
2015-11-29 08:06:36 +01:00
|
|
|
#endif
|
2015-05-10 06:54:02 +02:00
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
table_t::iterator find_bucket(node_id const& id);
|
|
|
|
|
2013-01-18 07:17:30 +01:00
|
|
|
void split_bucket();
|
|
|
|
|
2011-01-17 08:49:44 +01:00
|
|
|
// return a pointer the node_entry with the given endpoint
|
|
|
|
// or 0 if we don't have such a node. Both the address and the
|
|
|
|
// port has to match
|
2013-12-20 05:54:52 +01:00
|
|
|
node_entry* find_node(udp::endpoint const& ep
|
|
|
|
, routing_table::table_t::iterator* bucket);
|
2011-01-17 08:49:44 +01:00
|
|
|
|
2013-11-26 03:23:33 +01:00
|
|
|
dht_settings const& m_settings;
|
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
// (k-bucket, replacement cache) pairs
|
|
|
|
// the first entry is the bucket the furthest
|
|
|
|
// away from our own ID. Each time the bucket
|
|
|
|
// closest to us (m_buckets.back()) has more than
|
|
|
|
// bucket size nodes in it, another bucket is
|
|
|
|
// added to the end and it's split up between them
|
2006-08-01 17:27:08 +02:00
|
|
|
table_t m_buckets;
|
2010-01-03 12:08:39 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
node_id m_id; // our own node id
|
2016-02-12 04:56:52 +01:00
|
|
|
udp m_protocol; // protocol this table is for
|
2010-02-20 17:37:50 +01:00
|
|
|
|
2013-11-13 03:17:33 +01:00
|
|
|
// the last seen depth (i.e. levels in the routing table)
|
|
|
|
// it's mutable because it's updated by depth(), which is const
|
|
|
|
mutable int m_depth;
|
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
// the last time we refreshed our own bucket
|
|
|
|
// refreshed every 15 minutes
|
2015-03-12 05:34:54 +01:00
|
|
|
mutable time_point m_last_self_refresh;
|
2015-08-18 13:55:50 +02:00
|
|
|
|
2006-09-27 19:20:18 +02:00
|
|
|
// this is a set of all the endpoints that have
|
|
|
|
// been identified as router nodes. They will
|
|
|
|
// be used in searches, but they will never
|
|
|
|
// be added to the routing table.
|
|
|
|
std::set<udp::endpoint> m_router_nodes;
|
2011-01-08 09:54:51 +01:00
|
|
|
|
|
|
|
// these are all the IPs that are in the routing
|
|
|
|
// table. It's used to only allow a single entry
|
2015-08-17 01:21:10 +02:00
|
|
|
// per IP in the whole table.
|
|
|
|
ip_set m_ips;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// constant called k in paper
|
|
|
|
int m_bucket_size;
|
2006-08-01 17:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|
|
|
|
#endif // ROUTING_TABLE_HPP
|
|
|
|
|