premiere-libtorrent/src/http_tracker_connection.cpp

415 lines
11 KiB
C++
Raw Normal View History

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.
*/
#include "libtorrent/pch.hpp"
2004-01-31 11:46:15 +01:00
#include <vector>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <algorithm>
2004-01-31 11:46:15 +01:00
#include "libtorrent/config.hpp"
#include "libtorrent/gzip.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"
2004-01-31 11:46:15 +01:00
using namespace libtorrent;
using boost::bind;
2004-01-31 11:46:15 +01:00
namespace libtorrent
{
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
, address bind_infc
2004-09-16 03:14:16 +02:00
, boost::weak_ptr<request_callback> c
, session_settings const& stn
, proxy_settings const& ps
, std::string const& auth)
2008-01-08 06:47:43 +01:00
: tracker_connection(man, req, ios, bind_infc, c)
2004-07-25 22:57:44 +02:00
, m_man(man)
2004-01-31 11:46:15 +01:00
{
// TODO: authentication
std::string url = req.url;
if (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
{
fail(-1, ("scrape is not available on url: '"
+ req.url +"'").c_str());
2007-12-29 19:24:50 +01:00
return;
}
url.replace(pos, 8, "scrape");
2005-03-11 18:21:56 +01: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 += "?";
2005-03-11 18:21:56 +01:00
url += "info_hash=";
url += escape_string(
reinterpret_cast<const char*>(req.info_hash.begin()), 20);
if (req.kind == tracker_request::announce_request)
2005-03-11 18:21:56 +01:00
{
url += "&peer_id=";
url += escape_string(
reinterpret_cast<const char*>(req.pid.begin()), 20);
2004-01-31 11:46:15 +01:00
url += "&port=";
url += boost::lexical_cast<std::string>(req.listen_port);
2004-01-31 11:46:15 +01:00
url += "&uploaded=";
url += boost::lexical_cast<std::string>(req.uploaded);
2004-01-31 11:46:15 +01:00
url += "&downloaded=";
url += boost::lexical_cast<std::string>(req.downloaded);
2004-01-31 11:46:15 +01:00
url += "&left=";
url += boost::lexical_cast<std::string>(req.left);
2005-03-11 18:21:56 +01:00
if (req.event != tracker_request::none)
{
const char* event_string[] = {"completed", "started", "stopped"};
url += "&event=";
url += event_string[req.event - 1];
}
url += "&key=";
std::stringstream key_string;
key_string << std::hex << req.key;
url += key_string.str();
url += "&compact=1";
url += "&numwant=";
url += boost::lexical_cast<std::string>(
(std::min)(req.num_want, 999));
if (stn.announce_ip != address())
2007-06-13 02:20:06 +02:00
{
url += "&ip=";
url += stn.announce_ip.to_string();
2007-06-13 02:20:06 +02:00
}
2005-03-11 18:21:56 +01:00
2007-06-06 02:41:20 +02:00
#ifndef TORRENT_DISABLE_ENCRYPTION
url += "&supportcrypto=1";
2007-06-06 02:41:20 +02:00
#endif
url += "&ipv6=";
url += req.ipv6;
2005-03-11 18:21:56 +01:00
// extension that tells the tracker that
// we don't need any peer_id's in the response
url += "&no_peer_id=1";
2004-01-31 11:46:15 +01:00
}
2004-02-23 23:54:54 +01:00
m_tracker_connection.reset(new http_connection(ios, cc
, boost::bind(&http_tracker_connection::on_response, self(), _1, _2, _3, _4)));
m_tracker_connection->get(url, seconds(stn.tracker_completion_timeout)
, &ps, 5, stn.user_agent, bind_infc);
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)
{
cb->debug_log("==> TRACKER_REQUEST [ url: " + url + " ]");
}
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
void http_tracker_connection::on_response(asio::error_code const& ec
, http_parser const& parser, char const* data, int size)
{
if (!parser.header_finished())
{
fail(-1, "premature end of file");
return;
}
if (parser.status_code() != 200)
{
fail(parser.status_code(), parser.message().c_str());
return;
}
if (ec && ec != asio::error::eof)
{
fail(parser.status_code(), ec.message().c_str());
return;
2004-01-31 11:46:15 +01:00
}
// handle tracker response
entry e = bdecode(data, data + size);
if (e.type() != entry::undefined_t)
{
parse(parser.status_code(), e);
2004-05-21 01:26:40 +02:00
}
else
2004-05-21 01:26:40 +02:00
{
std::string error_str("invalid bencoding of tracker response: \"");
for (char const* i = data, *end(data + size); i != end; ++i)
{
if (std::isprint(*i)) error_str += *i;
else error_str += "0x" + boost::lexical_cast<std::string>((unsigned int)*i) + " ";
}
error_str += "\"";
fail(parser.status_code(), error_str.c_str());
2004-05-21 01:26:40 +02:00
}
close();
}
2004-01-31 11:46:15 +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)
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
{
if (i->type() != entry::string_t || i->string().length() != 20)
{
fail(-1, "invalid response from tracker (invalid peer id)");
return false;
}
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
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");
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");
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
return true;
2004-01-31 11:46:15 +01:00
}
void http_tracker_connection::parse(int status_code, 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
// 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
{
fail(status_code, failure->string().c_str());
return;
}
entry const* warning = e.find_key("warning message");
if (warning && warning->type() == entry::string_t)
{
cb->tracker_warning(warning->string());
}
std::vector<peer_entry> peer_list;
if (tracker_req().kind == tracker_request::scrape_request)
{
std::string ih;
std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end()
, std::back_inserter(ih));
2004-10-03 13:39:34 +02: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
}
entry const* scrape_data = e.find_key(ih.c_str());
if (scrape_data == 0 || scrape_data->type() != entry::dictionary_t)
2005-08-11 01:32:39 +02:00
{
fail(-1, "missing or invalid info-hash entry in scrape response");
return;
2005-08-11 01:32:39 +02: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
{
fail(-1, "missing 'complete' or 'incomplete' entries in scrape response");
2005-03-11 18:21:56 +01:00
return;
}
cb->tracker_scrape_response(tracker_req(), complete->integer()
, incomplete->integer(), downloaded->integer());
return;
}
2004-01-31 11:46:15 +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");
if (peers_ent == 0)
{
fail(-1, "missing 'peers' entry in tracker response");
return;
}
2004-01-31 11:46:15 +01:00
if (peers_ent->type() == entry::string_t)
{
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
{
if (std::distance(i, peers.end()) < 6) break;
2004-03-12 17:42:33 +01:00
peer_entry p;
p.pid.clear();
p.ip = detail::read_v4_address(i).to_string();
p.port = detail::read_uint16(i);
peer_list.push_back(p);
2004-03-12 17:42:33 +01:00
}
}
else if (peers_ent->type() == entry::list_t)
{
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
{
peer_entry p;
if (!extract_peer_info(*i, p)) return;
peer_list.push_back(p);
2004-01-31 11:46:15 +01:00
}
}
else
{
fail(-1, "invalid 'peers' entry in tracker response");
return;
}
2004-01-31 11:46:15 +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
{
if (std::distance(i, peers.end()) < 18) break;
peer_entry p;
p.pid.clear();
p.ip = detail::read_v6_address(i).to_string();
p.port = detail::read_uint16(i);
peer_list.push_back(p);
2007-09-19 23:54:26 +02:00
}
}
2007-09-19 23:54:26 +02:00
// look for optional scrape info
int complete = -1;
int incomplete = -1;
2005-02-23 09:57:54 +01:00
entry const* complete_ent = e.find_key("complete");
if (complete_ent && complete_ent->type() == entry::int_t)
complete = complete_ent->integer();
2005-02-23 09:57:54 +01:00
entry const* incomplete_ent = e.find_key("incomplete");
if (incomplete_ent && incomplete_ent->type() == entry::int_t)
incomplete = incomplete_ent->integer();
cb->tracker_response(tracker_req(), peer_list, interval->integer(), complete
, incomplete);
2004-01-31 11:46:15 +01:00
}
}
2005-03-19 13:22:40 +01:00