2004-01-31 11:46:15 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, 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_TRACKER_MANAGER_HPP_INCLUDED
|
|
|
|
#define TORRENT_TRACKER_MANAGER_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/cstdint.hpp>
|
2004-09-16 03:14:16 +02:00
|
|
|
#include <boost/weak_ptr.hpp>
|
2006-04-25 23:04:48 +02:00
|
|
|
#include <boost/intrusive_ptr.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/socket.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/address.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/peer.hpp" // peer_entry
|
|
|
|
#include "libtorrent/session_settings.hpp" // proxy_settings
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/deadline_timer.hpp"
|
2007-05-05 02:29:33 +02:00
|
|
|
#include "libtorrent/connection_queue.hpp"
|
2007-06-10 22:46:09 +02:00
|
|
|
#include "libtorrent/intrusive_ptr_base.hpp"
|
2009-11-26 22:05:57 +01:00
|
|
|
#include "libtorrent/size_type.hpp"
|
2010-02-14 02:39:55 +01:00
|
|
|
#include "libtorrent/union_endpoint.hpp"
|
2011-08-28 23:06:15 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
#include <boost/asio/ssl/context.hpp>
|
|
|
|
#endif
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
struct request_callback;
|
|
|
|
class tracker_manager;
|
2006-04-25 23:04:48 +02:00
|
|
|
struct timeout_handler;
|
|
|
|
struct tracker_connection;
|
2008-09-22 02:15:05 +02:00
|
|
|
namespace aux { struct session_impl; }
|
2004-10-11 23:50:04 +02:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
// returns -1 if gzip header is invalid or the header size in bytes
|
2012-03-19 00:31:04 +01:00
|
|
|
TORRENT_EXTRA_EXPORT int gzip_header(const char* buf, int size);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT tracker_request
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2005-03-11 18:21:56 +01:00
|
|
|
tracker_request()
|
|
|
|
: kind(announce_request)
|
2010-10-18 02:10:33 +02:00
|
|
|
, downloaded(-1)
|
|
|
|
, uploaded(-1)
|
|
|
|
, left(-1)
|
|
|
|
, corrupt(0)
|
|
|
|
, redundant(0)
|
2011-02-21 06:24:41 +01:00
|
|
|
, listen_port(0)
|
2005-03-11 18:21:56 +01:00
|
|
|
, event(none)
|
|
|
|
, key(0)
|
|
|
|
, num_want(0)
|
2009-09-25 20:17:59 +02:00
|
|
|
, send_stats(true)
|
2011-03-04 07:55:39 +01:00
|
|
|
, apply_ip_filter(true)
|
2011-08-28 23:06:15 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
, ssl_ctx(0)
|
|
|
|
#endif
|
2005-03-11 18:21:56 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
announce_request,
|
|
|
|
scrape_request
|
|
|
|
} kind;
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
enum event_t
|
|
|
|
{
|
|
|
|
none,
|
|
|
|
completed,
|
|
|
|
started,
|
2010-09-06 06:02:15 +02:00
|
|
|
stopped,
|
|
|
|
paused
|
2004-01-31 11:46:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sha1_hash info_hash;
|
2006-04-25 23:04:48 +02:00
|
|
|
peer_id pid;
|
2004-01-31 11:46:15 +01:00
|
|
|
size_type downloaded;
|
|
|
|
size_type uploaded;
|
|
|
|
size_type left;
|
2010-02-18 07:45:07 +01:00
|
|
|
size_type corrupt;
|
2010-10-18 02:10:33 +02:00
|
|
|
size_type redundant;
|
2004-01-31 11:46:15 +01:00
|
|
|
unsigned short listen_port;
|
|
|
|
event_t event;
|
|
|
|
std::string url;
|
2010-11-18 06:51:52 +01:00
|
|
|
std::string trackerid;
|
2004-03-21 03:03:37 +01:00
|
|
|
int key;
|
|
|
|
int num_want;
|
2007-09-22 18:27:29 +02:00
|
|
|
std::string ipv6;
|
2009-04-12 02:37:06 +02:00
|
|
|
std::string ipv4;
|
2009-05-15 23:23:41 +02:00
|
|
|
address bind_ip;
|
2009-09-25 20:17:59 +02:00
|
|
|
bool send_stats;
|
2011-03-04 07:55:39 +01:00
|
|
|
bool apply_ip_filter;
|
2011-08-28 23:06:15 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
boost::asio::ssl::context* ssl_ctx;
|
|
|
|
#endif
|
2004-01-31 11:46:15 +01:00
|
|
|
};
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT request_callback
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
|
|
|
friend class tracker_manager;
|
|
|
|
request_callback(): m_manager(0) {}
|
2004-09-16 03:14:16 +02:00
|
|
|
virtual ~request_callback() {}
|
2008-04-23 03:54:21 +02:00
|
|
|
virtual void tracker_warning(tracker_request const& req
|
|
|
|
, std::string const& msg) = 0;
|
2009-11-11 06:22:57 +01:00
|
|
|
virtual void tracker_scrape_response(tracker_request const& /*req*/
|
2010-09-06 06:02:15 +02:00
|
|
|
, int /*complete*/, int /*incomplete*/, int /*downloads*/
|
|
|
|
, int /*downloaders*/) {}
|
2004-01-31 11:46:15 +01:00
|
|
|
virtual void tracker_response(
|
2008-04-23 03:54:21 +02:00
|
|
|
tracker_request const& req
|
2008-12-01 09:48:54 +01:00
|
|
|
, address const& tracker_ip
|
2009-05-15 23:23:41 +02:00
|
|
|
, std::list<address> const& ip_list
|
2005-03-24 13:13:47 +01:00
|
|
|
, std::vector<peer_entry>& peers
|
2005-02-21 14:59:24 +01:00
|
|
|
, int interval
|
2009-12-02 05:05:24 +01:00
|
|
|
, int min_interval
|
2005-02-21 14:59:24 +01:00
|
|
|
, int complete
|
2008-03-29 23:45:55 +01:00
|
|
|
, int incomplete
|
2010-11-18 06:51:52 +01:00
|
|
|
, address const& external_ip
|
|
|
|
, std::string const& trackerid) = 0;
|
2004-01-31 11:46:15 +01:00
|
|
|
virtual void tracker_request_error(
|
2008-04-23 03:54:21 +02:00
|
|
|
tracker_request const& req
|
2005-03-24 13:13:47 +01:00
|
|
|
, int response_code
|
2010-02-23 22:53:45 +01:00
|
|
|
, error_code const& ec
|
|
|
|
, const std::string& msg
|
2009-12-21 10:47:32 +01:00
|
|
|
, int retry_interval) = 0;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
union_endpoint m_tracker_address;
|
2004-03-01 01:50:00 +01:00
|
|
|
|
2008-02-17 23:51:03 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2004-01-31 11:46:15 +01:00
|
|
|
virtual void debug_log(const std::string& line) = 0;
|
2011-09-21 11:27:07 +02:00
|
|
|
#else
|
2004-01-31 11:46:15 +01:00
|
|
|
private:
|
2011-09-21 11:27:07 +02:00
|
|
|
#endif
|
2004-01-31 11:46:15 +01:00
|
|
|
tracker_manager* m_manager;
|
|
|
|
};
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT timeout_handler
|
2007-06-10 22:46:09 +02:00
|
|
|
: intrusive_ptr_base<timeout_handler>
|
|
|
|
, boost::noncopyable
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2008-01-08 06:47:43 +01:00
|
|
|
timeout_handler(io_service& str);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
void set_timeout(int completion_timeout, int read_timeout);
|
|
|
|
void restart_read_timeout();
|
|
|
|
void cancel();
|
2010-03-02 10:04:27 +01:00
|
|
|
bool cancelled() const { return m_abort; }
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-11-18 02:06:33 +01:00
|
|
|
virtual void on_timeout(error_code const& ec) = 0;
|
2006-04-25 23:04:48 +02:00
|
|
|
virtual ~timeout_handler() {}
|
|
|
|
|
2011-09-21 11:27:07 +02:00
|
|
|
#if !defined TORRENT_VERBOSE_LOGGING \
|
|
|
|
&& !defined TORRENT_LOGGING \
|
|
|
|
&& !defined TORRENT_ERROR_LOGGING
|
|
|
|
// necessary for logging member offsets
|
2006-04-25 23:04:48 +02:00
|
|
|
private:
|
2011-09-21 11:27:07 +02:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void timeout_callback(error_code const&);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
boost::intrusive_ptr<timeout_handler> self()
|
|
|
|
{ return boost::intrusive_ptr<timeout_handler>(this); }
|
|
|
|
|
|
|
|
// used for timeouts
|
|
|
|
// this is set when the request has been sent
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime m_start_time;
|
2006-04-25 23:04:48 +02:00
|
|
|
// this is set every time something is received
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime m_read_time;
|
2006-04-25 23:04:48 +02:00
|
|
|
// the asio async operation
|
2007-04-05 00:27:36 +02:00
|
|
|
deadline_timer m_timeout;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
int m_completion_timeout;
|
|
|
|
int m_read_timeout;
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
typedef mutex mutex_t;
|
2006-04-25 23:04:48 +02:00
|
|
|
mutable mutex_t m_mutex;
|
2007-11-02 01:27:53 +01:00
|
|
|
bool m_abort;
|
2006-04-25 23:04:48 +02:00
|
|
|
};
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT tracker_connection
|
2006-04-25 23:04:48 +02:00
|
|
|
: timeout_handler
|
|
|
|
{
|
|
|
|
tracker_connection(tracker_manager& man
|
2007-11-20 23:46:27 +01:00
|
|
|
, tracker_request const& req
|
2008-01-08 06:47:43 +01:00
|
|
|
, io_service& ios
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::weak_ptr<request_callback> r);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> requester();
|
2004-01-31 11:46:15 +01:00
|
|
|
virtual ~tracker_connection() {}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
tracker_request const& tracker_req() const { return m_req; }
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2010-02-23 22:53:45 +01:00
|
|
|
void fail_disp(error_code ec) { fail(ec); }
|
|
|
|
void fail(error_code const& ec, int code = -1, char const* msg = ""
|
|
|
|
, int interval = 0, int min_interval = 0);
|
2008-09-07 12:03:59 +02:00
|
|
|
virtual void start() = 0;
|
2007-10-26 09:14:19 +02:00
|
|
|
virtual void close();
|
2009-05-15 23:23:41 +02:00
|
|
|
address const& bind_interface() const { return m_req.bind_ip; }
|
2008-09-22 02:15:05 +02:00
|
|
|
void sent_bytes(int bytes);
|
|
|
|
void received_bytes(int bytes);
|
2010-05-30 03:33:03 +02:00
|
|
|
virtual bool on_receive(error_code const& ec, udp::endpoint const& ep
|
|
|
|
, char const* buf, int size) { return false; }
|
2010-08-03 11:08:37 +02:00
|
|
|
virtual bool on_receive_hostname(error_code const& ec, char const* hostname
|
|
|
|
, char const* buf, int size) { return false; }
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2011-09-21 11:27:07 +02:00
|
|
|
#if !defined TORRENT_VERBOSE_LOGGING \
|
|
|
|
&& !defined TORRENT_LOGGING \
|
|
|
|
&& !defined TORRENT_ERROR_LOGGING
|
|
|
|
// necessary for logging member offsets
|
2006-04-25 23:04:48 +02:00
|
|
|
protected:
|
2011-09-21 11:27:07 +02:00
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::weak_ptr<request_callback> m_requester;
|
2011-09-21 11:27:07 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
tracker_manager& m_man;
|
2011-09-21 11:27:07 +02:00
|
|
|
|
|
|
|
#if !defined TORRENT_VERBOSE_LOGGING \
|
|
|
|
&& !defined TORRENT_LOGGING \
|
|
|
|
&& !defined TORRENT_ERROR_LOGGING
|
|
|
|
// necessary for logging member offsets
|
|
|
|
private:
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
const tracker_request m_req;
|
2004-01-31 11:46:15 +01:00
|
|
|
};
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT tracker_manager: boost::noncopyable
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2008-09-22 02:15:05 +02:00
|
|
|
tracker_manager(aux::session_impl& ses, proxy_settings const& ps)
|
|
|
|
: m_ses(ses)
|
2007-05-23 03:02:46 +02:00
|
|
|
, m_proxy(ps)
|
2008-09-22 01:25:09 +02:00
|
|
|
, m_abort(false) {}
|
2008-12-25 03:05:23 +01:00
|
|
|
~tracker_manager();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-02-22 23:40:45 +01:00
|
|
|
void queue_request(
|
2008-01-08 06:47:43 +01:00
|
|
|
io_service& ios
|
2007-05-05 02:29:33 +02:00
|
|
|
, connection_queue& cc
|
2006-04-25 23:04:48 +02:00
|
|
|
, tracker_request r
|
2005-02-23 10:13:42 +01:00
|
|
|
, std::string const& auth
|
2004-09-16 03:14:16 +02:00
|
|
|
, boost::weak_ptr<request_callback> c
|
2005-02-23 10:13:42 +01:00
|
|
|
= boost::weak_ptr<request_callback>());
|
2008-12-25 03:05:23 +01:00
|
|
|
void abort_all_requests(bool all = false);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void remove_request(tracker_connection const*);
|
2006-08-01 17:27:08 +02:00
|
|
|
bool empty() const;
|
2007-10-07 20:06:56 +02:00
|
|
|
int num_requests() const;
|
2008-09-22 02:15:05 +02:00
|
|
|
|
|
|
|
void sent_bytes(int bytes);
|
|
|
|
void received_bytes(int bytes);
|
2010-05-30 03:33:03 +02:00
|
|
|
|
|
|
|
bool incoming_udp(error_code const& e, udp::endpoint const& ep, char const* buf, int size);
|
2010-08-03 11:08:37 +02:00
|
|
|
|
|
|
|
// this is only used for SOCKS packets, since
|
|
|
|
// they may be addressed to hostname
|
|
|
|
bool incoming_udp(error_code const& e, char const* hostname, char const* buf, int size);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
private:
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
typedef mutex mutex_t;
|
2006-08-01 17:27:08 +02:00
|
|
|
mutable mutex_t m_mutex;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
typedef std::list<boost::intrusive_ptr<tracker_connection> >
|
2004-07-25 22:57:44 +02:00
|
|
|
tracker_connections_t;
|
|
|
|
tracker_connections_t m_connections;
|
2008-09-22 02:15:05 +02:00
|
|
|
aux::session_impl& m_ses;
|
2007-04-25 20:26:35 +02:00
|
|
|
proxy_settings const& m_proxy;
|
2007-05-23 03:02:46 +02:00
|
|
|
bool m_abort;
|
2004-01-31 11:46:15 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_TRACKER_MANAGER_HPP_INCLUDED
|
2005-03-19 13:22:40 +01:00
|
|
|
|