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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cctype>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/tracker_manager.hpp"
|
|
|
|
#include "libtorrent/http_tracker_connection.hpp"
|
|
|
|
#include "libtorrent/udp_tracker_connection.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/torrent.hpp"
|
2007-01-10 16:02:25 +01:00
|
|
|
#include "libtorrent/peer_connection.hpp"
|
2008-09-22 02:15:05 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
2006-01-07 14:48:14 +01:00
|
|
|
using boost::tuples::make_tuple;
|
|
|
|
using boost::tuples::tuple;
|
2006-04-25 23:04:48 +02:00
|
|
|
using boost::bind;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
minimum_tracker_response_length = 3,
|
|
|
|
http_buffer_size = 2048
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2008-01-08 06:47:43 +01:00
|
|
|
timeout_handler::timeout_handler(io_service& ios)
|
|
|
|
: m_start_time(time_now())
|
2007-04-05 00:27:36 +02:00
|
|
|
, m_read_time(time_now())
|
2008-01-08 06:47:43 +01:00
|
|
|
, m_timeout(ios)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_completion_timeout(0)
|
|
|
|
, m_read_timeout(0)
|
2007-11-02 01:27:53 +01:00
|
|
|
, m_abort(false)
|
2006-04-25 23:04:48 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
void timeout_handler::set_timeout(int completion_timeout, int read_timeout)
|
|
|
|
{
|
|
|
|
m_completion_timeout = completion_timeout;
|
|
|
|
m_read_timeout = read_timeout;
|
2007-10-26 09:14:19 +02:00
|
|
|
m_start_time = m_read_time = time_now();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-11-02 01:27:53 +01:00
|
|
|
if (m_abort) return;
|
|
|
|
|
|
|
|
int timeout = (std::min)(
|
|
|
|
m_read_timeout, (std::min)(m_completion_timeout, m_read_timeout));
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-29 07:13:49 +01:00
|
|
|
m_timeout.expires_at(m_read_time + seconds(timeout), ec);
|
2008-01-08 06:47:43 +01:00
|
|
|
m_timeout.async_wait(bind(
|
|
|
|
&timeout_handler::timeout_callback, self(), _1));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void timeout_handler::restart_read_timeout()
|
|
|
|
{
|
2007-04-05 00:27:36 +02:00
|
|
|
m_read_time = time_now();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void timeout_handler::cancel()
|
|
|
|
{
|
2007-11-02 01:27:53 +01:00
|
|
|
m_abort = true;
|
2006-04-25 23:04:48 +02:00
|
|
|
m_completion_timeout = 0;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-29 07:13:49 +01:00
|
|
|
m_timeout.cancel(ec);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void timeout_handler::timeout_callback(error_code const& error)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
if (error) return;
|
|
|
|
if (m_completion_timeout == 0) return;
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime now(time_now());
|
2006-04-25 23:04:48 +02:00
|
|
|
time_duration receive_timeout = now - m_read_time;
|
|
|
|
time_duration completion_timeout = now - m_start_time;
|
|
|
|
|
|
|
|
if (m_read_timeout
|
2007-04-05 00:27:36 +02:00
|
|
|
< total_seconds(receive_timeout)
|
2006-04-25 23:04:48 +02:00
|
|
|
|| m_completion_timeout
|
2007-04-05 00:27:36 +02:00
|
|
|
< total_seconds(completion_timeout))
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
on_timeout();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-02 01:27:53 +01:00
|
|
|
if (m_abort) return;
|
|
|
|
|
|
|
|
int timeout = (std::min)(
|
|
|
|
m_read_timeout, (std::min)(m_completion_timeout, m_read_timeout));
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-29 07:13:49 +01:00
|
|
|
m_timeout.expires_at(m_read_time + seconds(timeout), ec);
|
2008-01-08 06:47:43 +01:00
|
|
|
m_timeout.async_wait(
|
|
|
|
bind(&timeout_handler::timeout_callback, self(), _1));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tracker_connection::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
|
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)
|
2008-01-08 06:47:43 +01:00
|
|
|
: timeout_handler(ios)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_requester(r)
|
2007-03-02 21:39:23 +01:00
|
|
|
, m_bind_interface(bind_interface_)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_man(man)
|
|
|
|
, m_req(req)
|
|
|
|
{}
|
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> tracker_connection::requester()
|
2004-09-16 03:14:16 +02:00
|
|
|
{
|
2007-09-14 04:54:15 +02:00
|
|
|
return m_requester.lock();
|
2004-09-16 03:14:16 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void tracker_connection::fail(int code, char const* msg)
|
|
|
|
{
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb) cb->tracker_request_error(m_req, code, msg);
|
2006-04-25 23:04:48 +02:00
|
|
|
close();
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-09-22 02:15:05 +02:00
|
|
|
void tracker_connection::sent_bytes(int bytes)
|
|
|
|
{
|
|
|
|
m_man.sent_bytes(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_connection::received_bytes(int bytes)
|
|
|
|
{
|
|
|
|
m_man.received_bytes(bytes);
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void tracker_connection::fail_timeout()
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb) cb->tracker_request_timed_out(m_req);
|
2006-04-25 23:04:48 +02:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_connection::close()
|
|
|
|
{
|
|
|
|
cancel();
|
2006-06-17 03:29:36 +02:00
|
|
|
m_man.remove_request(this);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2008-09-22 02:15:05 +02:00
|
|
|
void tracker_manager::sent_bytes(int bytes)
|
|
|
|
{
|
|
|
|
aux::session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
m_ses.m_stat.sent_tracker_bytes(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_manager::received_bytes(int bytes)
|
|
|
|
{
|
|
|
|
aux::session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
m_ses.m_stat.received_tracker_bytes(bytes);
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void tracker_manager::remove_request(tracker_connection const* c)
|
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
|
|
|
|
tracker_connections_t::iterator i = std::find(m_connections.begin()
|
|
|
|
, m_connections.end(), boost::intrusive_ptr<const tracker_connection>(c));
|
|
|
|
if (i == m_connections.end()) return;
|
|
|
|
|
|
|
|
m_connections.erase(i);
|
|
|
|
}
|
2007-05-22 22:44:18 +02:00
|
|
|
|
2005-03-11 18:21:56 +01:00
|
|
|
void tracker_manager::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 req
|
2005-03-11 18:21:56 +01:00
|
|
|
, std::string const& auth
|
2007-03-02 19:40:02 +01:00
|
|
|
, address bind_infc
|
2005-03-11 18:21:56 +01:00
|
|
|
, boost::weak_ptr<request_callback> c)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(req.num_want >= 0);
|
2005-03-11 18:21:56 +01:00
|
|
|
if (req.event == tracker_request::stopped)
|
|
|
|
req.num_want = 0;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_abort || req.event == tracker_request::stopped);
|
2007-05-23 03:02:46 +02:00
|
|
|
if (m_abort && req.event != tracker_request::stopped)
|
|
|
|
return;
|
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
std::string protocol = req.url.substr(0, req.url.find(':'));
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-12-29 07:13:49 +01:00
|
|
|
boost::intrusive_ptr<tracker_connection> con;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (protocol == "http" || protocol == "https")
|
|
|
|
#else
|
2007-12-29 07:13:49 +01:00
|
|
|
if (protocol == "http")
|
2008-01-27 23:39:50 +01:00
|
|
|
#endif
|
2007-12-29 07:13:49 +01:00
|
|
|
{
|
|
|
|
con = new http_tracker_connection(
|
2008-02-05 07:32:10 +01:00
|
|
|
ios, cc, *this, req, bind_infc, c
|
2008-09-22 02:15:05 +02:00
|
|
|
, m_ses.settings(), m_proxy, auth);
|
2007-12-29 07:13:49 +01:00
|
|
|
}
|
|
|
|
else if (protocol == "udp")
|
|
|
|
{
|
|
|
|
con = new udp_tracker_connection(
|
2008-02-05 07:32:10 +01:00
|
|
|
ios, cc, *this, req, bind_infc
|
2008-09-22 02:15:05 +02:00
|
|
|
, c, m_ses.settings(), m_proxy);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2007-12-29 07:13:49 +01:00
|
|
|
else
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
if (boost::shared_ptr<request_callback> r = c.lock())
|
2007-12-29 07:13:49 +01:00
|
|
|
r->tracker_request_error(req, -1, "unknown protocol in tracker url: "
|
2008-02-05 07:32:10 +01:00
|
|
|
+ req.url);
|
2008-01-27 21:00:56 +01:00
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2007-12-29 07:13:49 +01:00
|
|
|
|
|
|
|
m_connections.push_back(con);
|
|
|
|
|
|
|
|
boost::shared_ptr<request_callback> cb = con->requester();
|
|
|
|
if (cb) cb->m_manager = this;
|
2008-09-07 12:03:59 +02:00
|
|
|
con->start();
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2005-03-11 18:21:56 +01:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
void tracker_manager::abort_all_requests()
|
|
|
|
{
|
|
|
|
// removes all connections from m_connections
|
|
|
|
// except those with a requester == 0 (since those are
|
|
|
|
// 'event=stopped'-requests)
|
2006-04-25 23:04:48 +02:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-05-23 03:02:46 +02:00
|
|
|
m_abort = true;
|
2004-07-25 22:57:44 +02:00
|
|
|
tracker_connections_t keep_connections;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-10-28 00:01:32 +02:00
|
|
|
while (!m_connections.empty())
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2007-10-28 00:01:32 +02:00
|
|
|
boost::intrusive_ptr<tracker_connection>& c = m_connections.back();
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
m_connections.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tracker_request const& req = c->tracker_req();
|
2006-04-25 23:04:48 +02:00
|
|
|
if (req.event == tracker_request::stopped)
|
2007-10-28 00:01:32 +02:00
|
|
|
{
|
|
|
|
keep_connections.push_back(c);
|
|
|
|
m_connections.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// close will remove the entry from m_connections
|
|
|
|
// so no need to pop
|
2008-03-30 21:00:37 +02:00
|
|
|
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
boost::shared_ptr<request_callback> rc = c->requester();
|
|
|
|
if (rc) rc->debug_log("aborting: " + req.url);
|
|
|
|
#endif
|
2007-10-28 00:01:32 +02:00
|
|
|
c->close();
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::swap(m_connections, keep_connections);
|
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
bool tracker_manager::empty() const
|
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
return m_connections.empty();
|
|
|
|
}
|
|
|
|
|
2007-10-07 20:06:56 +02:00
|
|
|
int tracker_manager::num_requests() const
|
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
return m_connections.size();
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|