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>
|
|
|
|
|
|
|
|
#include "zlib.h"
|
|
|
|
|
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"
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
FTEXT = 0x01,
|
|
|
|
FHCRC = 0x02,
|
|
|
|
FEXTRA = 0x04,
|
|
|
|
FNAME = 0x08,
|
|
|
|
FCOMMENT = 0x10,
|
|
|
|
FRESERVED = 0xe0,
|
|
|
|
|
|
|
|
GZIP_MAGIC0 = 0x1f,
|
|
|
|
GZIP_MAGIC1 = 0x8b
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
// returns -1 if gzip header is invalid or the header size in bytes
|
|
|
|
int gzip_header(const char* buf, int size)
|
|
|
|
{
|
|
|
|
assert(buf != 0);
|
|
|
|
assert(size > 0);
|
|
|
|
|
|
|
|
const unsigned char* buffer = reinterpret_cast<const unsigned char*>(buf);
|
|
|
|
const int total_size = size;
|
|
|
|
|
|
|
|
// The zip header cannot be shorter than 10 bytes
|
|
|
|
if (size < 10) return -1;
|
|
|
|
|
|
|
|
// check the magic header of gzip
|
|
|
|
if ((buffer[0] != GZIP_MAGIC0) || (buffer[1] != GZIP_MAGIC1)) return -1;
|
|
|
|
|
|
|
|
int method = buffer[2];
|
|
|
|
int flags = buffer[3];
|
|
|
|
|
|
|
|
// check for reserved flag and make sure it's compressed with the correct metod
|
|
|
|
if (method != Z_DEFLATED || (flags & FRESERVED) != 0) return -1;
|
|
|
|
|
|
|
|
// skip time, xflags, OS code
|
|
|
|
size -= 10;
|
|
|
|
buffer += 10;
|
|
|
|
|
|
|
|
if (flags & FEXTRA)
|
|
|
|
{
|
|
|
|
int extra_len;
|
|
|
|
|
|
|
|
if (size < 2) return -1;
|
|
|
|
|
|
|
|
extra_len = (buffer[1] << 8) | buffer[0];
|
|
|
|
|
|
|
|
if (size < (extra_len+2)) return -1;
|
|
|
|
size -= (extra_len + 2);
|
|
|
|
buffer += (extra_len + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FNAME)
|
|
|
|
{
|
|
|
|
while (size && *buffer)
|
|
|
|
{
|
|
|
|
--size;
|
|
|
|
++buffer;
|
|
|
|
}
|
|
|
|
if (!size || *buffer) return -1;
|
|
|
|
|
|
|
|
--size;
|
|
|
|
++buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FCOMMENT)
|
|
|
|
{
|
|
|
|
while (size && *buffer)
|
|
|
|
{
|
|
|
|
--size;
|
|
|
|
++buffer;
|
|
|
|
}
|
|
|
|
if (!size || *buffer) return -1;
|
|
|
|
|
|
|
|
--size;
|
|
|
|
++buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FHCRC)
|
|
|
|
{
|
|
|
|
if (size < 2) return -1;
|
|
|
|
|
|
|
|
size -= 2;
|
|
|
|
buffer += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_size - size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool inflate_gzip(
|
|
|
|
std::vector<char>& buffer
|
2005-03-24 13:13:47 +01:00
|
|
|
, tracker_request const& req
|
2004-01-31 11:46:15 +01:00
|
|
|
, request_callback* requester
|
|
|
|
, int maximum_tracker_response_length)
|
|
|
|
{
|
|
|
|
assert(maximum_tracker_response_length > 0);
|
|
|
|
|
|
|
|
int header_len = gzip_header(&buffer[0], (int)buffer.size());
|
|
|
|
if (header_len < 0)
|
|
|
|
{
|
2005-03-24 13:13:47 +01:00
|
|
|
requester->tracker_request_error(req, 200, "invalid gzip header in tracker response");
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start off wth one kilobyte and grow
|
|
|
|
// if needed
|
|
|
|
std::vector<char> inflate_buffer(1024);
|
|
|
|
|
|
|
|
// initialize the zlib-stream
|
|
|
|
z_stream str;
|
|
|
|
|
|
|
|
// subtract 8 from the end of the buffer since that's CRC32 and input size
|
|
|
|
// and those belong to the gzip file
|
|
|
|
str.avail_in = (int)buffer.size() - header_len - 8;
|
|
|
|
str.next_in = reinterpret_cast<Bytef*>(&buffer[header_len]);
|
|
|
|
str.next_out = reinterpret_cast<Bytef*>(&inflate_buffer[0]);
|
|
|
|
str.avail_out = (int)inflate_buffer.size();
|
|
|
|
str.zalloc = Z_NULL;
|
|
|
|
str.zfree = Z_NULL;
|
|
|
|
str.opaque = 0;
|
|
|
|
// -15 is really important. It will make inflate() not look for a zlib header
|
|
|
|
// and just deflate the buffer
|
|
|
|
if (inflateInit2(&str, -15) != Z_OK)
|
|
|
|
{
|
2005-03-24 13:13:47 +01:00
|
|
|
requester->tracker_request_error(req, 200, "gzip out of memory");
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inflate and grow inflate_buffer as needed
|
|
|
|
int ret = inflate(&str, Z_SYNC_FLUSH);
|
|
|
|
while (ret == Z_OK)
|
|
|
|
{
|
|
|
|
if (str.avail_out == 0)
|
|
|
|
{
|
|
|
|
if (inflate_buffer.size() >= (unsigned)maximum_tracker_response_length)
|
|
|
|
{
|
|
|
|
inflateEnd(&str);
|
2005-03-24 13:13:47 +01:00
|
|
|
requester->tracker_request_error(req, 200
|
|
|
|
, "tracker response too large");
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int new_size = (int)inflate_buffer.size() * 2;
|
|
|
|
if (new_size > maximum_tracker_response_length) new_size = maximum_tracker_response_length;
|
|
|
|
int old_size = (int)inflate_buffer.size();
|
|
|
|
|
|
|
|
inflate_buffer.resize(new_size);
|
|
|
|
str.next_out = reinterpret_cast<Bytef*>(&inflate_buffer[old_size]);
|
|
|
|
str.avail_out = new_size - old_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = inflate(&str, Z_SYNC_FLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
inflate_buffer.resize(inflate_buffer.size() - str.avail_out);
|
|
|
|
inflateEnd(&str);
|
|
|
|
|
|
|
|
if (ret != Z_STREAM_END)
|
|
|
|
{
|
2005-03-24 13:13:47 +01:00
|
|
|
requester->tracker_request_error(req, 200, "gzip error");
|
2004-01-31 11:46:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// commit the resulting buffer
|
|
|
|
std::swap(buffer, inflate_buffer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string base64encode(const std::string& s)
|
|
|
|
{
|
|
|
|
static const char base64_table[] =
|
|
|
|
{
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
|
|
|
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
|
|
|
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
|
|
|
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
|
|
|
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
|
|
|
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
|
|
|
'4', '5', '6', '7', '8', '9', '+', '/'
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned char inbuf[3];
|
|
|
|
unsigned char outbuf[4];
|
|
|
|
|
|
|
|
std::string ret;
|
|
|
|
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
|
|
|
{
|
|
|
|
// available input is 1,2 or 3 bytes
|
|
|
|
// since we read 3 bytes at a time at most
|
|
|
|
int available_input = std::min(3, (int)std::distance(i, s.end()));
|
|
|
|
|
|
|
|
// clear input buffer
|
|
|
|
std::fill(inbuf, inbuf+3, 0);
|
|
|
|
|
|
|
|
// read a chunk of input into inbuf
|
|
|
|
for (int j = 0; j < available_input; ++j)
|
|
|
|
{
|
|
|
|
inbuf[j] = *i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode inbuf to outbuf
|
|
|
|
outbuf[0] = (inbuf[0] & 0xfc) >> 2;
|
|
|
|
outbuf[1] = ((inbuf[0] & 0x03) << 4) | ((inbuf [1] & 0xf0) >> 4);
|
|
|
|
outbuf[2] = ((inbuf[1] & 0x0f) << 2) | ((inbuf [2] & 0xc0) >> 6);
|
|
|
|
outbuf[3] = inbuf[2] & 0x3f;
|
|
|
|
|
|
|
|
// write output
|
|
|
|
for (int j = 0; j < available_input+1; ++j)
|
|
|
|
{
|
|
|
|
ret += base64_table[outbuf[j]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// write pad
|
|
|
|
for (int j = 0; j < 3 - available_input; ++j)
|
|
|
|
{
|
|
|
|
ret += '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-12-15 18:47:21 +01:00
|
|
|
timeout_handler::timeout_handler(asio::strand& str)
|
|
|
|
: m_strand(str)
|
2007-04-05 00:27:36 +02:00
|
|
|
, m_start_time(time_now())
|
|
|
|
, m_read_time(time_now())
|
2006-12-15 18:47:21 +01:00
|
|
|
, m_timeout(str.io_service())
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_completion_timeout(0)
|
|
|
|
, m_read_timeout(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void timeout_handler::set_timeout(int completion_timeout, int read_timeout)
|
|
|
|
{
|
|
|
|
m_completion_timeout = completion_timeout;
|
|
|
|
m_read_timeout = read_timeout;
|
2007-04-05 00:27:36 +02:00
|
|
|
m_start_time = time_now();
|
|
|
|
m_read_time = time_now();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
m_timeout.expires_at(std::min(
|
|
|
|
m_read_time + seconds(m_read_timeout)
|
|
|
|
, m_start_time + seconds(m_completion_timeout)));
|
2006-12-15 18:47:21 +01:00
|
|
|
m_timeout.async_wait(m_strand.wrap(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()
|
|
|
|
{
|
|
|
|
m_completion_timeout = 0;
|
2006-06-20 00:34:25 +02:00
|
|
|
m_timeout.cancel();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void timeout_handler::timeout_callback(asio::error_code const& error) try
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_timeout.expires_at(std::min(
|
|
|
|
m_read_time + seconds(m_read_timeout)
|
|
|
|
, m_start_time + seconds(m_completion_timeout)));
|
2006-12-15 18:47:21 +01:00
|
|
|
m_timeout.async_wait(m_strand.wrap(
|
|
|
|
bind(&timeout_handler::timeout_callback, self(), _1)));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
tracker_connection::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)
|
2006-12-15 18:47:21 +01:00
|
|
|
: timeout_handler(str)
|
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)
|
|
|
|
{}
|
|
|
|
|
2004-09-16 03:14:16 +02:00
|
|
|
request_callback& tracker_connection::requester()
|
|
|
|
{
|
|
|
|
boost::shared_ptr<request_callback> r = m_requester.lock();
|
|
|
|
assert(r);
|
|
|
|
return *r;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void tracker_connection::fail(int code, char const* msg)
|
|
|
|
{
|
|
|
|
if (has_requester()) requester().tracker_request_error(
|
|
|
|
m_req, code, msg);
|
|
|
|
close();
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void tracker_connection::fail_timeout()
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
if (has_requester()) requester().tracker_request_timed_out(m_req);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// returns protocol, auth, hostname, port, path
|
|
|
|
tuple<std::string, std::string, std::string, int, std::string>
|
2006-04-25 23:04:48 +02:00
|
|
|
parse_url_components(std::string url)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
std::string hostname; // hostname only
|
2007-05-22 22:44:18 +02:00
|
|
|
std::string auth; // user:pass
|
2006-04-25 23:04:48 +02:00
|
|
|
std::string protocol; // should be http
|
|
|
|
int port = 80;
|
2004-04-15 00:16:56 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// PARSE URL
|
|
|
|
std::string::iterator start = url.begin();
|
|
|
|
// remove white spaces in front of the url
|
|
|
|
while (start != url.end() && (*start == ' ' || *start == '\t'))
|
|
|
|
++start;
|
|
|
|
std::string::iterator end
|
|
|
|
= std::find(url.begin(), url.end(), ':');
|
2007-05-22 22:44:18 +02:00
|
|
|
protocol.assign(start, end);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (end == url.end()) throw std::runtime_error("invalid url");
|
|
|
|
++end;
|
|
|
|
if (end == url.end()) throw std::runtime_error("invalid url");
|
|
|
|
if (*end != '/') throw std::runtime_error("invalid url");
|
|
|
|
++end;
|
|
|
|
if (end == url.end()) throw std::runtime_error("invalid url");
|
|
|
|
if (*end != '/') throw std::runtime_error("invalid url");
|
|
|
|
++end;
|
|
|
|
start = end;
|
|
|
|
|
2007-05-22 22:44:18 +02:00
|
|
|
std::string::iterator at = std::find(start, url.end(), '@');
|
|
|
|
std::string::iterator colon = std::find(start, url.end(), ':');
|
2006-04-25 23:04:48 +02:00
|
|
|
end = std::find(start, url.end(), '/');
|
2007-05-22 22:44:18 +02:00
|
|
|
|
|
|
|
if (at != url.end()
|
|
|
|
&& colon != url.end()
|
|
|
|
&& colon < at
|
|
|
|
&& at < end)
|
|
|
|
{
|
|
|
|
auth.assign(start, at);
|
|
|
|
start = at;
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
|
2007-06-09 02:11:11 +02:00
|
|
|
std::string::iterator port_pos;
|
|
|
|
|
|
|
|
// this is for IPv6 addresses
|
|
|
|
if (start != url.end() && *start == '[')
|
|
|
|
{
|
|
|
|
port_pos = std::find(start, url.end(), ']');
|
|
|
|
if (port_pos == url.end()) throw std::runtime_error("invalid hostname syntax");
|
|
|
|
port_pos = std::find(port_pos, url.end(), ':');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
port_pos = std::find(start, url.end(), ':');
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (port_pos < end)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
hostname.assign(start, port_pos);
|
|
|
|
++port_pos;
|
|
|
|
try
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
port = boost::lexical_cast<int>(std::string(port_pos, end));
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
catch(boost::bad_lexical_cast&)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
throw std::runtime_error("invalid url: \"" + url
|
|
|
|
+ "\", port number expected");
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2005-03-11 18:21:56 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hostname.assign(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
start = end;
|
2007-05-22 22:44:18 +02:00
|
|
|
return make_tuple(protocol, auth, hostname, port
|
2006-04-25 23:04:48 +02:00
|
|
|
, std::string(start, url.end()));
|
2005-03-11 18:21:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_manager::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 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);
|
2005-03-11 18:21:56 +01:00
|
|
|
assert(req.num_want >= 0);
|
|
|
|
if (req.event == tracker_request::stopped)
|
|
|
|
req.num_want = 0;
|
|
|
|
|
2007-05-23 03:02:46 +02:00
|
|
|
assert(!m_abort || req.event == tracker_request::stopped);
|
|
|
|
if (m_abort && req.event != tracker_request::stopped)
|
|
|
|
return;
|
|
|
|
|
2005-03-11 18:21:56 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string protocol;
|
|
|
|
std::string hostname;
|
|
|
|
int port;
|
|
|
|
std::string request_string;
|
|
|
|
|
2007-05-22 22:44:18 +02:00
|
|
|
using boost::tuples::ignore;
|
|
|
|
// TODO: should auth be used here?
|
|
|
|
boost::tie(protocol, ignore, hostname, port, request_string)
|
2005-03-11 18:21:56 +01:00
|
|
|
= parse_url_components(req.url);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::intrusive_ptr<tracker_connection> con;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
if (protocol == "http")
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
con = new http_tracker_connection(
|
2006-12-15 18:47:21 +01:00
|
|
|
str
|
2007-05-05 02:29:33 +02:00
|
|
|
, cc
|
2006-04-25 23:04:48 +02:00
|
|
|
, *this
|
2004-07-25 22:57:44 +02:00
|
|
|
, req
|
2004-01-31 11:46:15 +01:00
|
|
|
, hostname
|
|
|
|
, port
|
|
|
|
, request_string
|
2007-03-02 19:40:02 +01:00
|
|
|
, bind_infc
|
2004-01-31 11:46:15 +01:00
|
|
|
, c
|
2005-08-25 15:11:39 +02:00
|
|
|
, m_settings
|
2007-04-25 20:26:35 +02:00
|
|
|
, m_proxy
|
2006-04-25 23:04:48 +02:00
|
|
|
, auth);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
else if (protocol == "udp")
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
con = new udp_tracker_connection(
|
2006-12-15 18:47:21 +01:00
|
|
|
str
|
2006-04-25 23:04:48 +02:00
|
|
|
, *this
|
|
|
|
, req
|
2004-01-31 11:46:15 +01:00
|
|
|
, hostname
|
|
|
|
, port
|
2007-03-02 19:40:02 +01:00
|
|
|
, bind_infc
|
2004-01-31 11:46:15 +01:00
|
|
|
, c
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_settings);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("unkown protocol in tracker url");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_connections.push_back(con);
|
|
|
|
|
2004-09-16 03:14:16 +02:00
|
|
|
if (con->has_requester()) con->requester().m_manager = this;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
if (boost::shared_ptr<request_callback> r = c.lock())
|
2005-03-24 13:13:47 +01:00
|
|
|
r->tracker_request_error(req, -1, e.what());
|
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
|
|
|
|
2004-07-25 22:57:44 +02:00
|
|
|
for (tracker_connections_t::const_iterator i =
|
2006-04-25 23:04:48 +02:00
|
|
|
m_connections.begin(); i != m_connections.end(); ++i)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
tracker_request const& req = (*i)->tracker_req();
|
|
|
|
if (req.event == tracker_request::stopped)
|
|
|
|
keep_connections.push_back(*i);
|
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();
|
|
|
|
}
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|