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>
|
|
|
|
|
|
|
|
#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>
|
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>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/iterator/transform_iterator.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
2008-10-09 05:32:57 +02:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
|
|
|
namespace aux { struct session_impl; }
|
|
|
|
struct session_status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_DECLARE_LOG(node);
|
|
|
|
#endif
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
struct traversal_algorithm;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
std::set<peer_entry> peers;
|
|
|
|
};
|
|
|
|
|
|
|
|
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:
|
|
|
|
announce_observer(boost::pool<>& allocator
|
|
|
|
, sha1_hash const& info_hash
|
|
|
|
, int listen_port
|
|
|
|
, entry const& write_token)
|
|
|
|
: observer(allocator)
|
|
|
|
, m_info_hash(info_hash)
|
|
|
|
, m_listen_port(listen_port)
|
|
|
|
, m_token(write_token)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void send(msg& m)
|
|
|
|
{
|
|
|
|
m.port = m_listen_port;
|
|
|
|
m.info_hash = m_info_hash;
|
|
|
|
m.write_token = m_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
void timeout() {}
|
|
|
|
void reply(msg const&) {}
|
|
|
|
void abort() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sha1_hash m_info_hash;
|
|
|
|
int m_listen_port;
|
|
|
|
entry m_token;
|
|
|
|
};
|
|
|
|
|
|
|
|
class get_peers_observer : public observer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
get_peers_observer(sha1_hash const& info_hash
|
|
|
|
, int listen_port
|
|
|
|
, rpc_manager& rpc
|
|
|
|
, boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> f)
|
|
|
|
: observer(rpc.allocator())
|
|
|
|
, m_info_hash(info_hash)
|
|
|
|
, m_listen_port(listen_port)
|
|
|
|
, m_rpc(rpc)
|
|
|
|
, m_fun(f)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void send(msg& m)
|
|
|
|
{
|
|
|
|
m.port = m_listen_port;
|
|
|
|
m.info_hash = m_info_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void timeout() {}
|
|
|
|
void reply(msg const& r)
|
|
|
|
{
|
2008-02-10 03:26:45 +01:00
|
|
|
observer_ptr o(new (m_rpc.allocator().malloc()) announce_observer(
|
|
|
|
m_rpc.allocator(), m_info_hash, m_listen_port, r.write_token));
|
|
|
|
#ifndef NDEBUG
|
|
|
|
o->m_in_constructor = false;
|
|
|
|
#endif
|
|
|
|
m_rpc.invoke(messages::announce_peer, r.addr, o);
|
2007-05-23 10:45:12 +02:00
|
|
|
m_fun(r.peers, m_info_hash);
|
|
|
|
}
|
|
|
|
void abort() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sha1_hash m_info_hash;
|
|
|
|
int m_listen_port;
|
|
|
|
rpc_manager& m_rpc;
|
|
|
|
boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> m_fun;
|
|
|
|
};
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
class node_impl : boost::noncopyable
|
|
|
|
{
|
|
|
|
typedef std::map<node_id, torrent_entry> table_t;
|
|
|
|
public:
|
2008-09-20 19:42:25 +02:00
|
|
|
node_impl(libtorrent::aux::session_impl& ses, boost::function<void(msg const&)> const& f
|
2008-09-02 08:37:40 +02:00
|
|
|
, dht_settings const& settings);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
virtual ~node_impl() {}
|
|
|
|
|
|
|
|
void refresh(node_id const& id, boost::function0<void> f);
|
|
|
|
void bootstrap(std::vector<udp::endpoint> const& nodes
|
|
|
|
, boost::function0<void> f);
|
|
|
|
void find_node(node_id const& id, boost::function<
|
|
|
|
void(std::vector<node_entry> 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);
|
|
|
|
|
|
|
|
void refresh();
|
|
|
|
void refresh_bucket(int bucket);
|
|
|
|
int bucket_size(int bucket);
|
|
|
|
|
|
|
|
typedef routing_table::iterator iterator;
|
|
|
|
|
|
|
|
iterator begin() const { return m_table.begin(); }
|
|
|
|
iterator end() const { return m_table.end(); }
|
|
|
|
|
|
|
|
typedef table_t::iterator data_iterator;
|
|
|
|
|
2008-09-02 08:37:40 +02:00
|
|
|
void set_node_id(node_id const& nid) { m_id = nid; }
|
2006-08-01 17:27:08 +02:00
|
|
|
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
|
|
|
|
|
|
|
data_iterator begin_data() { return m_map.begin(); }
|
|
|
|
data_iterator end_data() { return m_map.end(); }
|
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
|
|
|
|
, boost::function<void(std::vector<tcp::endpoint> const&
|
|
|
|
, sha1_hash const&)> f);
|
|
|
|
|
|
|
|
bool verify_token(msg const& m);
|
|
|
|
entry generate_token(msg const& m);
|
|
|
|
|
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();
|
|
|
|
time_duration refresh_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);
|
|
|
|
|
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'.
|
|
|
|
bool on_find(msg const& m, std::vector<tcp::endpoint>& peers) const;
|
|
|
|
|
|
|
|
// this is called when a store request is received. The data
|
|
|
|
// is store-parameters and the data to be stored.
|
|
|
|
void on_announce(msg const& m, msg& reply);
|
|
|
|
|
|
|
|
dht_settings const& m_settings;
|
|
|
|
|
|
|
|
// the maximum number of peers to send in a get_peers
|
|
|
|
// reply. Ordinary trackers usually limit this to 50.
|
|
|
|
// 50 => 6 * 50 = 250 bytes + packet overhead
|
|
|
|
int m_max_peers_reply;
|
|
|
|
|
|
|
|
private:
|
2008-10-09 05:32:57 +02:00
|
|
|
typedef boost::mutex mutex_t;
|
|
|
|
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;
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void incoming_request(msg const& h);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
|
|
|
libtorrent::aux::session_impl& m_ses;
|
2006-08-01 17:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|
|
|
|
|
|
|
|
#endif // NODE_HPP
|
|
|
|
|