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 <cctype>
|
2007-03-02 02:16:59 +01:00
|
|
|
#include <algorithm>
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-03-02 02:16:59 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2008-01-30 19:32:13 +01:00
|
|
|
#include "libtorrent/gzip.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/tracker_manager.hpp"
|
|
|
|
#include "libtorrent/http_tracker_connection.hpp"
|
2008-01-31 09:24:01 +01:00
|
|
|
#include "libtorrent/http_connection.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/torrent.hpp"
|
2004-03-12 17:42:33 +01:00
|
|
|
#include "libtorrent/io.hpp"
|
2008-03-29 23:45:55 +01:00
|
|
|
#include "libtorrent/socket.hpp"
|
2008-10-22 21:40:32 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
2006-04-25 23:04:48 +02:00
|
|
|
using boost::bind;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2009-09-06 02:57:01 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
// defined in torrent_info.cpp
|
|
|
|
bool is_i2p_url(std::string const& url);
|
|
|
|
#endif
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
http_tracker_connection::http_tracker_connection(
|
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_manager& man
|
2004-07-25 22:57:44 +02:00
|
|
|
, tracker_request const& req
|
2004-09-16 03:14:16 +02:00
|
|
|
, boost::weak_ptr<request_callback> c
|
2008-10-22 21:40:32 +02:00
|
|
|
, aux::session_impl const& ses
|
2007-04-25 20:26:35 +02:00
|
|
|
, proxy_settings const& ps
|
2009-08-20 05:19:12 +02:00
|
|
|
, std::string const& auth
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
, i2p_connection* i2p_conn
|
|
|
|
#endif
|
|
|
|
)
|
2009-05-15 23:23:41 +02:00
|
|
|
: tracker_connection(man, req, ios, c)
|
2004-07-25 22:57:44 +02:00
|
|
|
, m_man(man)
|
2008-10-22 21:40:32 +02:00
|
|
|
, m_ses(ses)
|
2008-09-07 12:03:59 +02:00
|
|
|
, m_ps(ps)
|
|
|
|
, m_cc(cc)
|
|
|
|
, m_ios(ios)
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
, m_i2p_conn(i2p_conn)
|
|
|
|
#endif
|
2008-09-07 12:03:59 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
void http_tracker_connection::start()
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
// TODO: authentication
|
2008-09-07 12:03:59 +02:00
|
|
|
std::string url = tracker_req().url;
|
2005-09-27 14:42:30 +02:00
|
|
|
|
2008-09-07 12:03:59 +02:00
|
|
|
if (tracker_req().kind == tracker_request::scrape_request)
|
2005-03-11 18:21:56 +01:00
|
|
|
{
|
2005-03-24 13:13:47 +01:00
|
|
|
// find and replace "announce" with "scrape"
|
2005-03-11 18:21:56 +01:00
|
|
|
// in request
|
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
std::size_t pos = url.find("announce");
|
2005-03-11 18:21:56 +01:00
|
|
|
if (pos == std::string::npos)
|
2007-12-29 19:24:50 +01:00
|
|
|
{
|
2009-06-09 07:51:10 +02:00
|
|
|
m_ios.post(boost::bind(&http_tracker_connection::fail_disp, self()
|
|
|
|
, -1, "scrape is not available on url: '" + tracker_req().url +"'"));
|
2007-12-29 19:24:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-01-31 09:24:01 +01:00
|
|
|
url.replace(pos, 8, "scrape");
|
2005-03-11 18:21:56 +01:00
|
|
|
}
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
bool i2p = is_i2p_url(url);
|
|
|
|
#else
|
|
|
|
static const bool i2p = false;
|
|
|
|
#endif
|
|
|
|
|
2008-10-22 21:40:32 +02:00
|
|
|
session_settings const& settings = m_ses.settings();
|
|
|
|
|
2005-03-11 18:21:56 +01:00
|
|
|
// if request-string already contains
|
|
|
|
// some parameters, append an ampersand instead
|
|
|
|
// of a question mark
|
2008-01-31 09:24:01 +01:00
|
|
|
size_t arguments_start = url.find('?');
|
2007-01-30 18:56:42 +01:00
|
|
|
if (arguments_start != std::string::npos)
|
2008-01-31 09:24:01 +01:00
|
|
|
url += "&";
|
2005-03-11 18:21:56 +01:00
|
|
|
else
|
2008-01-31 09:24:01 +01:00
|
|
|
url += "?";
|
2005-03-11 18:21:56 +01:00
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
url += "info_hash=";
|
2009-05-15 00:10:00 +02:00
|
|
|
url += escape_string((const char*)&tracker_req().info_hash[0], 20);
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2008-09-07 12:03:59 +02:00
|
|
|
if (tracker_req().kind == tracker_request::announce_request)
|
2005-03-11 18:21:56 +01:00
|
|
|
{
|
2009-05-15 00:10:00 +02:00
|
|
|
char str[1024];
|
2009-09-06 18:47:48 +02:00
|
|
|
snprintf(str, sizeof(str), "&peer_id=%s&port=%d&uploaded=%"PRId64
|
|
|
|
"&downloaded=%"PRId64"&left=%"PRId64"&compact=1&numwant=%d&key=%x&no_peer_id=1"
|
2009-05-15 00:10:00 +02:00
|
|
|
, escape_string((const char*)&tracker_req().pid[0], 20).c_str()
|
2009-08-20 05:19:12 +02:00
|
|
|
// the i2p tracker seems to verify that the port is not 0,
|
|
|
|
// even though it ignores it otherwise
|
|
|
|
, i2p ? 1 : tracker_req().listen_port
|
2009-05-15 00:10:00 +02:00
|
|
|
, tracker_req().uploaded
|
|
|
|
, tracker_req().downloaded
|
|
|
|
, tracker_req().left
|
|
|
|
, tracker_req().num_want
|
2009-09-06 09:23:01 +02:00
|
|
|
, tracker_req().key);
|
|
|
|
url += str;
|
2009-06-19 18:38:06 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2009-09-06 09:23:01 +02:00
|
|
|
if (m_ses.get_pe_settings().in_enc_policy != pe_settings::disabled)
|
|
|
|
url += "&supportcrypto=1";
|
2009-06-19 18:38:06 +02:00
|
|
|
#endif
|
2006-11-24 15:22:52 +01:00
|
|
|
|
2008-09-07 12:03:59 +02:00
|
|
|
if (tracker_req().event != tracker_request::none)
|
2005-03-11 18:21:56 +01:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
const char* event_string[] = {"completed", "started", "stopped"};
|
|
|
|
url += "&event=";
|
2008-09-07 12:03:59 +02:00
|
|
|
url += event_string[tracker_req().event - 1];
|
2007-01-30 18:56:42 +01:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
if (i2p)
|
|
|
|
{
|
|
|
|
url += "&ip=";
|
|
|
|
url += escape_string(m_i2p_conn->local_endpoint().c_str()
|
|
|
|
, m_i2p_conn->local_endpoint().size());
|
|
|
|
url += ".i2p";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2008-10-22 21:40:32 +02:00
|
|
|
if (settings.announce_ip != address())
|
2007-06-13 02:20:06 +02:00
|
|
|
{
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
2008-10-22 21:40:32 +02:00
|
|
|
std::string ip = settings.announce_ip.to_string(ec);
|
2008-10-21 10:45:42 +02:00
|
|
|
if (!ec) url += "&ip=" + ip;
|
2007-06-13 02:20:06 +02:00
|
|
|
}
|
2005-03-11 18:21:56 +01:00
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
if (!tracker_req().ipv6.empty() && !i2p)
|
2009-04-12 02:37:06 +02:00
|
|
|
{
|
|
|
|
url += "&ipv6=";
|
|
|
|
url += tracker_req().ipv6;
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
if (!tracker_req().ipv4.empty() && !i2p)
|
2009-04-12 02:37:06 +02:00
|
|
|
{
|
|
|
|
url += "&ipv4=";
|
|
|
|
url += tracker_req().ipv4;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2008-09-07 12:03:59 +02:00
|
|
|
m_tracker_connection.reset(new http_connection(m_ios, m_cc
|
2008-10-22 21:40:32 +02:00
|
|
|
, boost::bind(&http_tracker_connection::on_response, self(), _1, _2, _3, _4)
|
2008-12-01 09:48:54 +01:00
|
|
|
, true
|
|
|
|
, boost::bind(&http_tracker_connection::on_connect, self(), _1)
|
2008-10-22 21:40:32 +02:00
|
|
|
, boost::bind(&http_tracker_connection::on_filter, self(), _1, _2)));
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2008-09-07 12:03:59 +02:00
|
|
|
int timeout = tracker_req().event==tracker_request::stopped
|
2008-10-22 21:40:32 +02:00
|
|
|
?settings.stop_tracker_timeout
|
|
|
|
:settings.tracker_completion_timeout;
|
2008-03-30 21:00:37 +02:00
|
|
|
|
|
|
|
m_tracker_connection->get(url, seconds(timeout)
|
2009-08-20 05:19:12 +02:00
|
|
|
, 1, &m_ps, 5, settings.user_agent, bind_interface()
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
, m_i2p_conn
|
|
|
|
#endif
|
|
|
|
);
|
2006-11-15 22:39:58 +01:00
|
|
|
|
2008-09-22 02:15:05 +02:00
|
|
|
// the url + 100 estimated header size
|
|
|
|
sent_bytes(url.size() + 100);
|
|
|
|
|
2005-07-06 15:18:10 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2007-09-14 04:54:15 +02:00
|
|
|
|
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb)
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
cb->debug_log("==> TRACKER_REQUEST [ url: " + url + " ]");
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
2005-03-19 13:22:40 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
void http_tracker_connection::close()
|
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
if (m_tracker_connection)
|
2004-05-21 01:26:40 +02:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
m_tracker_connection->close();
|
|
|
|
m_tracker_connection.reset();
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
2008-01-31 09:24:01 +01:00
|
|
|
tracker_connection::close();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-10-22 21:40:32 +02:00
|
|
|
void http_tracker_connection::on_filter(http_connection& c, std::list<tcp::endpoint>& endpoints)
|
|
|
|
{
|
|
|
|
// remove endpoints that are filtered by the IP filter
|
2009-04-30 07:49:46 +02:00
|
|
|
for (std::list<tcp::endpoint>::iterator i = endpoints.begin();
|
|
|
|
i != endpoints.end();)
|
|
|
|
{
|
|
|
|
if (m_ses.m_ip_filter.access(i->address()) == ip_filter::blocked)
|
|
|
|
i = endpoints.erase(i);
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
2008-10-22 21:40:32 +02:00
|
|
|
|
|
|
|
if (endpoints.empty())
|
|
|
|
fail(-1, "blocked by IP filter");
|
|
|
|
}
|
|
|
|
|
2008-12-01 09:48:54 +01:00
|
|
|
void http_tracker_connection::on_connect(http_connection& c)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
tcp::endpoint ep = c.socket().remote_endpoint(ec);
|
|
|
|
m_tracker_ip = ep.address();
|
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb) cb->m_tracker_address = ep;
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void http_tracker_connection::on_response(error_code const& ec
|
2008-01-31 09:24:01 +01:00
|
|
|
, http_parser const& parser, char const* data, int size)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-02-07 08:15:23 +01:00
|
|
|
// keep this alive
|
|
|
|
boost::intrusive_ptr<http_tracker_connection> me(this);
|
|
|
|
|
2008-02-25 01:55:31 +01:00
|
|
|
if (ec && ec != asio::error::eof)
|
|
|
|
{
|
|
|
|
fail(-1, ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
if (!parser.header_finished())
|
2006-12-12 03:28:53 +01:00
|
|
|
{
|
|
|
|
fail(-1, "premature end of file");
|
|
|
|
return;
|
|
|
|
}
|
2007-04-25 20:26:35 +02:00
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
if (parser.status_code() != 200)
|
2007-04-25 20:26:35 +02:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
fail(parser.status_code(), parser.message().c_str());
|
2007-04-25 20:26:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-12-12 03:28:53 +01:00
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
if (ec && ec != asio::error::eof)
|
2006-12-12 03:28:53 +01:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
fail(parser.status_code(), ec.message().c_str());
|
2006-12-12 03:28:53 +01:00
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2008-09-22 02:15:05 +02:00
|
|
|
received_bytes(size + parser.body_start());
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// handle tracker response
|
2008-06-03 22:03:51 +02:00
|
|
|
entry e;
|
|
|
|
e = bdecode(data, data + size);
|
2007-12-30 02:57:57 +01:00
|
|
|
|
2008-09-20 17:25:46 +02:00
|
|
|
if (e.type() == entry::dictionary_t)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
parse(parser.status_code(), e);
|
2004-05-21 01:26:40 +02:00
|
|
|
}
|
2007-12-30 02:57:57 +01:00
|
|
|
else
|
2004-05-21 01:26:40 +02:00
|
|
|
{
|
2007-12-30 02:57:57 +01:00
|
|
|
std::string error_str("invalid bencoding of tracker response: \"");
|
2008-01-31 09:24:01 +01:00
|
|
|
for (char const* i = data, *end(data + size); i != end; ++i)
|
2006-12-12 03:28:53 +01:00
|
|
|
{
|
2008-09-28 09:26:05 +02:00
|
|
|
if (*i >= ' ' && *i <= '~') error_str += *i;
|
2009-08-25 20:13:46 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char val[30];
|
|
|
|
snprintf(val, sizeof(val), "0x%02x ", *i);
|
|
|
|
error_str += val;
|
|
|
|
}
|
2006-12-12 03:28:53 +01:00
|
|
|
}
|
|
|
|
error_str += "\"";
|
2008-01-31 09:24:01 +01:00
|
|
|
fail(parser.status_code(), error_str.c_str());
|
2004-05-21 01:26:40 +02:00
|
|
|
}
|
2007-10-26 09:14:19 +02:00
|
|
|
close();
|
2005-03-24 13:13:47 +01:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
bool http_tracker_connection::extract_peer_info(const entry& info, peer_entry& ret)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
|
|
|
// extract peer id (if any)
|
2007-12-30 02:57:57 +01:00
|
|
|
if (info.type() != entry::dictionary_t)
|
|
|
|
{
|
|
|
|
fail(-1, "invalid response from tracker (invalid peer entry)");
|
|
|
|
return false;
|
|
|
|
}
|
2004-03-17 13:14:44 +01:00
|
|
|
entry const* i = info.find_key("peer id");
|
|
|
|
if (i != 0)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2007-12-30 02:57:57 +01:00
|
|
|
if (i->type() != entry::string_t || i->string().length() != 20)
|
|
|
|
{
|
|
|
|
fail(-1, "invalid response from tracker (invalid peer id)");
|
|
|
|
return false;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
std::copy(i->string().begin(), i->string().end(), ret.pid.begin());
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if there's no peer_id, just initialize it to a bunch of zeroes
|
2006-04-25 23:04:48 +02:00
|
|
|
std::fill_n(ret.pid.begin(), 20, 0);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract ip
|
2004-03-17 13:14:44 +01:00
|
|
|
i = info.find_key("ip");
|
2007-12-30 02:57:57 +01:00
|
|
|
if (i == 0 || i->type() != entry::string_t)
|
|
|
|
{
|
|
|
|
fail(-1, "invalid response from tracker");
|
|
|
|
return false;
|
|
|
|
}
|
2004-03-17 13:14:44 +01:00
|
|
|
ret.ip = i->string();
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
// extract port
|
2004-03-17 13:14:44 +01:00
|
|
|
i = info.find_key("port");
|
2007-12-30 02:57:57 +01:00
|
|
|
if (i == 0 || i->type() != entry::int_t)
|
|
|
|
{
|
|
|
|
fail(-1, "invalid response from tracker");
|
|
|
|
return false;
|
|
|
|
}
|
2004-03-17 13:14:44 +01:00
|
|
|
ret.port = (unsigned short)i->integer();
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
return true;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
void http_tracker_connection::parse(int status_code, entry const& e)
|
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) return;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
// parse the response
|
|
|
|
entry const* failure = e.find_key("failure reason");
|
|
|
|
if (failure && failure->type() == entry::string_t)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2008-01-31 09:24:01 +01:00
|
|
|
fail(status_code, failure->string().c_str());
|
2007-12-30 02:57:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry const* warning = e.find_key("warning message");
|
|
|
|
if (warning && warning->type() == entry::string_t)
|
|
|
|
{
|
2008-04-23 03:54:21 +02:00
|
|
|
cb->tracker_warning(tracker_req(), warning->string());
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<peer_entry> peer_list;
|
|
|
|
|
|
|
|
if (tracker_req().kind == tracker_request::scrape_request)
|
|
|
|
{
|
2008-05-19 09:15:44 +02:00
|
|
|
std::string ih = tracker_req().info_hash.to_string();
|
2004-10-03 13:39:34 +02:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
entry const* files = e.find_key("files");
|
|
|
|
if (files == 0 || files->type() != entry::dictionary_t)
|
|
|
|
{
|
|
|
|
fail(-1, "invalid or missing 'files' entry in scrape response");
|
2004-10-03 13:39:34 +02:00
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2008-05-19 09:15:44 +02:00
|
|
|
entry const* scrape_data = files->find_key(ih);
|
2007-12-30 02:57:57 +01:00
|
|
|
if (scrape_data == 0 || scrape_data->type() != entry::dictionary_t)
|
2005-08-11 01:32:39 +02:00
|
|
|
{
|
2007-12-30 02:57:57 +01:00
|
|
|
fail(-1, "missing or invalid info-hash entry in scrape response");
|
|
|
|
return;
|
2005-08-11 01:32:39 +02:00
|
|
|
}
|
2007-12-30 02:57:57 +01:00
|
|
|
entry const* complete = scrape_data->find_key("complete");
|
|
|
|
entry const* incomplete = scrape_data->find_key("incomplete");
|
|
|
|
entry const* downloaded = scrape_data->find_key("downloaded");
|
|
|
|
if (complete == 0 || incomplete == 0 || downloaded == 0
|
|
|
|
|| complete->type() != entry::int_t
|
|
|
|
|| incomplete->type() != entry::int_t
|
|
|
|
|| downloaded->type() != entry::int_t)
|
2005-03-11 18:21:56 +01:00
|
|
|
{
|
2007-12-30 02:57:57 +01:00
|
|
|
fail(-1, "missing 'complete' or 'incomplete' entries in scrape response");
|
2005-03-11 18:21:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-04-05 23:18:27 +02:00
|
|
|
cb->tracker_scrape_response(tracker_req(), int(complete->integer())
|
|
|
|
, int(incomplete->integer()), int(downloaded->integer()));
|
2007-12-30 02:57:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
entry const* interval = e.find_key("interval");
|
|
|
|
if (interval == 0 || interval->type() != entry::int_t)
|
|
|
|
{
|
|
|
|
fail(-1, "missing or invalid 'interval' entry in tracker response");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry const* peers_ent = e.find_key("peers");
|
2009-03-22 23:24:11 +01:00
|
|
|
if (peers_ent && peers_ent->type() == entry::string_t)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
|
|
|
std::string const& peers = peers_ent->string();
|
|
|
|
for (std::string::const_iterator i = peers.begin();
|
|
|
|
i != peers.end();)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2009-08-30 09:38:52 +02:00
|
|
|
if (peers.end() - i < 6) break;
|
2004-03-12 17:42:33 +01:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
peer_entry p;
|
|
|
|
p.pid.clear();
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
|
|
|
p.ip = detail::read_v4_address(i).to_string(ec);
|
|
|
|
if (ec) continue;
|
2007-12-30 02:57:57 +01:00
|
|
|
p.port = detail::read_uint16(i);
|
|
|
|
peer_list.push_back(p);
|
2004-03-12 17:42:33 +01:00
|
|
|
}
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
2009-03-22 23:24:11 +01:00
|
|
|
else if (peers_ent && peers_ent->type() == entry::list_t)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
|
|
|
entry::list_type const& l = peers_ent->list();
|
|
|
|
for(entry::list_type::const_iterator i = l.begin(); i != l.end(); ++i)
|
2004-03-12 17:42:33 +01:00
|
|
|
{
|
2007-12-30 02:57:57 +01:00
|
|
|
peer_entry p;
|
|
|
|
if (!extract_peer_info(*i, p)) return;
|
|
|
|
peer_list.push_back(p);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-22 23:24:11 +01:00
|
|
|
peers_ent = 0;
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2009-04-04 18:59:53 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2007-12-30 02:57:57 +01:00
|
|
|
entry const* ipv6_peers = e.find_key("peers6");
|
|
|
|
if (ipv6_peers && ipv6_peers->type() == entry::string_t)
|
|
|
|
{
|
|
|
|
std::string const& peers = ipv6_peers->string();
|
|
|
|
for (std::string::const_iterator i = peers.begin();
|
|
|
|
i != peers.end();)
|
2007-09-19 23:54:26 +02:00
|
|
|
{
|
2009-08-30 09:38:52 +02:00
|
|
|
if (peers.end() - i < 18) break;
|
2007-12-30 02:57:57 +01:00
|
|
|
|
|
|
|
peer_entry p;
|
|
|
|
p.pid.clear();
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
|
|
|
p.ip = detail::read_v6_address(i).to_string(ec);
|
|
|
|
if (ec) continue;
|
2007-12-30 02:57:57 +01:00
|
|
|
p.port = detail::read_uint16(i);
|
|
|
|
peer_list.push_back(p);
|
2007-09-19 23:54:26 +02:00
|
|
|
}
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
2009-03-22 23:24:11 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ipv6_peers = 0;
|
|
|
|
}
|
2009-04-04 18:59:53 +02:00
|
|
|
#else
|
|
|
|
entry const* ipv6_peers = 0;
|
|
|
|
#endif
|
2009-03-22 23:24:11 +01:00
|
|
|
|
|
|
|
if (peers_ent == 0 && ipv6_peers == 0)
|
|
|
|
{
|
|
|
|
fail(-1, "missing 'peers' and 'peers6' entry in tracker response");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-19 23:54:26 +02:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
// look for optional scrape info
|
|
|
|
int complete = -1;
|
|
|
|
int incomplete = -1;
|
2008-03-29 23:45:55 +01:00
|
|
|
address external_ip;
|
2005-02-23 09:57:54 +01:00
|
|
|
|
2008-03-29 23:45:55 +01:00
|
|
|
entry const* ip_ent = e.find_key("external ip");
|
|
|
|
if (ip_ent && ip_ent->type() == entry::string_t)
|
|
|
|
{
|
|
|
|
std::string const& ip = ip_ent->string();
|
|
|
|
char const* p = &ip[0];
|
|
|
|
if (ip.size() == address_v4::bytes_type::static_size)
|
|
|
|
external_ip = detail::read_v4_address(p);
|
2009-04-04 18:59:53 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2008-03-29 23:45:55 +01:00
|
|
|
else if (ip.size() == address_v6::bytes_type::static_size)
|
|
|
|
external_ip = detail::read_v6_address(p);
|
2009-04-04 18:59:53 +02:00
|
|
|
#endif
|
2008-03-29 23:45:55 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
entry const* complete_ent = e.find_key("complete");
|
|
|
|
if (complete_ent && complete_ent->type() == entry::int_t)
|
2008-04-05 23:18:27 +02:00
|
|
|
complete = int(complete_ent->integer());
|
2005-02-23 09:57:54 +01:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
entry const* incomplete_ent = e.find_key("incomplete");
|
|
|
|
if (incomplete_ent && incomplete_ent->type() == entry::int_t)
|
2008-04-05 23:18:27 +02:00
|
|
|
incomplete = int(incomplete_ent->integer());
|
2007-12-30 02:57:57 +01:00
|
|
|
|
2009-05-15 23:23:41 +02:00
|
|
|
std::list<address> ip_list;
|
2009-09-20 17:32:23 +02:00
|
|
|
if (m_tracker_connection)
|
2009-09-06 02:57:01 +02:00
|
|
|
{
|
2009-09-20 17:32:23 +02:00
|
|
|
std::list<tcp::endpoint> const& epts = m_tracker_connection->endpoints();
|
|
|
|
for (std::list<tcp::endpoint>::const_iterator i = epts.begin()
|
|
|
|
, end(epts.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
ip_list.push_back(i->address());
|
|
|
|
}
|
2009-09-06 02:57:01 +02:00
|
|
|
}
|
2009-05-15 23:23:41 +02:00
|
|
|
|
|
|
|
cb->tracker_response(tracker_req(), m_tracker_ip, ip_list, peer_list
|
|
|
|
, interval->integer(), complete, incomplete, external_ip);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2005-03-19 13:22:40 +01:00
|
|
|
|