premiere-libtorrent/src/http_tracker_connection.cpp

549 lines
15 KiB
C++
Raw Normal View History

2004-01-31 11:46:15 +01:00
/*
2014-02-23 20:12:25 +01:00
Copyright (c) 2003-2014, 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.
*/
#include <vector>
2014-07-06 21:18:00 +02:00
#include <list>
2004-01-31 11:46:15 +01:00
#include <cctype>
#include <algorithm>
2004-01-31 11:46:15 +01:00
#include "libtorrent/config.hpp"
#include "libtorrent/gzip.hpp"
#include "libtorrent/socket_io.hpp"
2004-01-31 11:46:15 +01: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"
#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"
#include "libtorrent/socket.hpp"
2008-10-22 21:40:32 +02:00
#include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/broadcast_socket.hpp" // for is_local
2004-01-31 11:46:15 +01:00
using namespace libtorrent;
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
, connection_queue& cc
, 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
2014-07-06 21:18:00 +02:00
, aux::session_impl& ses
2009-08-20 05:19:12 +02:00
, std::string const& auth
#if TORRENT_USE_I2P
, i2p_connection* i2p_conn
#endif
)
: 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)
, m_cc(cc)
, m_ios(ios)
2009-08-20 05:19:12 +02:00
#if TORRENT_USE_I2P
, m_i2p_conn(i2p_conn)
#endif
{}
void http_tracker_connection::start()
2004-01-31 11:46:15 +01:00
{
// TODO: 0 support authentication (i.e. user name and password) in the URL
std::string url = tracker_req().url;
if (tracker_req().kind == tracker_request::scrape_request)
2005-03-11 18:21:56 +01:00
{
// find and replace "announce" with "scrape"
2005-03-11 18:21:56 +01:00
// in request
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
{
2013-11-03 00:08:26 +01:00
tracker_connection::fail(error_code(errors::scrape_not_available));
2007-12-29 19:24:50 +01:00
return;
}
url.replace(pos, 8, "scrape");
2005-03-11 18:21:56 +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
2014-07-06 21:18:00 +02:00
aux::session_settings const& settings = m_ses.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
size_t arguments_start = url.find('?');
if (arguments_start != std::string::npos)
url += "&";
2005-03-11 18:21:56 +01:00
else
url += "?";
if (tracker_req().kind == tracker_request::announce_request)
2005-03-11 18:21:56 +01:00
{
const char* event_string[] = {"completed", "started", "stopped", "paused"};
char str[1024];
const bool stats = tracker_req().send_stats;
snprintf(str, sizeof(str)
, "info_hash=%s"
"&peer_id=%s"
"&port=%d"
"&uploaded=%" PRId64
"&downloaded=%" PRId64
"&left=%" PRId64
"&corrupt=%" PRId64
2014-07-06 21:18:00 +02:00
"&key=%08X"
"%s%s" // event
"&numwant=%d"
"&compact=1"
"&no_peer_id=1"
, escape_string((const char*)&tracker_req().info_hash[0], 20).c_str()
, 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
, stats ? tracker_req().uploaded : 0
, stats ? tracker_req().downloaded : 0
, stats ? tracker_req().left : 0
, stats ? tracker_req().corrupt : 0
, 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;
2009-06-19 18:38:06 +02:00
#ifndef TORRENT_DISABLE_ENCRYPTION
2014-07-06 21:18:00 +02:00
if (m_ses.settings().get_int(settings_pack::in_enc_policy) != settings_pack::pe_disabled
&& m_ses.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
2014-07-06 21:18:00 +02:00
if (stats && m_ses.settings().get_bool(settings_pack::report_redundant_bytes))
{
url += "&redundant=";
url += to_string(tracker_req().redundant).elems;
}
2010-11-18 06:51:52 +01:00
if (!tracker_req().trackerid.empty())
{
std::string id = tracker_req().trackerid;
url += "&trackerid=";
url += escape_string(id.c_str(), id.length());
}
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
2014-07-06 21:18:00 +02:00
if (!m_ses.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())
{
2014-07-06 21:18:00 +02:00
url += "&ip=" + escape_string(announce_ip.c_str(), announce_ip.size());
}
2014-07-06 21:18:00 +02:00
else if (m_ses.settings().get_bool(settings_pack::announce_double_nat)
&& is_local(m_ses.listen_address()))
{
// only use the global external listen address here
// if it turned out to be on a local network
// since otherwise the tracker should use our
// source IP to determine our origin
url += "&ip=" + print_address(m_ses.listen_address());
}
2009-04-12 02:37:06 +02:00
}
2004-01-31 11:46:15 +01:00
}
2004-02-23 23:54:54 +01:00
2014-07-06 21:18:00 +02:00
m_tracker_connection.reset(new http_connection(m_ios, m_cc, m_ses.m_host_resolver
2008-10-22 21:40:32 +02:00
, boost::bind(&http_tracker_connection::on_response, self(), _1, _2, _3, _4)
2014-07-06 21:18:00 +02:00
, true, settings.get_int(settings_pack::max_http_recv_buffer_size)
, boost::bind(&http_tracker_connection::on_connect, self(), _1)
, boost::bind(&http_tracker_connection::on_filter, self(), _1, _2)
#ifdef TORRENT_USE_OPENSSL
, tracker_req().ssl_ctx
#endif
));
int timeout = tracker_req().event==tracker_request::stopped
2014-07-06 21:18:00 +02:00
?settings.get_int(settings_pack::stop_tracker_timeout)
:settings.get_int(settings_pack::tracker_completion_timeout);
// 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.
proxy_settings ps = m_ses.proxy();
m_tracker_connection->get(url, seconds(timeout)
, tracker_req().event == tracker_request::stopped ? 2 : 1
2014-07-06 21:18:00 +02:00
, &ps, 5, settings.get_bool(settings_pack::anonymous_mode)
? "" : settings.get_str(settings_pack::user_agent)
, bind_interface()
2014-07-06 21:18:00 +02:00
, tracker_req().event == tracker_request::stopped
? resolver_interface::prefer_cache
: 0
2009-08-20 05:19:12 +02:00
#if TORRENT_USE_I2P
, m_i2p_conn
#endif
);
// 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)
boost::shared_ptr<request_callback> cb = requester();
if (cb)
{
2012-09-28 01:04:51 +02:00
cb->debug_log("==> TRACKER_REQUEST [ url: %s ]", url.c_str());
}
2005-03-19 13:22:40 +01:00
#endif
}
void http_tracker_connection::close()
{
if (m_tracker_connection)
2004-05-21 01:26:40 +02:00
{
m_tracker_connection->close();
m_tracker_connection.reset();
}
tracker_connection::close();
}
2004-01-31 11:46:15 +01:00
2014-07-06 21:18:00 +02:00
void http_tracker_connection::on_filter(http_connection& c, std::vector<tcp::endpoint>& endpoints)
2008-10-22 21:40:32 +02:00
{
if (tracker_req().apply_ip_filter == false) return;
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();
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 defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
boost::shared_ptr<request_callback> cb = requester();
if (cb)
{
cb->debug_log("*** TRACKER_FILTER");
}
#endif
2008-10-22 21:40:32 +02:00
if (endpoints.empty())
2010-02-23 22:53:45 +01:00
fail(error_code(errors::banned_by_ip_filter));
2008-10-22 21:40:32 +02: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();
}
2008-05-03 18:05:42 +02:00
void http_tracker_connection::on_response(error_code const& ec
, http_parser const& parser, char const* data, int size)
{
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)
{
2010-02-23 22:53:45 +01:00
fail(ec);
2008-02-25 01:55:31 +01:00
return;
}
if (!parser.header_finished())
{
2010-02-23 22:53:45 +01:00
fail(asio::error::eof);
return;
}
if (parser.status_code() != 200)
{
fail(error_code(parser.status_code(), get_http_category())
, parser.status_code(), parser.message().c_str());
return;
}
if (ec && ec != asio::error::eof)
{
2010-02-23 22:53:45 +01:00
fail(ec, parser.status_code());
return;
2004-01-31 11:46:15 +01:00
}
received_bytes(size + parser.body_start());
// handle tracker response
2009-12-02 05:05:24 +01:00
lazy_entry e;
error_code ecode;
int res = lazy_bdecode(data, data + size, e, ecode);
2009-12-02 05:05:24 +01:00
if (res == 0 && e.type() == lazy_entry::dict_t)
{
parse(parser.status_code(), e);
2004-05-21 01:26:40 +02:00
}
else
2004-05-21 01:26:40 +02:00
{
fail(ecode, parser.status_code());
2004-05-21 01:26:40 +02:00
}
close();
}
2004-01-31 11:46:15 +01:00
2009-12-02 05:05:24 +01:00
bool http_tracker_connection::extract_peer_info(lazy_entry const& info, peer_entry& ret)
2004-01-31 11:46:15 +01:00
{
// extract peer id (if any)
2009-12-02 05:05:24 +01:00
if (info.type() != lazy_entry::dict_t)
{
2010-02-23 22:53:45 +01:00
fail(error_code(errors::invalid_peer_dict));
return false;
}
2009-12-02 05:05:24 +01:00
lazy_entry const* i = info.dict_find_string("peer id");
if (i != 0 && i->string_length() == 20)
2004-01-31 11:46:15 +01:00
{
2009-12-02 05:05:24 +01: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
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");
if (i == 0)
{
2010-02-23 22:53:45 +01:00
fail(error_code(errors::invalid_tracker_response));
return false;
}
ret.hostname = i->string_value();
2004-01-31 11:46:15 +01:00
// extract port
2009-12-02 05:05:24 +01:00
i = info.dict_find_int("port");
if (i == 0)
{
2010-02-23 22:53:45 +01:00
fail(error_code(errors::invalid_tracker_response));
return false;
}
2009-12-02 05:05:24 +01:00
ret.port = (unsigned short)i->int_value();
2004-01-31 11:46:15 +01:00
return true;
2004-01-31 11:46:15 +01:00
}
// TODO: 2 make this a free function that can be easily unit tested
2009-12-02 05:05:24 +01:00
void http_tracker_connection::parse(int status_code, lazy_entry const& e)
2004-01-31 11:46:15 +01:00
{
boost::shared_ptr<request_callback> cb = requester();
if (!cb) return;
2004-01-31 11:46:15 +01:00
int interval = int(e.dict_find_int_value("interval", 0));
// if no interval is specified, default to 30 minutes
if (interval == 0) interval = 1800;
int min_interval = int(e.dict_find_int_value("min interval", 30));
tracker_response resp;
resp.interval = interval;
resp.min_interval = min_interval;
2010-11-18 06:51:52 +01:00
lazy_entry const* tracker_id = e.dict_find_string("tracker id");
if (tracker_id)
resp.trackerid = tracker_id->string_value();
// parse the response
2009-12-02 05:05:24 +01:00
lazy_entry const* failure = e.dict_find_string("failure reason");
if (failure)
2004-01-31 11:46:15 +01:00
{
2010-02-23 22:53:45 +01:00
fail(error_code(errors::tracker_failure), status_code
, failure->string_value().c_str(), interval, min_interval);
return;
}
2009-12-02 05:05:24 +01:00
lazy_entry const* warning = e.dict_find_string("warning message");
if (warning)
cb->tracker_warning(tracker_req(), warning->string_value());
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
2009-12-02 05:05:24 +01:00
lazy_entry const* files = e.dict_find_dict("files");
if (files == 0)
{
2010-02-23 22:53:45 +01:00
fail(error_code(errors::invalid_files_entry), -1, ""
, interval, min_interval);
2004-10-03 13:39:34 +02:00
return;
2004-01-31 11:46:15 +01:00
}
2009-12-02 05:05:24 +01:00
lazy_entry const* scrape_data = files->dict_find_dict(ih.c_str());
if (scrape_data == 0)
2005-08-11 01:32:39 +02:00
{
2010-02-23 22:53:45 +01:00
fail(error_code(errors::invalid_hash_entry), -1, ""
, interval, min_interval);
return;
2005-08-11 01:32:39 +02:00
}
int complete = int(scrape_data->dict_find_int_value("complete", -1));
int incomplete = int(scrape_data->dict_find_int_value("incomplete", -1));
int downloaded = int(scrape_data->dict_find_int_value("downloaded", -1));
int downloaders = int(scrape_data->dict_find_int_value("downloaders", -1));
2009-12-02 05:05:24 +01:00
cb->tracker_scrape_response(tracker_req(), complete
2010-09-06 06:02:15 +02:00
, incomplete, downloaded, downloaders);
return;
}
2004-01-31 11:46:15 +01:00
2009-12-02 05:05:24 +01:00
lazy_entry const* peers_ent = e.dict_find("peers");
if (peers_ent && peers_ent->type() == lazy_entry::string_t)
{
2009-12-02 05:05:24 +01:00
char const* peers = peers_ent->string_ptr();
int len = peers_ent->string_length();
resp.peers4.reserve(len / 6);
2009-12-03 18:44:11 +01:00
for (int i = 0; i < len; i += 6)
2004-01-31 11:46:15 +01:00
{
2009-12-02 05:05:24 +01:00
if (len - i < 6) break;
2004-03-12 17:42:33 +01:00
ipv4_peer_entry p;
error_code ec;
p.ip = detail::read_v4_address(peers).to_v4().to_bytes();
2009-12-02 05:05:24 +01:00
p.port = detail::read_uint16(peers);
resp.peers4.push_back(p);
2004-03-12 17:42:33 +01:00
}
}
2009-12-02 05:05:24 +01:00
else if (peers_ent && peers_ent->type() == lazy_entry::list_t)
{
2009-12-02 05:05:24 +01:00
int len = peers_ent->list_size();
resp.peers.reserve(len);
2009-12-02 05:05:24 +01:00
for (int i = 0; i < len; ++i)
2004-03-12 17:42:33 +01:00
{
peer_entry p;
2009-12-02 05:05:24 +01:00
if (!extract_peer_info(*peers_ent->list_at(i), p)) return;
resp.peers.push_back(p);
2004-01-31 11:46:15 +01:00
}
}
else
{
2009-03-22 23:24:11 +01:00
peers_ent = 0;
}
2004-01-31 11:46:15 +01:00
#if TORRENT_USE_IPV6
2009-12-02 05:05:24 +01:00
lazy_entry const* ipv6_peers = e.dict_find_string("peers6");
if (ipv6_peers)
{
2009-12-02 05:05:24 +01:00
char const* peers = ipv6_peers->string_ptr();
int len = ipv6_peers->string_length();
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;
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);
resp.peers6.push_back(p);
2007-09-19 23:54:26 +02:00
}
}
2009-03-22 23:24:11 +01:00
else
{
ipv6_peers = 0;
}
#else
2009-12-02 05:05:24 +01:00
lazy_entry const* ipv6_peers = 0;
#endif
2009-03-22 23:24:11 +01: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
{
2010-02-23 22:53:45 +01:00
fail(error_code(errors::invalid_peers_entry), -1, ""
, interval, min_interval);
2009-03-22 23:24:11 +01:00
return;
}
2009-12-02 05:05:24 +01:00
lazy_entry const* ip_ent = e.dict_find_string("external ip");
if (ip_ent)
{
2009-12-02 05:05:24 +01:00
char const* p = ip_ent->string_ptr();
2014-07-05 01:40:31 +02:00
if (ip_ent->string_length() == int(address_v4::bytes_type().size()))
resp.external_ip = detail::read_v4_address(p);
#if TORRENT_USE_IPV6
2014-07-05 01:40:31 +02:00
else if (ip_ent->string_length() == int(address_v6::bytes_type().size()))
resp.external_ip = detail::read_v6_address(p);
#endif
}
// 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));
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
{
error_code ec;
ip_list.push_back(m_tracker_connection->socket().remote_endpoint(ec).address());
2014-07-06 21:18:00 +02:00
std::vector<tcp::endpoint> const& epts = m_tracker_connection->endpoints();
for (std::vector<tcp::endpoint>::const_iterator i = epts.begin()
2009-09-20 17:32:23 +02:00
, end(epts.end()); i != end; ++i)
{
ip_list.push_back(i->address());
}
2009-09-06 02:57:01 +02:00
}
cb->tracker_response(tracker_req(), m_tracker_ip, ip_list, resp);
2004-01-31 11:46:15 +01:00
}
}
2005-03-19 13:22:40 +01:00