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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ROUTING_TABLE_HPP
|
|
|
|
#define ROUTING_TABLE_HPP
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <deque>
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
|
|
|
|
#include <boost/iterator/iterator_facade.hpp>
|
|
|
|
#include <boost/iterator/iterator_categories.hpp>
|
|
|
|
#include <boost/utility.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/array.hpp>
|
2006-10-12 22:42:58 +02:00
|
|
|
#include <set>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <libtorrent/kademlia/logging.hpp>
|
|
|
|
|
|
|
|
#include <libtorrent/kademlia/node_id.hpp>
|
|
|
|
#include <libtorrent/kademlia/node_entry.hpp>
|
|
|
|
#include <libtorrent/session_settings.hpp>
|
2007-05-12 03:52:25 +02:00
|
|
|
#include <libtorrent/size_type.hpp>
|
2007-09-10 08:12:41 +02:00
|
|
|
#include <libtorrent/assert.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
2008-05-10 07:51:58 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_DECLARE_LOG(table);
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
// differences in the implementation from the description in
|
|
|
|
// the paper:
|
|
|
|
//
|
|
|
|
// * The routing table tree is not allocated dynamically, there
|
|
|
|
// are always 160 buckets.
|
|
|
|
// * 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).
|
|
|
|
|
|
|
|
class routing_table;
|
|
|
|
|
|
|
|
namespace aux
|
|
|
|
{
|
|
|
|
|
|
|
|
// Iterates over a flattened routing_table structure.
|
|
|
|
class routing_table_iterator
|
|
|
|
: public boost::iterator_facade<
|
|
|
|
routing_table_iterator
|
|
|
|
, node_entry const
|
|
|
|
, boost::forward_traversal_tag
|
|
|
|
>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
routing_table_iterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class libtorrent::dht::routing_table;
|
|
|
|
friend class boost::iterator_core_access;
|
|
|
|
|
|
|
|
typedef boost::array<std::pair<bucket_t, bucket_t>, 160>::const_iterator
|
|
|
|
bucket_iterator_t;
|
|
|
|
|
|
|
|
routing_table_iterator(
|
|
|
|
bucket_iterator_t begin
|
|
|
|
, bucket_iterator_t end)
|
|
|
|
: m_bucket_iterator(begin)
|
|
|
|
, m_bucket_end(end)
|
|
|
|
{
|
|
|
|
if (m_bucket_iterator == m_bucket_end) return;
|
2008-09-16 19:21:24 +02:00
|
|
|
m_iterator = begin->first.begin();
|
2006-08-01 17:27:08 +02:00
|
|
|
while (m_iterator == m_bucket_iterator->first.end())
|
|
|
|
{
|
|
|
|
if (++m_bucket_iterator == m_bucket_end)
|
|
|
|
break;
|
|
|
|
m_iterator = m_bucket_iterator->first.begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equal(routing_table_iterator const& other) const
|
|
|
|
{
|
|
|
|
return m_bucket_iterator == other.m_bucket_iterator
|
2006-08-11 08:05:16 +02:00
|
|
|
&& (m_bucket_iterator == m_bucket_end
|
2008-09-16 19:21:24 +02:00
|
|
|
|| *m_iterator == other.m_iterator);
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void increment()
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_bucket_iterator != m_bucket_end);
|
2008-09-16 19:21:24 +02:00
|
|
|
++*m_iterator;
|
|
|
|
while (*m_iterator == m_bucket_iterator->first.end())
|
2006-08-01 17:27:08 +02:00
|
|
|
{
|
|
|
|
if (++m_bucket_iterator == m_bucket_end)
|
|
|
|
break;
|
|
|
|
m_iterator = m_bucket_iterator->first.begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node_entry const& dereference() const
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_bucket_iterator != m_bucket_end);
|
2008-09-16 19:21:24 +02:00
|
|
|
return **m_iterator;
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket_iterator_t m_bucket_iterator;
|
|
|
|
bucket_iterator_t m_bucket_end;
|
2008-09-16 19:21:24 +02:00
|
|
|
// when debug iterators are enabled, default constructed
|
|
|
|
// iterators are not allowed to be copied. In the case
|
|
|
|
// where the routing table is empty, m_iterator would be
|
|
|
|
// default constructed and not copyable.
|
|
|
|
boost::optional<bucket_t::const_iterator> m_iterator;
|
2006-08-01 17:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace aux
|
|
|
|
|
|
|
|
class routing_table
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef aux::routing_table_iterator iterator;
|
|
|
|
typedef iterator const_iterator;
|
|
|
|
|
|
|
|
routing_table(node_id const& id, int bucket_size
|
|
|
|
, dht_settings const& settings);
|
|
|
|
|
|
|
|
void node_failed(node_id const& id);
|
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
|
|
|
|
|
|
|
// 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.
|
|
|
|
bool node_seen(node_id const& id, udp::endpoint addr);
|
|
|
|
|
2006-08-06 18:36:00 +02:00
|
|
|
// returns time when the given bucket needs another refresh.
|
|
|
|
// if the given bucket is empty but there are nodes
|
2006-08-01 17:27:08 +02:00
|
|
|
// in a bucket closer to us, or if the bucket is non-empty and
|
|
|
|
// the time from the last activity is more than 15 minutes
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime next_refresh(int bucket);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-10 03:08:42 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
include_self = 1,
|
|
|
|
include_failed = 2
|
|
|
|
};
|
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);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-11-10 03:08:42 +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);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2006-08-06 18:36:00 +02:00
|
|
|
// this will set the given bucket's latest activity
|
|
|
|
// to the current time
|
|
|
|
void touch_bucket(int bucket);
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
int bucket_size(int bucket)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(bucket >= 0 && bucket < 160);
|
2006-08-01 17:27:08 +02:00
|
|
|
return (int)m_buckets[bucket].first.size();
|
|
|
|
}
|
|
|
|
int bucket_size() const { return m_bucket_size; }
|
|
|
|
|
|
|
|
iterator begin() const;
|
|
|
|
iterator end() const;
|
|
|
|
|
|
|
|
boost::tuple<int, int> size() const;
|
2007-05-12 03:52:25 +02:00
|
|
|
size_type num_global_nodes() const;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
// returns true if there are no working nodes
|
|
|
|
// in the routing table
|
|
|
|
bool need_bootstrap() const;
|
2007-05-12 03:52:25 +02:00
|
|
|
int num_active_buckets() const
|
|
|
|
{ return 160 - m_lowest_active_bucket + 1; }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
void replacement_cache(bucket_t& nodes) const;
|
2007-05-12 03:52:25 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
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
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
// constant called k in paper
|
|
|
|
int m_bucket_size;
|
|
|
|
|
|
|
|
dht_settings const& m_settings;
|
|
|
|
|
|
|
|
// 160 (k-bucket, replacement cache) pairs
|
|
|
|
typedef boost::array<std::pair<bucket_t, bucket_t>, 160> table_t;
|
|
|
|
table_t m_buckets;
|
|
|
|
// timestamps of the last activity in each bucket
|
2007-04-05 00:27:36 +02:00
|
|
|
typedef boost::array<ptime, 160> table_activity_t;
|
2006-08-30 02:09:58 +02:00
|
|
|
table_activity_t m_bucket_activity;
|
2006-08-01 17:27:08 +02:00
|
|
|
node_id m_id; // our own node id
|
|
|
|
|
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;
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// this is the lowest bucket index with nodes in it
|
|
|
|
int m_lowest_active_bucket;
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|
|
|
|
#endif // ROUTING_TABLE_HPP
|
|
|
|
|