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 NODE_HPP
|
|
|
|
#define NODE_HPP
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2009-12-02 18:46:25 +01:00
|
|
|
#include <libtorrent/config.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>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <libtorrent/io.hpp>
|
|
|
|
#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;
|
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(node);
|
|
|
|
#endif
|
|
|
|
|
2009-12-02 18:46:25 +01:00
|
|
|
struct traversal_algorithm;
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2009-09-27 19:41:51 +02:00
|
|
|
struct key_desc_t
|
|
|
|
{
|
|
|
|
char const* name;
|
|
|
|
int type;
|
|
|
|
int size;
|
|
|
|
int flags;
|
|
|
|
|
2011-01-19 06:57:44 +01:00
|
|
|
enum {
|
|
|
|
// this argument is optional, parsing will not
|
|
|
|
// fail if it's not present
|
|
|
|
optional = 1,
|
|
|
|
// for dictionaries, the following entries refer
|
|
|
|
// to child nodes to this node, up until and including
|
|
|
|
// the next item that has the last_child flag set.
|
|
|
|
// these flags are nestable
|
|
|
|
parse_children = 2,
|
|
|
|
// this is the last item in a child dictionary
|
|
|
|
last_child = 4,
|
|
|
|
// the size argument refers to that the size
|
|
|
|
// has to be divisible by the number, instead
|
|
|
|
// of having that exact size
|
|
|
|
size_divisible = 8
|
|
|
|
};
|
2009-09-27 19:41:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool TORRENT_EXPORT verify_message(lazy_entry const* msg, key_desc_t const desc[], lazy_entry const* ret[]
|
|
|
|
, int size , char* error, int error_size);
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// this is the entry for every peer
|
|
|
|
// the timestamp is there to make it possible
|
|
|
|
// to remove stale peers
|
|
|
|
struct peer_entry
|
|
|
|
{
|
|
|
|
tcp::endpoint addr;
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime added;
|
2006-08-01 17:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// this is a group. It contains a set of group members
|
|
|
|
struct torrent_entry
|
|
|
|
{
|
2010-11-27 04:09:28 +01:00
|
|
|
std::string name;
|
2006-08-01 17:27:08 +02:00
|
|
|
std::set<peer_entry> peers;
|
|
|
|
};
|
|
|
|
|
2011-01-19 06:57:44 +01:00
|
|
|
struct feed_item
|
|
|
|
{
|
|
|
|
feed_item() : sequence_number(0), num_announcers(0) {}
|
|
|
|
enum { list_head, list_item } type;
|
|
|
|
size_type sequence_number;
|
|
|
|
std::string name;
|
|
|
|
unsigned char signature[64];
|
|
|
|
entry item;
|
|
|
|
ptime last_seen;
|
|
|
|
// this counts the number of IPs we have seen
|
|
|
|
// announcing this item, this is used to determine
|
|
|
|
// popularity if we reach the limit of items to store
|
|
|
|
bloom_filter<8> ips;
|
|
|
|
int num_announcers;
|
|
|
|
};
|
|
|
|
|
2009-09-27 05:38:41 +02:00
|
|
|
// this is the entry for a torrent that has been published
|
|
|
|
// in the DHT.
|
2009-12-02 18:46:25 +01:00
|
|
|
struct TORRENT_EXPORT search_torrent_entry
|
2009-09-27 05:38:41 +02:00
|
|
|
{
|
2009-10-23 22:29:26 +02:00
|
|
|
search_torrent_entry(): total_tag_points(0), total_name_points(0) {}
|
|
|
|
|
2009-09-27 05:38:41 +02:00
|
|
|
// the tags of the torrent. The key of
|
|
|
|
// this entry is the sha-1 hash of one of
|
|
|
|
// these tags. The counter is the number of
|
|
|
|
// times a tag has been included in a publish
|
|
|
|
// call. The counters are periodically
|
|
|
|
// decremented by a factor, so that the
|
|
|
|
// popularity ratio between the tags is
|
|
|
|
// maintained. The decrement is rounded down.
|
|
|
|
std::map<std::string, int> tags;
|
|
|
|
|
|
|
|
// this is the sum of all values in the tags
|
|
|
|
// map. It is only an optimization to avoid
|
|
|
|
// recalculating it constantly
|
|
|
|
int total_tag_points;
|
|
|
|
|
|
|
|
// the name of the torrent
|
|
|
|
std::map<std::string, int> name;
|
|
|
|
int total_name_points;
|
|
|
|
|
|
|
|
// increase the popularity counters for this torrent
|
|
|
|
void publish(std::string const& name, char const* in_tags[], int num_tags);
|
|
|
|
|
|
|
|
// return a score of how well this torrent matches
|
|
|
|
// the given set of tags. Each word in the string
|
|
|
|
// (separated by a space) is considered a tag.
|
|
|
|
// tags with 2 letters or fewer are ignored
|
|
|
|
int match(char const* tags[], int num_tags) const;
|
|
|
|
|
|
|
|
// this is called once every hour, and will
|
|
|
|
// decrement the popularity counters of the
|
|
|
|
// tags. Returns true if this entry should
|
|
|
|
// be deleted
|
|
|
|
bool tick();
|
|
|
|
|
|
|
|
void get_name(std::string& t) const;
|
|
|
|
void get_tags(std::string& t) const;
|
|
|
|
};
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
inline bool operator<(peer_entry const& lhs, peer_entry const& rhs)
|
|
|
|
{
|
|
|
|
return lhs.addr.address() == rhs.addr.address()
|
|
|
|
? lhs.addr.port() < rhs.addr.port()
|
|
|
|
: lhs.addr.address() < rhs.addr.address();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2009-09-27 05:38:41 +02:00
|
|
|
struct count_peers
|
|
|
|
{
|
|
|
|
int& count;
|
|
|
|
count_peers(int& c): count(c) {}
|
|
|
|
void operator()(std::pair<libtorrent::dht::node_id
|
|
|
|
, libtorrent::dht::torrent_entry> const& t)
|
|
|
|
{
|
|
|
|
count += t.second.peers.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
class node_impl : boost::noncopyable
|
|
|
|
{
|
|
|
|
typedef std::map<node_id, torrent_entry> table_t;
|
2011-01-19 06:57:44 +01:00
|
|
|
typedef std::map<node_id, feed_item> feed_table_t;
|
2009-09-27 05:38:41 +02:00
|
|
|
typedef std::map<std::pair<node_id, sha1_hash>, search_torrent_entry> search_table_t;
|
2011-01-19 06:57:44 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
public:
|
2011-01-19 06:57:44 +01:00
|
|
|
typedef boost::function3<void, address, int, address> external_ip_fun;
|
|
|
|
|
|
|
|
node_impl(libtorrent::alert_manager& alerts
|
2011-01-23 03:02:04 +01:00
|
|
|
, bool (*f)(void*, entry&, udp::endpoint const&, int)
|
2011-01-19 06:57:44 +01:00
|
|
|
, dht_settings const& settings, node_id nid, address const& external_address
|
|
|
|
, external_ip_fun ext_ip, void* userdata);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
virtual ~node_impl() {}
|
|
|
|
|
2010-01-03 12:08:39 +01:00
|
|
|
void tick();
|
2009-09-20 02:23:36 +02:00
|
|
|
void refresh(node_id const& id, find_data::nodes_callback const& f);
|
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);
|
2006-08-01 17:27:08 +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);
|
|
|
|
|
2009-09-27 05:38:41 +02:00
|
|
|
int num_torrents() const { return m_map.size(); }
|
|
|
|
int num_peers() const
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
std::for_each(m_map.begin(), m_map.end(), count_peers(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
boost::tuple<int, int> size() const{ return m_table.size(); }
|
2007-05-12 03:52:25 +02:00
|
|
|
size_type num_global_nodes() const
|
|
|
|
{ return m_table.num_global_nodes(); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2006-09-05 01:22:21 +02:00
|
|
|
int data_size() const { return int(m_map.size()); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-05-12 20:24:14 +02:00
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
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
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void announce(sha1_hash const& info_hash, int listen_port
|
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
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
bool verify_token(std::string const& token, char const* info_hash
|
|
|
|
, udp::endpoint const& addr);
|
|
|
|
|
|
|
|
std::string generate_token(udp::endpoint const& addr, char const* info_hash);
|
2006-08-01 17:27:08 +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
|
|
|
|
|
|
|
void status(libtorrent::session_status& s);
|
|
|
|
|
2011-01-08 09:54:51 +01:00
|
|
|
dht_settings const& settings() const { return m_settings; }
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
protected:
|
|
|
|
// is called when a find data request is received. Should
|
|
|
|
// return false if the data is not stored on this node. If
|
|
|
|
// the data is stored, it should be serialized into 'data'.
|
2010-12-06 07:39:16 +01:00
|
|
|
bool lookup_peers(sha1_hash const& info_hash, int prefix, entry& reply) 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
|
|
|
|
|
|
|
dht_settings const& m_settings;
|
|
|
|
|
|
|
|
private:
|
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:
|
2006-08-01 17:27:08 +02:00
|
|
|
table_t m_map;
|
2011-01-19 06:57:44 +01:00
|
|
|
feed_table_t m_feeds;
|
2009-09-27 05:38:41 +02:00
|
|
|
search_table_t m_search_map;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime m_last_tracker_tick;
|
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
|
|
|
|
2011-01-19 06:57:44 +01:00
|
|
|
libtorrent::alert_manager& m_alerts;
|
2011-01-23 03:02:04 +01:00
|
|
|
bool (*m_send)(void*, entry&, udp::endpoint const&, int);
|
2009-09-20 02:23:36 +02:00
|
|
|
void* m_userdata;
|
2006-08-01 17:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|
|
|
|
#endif // NODE_HPP
|
|
|
|
|