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/thread/mutex.hpp>
|
|
|
|
#include <boost/thread/recursive_mutex.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
2006-05-21 01:24:19 +02:00
|
|
|
#include "libtorrent/session_settings.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
|
|
|
#include "libtorrent/peer.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2007-05-05 02:29:33 +02:00
|
|
|
#include "libtorrent/connection_queue.hpp"
|
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;
|
2004-10-11 23:50:04 +02:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
// encodes a string using the base64 scheme
|
2005-11-01 19:30:39 +01:00
|
|
|
TORRENT_EXPORT std::string base64encode(const std::string& s);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
// returns -1 if gzip header is invalid or the header size in bytes
|
2005-11-01 19:30:39 +01:00
|
|
|
TORRENT_EXPORT int gzip_header(const char* buf, int size);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-05-22 22:44:18 +02:00
|
|
|
TORRENT_EXPORT boost::tuple<std::string, std::string, std::string, int, std::string>
|
2006-04-25 23:04:48 +02:00
|
|
|
parse_url_components(std::string url);
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_EXPORT tracker_request
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2005-03-11 18:21:56 +01:00
|
|
|
tracker_request()
|
|
|
|
: kind(announce_request)
|
|
|
|
, event(none)
|
|
|
|
, key(0)
|
|
|
|
, num_want(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
announce_request,
|
|
|
|
scrape_request
|
|
|
|
} kind;
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
enum event_t
|
|
|
|
{
|
|
|
|
none,
|
|
|
|
completed,
|
|
|
|
started,
|
|
|
|
stopped
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
unsigned short listen_port;
|
|
|
|
event_t event;
|
|
|
|
std::string url;
|
2004-03-21 03:03:37 +01:00
|
|
|
int key;
|
|
|
|
int num_want;
|
2004-01-31 11:46:15 +01:00
|
|
|
};
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_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() {}
|
2005-08-11 01:32:39 +02:00
|
|
|
virtual void tracker_warning(std::string const& msg) = 0;
|
2004-01-31 11:46:15 +01:00
|
|
|
virtual void tracker_response(
|
2005-03-24 13:13:47 +01:00
|
|
|
tracker_request const&
|
|
|
|
, std::vector<peer_entry>& peers
|
2005-02-21 14:59:24 +01:00
|
|
|
, int interval
|
|
|
|
, int complete
|
2005-02-23 09:57:54 +01:00
|
|
|
, int incomplete) = 0;
|
2005-03-24 13:13:47 +01:00
|
|
|
virtual void tracker_request_timed_out(
|
|
|
|
tracker_request const&) = 0;
|
2004-01-31 11:46:15 +01:00
|
|
|
virtual void tracker_request_error(
|
2005-03-24 13:13:47 +01:00
|
|
|
tracker_request const&
|
|
|
|
, int response_code
|
2004-01-31 11:46:15 +01:00
|
|
|
, const std::string& description) = 0;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
tcp::endpoint m_tracker_address;
|
2004-03-01 01:50:00 +01:00
|
|
|
|
2005-07-06 15:18:10 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2004-01-31 11:46:15 +01:00
|
|
|
virtual void debug_log(const std::string& line) = 0;
|
|
|
|
#endif
|
|
|
|
private:
|
|
|
|
tracker_manager* m_manager;
|
|
|
|
};
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
TORRENT_EXPORT bool inflate_gzip(
|
2005-03-24 13:13:47 +01:00
|
|
|
std::vector<char>& buffer
|
|
|
|
, tracker_request const& req
|
|
|
|
, request_callback* requester
|
|
|
|
, int maximum_tracker_response_length);
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
TORRENT_EXPORT void intrusive_ptr_add_ref(timeout_handler const*);
|
|
|
|
TORRENT_EXPORT void intrusive_ptr_release(timeout_handler const*);
|
|
|
|
|
|
|
|
struct TORRENT_EXPORT timeout_handler
|
|
|
|
: boost::noncopyable
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2007-05-21 21:31:37 +02:00
|
|
|
friend TORRENT_EXPORT void intrusive_ptr_add_ref(timeout_handler const*);
|
|
|
|
friend TORRENT_EXPORT void intrusive_ptr_release(timeout_handler const*);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-12-15 18:47:21 +01:00
|
|
|
timeout_handler(asio::strand& str);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
void set_timeout(int completion_timeout, int read_timeout);
|
|
|
|
void restart_read_timeout();
|
|
|
|
void cancel();
|
|
|
|
|
|
|
|
virtual void on_timeout() = 0;
|
|
|
|
virtual ~timeout_handler() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void timeout_callback(asio::error_code const&);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
boost::intrusive_ptr<timeout_handler> self()
|
|
|
|
{ return boost::intrusive_ptr<timeout_handler>(this); }
|
|
|
|
|
2006-12-15 18:47:21 +01:00
|
|
|
asio::strand& m_strand;
|
2006-04-25 23:04:48 +02:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
typedef boost::mutex mutex_t;
|
|
|
|
mutable mutex_t m_mutex;
|
|
|
|
mutable int m_refs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TORRENT_EXPORT tracker_connection
|
|
|
|
: timeout_handler
|
|
|
|
{
|
|
|
|
tracker_connection(tracker_manager& man
|
|
|
|
, tracker_request req
|
2006-12-15 18:47:21 +01:00
|
|
|
, asio::strand& str
|
2007-03-02 19:40:02 +01:00
|
|
|
, address bind_interface
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::weak_ptr<request_callback> r);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2004-09-16 03:14:16 +02:00
|
|
|
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; }
|
|
|
|
bool has_requester() const { return !m_requester.expired(); }
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void fail(int code, char const* msg);
|
|
|
|
void fail_timeout();
|
|
|
|
void close();
|
2007-03-02 19:40:02 +01:00
|
|
|
address const& bind_interface() const { return m_bind_interface; }
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
protected:
|
|
|
|
boost::weak_ptr<request_callback> m_requester;
|
|
|
|
private:
|
2007-03-02 19:40:02 +01:00
|
|
|
address m_bind_interface;
|
2006-04-25 23:04:48 +02:00
|
|
|
tracker_manager& m_man;
|
|
|
|
const tracker_request m_req;
|
2004-01-31 11:46:15 +01:00
|
|
|
};
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT tracker_manager: boost::noncopyable
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2007-04-25 20:26:35 +02:00
|
|
|
tracker_manager(session_settings const& s, proxy_settings const& ps)
|
|
|
|
: m_settings(s)
|
2007-05-23 03:02:46 +02:00
|
|
|
, m_proxy(ps)
|
|
|
|
, m_abort(false) {}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-02-22 23:40:45 +01:00
|
|
|
void queue_request(
|
2006-12-15 18:47:21 +01:00
|
|
|
asio::strand& str
|
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
|
2007-03-02 19:40:02 +01:00
|
|
|
, address bind_infc
|
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>());
|
2004-01-31 11:46:15 +01:00
|
|
|
void abort_all_requests();
|
|
|
|
|
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;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
private:
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
typedef boost::recursive_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;
|
2006-05-21 01:24:19 +02:00
|
|
|
session_settings const& m_settings;
|
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
|
|
|
|