premiere-libtorrent/include/libtorrent/tracker_manager.hpp

423 lines
12 KiB
C++
Raw Normal View History

2004-01-31 11:46:15 +01:00
/*
Copyright (c) 2003-2016, Arvid Norberg
2004-01-31 11:46:15 +01:00
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
2015-04-19 15:45:13 +02:00
#include "libtorrent/config.hpp"
2004-01-31 11:46:15 +01:00
#include <vector>
#include <string>
2014-07-06 21:18:00 +02:00
#include <list>
2004-01-31 11:46:15 +01:00
#include <utility>
#include <cstdint>
#include <tuple>
#include <functional>
#include <memory>
#include <unordered_map>
2004-01-31 11:46:15 +01:00
#ifdef TORRENT_USE_OPENSSL
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/asio/ssl/context.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif
2004-01-31 11:46:15 +01:00
#include "libtorrent/socket.hpp"
#include "libtorrent/address.hpp"
2004-01-31 11:46:15 +01:00
#include "libtorrent/peer_id.hpp"
#include "libtorrent/peer.hpp" // peer_entry
#include "libtorrent/deadline_timer.hpp"
#include "libtorrent/union_endpoint.hpp"
#include "libtorrent/io_service.hpp"
#include "libtorrent/span.hpp"
#include "libtorrent/time.hpp"
#include "libtorrent/debug.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/aux_/listen_socket_handle.hpp"
2017-07-26 19:38:40 +02:00
#include "libtorrent/udp_socket.hpp"
2004-01-31 11:46:15 +01:00
namespace libtorrent {
2004-01-31 11:46:15 +01:00
struct request_callback;
class tracker_manager;
struct timeout_handler;
class udp_tracker_connection;
class http_tracker_connection;
struct resolver_interface;
struct counters;
struct ip_filter;
#if TORRENT_USE_I2P
class i2p_connection;
#endif
namespace aux { struct session_logger; struct session_settings; }
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
TORRENT_EXTRA_EXPORT int gzip_header(const char* buf, int size);
2004-01-31 11:46:15 +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()
2014-07-06 21:18:00 +02:00
: downloaded(-1)
, uploaded(-1)
, left(-1)
, corrupt(0)
, redundant(0)
, listen_port(0)
2005-03-11 18:21:56 +01:00
, event(none)
2014-07-06 21:18:00 +02:00
, kind(announce_request)
2005-03-11 18:21:56 +01:00
, key(0)
, num_want(0)
, private_torrent(false)
, triggered_manually(false)
#ifdef TORRENT_USE_OPENSSL
, ssl_ctx(0)
#endif
#if TORRENT_USE_I2P
, i2pconn(0)
#endif
2005-03-11 18:21:56 +01:00
{}
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
};
2014-07-06 21:18:00 +02:00
enum kind_t
{
// do not compare against announce_request ! check if not scrape instead
announce_request = 0,
scrape_request = 1,
// affects interpretation of peers string in HTTP response
// see parse_tracker_response()
i2p = 2
2014-07-06 21:18:00 +02:00
};
std::string url;
std::string trackerid;
2015-12-13 21:14:19 +01:00
#ifndef TORRENT_NO_DEPRECATE
std::string auth;
2015-12-13 21:14:19 +01:00
#endif
2014-07-06 21:18:00 +02:00
std::shared_ptr<const ip_filter> filter;
std::int64_t downloaded;
std::int64_t uploaded;
std::int64_t left;
std::int64_t corrupt;
std::int64_t redundant;
std::uint16_t listen_port;
2014-07-06 21:18:00 +02:00
// values from event_t
std::uint8_t event;
2014-07-06 21:18:00 +02:00
// values from kind_t
std::uint8_t kind;
2014-07-06 21:18:00 +02:00
std::uint32_t key;
2004-03-21 03:03:37 +01:00
int num_want;
#if TORRENT_USE_IPV6
std::vector<address_v6> ipv6;
#endif
2014-07-06 21:18:00 +02:00
sha1_hash info_hash;
peer_id pid;
aux::listen_socket_handle outgoing_socket;
2014-07-06 21:18:00 +02:00
// set to true if the .torrent file this tracker announce is for is marked
// as private (i.e. has the "priv": 1 key)
bool private_torrent;
// this is set to true if this request was triggered by a "manual" call to
// scrape_tracker() or force_reannounce()
bool triggered_manually;
#ifdef TORRENT_USE_OPENSSL
boost::asio::ssl::context* ssl_ctx;
#endif
#if TORRENT_USE_I2P
i2p_connection* i2pconn;
#endif
2004-01-31 11:46:15 +01:00
};
struct tracker_response
{
tracker_response()
: interval(1800)
, min_interval(120)
, complete(-1)
, incomplete(-1)
, downloaders(-1)
, downloaded(-1)
{}
// peers from the tracker, in various forms
std::vector<peer_entry> peers;
std::vector<ipv4_peer_entry> peers4;
#if TORRENT_USE_IPV6
std::vector<ipv6_peer_entry> peers6;
#endif
// our external IP address (if the tracker responded with ti, otherwise
// INADDR_ANY)
address external_ip;
// the tracker id, if it was included in the response, otherwise
// an empty string
std::string trackerid;
// if the tracker returned an error, this is set to that error
std::string failure_reason;
// contains a warning message from the tracker, if included in
// the response
std::string warning_message;
// re-announce interval, in seconds
seconds32 interval;
// the lowest force-announce interval
seconds32 min_interval;
// the number of seeds in the swarm
int complete;
// the number of downloaders in the swarm
int incomplete;
// if supported by the tracker, the number of actively downloading peers.
2016-08-29 14:31:23 +02:00
// i.e. partial seeds. If not supported, -1
int downloaders;
// the number of times the torrent has been downloaded
int downloaded;
};
struct TORRENT_EXTRA_EXPORT request_callback
2004-01-31 11:46:15 +01:00
{
friend class tracker_manager;
2014-07-06 21:18:00 +02:00
request_callback() {}
2004-09-16 03:14:16 +02:00
virtual ~request_callback() {}
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(
tracker_request const& req
, address const& tracker_ip
, std::list<address> const& ip_list
, struct tracker_response const& response) = 0;
2004-01-31 11:46:15 +01:00
virtual void tracker_request_error(
tracker_request const& req
, int response_code
2010-02-23 22:53:45 +01:00
, error_code const& ec
, const std::string& msg
, seconds32 retry_interval) = 0;
2004-01-31 11:46:15 +01:00
#ifndef TORRENT_DISABLE_LOGGING
virtual bool should_log() const = 0;
virtual void debug_log(const char* fmt, ...) const TORRENT_FORMAT(2,3) = 0;
#endif
2004-01-31 11:46:15 +01:00
};
struct TORRENT_EXTRA_EXPORT timeout_handler
2016-08-29 14:31:23 +02:00
: std::enable_shared_from_this<timeout_handler>
2007-06-10 22:46:09 +02:00
, boost::noncopyable
2004-01-31 11:46:15 +01:00
{
explicit timeout_handler(io_service& str);
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; }
2010-11-18 02:06:33 +01:00
virtual void on_timeout(error_code const& ec) = 0;
virtual ~timeout_handler();
2013-11-03 00:08:26 +01:00
io_service& get_io_service() { return m_timeout.get_io_service(); }
private:
2015-05-16 09:21:23 +02:00
2008-05-03 18:05:42 +02:00
void timeout_callback(error_code const&);
int m_completion_timeout = 0;
2014-07-06 21:18:00 +02:00
// used for timeouts
// this is set when the request has been sent
time_point m_start_time;
2014-07-06 21:18:00 +02:00
// this is set every time something is received
time_point m_read_time;
2014-07-06 21:18:00 +02:00
// the asio async operation
deadline_timer m_timeout;
2015-05-16 09:21:23 +02:00
int m_read_timeout = 0;
bool m_abort = false;
#if TORRENT_USE_ASSERTS
int m_outstanding_timer_wait = 0;
#endif
};
struct TORRENT_EXTRA_EXPORT tracker_connection
: 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
, std::weak_ptr<request_callback> r);
2004-01-31 11:46:15 +01:00
std::shared_ptr<request_callback> requester() const;
2004-01-31 11:46:15 +01:00
virtual ~tracker_connection() {}
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(error_code const& ec, int code = -1, char const* msg = ""
, seconds32 interval = seconds32(0), seconds32 min_interval = seconds32(0));
virtual void start() = 0;
virtual void close() = 0;
address bind_interface() const;
aux::listen_socket_handle const& bind_socket() const { return m_req.outgoing_socket; }
void sent_bytes(int bytes);
void received_bytes(int bytes);
2004-01-31 11:46:15 +01:00
2016-08-29 14:31:23 +02:00
std::shared_ptr<tracker_connection> shared_from_this()
{
2016-08-29 14:31:23 +02:00
return std::static_pointer_cast<tracker_connection>(
timeout_handler::shared_from_this());
}
2013-11-03 00:08:26 +01:00
2014-07-06 21:18:00 +02:00
private:
const tracker_request m_req;
protected:
2013-11-03 00:08:26 +01:00
void fail_impl(error_code const& ec, int code = -1, std::string msg = std::string()
, seconds32 interval = seconds32(0), seconds32 min_interval = seconds32(0));
2013-11-03 00:08:26 +01:00
std::weak_ptr<request_callback> m_requester;
tracker_manager& m_man;
2004-01-31 11:46:15 +01:00
};
class TORRENT_EXTRA_EXPORT tracker_manager final
: boost::noncopyable
, single_threaded
2004-01-31 11:46:15 +01:00
{
public:
typedef std::function<void(aux::listen_socket_handle const&
, udp::endpoint const&
, span<char const>
2017-07-26 19:38:40 +02:00
, error_code&, udp_send_flags_t)> send_fun_t;
typedef std::function<void(aux::listen_socket_handle const&
, char const*, int
, span<char const>
2017-07-26 19:38:40 +02:00
, error_code&, udp_send_flags_t)> send_fun_hostname_t;
tracker_manager(send_fun_t const& send_fun
, send_fun_hostname_t const& send_fun_hostname
, counters& stats_counters
, resolver_interface& resolver
, aux::session_settings const& sett
#if !defined TORRENT_DISABLE_LOGGING || TORRENT_USE_ASSERTS
, aux::session_logger& ses
#endif
);
2015-05-16 09:21:23 +02:00
virtual ~tracker_manager();
void queue_request(
2008-01-08 06:47:43 +01:00
io_service& ios
, tracker_request r
, std::weak_ptr<request_callback> c
= std::weak_ptr<request_callback>());
void abort_all_requests(bool all = false);
2004-01-31 11:46:15 +01:00
void remove_request(http_tracker_connection const* c);
void remove_request(udp_tracker_connection const* c);
bool empty() const;
int num_requests() const;
void sent_bytes(int bytes);
void received_bytes(int bytes);
void incoming_error(error_code const& ec, udp::endpoint const& ep);
bool incoming_packet(udp::endpoint const& ep, span<char const> buf);
2010-08-03 11:08:37 +02:00
// this is only used for SOCKS packets, since
// they may be addressed to hostname
// TODO: 3 make sure the udp_socket supports passing on string-hostnames
// too, and that this function is used
bool incoming_packet(char const* hostname, span<char const> buf);
2015-05-16 09:21:23 +02:00
void update_transaction_id(
2016-08-29 14:31:23 +02:00
std::shared_ptr<udp_tracker_connection> c
, std::uint32_t tid);
aux::session_settings const& settings() const { return m_settings; }
resolver_interface& host_resolver() { return m_host_resolver; }
void send_hostname(aux::listen_socket_handle const& sock
, char const* hostname, int port, span<char const> p
2017-07-26 19:38:40 +02:00
, error_code& ec, udp_send_flags_t flags = {});
void send(aux::listen_socket_handle const& sock
, udp::endpoint const& ep, span<char const> p
2017-07-26 19:38:40 +02:00
, error_code& ec, udp_send_flags_t flags = {});
2004-01-31 11:46:15 +01:00
private:
// maps transactionid to the udp_tracker_connection
// These must use shared_ptr to avoid a dangling reference
// if a connection is erased while a timeout event is in the queue
std::unordered_map<std::uint32_t, std::shared_ptr<udp_tracker_connection>> m_udp_conns;
std::vector<std::shared_ptr<http_tracker_connection>> m_http_conns;
send_fun_t m_send_fun;
send_fun_hostname_t m_send_fun_hostname;
resolver_interface& m_host_resolver;
aux::session_settings const& m_settings;
counters& m_stats_counters;
bool m_abort = false;
#if !defined TORRENT_DISABLE_LOGGING || TORRENT_USE_ASSERTS
aux::session_logger& m_ses;
#endif
2004-01-31 11:46:15 +01:00
};
}
#endif // TORRENT_TRACKER_MANAGER_HPP_INCLUDED