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 TORRENT_DISABLE_DHT
|
|
|
|
|
|
|
|
#ifndef TORRENT_DHT_TRACKER
|
|
|
|
#define TORRENT_DHT_TRACKER
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <set>
|
|
|
|
#include <numeric>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
|
|
|
#include <boost/date_time/posix_time/ptime.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2007-02-25 10:42:43 +01:00
|
|
|
#include <boost/intrusive_ptr.hpp>
|
|
|
|
#include <boost/detail/atomic_count.hpp>
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#include "libtorrent/kademlia/node.hpp"
|
|
|
|
#include "libtorrent/kademlia/node_id.hpp"
|
|
|
|
#include "libtorrent/kademlia/traversal_algorithm.hpp"
|
|
|
|
#include "libtorrent/kademlia/packet_iterator.hpp"
|
|
|
|
#include "libtorrent/session_settings.hpp"
|
2006-09-05 01:22:21 +02:00
|
|
|
#include "libtorrent/session_status.hpp"
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
namespace libtorrent { namespace dht
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
TORRENT_DECLARE_LOG(dht_tracker);
|
|
|
|
#endif
|
|
|
|
|
2007-02-25 10:42:43 +01:00
|
|
|
struct dht_tracker;
|
|
|
|
|
|
|
|
TORRENT_EXPORT void intrusive_ptr_add_ref(dht_tracker const*);
|
|
|
|
TORRENT_EXPORT void intrusive_ptr_release(dht_tracker const*);
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
struct dht_tracker
|
|
|
|
{
|
2007-02-25 10:42:43 +01:00
|
|
|
friend void intrusive_ptr_add_ref(dht_tracker const*);
|
|
|
|
friend void intrusive_ptr_release(dht_tracker const*);
|
2006-12-15 18:47:21 +01:00
|
|
|
dht_tracker(asio::io_service& ios, dht_settings const& settings
|
2006-08-30 02:09:58 +02:00
|
|
|
, asio::ip::address listen_interface, entry const& bootstrap);
|
2007-02-25 10:42:43 +01:00
|
|
|
void stop();
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
void add_node(udp::endpoint node);
|
|
|
|
void add_node(std::pair<std::string, int> const& node);
|
2006-09-27 19:20:18 +02:00
|
|
|
void add_router_node(std::pair<std::string, int> const& node);
|
|
|
|
|
2006-09-12 01:34:07 +02:00
|
|
|
void rebind(asio::ip::address listen_interface, int listen_port);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
entry state() const;
|
|
|
|
|
|
|
|
void announce(sha1_hash const& ih, int listen_port
|
|
|
|
, boost::function<void(std::vector<tcp::endpoint> const&
|
|
|
|
, sha1_hash const&)> f);
|
|
|
|
|
2006-09-05 01:22:21 +02:00
|
|
|
void dht_status(session_status& s);
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
private:
|
2007-02-25 10:42:43 +01:00
|
|
|
|
|
|
|
boost::intrusive_ptr<dht_tracker> self()
|
|
|
|
{ return boost::intrusive_ptr<dht_tracker>(this); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void on_name_lookup(asio::error_code const& e
|
2006-08-01 17:27:08 +02:00
|
|
|
, udp::resolver::iterator host);
|
2006-11-14 01:08:16 +01:00
|
|
|
void on_router_name_lookup(asio::error_code const& e
|
2006-09-27 19:20:18 +02:00
|
|
|
, udp::resolver::iterator host);
|
2006-11-14 01:08:16 +01:00
|
|
|
void connection_timeout(asio::error_code const& e);
|
|
|
|
void refresh_timeout(asio::error_code const& e);
|
|
|
|
void tick(asio::error_code const& e);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2007-02-21 18:15:47 +01:00
|
|
|
// translate bittorrent kademlia message into the generic kademlia message
|
2006-08-01 17:27:08 +02:00
|
|
|
// used by the library
|
2006-11-14 01:08:16 +01:00
|
|
|
void on_receive(asio::error_code const& error, size_t bytes_transferred);
|
2006-08-01 17:27:08 +02:00
|
|
|
void on_bootstrap();
|
|
|
|
void send_packet(msg const& m);
|
|
|
|
|
2006-12-15 18:47:21 +01:00
|
|
|
asio::strand m_strand;
|
2006-08-01 17:27:08 +02:00
|
|
|
asio::ip::udp::socket m_socket;
|
|
|
|
|
|
|
|
node_impl m_dht;
|
|
|
|
|
|
|
|
// this is the index of the receive buffer we are currently receiving to
|
|
|
|
// the other buffer is the one containing the last message
|
|
|
|
int m_buffer;
|
|
|
|
std::vector<char> m_in_buf[2];
|
|
|
|
udp::endpoint m_remote_endpoint[2];
|
|
|
|
std::vector<char> m_send_buf;
|
|
|
|
|
|
|
|
boost::posix_time::ptime m_last_refresh;
|
|
|
|
deadline_timer m_timer;
|
2006-08-06 18:36:00 +02:00
|
|
|
deadline_timer m_connection_timer;
|
|
|
|
deadline_timer m_refresh_timer;
|
2006-08-01 17:27:08 +02:00
|
|
|
dht_settings const& m_settings;
|
|
|
|
int m_refresh_bucket;
|
|
|
|
|
|
|
|
// used to resolve hostnames for nodes
|
|
|
|
udp::resolver m_host_resolver;
|
2007-02-25 10:42:43 +01:00
|
|
|
|
|
|
|
// reference counter for intrusive_ptr
|
|
|
|
mutable boost::detail::atomic_count m_refs;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DHT_VERBOSE_LOGGING
|
|
|
|
int m_replies_sent[5];
|
|
|
|
int m_queries_received[5];
|
|
|
|
int m_replies_bytes_sent[5];
|
|
|
|
int m_queries_bytes_received[5];
|
|
|
|
int m_counter;
|
|
|
|
int m_announces;
|
|
|
|
int m_failed_announces;
|
|
|
|
|
|
|
|
int m_total_message_input;
|
|
|
|
int m_ut_message_input;
|
|
|
|
int m_lt_message_input;
|
|
|
|
int m_mp_message_input;
|
|
|
|
int m_gr_message_input;
|
2006-08-06 18:36:00 +02:00
|
|
|
|
|
|
|
int m_total_in_bytes;
|
|
|
|
int m_total_out_bytes;
|
|
|
|
|
|
|
|
int m_queries_out_bytes;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|