2004-01-31 11:46:15 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2015-08-20 01:33:20 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
#include <cctype>
|
|
|
|
#include <algorithm>
|
2016-05-17 15:24:06 +02:00
|
|
|
#include <cstdio> // for snprintf
|
|
|
|
#include <cinttypes> // for PRId64 et.al.
|
2006-04-25 23:04:48 +02:00
|
|
|
|
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"
|
2010-12-05 21:40:28 +01:00
|
|
|
#include "libtorrent/broadcast_socket.hpp" // for is_local
|
2015-04-21 02:23:00 +02:00
|
|
|
#include "libtorrent/string_util.hpp" // for is_i2p_url
|
2014-10-21 02:28:51 +02:00
|
|
|
#include "libtorrent/aux_/session_settings.hpp"
|
|
|
|
#include "libtorrent/resolver_interface.hpp"
|
|
|
|
#include "libtorrent/ip_filter.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
http_tracker_connection::http_tracker_connection(
|
2008-01-08 06:47:43 +01:00
|
|
|
io_service& ios
|
2006-04-25 23:04:48 +02:00
|
|
|
, tracker_manager& man
|
2004-07-25 22:57:44 +02:00
|
|
|
, tracker_request const& req
|
2016-08-31 14:27:36 +02:00
|
|
|
, std::weak_ptr<request_callback> c)
|
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)
|
2015-05-05 04:32:14 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2016-06-20 17:32:06 +02:00
|
|
|
, m_i2p_conn(nullptr)
|
2015-05-05 04:32:14 +02:00
|
|
|
#endif
|
2008-09-07 12:03:59 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
void http_tracker_connection::start()
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2008-09-07 12:03:59 +02:00
|
|
|
std::string url = tracker_req().url;
|
2005-09-27 14:42:30 +02:00
|
|
|
|
2015-06-27 23:30:00 +02:00
|
|
|
if (0 != (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
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
tracker_connection::fail(errors::scrape_not_available);
|
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
|
|
|
}
|
2015-05-18 03:30:32 +02: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
|
|
|
|
|
2014-10-21 02:28:51 +02:00
|
|
|
aux::session_settings const& settings = m_man.settings();
|
2008-10-22 21:40:32 +02:00
|
|
|
|
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 += "?";
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2015-06-18 07:05:36 +02:00
|
|
|
url += "info_hash=";
|
2016-08-30 04:37:19 +02:00
|
|
|
url += escape_string({tracker_req().info_hash.data(), 20});
|
2015-06-18 07:05:36 +02:00
|
|
|
|
2015-06-27 23:30:00 +02:00
|
|
|
if (0 == (tracker_req().kind & tracker_request::scrape_request))
|
2005-03-11 18:21:56 +01:00
|
|
|
{
|
2013-02-23 23:13:25 +01:00
|
|
|
const char* event_string[] = {"completed", "started", "stopped", "paused"};
|
|
|
|
|
2009-05-15 00:10:00 +02:00
|
|
|
char str[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str)
|
2015-06-18 07:05:36 +02:00
|
|
|
, "&peer_id=%s"
|
2013-02-23 23:13:25 +01:00
|
|
|
"&port=%d"
|
2013-09-25 21:44:29 +02:00
|
|
|
"&uploaded=%" PRId64
|
|
|
|
"&downloaded=%" PRId64
|
|
|
|
"&left=%" PRId64
|
|
|
|
"&corrupt=%" PRId64
|
2014-07-06 21:18:00 +02:00
|
|
|
"&key=%08X"
|
2013-02-23 23:13:25 +01:00
|
|
|
"%s%s" // event
|
|
|
|
"&numwant=%d"
|
|
|
|
"&compact=1"
|
|
|
|
"&no_peer_id=1"
|
2016-08-30 04:37:19 +02:00
|
|
|
, escape_string({tracker_req().pid.data(), 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
|
2016-09-22 01:54:49 +02:00
|
|
|
, tracker_req().uploaded
|
|
|
|
, tracker_req().downloaded
|
|
|
|
, tracker_req().left
|
|
|
|
, tracker_req().corrupt
|
2013-02-23 23:13:25 +01:00
|
|
|
, tracker_req().key
|
|
|
|
, (tracker_req().event != tracker_request::none) ? "&event=" : ""
|
|
|
|
, (tracker_req().event != tracker_request::none) ? event_string[tracker_req().event - 1] : ""
|
|
|
|
, tracker_req().num_want);
|
2009-09-06 09:23:01 +02:00
|
|
|
url += str;
|
2014-11-23 07:14:47 +01:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2014-10-21 02:28:51 +02:00
|
|
|
if (settings.get_int(settings_pack::in_enc_policy) != settings_pack::pe_disabled
|
|
|
|
&& settings.get_bool(settings_pack::announce_crypto_support))
|
2009-09-06 09:23:01 +02:00
|
|
|
url += "&supportcrypto=1";
|
2009-06-19 18:38:06 +02:00
|
|
|
#endif
|
2016-09-22 01:54:49 +02:00
|
|
|
if (settings.get_bool(settings_pack::report_redundant_bytes))
|
2013-02-23 23:13:25 +01:00
|
|
|
{
|
|
|
|
url += "&redundant=";
|
2016-05-01 05:10:47 +02:00
|
|
|
url += to_string(tracker_req().redundant).data();
|
2013-02-23 23:13:25 +01:00
|
|
|
}
|
2010-11-18 06:51:52 +01:00
|
|
|
if (!tracker_req().trackerid.empty())
|
|
|
|
{
|
|
|
|
url += "&trackerid=";
|
2016-08-30 04:37:19 +02:00
|
|
|
url += escape_string(tracker_req().trackerid);
|
2010-11-18 06:51:52 +01:00
|
|
|
}
|
2006-11-24 15:22:52 +01:00
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2015-02-01 15:30:43 +01:00
|
|
|
if (i2p && tracker_req().i2pconn)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2015-06-21 02:35:33 +02:00
|
|
|
if (tracker_req().i2pconn->local_endpoint().empty())
|
2015-08-14 04:22:20 +02:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
fail(errors::no_i2p_endpoint, -1, "Waiting for i2p acceptor from SAM bridge", 5);
|
2015-08-14 04:22:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-06-21 02:35:33 +02:00
|
|
|
else
|
2015-08-14 04:22:20 +02:00
|
|
|
{
|
2016-09-22 01:54:49 +02:00
|
|
|
url += "&ip=" + tracker_req().i2pconn->local_endpoint () + ".i2p";
|
2015-08-14 04:22:20 +02:00
|
|
|
}
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2014-10-21 02:28:51 +02:00
|
|
|
if (!settings.get_bool(settings_pack::anonymous_mode))
|
2007-06-13 02:20:06 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
std::string announce_ip = settings.get_str(settings_pack::announce_ip);
|
|
|
|
if (!announce_ip.empty())
|
2010-12-05 21:40:28 +01:00
|
|
|
{
|
2016-08-30 04:37:19 +02:00
|
|
|
url += "&ip=" + escape_string(announce_ip);
|
2010-12-05 21:40:28 +01:00
|
|
|
}
|
2009-04-12 02:37:06 +02:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2016-04-07 06:06:04 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (tracker_req().ipv6 != address_v6() && !i2p)
|
|
|
|
{
|
|
|
|
error_code err;
|
|
|
|
std::string const ip = tracker_req().ipv6.to_string(err);
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
url += "&ipv6=";
|
|
|
|
url += ip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-17 20:34:49 +01:00
|
|
|
m_tracker_connection = std::make_shared<http_connection>(get_io_service(), m_man.host_resolver()
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&http_tracker_connection::on_response, shared_from_this(), _1, _2, _3, _4)
|
2014-07-06 21:18:00 +02:00
|
|
|
, true, settings.get_int(settings_pack::max_http_recv_buffer_size)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&http_tracker_connection::on_connect, shared_from_this(), _1)
|
|
|
|
, std::bind(&http_tracker_connection::on_filter, shared_from_this(), _1, _2)
|
2011-08-28 23:06:15 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
, tracker_req().ssl_ctx
|
|
|
|
#endif
|
2016-11-17 20:34:49 +01:00
|
|
|
);
|
2008-01-31 09:24:01 +01:00
|
|
|
|
2016-11-17 20:34:49 +01:00
|
|
|
int const timeout = tracker_req().event==tracker_request::stopped
|
|
|
|
? settings.get_int(settings_pack::stop_tracker_timeout)
|
|
|
|
: settings.get_int(settings_pack::tracker_completion_timeout);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// when sending stopped requests, prefer the cached DNS entry
|
|
|
|
// to avoid being blocked for slow or failing responses. Chances
|
|
|
|
// are that we're shutting down, and this should be a best-effort
|
|
|
|
// attempt. It's not worth stalling shutdown.
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings ps(settings);
|
2008-03-30 21:00:37 +02:00
|
|
|
m_tracker_connection->get(url, seconds(timeout)
|
2011-09-17 23:15:42 +02:00
|
|
|
, tracker_req().event == tracker_request::stopped ? 2 : 1
|
2016-06-20 17:32:06 +02:00
|
|
|
, ps.proxy_tracker_connections ? &ps : nullptr
|
2015-08-25 04:18:10 +02:00
|
|
|
, 5, settings.get_bool(settings_pack::anonymous_mode)
|
2014-07-06 21:18:00 +02:00
|
|
|
? "" : settings.get_str(settings_pack::user_agent)
|
2010-04-13 06:30:34 +02:00
|
|
|
, bind_interface()
|
2014-07-06 21:18:00 +02:00
|
|
|
, tracker_req().event == tracker_request::stopped
|
|
|
|
? resolver_interface::prefer_cache
|
2014-12-17 03:44:27 +01:00
|
|
|
: resolver_interface::abort_on_shutdown
|
2015-12-13 21:14:19 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2015-02-08 17:03:09 +01:00
|
|
|
, tracker_req().auth
|
2015-12-13 21:14:19 +01:00
|
|
|
#else
|
|
|
|
, ""
|
|
|
|
#endif
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2015-02-01 15:30:43 +01:00
|
|
|
, tracker_req().i2pconn
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
|
|
|
);
|
2006-11-15 22:39:58 +01:00
|
|
|
|
2008-09-22 02:15:05 +02:00
|
|
|
// the url + 100 estimated header size
|
2016-04-25 23:22:09 +02:00
|
|
|
sent_bytes(int(url.size()) + 100);
|
2008-09-22 02:15:05 +02:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2007-09-14 04:54:15 +02:00
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2007-09-14 04:54:15 +02:00
|
|
|
if (cb)
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2012-09-28 01:04:51 +02:00
|
|
|
cb->debug_log("==> TRACKER_REQUEST [ url: %s ]", url.c_str());
|
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
|
|
|
}
|
2016-12-11 16:56:44 +01:00
|
|
|
cancel();
|
|
|
|
m_man.remove_request(this);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2015-05-18 03:30:32 +02:00
|
|
|
// endpoints is an in-out parameter
|
|
|
|
void http_tracker_connection::on_filter(http_connection& c
|
|
|
|
, std::vector<tcp::endpoint>& endpoints)
|
2008-10-22 21:40:32 +02:00
|
|
|
{
|
2015-05-18 03:30:32 +02:00
|
|
|
TORRENT_UNUSED(c);
|
2015-05-16 08:33:37 +02:00
|
|
|
if (!tracker_req().filter) return;
|
2011-03-04 07:55:39 +01:00
|
|
|
|
2008-10-22 21:40:32 +02:00
|
|
|
// remove endpoints that are filtered by the IP filter
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<tcp::endpoint>::iterator i = endpoints.begin();
|
2009-04-30 07:49:46 +02:00
|
|
|
i != endpoints.end();)
|
|
|
|
{
|
2015-05-18 03:30:32 +02:00
|
|
|
if (tracker_req().filter->access(i->address()) == ip_filter::blocked)
|
2009-04-30 07:49:46 +02:00
|
|
|
i = endpoints.erase(i);
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
2008-10-22 21:40:32 +02:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2010-06-06 04:31:20 +02:00
|
|
|
if (cb)
|
|
|
|
{
|
|
|
|
cb->debug_log("*** TRACKER_FILTER");
|
|
|
|
}
|
|
|
|
#endif
|
2008-10-22 21:40:32 +02:00
|
|
|
if (endpoints.empty())
|
2016-10-02 21:27:50 +02:00
|
|
|
fail(errors::banned_by_ip_filter);
|
2008-10-22 21:40:32 +02:00
|
|
|
}
|
|
|
|
|
2008-12-01 09:48:54 +01:00
|
|
|
void http_tracker_connection::on_connect(http_connection& c)
|
|
|
|
{
|
2010-04-15 05:29:34 +02:00
|
|
|
error_code ec;
|
2008-12-01 09:48:54 +01:00
|
|
|
tcp::endpoint ep = c.socket().remote_endpoint(ec);
|
|
|
|
m_tracker_ip = ep.address();
|
|
|
|
}
|
|
|
|
|
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
|
2016-08-29 14:31:23 +02:00
|
|
|
std::shared_ptr<http_tracker_connection> me(shared_from_this());
|
2008-02-07 08:15:23 +01:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (ec && ec != boost::asio::error::eof)
|
2008-02-25 01:55:31 +01:00
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(ec);
|
2008-02-25 01:55:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2008-01-31 09:24:01 +01:00
|
|
|
if (!parser.header_finished())
|
2006-12-12 03:28:53 +01:00
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
fail(boost::asio::error::eof);
|
2006-12-12 03:28:53 +01:00
|
|
|
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
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
fail(error_code(parser.status_code(), http_category())
|
2011-01-16 03:54:59 +01:00
|
|
|
, parser.status_code(), parser.message().c_str());
|
2007-04-25 20:26:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (ec && ec != boost::asio::error::eof)
|
2006-12-12 03:28:53 +01:00
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(ec, parser.status_code());
|
2006-12-12 03:28:53 +01:00
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2015-05-18 03:30:32 +02: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
|
2010-10-28 06:01:59 +02:00
|
|
|
error_code ecode;
|
2007-12-30 02:57:57 +01:00
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2014-09-29 08:10:22 +02:00
|
|
|
if (!cb)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2014-09-29 08:10:22 +02:00
|
|
|
close();
|
|
|
|
return;
|
2004-05-21 01:26:40 +02:00
|
|
|
}
|
2014-09-29 08:10:22 +02:00
|
|
|
|
|
|
|
tracker_response resp = parse_tracker_response(data, size, ecode
|
2015-06-27 23:30:00 +02:00
|
|
|
, tracker_req().kind, tracker_req().info_hash);
|
2014-09-29 08:10:22 +02:00
|
|
|
|
|
|
|
if (!resp.warning_message.empty())
|
|
|
|
cb->tracker_warning(tracker_req(), resp.warning_message);
|
|
|
|
|
|
|
|
if (ecode)
|
2004-05-21 01:26:40 +02:00
|
|
|
{
|
2015-11-25 05:38:19 +01:00
|
|
|
fail(ecode, parser.status_code(), resp.failure_reason.c_str()
|
|
|
|
, resp.interval, resp.min_interval);
|
2014-09-29 08:10:22 +02:00
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do slightly different things for scrape requests
|
2015-06-27 23:30:00 +02:00
|
|
|
if (0 != (tracker_req().kind & tracker_request::scrape_request))
|
2014-09-29 08:10:22 +02:00
|
|
|
{
|
|
|
|
cb->tracker_scrape_response(tracker_req(), resp.complete
|
|
|
|
, resp.incomplete, resp.downloaded, resp.downloaders);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::list<address> ip_list;
|
|
|
|
if (m_tracker_connection)
|
|
|
|
{
|
2016-09-09 21:02:20 +02:00
|
|
|
for (auto const& endp : m_tracker_connection->endpoints())
|
2014-09-29 08:10:22 +02:00
|
|
|
{
|
2016-09-09 21:02:20 +02:00
|
|
|
ip_list.push_back(endp.address());
|
2014-09-29 08:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb->tracker_response(tracker_req(), m_tracker_ip, ip_list, resp);
|
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
|
|
|
|
2015-05-10 20:24:38 +02:00
|
|
|
// TODO: 2 returning a bool here is redundant. Instead this function should
|
|
|
|
// return the peer_entry
|
2015-03-12 06:20:12 +01:00
|
|
|
bool extract_peer_info(bdecode_node const& info, peer_entry& ret, error_code& ec)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
|
|
|
// extract peer id (if any)
|
2015-03-12 06:20:12 +01:00
|
|
|
if (info.type() != bdecode_node::dict_t)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::invalid_peer_dict;
|
2007-12-30 02:57:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node i = info.dict_find_string("peer id");
|
|
|
|
if (i && i.string_length() == 20)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2016-09-09 21:02:20 +02:00
|
|
|
std::copy(i.string_ptr(), i.string_ptr() + 20, 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
|
2009-12-02 05:05:24 +01:00
|
|
|
i = info.dict_find_string("ip");
|
2016-12-12 00:44:52 +01:00
|
|
|
if (!i)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::invalid_tracker_response;
|
2007-12-30 02:57:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-13 13:04:53 +02:00
|
|
|
ret.hostname = i.string_value().to_string();
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
// extract port
|
2009-12-02 05:05:24 +01:00
|
|
|
i = info.dict_find_int("port");
|
2016-12-12 00:44:52 +01:00
|
|
|
if (!i)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::invalid_tracker_response;
|
2007-12-30 02:57:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-18 20:01:38 +02:00
|
|
|
ret.port = std::uint16_t(i.int_value());
|
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
|
|
|
}
|
|
|
|
|
2014-09-29 08:10:22 +02:00
|
|
|
tracker_response parse_tracker_response(char const* data, int size, error_code& ec
|
2015-06-27 23:30:00 +02:00
|
|
|
, int flags, sha1_hash scrape_ih)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2014-09-29 08:10:22 +02:00
|
|
|
tracker_response resp;
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node e;
|
|
|
|
int res = bdecode(data, data + size, e, ec);
|
2014-09-29 08:10:22 +02:00
|
|
|
|
|
|
|
if (ec) return resp;
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
if (res != 0 || e.type() != bdecode_node::dict_t)
|
2014-09-29 08:10:22 +02:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::invalid_tracker_response;
|
2014-09-29 08:10:22 +02:00
|
|
|
return resp;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2012-03-24 16:15:36 +01:00
|
|
|
int interval = int(e.dict_find_int_value("interval", 0));
|
2012-06-26 05:35:31 +02:00
|
|
|
// if no interval is specified, default to 30 minutes
|
|
|
|
if (interval == 0) interval = 1800;
|
2014-09-28 08:36:03 +02:00
|
|
|
int min_interval = int(e.dict_find_int_value("min interval", 30));
|
|
|
|
|
|
|
|
resp.interval = interval;
|
|
|
|
resp.min_interval = min_interval;
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node tracker_id = e.dict_find_string("tracker id");
|
2010-11-18 06:51:52 +01:00
|
|
|
if (tracker_id)
|
2016-08-13 13:04:53 +02:00
|
|
|
resp.trackerid = tracker_id.string_value().to_string();
|
2014-09-28 08:36:03 +02:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
// parse the response
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node failure = e.dict_find_string("failure reason");
|
2009-12-02 05:05:24 +01:00
|
|
|
if (failure)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
resp.failure_reason = failure.string_value().to_string();
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::tracker_failure;
|
2014-09-29 08:10:22 +02:00
|
|
|
return resp;
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node warning = e.dict_find_string("warning message");
|
2009-12-02 05:05:24 +01:00
|
|
|
if (warning)
|
2016-08-13 13:04:53 +02:00
|
|
|
resp.warning_message = warning.string_value().to_string();
|
2007-12-30 02:57:57 +01:00
|
|
|
|
2015-06-27 23:30:00 +02:00
|
|
|
if (0 != (flags & tracker_request::scrape_request))
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node files = e.dict_find_dict("files");
|
|
|
|
if (!files)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::invalid_files_entry;
|
2014-09-29 08:10:22 +02:00
|
|
|
return resp;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node scrape_data = files.dict_find_dict(
|
2014-09-29 09:06:18 +02:00
|
|
|
scrape_ih.to_string());
|
2014-09-29 08:10:22 +02:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
if (!scrape_data)
|
2005-08-11 01:32:39 +02:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::invalid_hash_entry;
|
2014-09-29 08:10:22 +02:00
|
|
|
return resp;
|
2005-08-11 01:32:39 +02:00
|
|
|
}
|
2012-03-24 16:15:36 +01:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
resp.complete = int(scrape_data.dict_find_int_value("complete", -1));
|
|
|
|
resp.incomplete = int(scrape_data.dict_find_int_value("incomplete", -1));
|
|
|
|
resp.downloaded = int(scrape_data.dict_find_int_value("downloaded", -1));
|
|
|
|
resp.downloaders = int(scrape_data.dict_find_int_value("downloaders", -1));
|
2014-09-29 08:10:22 +02:00
|
|
|
|
|
|
|
return resp;
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2014-09-29 08:10:22 +02:00
|
|
|
// look for optional scrape info
|
|
|
|
resp.complete = int(e.dict_find_int_value("complete", -1));
|
|
|
|
resp.incomplete = int(e.dict_find_int_value("incomplete", -1));
|
|
|
|
resp.downloaded = int(e.dict_find_int_value("downloaded", -1));
|
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node peers_ent = e.dict_find("peers");
|
|
|
|
if (peers_ent && peers_ent.type() == bdecode_node::string_t)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
char const* peers = peers_ent.string_ptr();
|
|
|
|
int len = peers_ent.string_length();
|
2015-06-28 00:11:50 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2015-06-27 23:30:00 +02:00
|
|
|
if (0 != (flags & tracker_request::i2p))
|
|
|
|
{
|
2015-06-28 00:11:50 +02:00
|
|
|
for (int i = 0; i < len; i += 32)
|
|
|
|
{
|
|
|
|
if (len - i < 32) break;
|
|
|
|
peer_entry p;
|
|
|
|
p.hostname = base32encode(std::string(peers + i, 32), string::i2p);
|
|
|
|
p.hostname += ".b32.i2p";
|
|
|
|
p.port = 6881;
|
2015-08-29 22:04:35 +02:00
|
|
|
resp.peers.push_back(p);
|
2015-06-28 00:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2015-06-28 00:11:50 +02:00
|
|
|
resp.peers4.reserve(len / 6);
|
|
|
|
for (int i = 0; i < len; i += 6)
|
|
|
|
{
|
|
|
|
if (len - i < 6) break;
|
2004-03-12 17:42:33 +01:00
|
|
|
|
2015-06-28 00:11:50 +02:00
|
|
|
ipv4_peer_entry p;
|
|
|
|
p.ip = detail::read_v4_address(peers).to_v4().to_bytes();
|
|
|
|
p.port = detail::read_uint16(peers);
|
|
|
|
resp.peers4.push_back(p);
|
|
|
|
}
|
2004-03-12 17:42:33 +01:00
|
|
|
}
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
2015-03-12 06:20:12 +01:00
|
|
|
else if (peers_ent && peers_ent.type() == bdecode_node::list_t)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2017-01-09 07:43:57 +01:00
|
|
|
int const len = peers_ent.list_size();
|
|
|
|
resp.peers.reserve(std::size_t(len));
|
2014-09-29 08:10:22 +02:00
|
|
|
error_code parse_error;
|
2009-12-02 05:05:24 +01:00
|
|
|
for (int i = 0; i < len; ++i)
|
2004-03-12 17:42:33 +01:00
|
|
|
{
|
2007-12-30 02:57:57 +01:00
|
|
|
peer_entry p;
|
2015-03-12 06:20:12 +01:00
|
|
|
if (!extract_peer_info(peers_ent.list_at(i), p, parse_error))
|
2014-09-29 08:10:22 +02:00
|
|
|
continue;
|
2014-09-28 08:36:03 +02:00
|
|
|
resp.peers.push_back(p);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2014-09-29 08:10:22 +02:00
|
|
|
|
|
|
|
// only report an error if all peer entries are invalid
|
|
|
|
if (resp.peers.empty() && parse_error)
|
|
|
|
{
|
|
|
|
ec = parse_error;
|
|
|
|
return resp;
|
|
|
|
}
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
peers_ent.clear();
|
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
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node ipv6_peers = e.dict_find_string("peers6");
|
2009-12-02 05:05:24 +01:00
|
|
|
if (ipv6_peers)
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
char const* peers = ipv6_peers.string_ptr();
|
|
|
|
int len = ipv6_peers.string_length();
|
2014-09-28 08:36:03 +02:00
|
|
|
resp.peers6.reserve(len / 18);
|
2009-12-03 18:44:11 +01:00
|
|
|
for (int i = 0; i < len; i += 18)
|
2007-09-19 23:54:26 +02:00
|
|
|
{
|
2009-12-02 05:05:24 +01:00
|
|
|
if (len - i < 18) break;
|
2007-12-30 02:57:57 +01:00
|
|
|
|
2014-09-28 08:36:03 +02:00
|
|
|
ipv6_peer_entry p;
|
|
|
|
p.ip = detail::read_v6_address(peers).to_v6().to_bytes();
|
2009-12-02 05:05:24 +01:00
|
|
|
p.port = detail::read_uint16(peers);
|
2014-09-28 08:36:03 +02:00
|
|
|
resp.peers6.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
|
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
ipv6_peers.clear();
|
2009-03-22 23:24:11 +01:00
|
|
|
}
|
2009-04-04 18:59:53 +02:00
|
|
|
#else
|
2015-03-13 06:53:22 +01:00
|
|
|
bdecode_node ipv6_peers;
|
2009-04-04 18:59:53 +02:00
|
|
|
#endif
|
2014-09-29 08:10:22 +02:00
|
|
|
/*
|
2010-09-23 18:38:50 +02:00
|
|
|
// if we didn't receive any peers. We don't care if we're stopping anyway
|
|
|
|
if (peers_ent == 0 && ipv6_peers == 0
|
|
|
|
&& tracker_req().event != tracker_request::stopped)
|
2009-03-22 23:24:11 +01:00
|
|
|
{
|
2016-10-02 21:27:50 +02:00
|
|
|
ec = errors::invalid_peers_entry;
|
2014-09-29 08:10:22 +02:00
|
|
|
return resp;
|
2009-03-22 23:24:11 +01:00
|
|
|
}
|
2014-09-29 08:10:22 +02:00
|
|
|
*/
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node ip_ent = e.dict_find_string("external ip");
|
2009-12-02 05:05:24 +01:00
|
|
|
if (ip_ent)
|
2008-03-29 23:45:55 +01:00
|
|
|
{
|
2015-03-12 06:20:12 +01:00
|
|
|
char const* p = ip_ent.string_ptr();
|
|
|
|
if (ip_ent.string_length() == int(address_v4::bytes_type().size()))
|
2014-09-28 08:36:03 +02:00
|
|
|
resp.external_ip = detail::read_v4_address(p);
|
2009-04-04 18:59:53 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2015-03-12 06:20:12 +01:00
|
|
|
else if (ip_ent.string_length() == int(address_v6::bytes_type().size()))
|
2014-09-28 08:36:03 +02:00
|
|
|
resp.external_ip = detail::read_v6_address(p);
|
2009-04-04 18:59:53 +02:00
|
|
|
#endif
|
2008-03-29 23:45:55 +01:00
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2014-09-29 08:10:22 +02:00
|
|
|
return resp;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
}
|