00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef NODE_HPP
00034 #define NODE_HPP
00035
00036 #include <algorithm>
00037 #include <cassert>
00038 #include <map>
00039 #include <set>
00040
00041 #include <libtorrent/kademlia/routing_table.hpp>
00042 #include <libtorrent/kademlia/rpc_manager.hpp>
00043 #include <libtorrent/kademlia/node_id.hpp>
00044
00045 #include <libtorrent/io.hpp>
00046 #include <libtorrent/session_settings.hpp>
00047
00048 #include <boost/cstdint.hpp>
00049 #include <boost/optional.hpp>
00050 #include <boost/date_time/posix_time/ptime.hpp>
00051 #include <boost/date_time/posix_time/posix_time_types.hpp>
00052 #include <boost/iterator/transform_iterator.hpp>
00053 #include <boost/ref.hpp>
00054
00055 namespace libtorrent { namespace dht
00056 {
00057
00058 using asio::ip::udp;
00059
00060 #ifdef TORRENT_DHT_VERBOSE_LOGGING
00061 TORRENT_DECLARE_LOG(node);
00062 #endif
00063
00064
00065
00066
00067 struct peer_entry
00068 {
00069 tcp::endpoint addr;
00070 boost::posix_time::ptime added;
00071 };
00072
00073
00074 struct torrent_entry
00075 {
00076 std::set<peer_entry> peers;
00077 };
00078
00079 inline bool operator<(peer_entry const& lhs, peer_entry const& rhs)
00080 {
00081 return lhs.addr.address() == rhs.addr.address()
00082 ? lhs.addr.port() < rhs.addr.port()
00083 : lhs.addr.address() < rhs.addr.address();
00084 }
00085
00086 struct null_type {};
00087
00088 class node_impl : boost::noncopyable
00089 {
00090 typedef std::map<node_id, torrent_entry> table_t;
00091 public:
00092 node_impl(boost::function<void(msg const&)> const& f
00093 , dht_settings const& settings, boost::optional<node_id> node_id);
00094
00095 virtual ~node_impl() {}
00096
00097 void refresh(node_id const& id, boost::function0<void> f);
00098 void bootstrap(std::vector<udp::endpoint> const& nodes
00099 , boost::function0<void> f);
00100 void find_node(node_id const& id, boost::function<
00101 void(std::vector<node_entry> const&)> f);
00102 void add_router_node(udp::endpoint router);
00103
00104 void incoming(msg const& m);
00105
00106 void refresh();
00107 void refresh_bucket(int bucket);
00108 int bucket_size(int bucket);
00109
00110 typedef routing_table::iterator iterator;
00111
00112 iterator begin() const { return m_table.begin(); }
00113 iterator end() const { return m_table.end(); }
00114
00115 typedef table_t::iterator data_iterator;
00116
00117 node_id const& nid() const { return m_id; }
00118 boost::tuple<int, int> size() const{ return m_table.size(); }
00119
00120 data_iterator begin_data() { return m_map.begin(); }
00121 data_iterator end_data() { return m_map.end(); }
00122 int data_size() const { return int(m_map.size()); }
00123
00124 void print_state(std::ostream& os) const
00125 { m_table.print_state(os); }
00126
00127 void announce(sha1_hash const& info_hash, int listen_port
00128 , boost::function<void(std::vector<tcp::endpoint> const&
00129 , sha1_hash const&)> f);
00130
00131 bool verify_token(msg const& m);
00132 entry generate_token(msg const& m);
00133
00134
00135
00136 boost::posix_time::time_duration connection_timeout();
00137 boost::posix_time::time_duration refresh_timeout();
00138
00139
00140 void new_write_key();
00141
00142
00143
00144
00145 void add_node(udp::endpoint node);
00146
00147 void replacement_cache(bucket_t& nodes) const
00148 { m_table.replacement_cache(nodes); }
00149
00150 protected:
00151
00152
00153
00154 bool on_find(msg const& m, std::vector<tcp::endpoint>& peers) const;
00155
00156
00157
00158 void on_announce(msg const& m, msg& reply);
00159
00160 dht_settings const& m_settings;
00161
00162
00163
00164
00165 int m_max_peers_reply;
00166
00167 private:
00168 void incoming_request(msg const& h);
00169
00170 node_id m_id;
00171 routing_table m_table;
00172 rpc_manager m_rpc;
00173 table_t m_map;
00174
00175 boost::posix_time::ptime m_last_tracker_tick;
00176
00177
00178 int m_secret[2];
00179 };
00180
00181
00182 } }
00183
00184 #endif // NODE_HPP
00185