premiere-libtorrent/src/http_tracker_connection.cpp

689 lines
19 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 <vector>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <sstream>
#include "zlib.h"
#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"
2004-03-12 17:42:33 +01:00
#include "libtorrent/io.hpp"
#include "libtorrent/async_gethostbyname.hpp"
2004-01-31 11:46:15 +01:00
using namespace libtorrent;
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
};
}
2004-10-14 03:17:04 +02:00
using namespace boost::posix_time;
2004-01-31 11:46:15 +01:00
namespace libtorrent
{
http_tracker_connection::http_tracker_connection(
2004-07-25 22:57:44 +02:00
tracker_manager& man
, tracker_request const& req
2004-01-31 11:46:15 +01:00
, std::string const& hostname
, unsigned short port
2005-03-11 18:21:56 +01:00
, std::string request
2004-09-16 03:14:16 +02:00
, boost::weak_ptr<request_callback> c
2005-08-25 15:11:39 +02:00
, const http_settings& stn
, std::string const& auth)
2004-01-31 11:46:15 +01:00
: tracker_connection(c)
2004-07-25 22:57:44 +02:00
, m_man(man)
2004-01-31 11:46:15 +01:00
, m_state(read_status)
, m_content_encoding(plain)
, m_content_length(0)
, m_recv_pos(0)
2004-10-14 03:17:04 +02:00
, m_request_time(second_clock::universal_time())
2005-08-25 15:11:39 +02:00
, m_settings(stn)
2004-07-25 22:57:44 +02:00
, m_req(req)
, m_password(auth)
2004-07-25 22:57:44 +02:00
, m_code(0)
2004-01-31 11:46:15 +01:00
{
const std::string* connect_to_host;
int connect_to_port = port;
2004-01-31 11:46:15 +01:00
bool using_proxy = false;
m_send_buffer.assign("GET ");
2004-01-31 11:46:15 +01:00
// should we use the proxy?
2005-08-25 15:11:39 +02:00
if (!m_settings.proxy_ip.empty())
2004-01-31 11:46:15 +01:00
{
2005-08-25 15:11:39 +02:00
connect_to_host = &m_settings.proxy_ip;
2004-01-31 11:46:15 +01:00
using_proxy = true;
m_send_buffer += "http://";
m_send_buffer += hostname;
if (port != 80)
{
m_send_buffer += ":";
2004-01-31 11:46:15 +01:00
m_send_buffer += boost::lexical_cast<std::string>(port);
}
connect_to_port = m_settings.proxy_port != 0
? m_settings.proxy_port : 80 ;
}
else
{
connect_to_host = &hostname;
2004-01-31 11:46:15 +01:00
}
2005-03-11 18:21:56 +01:00
if (m_req.kind == tracker_request::scrape_request)
{
// find and replace "announce" with "scrape"
2005-03-11 18:21:56 +01:00
// in request
std::size_t pos = request.find("announce");
if (pos == std::string::npos)
throw std::runtime_error("scrape is not available on url: '"
+ m_req.url +"'");
request.replace(pos, 8, "scrape");
}
2004-01-31 11:46:15 +01:00
m_send_buffer += request;
2004-02-23 23:54:54 +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
if (request.find('?') != std::string::npos)
m_send_buffer += "&";
else
m_send_buffer += "?";
m_send_buffer += "info_hash=";
2004-01-31 11:46:15 +01:00
m_send_buffer += escape_string(
reinterpret_cast<const char*>(req.info_hash.begin()), 20);
2005-03-11 18:21:56 +01:00
if (m_req.kind == tracker_request::announce_request)
{
m_send_buffer += "&peer_id=";
m_send_buffer += escape_string(
reinterpret_cast<const char*>(req.id.begin()), 20);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&port=";
m_send_buffer += boost::lexical_cast<std::string>(req.listen_port);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&uploaded=";
m_send_buffer += boost::lexical_cast<std::string>(req.uploaded);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&downloaded=";
m_send_buffer += boost::lexical_cast<std::string>(req.downloaded);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
m_send_buffer += "&left=";
m_send_buffer += boost::lexical_cast<std::string>(req.left);
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
if (req.event != tracker_request::none)
{
const char* event_string[] = {"completed", "started", "stopped"};
m_send_buffer += "&event=";
m_send_buffer += event_string[req.event - 1];
}
m_send_buffer += "&key=";
std::stringstream key_string;
key_string << std::hex << req.key;
m_send_buffer += key_string.str();
m_send_buffer += "&compact=1";
m_send_buffer += "&numwant=";
m_send_buffer += boost::lexical_cast<std::string>(
std::min(req.num_want, 999));
// extension that tells the tracker that
// we don't need any peer_id's in the response
m_send_buffer += "&no_peer_id=1";
2004-01-31 11:46:15 +01:00
}
2004-02-23 23:54:54 +01:00
2004-01-31 11:46:15 +01:00
m_send_buffer += " HTTP/1.0\r\nAccept-Encoding: gzip\r\n"
"User-Agent: ";
2005-08-25 15:11:39 +02:00
m_send_buffer += m_settings.user_agent;
2004-01-31 11:46:15 +01:00
m_send_buffer += " (libtorrent)\r\n"
"Host: ";
m_send_buffer += hostname;
if (port != 80)
{
m_send_buffer += ':';
m_send_buffer += boost::lexical_cast<std::string>(port);
}
2005-08-25 15:11:39 +02:00
if (using_proxy && !m_settings.proxy_login.empty())
2004-01-31 11:46:15 +01:00
{
m_send_buffer += "\r\nProxy-Authorization: Basic ";
2005-08-25 15:11:39 +02:00
m_send_buffer += base64encode(m_settings.proxy_login + ":" + m_settings.proxy_password);
2004-01-31 11:46:15 +01:00
}
if (auth != "")
{
m_send_buffer += "\r\nAuthorization: Basic ";
m_send_buffer += base64encode(auth);
}
2004-01-31 11:46:15 +01:00
m_send_buffer += "\r\n\r\n";
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester())
{
2004-09-16 03:14:16 +02:00
requester().debug_log("==> TRACKER_REQUEST [ str: " + m_send_buffer + " ]");
std::stringstream info_hash_str;
info_hash_str << req.info_hash;
2004-09-16 03:14:16 +02:00
requester().debug_log("info_hash: " + info_hash_str.str() + "\n");
}
2005-03-19 13:22:40 +01:00
#endif
m_name_lookup = dns_lookup(connect_to_host->c_str(), connect_to_port);
m_socket.reset(new socket(socket::tcp, false));
2004-01-31 11:46:15 +01:00
}
// returns true if this connection is finished and should be removed from
// the connections list.
bool http_tracker_connection::tick()
{
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-05-21 01:26:40 +02:00
try
{
#endif
if (m_name_lookup.running())
{
if (!m_name_lookup.finished()) return false;
if (m_name_lookup.failed())
{
if (has_requester()) requester().tracker_request_error(
2005-08-10 20:04:39 +02:00
m_req, -1, m_name_lookup.error());
return true;
}
address a(m_name_lookup.ip());
if (has_requester()) requester().m_tracker_address = a;
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
if (has_requester()) requester().debug_log("name lookup successful");
#endif
m_socket->connect(a);
// clear the lookup entry so it will not be
// marked as running anymore
m_name_lookup = dns_lookup();
}
2004-01-31 11:46:15 +01:00
using namespace boost::posix_time;
2004-10-14 03:17:04 +02:00
time_duration d = second_clock::universal_time() - m_request_time;
2005-08-25 15:11:39 +02:00
if (d > seconds(m_settings.tracker_timeout) ||
(!has_requester() && d > seconds(m_settings.stop_tracker_timeout)))
2004-01-31 11:46:15 +01:00
{
if (has_requester()) requester().tracker_request_timed_out(m_req);
2004-01-31 11:46:15 +01:00
return true;
}
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log("tracker connection tick");
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
// if we have a send buffer and the socket is ready for writing
// send the buffer
if (!m_send_buffer.empty() && m_socket->is_writable())
{
int sent = m_socket->send(m_send_buffer.c_str(),(int)m_send_buffer.size());
if (sent == (int)m_send_buffer.size())
{
m_send_buffer.clear();
}
else if (sent > 0)
{
m_send_buffer.erase(
m_send_buffer.begin()
, m_send_buffer.begin() + sent);
}
if (sent != 0)
2004-10-14 03:17:04 +02:00
m_request_time = second_clock::universal_time();
2004-01-31 11:46:15 +01:00
}
2004-10-10 02:42:48 +02:00
if (m_socket->has_error())
{
if (has_requester()) requester().tracker_request_error(
m_req, -1, "connection refused");
2004-10-10 02:42:48 +02:00
return true;
}
2004-01-31 11:46:15 +01:00
// if the socket isn't ready for reading, there's no point in continuing
// trying to read from it
if (!m_socket->is_readable()) return false;
2004-10-14 03:17:04 +02:00
m_request_time = second_clock::universal_time();
2004-01-31 11:46:15 +01:00
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log("tracker connection socket readable");
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
// if the receive buffer is full, expand it with http_buffer_size
if ((int)m_buffer.size() == m_recv_pos)
{
2005-08-25 15:11:39 +02:00
if ((int)m_buffer.size() > m_settings.tracker_maximum_response_length)
2004-01-31 11:46:15 +01:00
{
2004-09-16 03:14:16 +02:00
if (has_requester())
2004-01-31 11:46:15 +01:00
{
requester().tracker_request_error(m_req, 200
2004-01-31 11:46:15 +01:00
, "too large tracker response");
}
return true;
}
assert(http_buffer_size > 0);
m_buffer.resize(m_buffer.size() + http_buffer_size);
}
assert(m_recv_pos >= 0);
assert(m_recv_pos < (int)m_buffer.size());
int received = m_socket->receive(&m_buffer[m_recv_pos], (int)m_buffer.size() - m_recv_pos);
assert(received <= (int)m_buffer.size() - m_recv_pos);
if (received > 0) m_recv_pos += received;
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log("received: " + boost::lexical_cast<std::string>(m_recv_pos));
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
if (m_state == read_status)
{
if (received <= 0)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
2004-01-31 11:46:15 +01:00
{
requester().tracker_request_error(m_req, -1
2004-01-31 11:46:15 +01:00
, "invalid tracker response, connection closed");
}
return true;
}
std::vector<char>::iterator end = m_buffer.begin()+m_recv_pos;
std::vector<char>::iterator newline = std::find(m_buffer.begin(), end, '\n');
// if we don't have a full line yet, wait.
if (newline == end) return false;
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log(std::string(m_buffer.begin(), newline));
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
std::istringstream line(std::string(m_buffer.begin(), newline));
++newline;
m_recv_pos -= (int)std::distance(m_buffer.begin(), newline);
m_buffer.erase(m_buffer.begin(), newline);
std::string protocol;
line >> m_server_protocol;
if (m_server_protocol.substr(0, 5) != "HTTP/")
{
std::string error_msg = "unknown protocol in response: " + m_server_protocol;
if (has_requester()) requester().tracker_request_error(m_req, -1, error_msg.c_str());
2004-01-31 11:46:15 +01:00
return true;
}
2004-07-25 22:57:44 +02:00
line >> m_code;
2004-01-31 11:46:15 +01:00
std::getline(line, m_server_message);
m_state = read_header;
}
if (m_state == read_header)
{
if (received <= 0)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().tracker_request_error(m_req, -1
, "invalid tracker response, connection closed "
"while reading header");
2004-01-31 11:46:15 +01:00
return true;
}
std::vector<char>::iterator end = m_buffer.begin()+m_recv_pos;
2004-08-08 23:26:40 +02:00
std::vector<char>::iterator newline
= std::find(m_buffer.begin(), end, '\n');
2004-01-31 11:46:15 +01:00
std::string line;
while (newline != end && m_state == read_header)
{
line.assign(m_buffer.begin(), newline);
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log(line);
2005-03-19 13:22:40 +01:00
#endif
2004-01-31 11:46:15 +01:00
if (line.substr(0, 16) == "Content-Length: ")
{
try
{
2004-08-08 23:26:40 +02:00
m_content_length = boost::lexical_cast<int>(
line.substr(16, line.length() - 17));
2004-01-31 11:46:15 +01:00
}
catch(boost::bad_lexical_cast&)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
2004-01-31 11:46:15 +01:00
{
requester().tracker_request_error(m_req, -1,
2004-01-31 11:46:15 +01:00
"invalid content-length in tracker response");
}
return true;
}
2005-08-25 15:11:39 +02:00
if (m_content_length > m_settings.tracker_maximum_response_length)
2004-01-31 11:46:15 +01:00
{
2004-09-16 03:14:16 +02:00
if (has_requester())
2004-01-31 11:46:15 +01:00
{
requester().tracker_request_error(m_req, -1
2004-01-31 11:46:15 +01:00
, "content-length is greater than maximum response length");
}
return true;
}
2004-07-25 22:57:44 +02:00
if (m_content_length < minimum_tracker_response_length && m_code == 200)
2004-01-31 11:46:15 +01:00
{
2004-09-16 03:14:16 +02:00
if (has_requester())
2004-01-31 11:46:15 +01:00
{
requester().tracker_request_error(m_req, -1
2004-01-31 11:46:15 +01:00
, "content-length is smaller than minimum response length");
}
return true;
}
}
else if (line.substr(0, 18) == "Content-Encoding: ")
{
if (line.substr(18, 4) == "gzip" || line.substr(18, 6) == "x-gzip")
{
m_content_encoding = gzip;
}
else
{
std::string error_str = "unknown content encoding in response: \"";
error_str += line.substr(18, line.length() - 18 - 2);
error_str += "\"";
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().tracker_request_error(m_req, -1
, error_str.c_str());
2004-01-31 11:46:15 +01:00
return true;
}
}
2004-07-25 22:57:44 +02:00
else if (line.substr(0, 10) == "Location: ")
{
m_location.assign(line.begin() + 10, line.end());
}
2004-08-08 23:26:40 +02:00
else if (line.substr(0, 7) == "Server: ")
{
m_server.assign(line.begin() + 7, line.end());
}
2004-01-31 11:46:15 +01:00
else if (line.size() < 3)
{
m_state = read_body;
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log("end of http header");
2005-03-19 13:22:40 +01:00
#endif
2004-07-25 22:57:44 +02:00
if (m_code >= 300 && m_code < 400)
{
if (m_location.empty())
{
std::string error_str = "got redirection response (";
error_str += boost::lexical_cast<std::string>(m_code);
error_str += ") without 'Location' header";
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().tracker_request_error(m_req
, m_code, error_str.c_str());
2004-07-25 22:57:44 +02:00
return true;
}
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().debug_log("Redirecting to \"" + m_location + "\"");
2004-07-25 22:57:44 +02:00
#endif
std::string::size_type i = m_location.find('?');
if (i == std::string::npos)
m_req.url = m_location;
else
m_req.url.assign(m_location.begin(), m_location.begin() + i);
m_man.queue_request(m_req, m_password, m_requester);
2004-07-25 22:57:44 +02:00
return true;
}
2004-01-31 11:46:15 +01:00
}
++newline;
assert(m_recv_pos <= (int)m_buffer.size());
m_recv_pos -= (int)std::distance(m_buffer.begin(), newline);
m_buffer.erase(m_buffer.begin(), newline);
assert(m_recv_pos <= (int)m_buffer.size());
end = m_buffer.begin() + m_recv_pos;
newline = std::find(m_buffer.begin(), end, '\n');
}
}
if (m_state == read_body)
{
if (m_recv_pos == m_content_length || received <= 0)
{
// GZIP
if (m_content_encoding == gzip)
{
2004-09-16 03:14:16 +02:00
boost::shared_ptr<request_callback> r = m_requester.lock();
if (!r) return true;
if (inflate_gzip(m_buffer, m_req, r.get(),
2005-08-25 15:11:39 +02:00
m_settings.tracker_maximum_response_length))
2004-01-31 11:46:15 +01:00
return true;
}
// handle tracker response
try
{
entry e = bdecode(m_buffer.begin(), m_buffer.end());
parse(e);
}
catch (std::exception&)
{
std::string error_str(m_buffer.begin(), m_buffer.end());
if (has_requester()) requester().tracker_request_error(m_req, m_code, error_str);
}
2004-01-31 11:46:15 +01:00
return true;
}
return false;
}
else if (m_recv_pos > m_content_length && m_content_length > 0)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
2004-01-31 11:46:15 +01:00
{
requester().tracker_request_error(m_req, -1
2004-01-31 11:46:15 +01:00
, "invalid tracker response (body > content_length)");
}
return true;
}
return false;
2005-07-06 15:18:10 +02:00
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
2004-05-21 01:26:40 +02:00
}
catch (std::exception&)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().debug_log(std::string(m_buffer.begin(), m_buffer.end()));
2004-05-21 01:26:40 +02:00
throw;
}
#endif
}
2004-01-31 11:46:15 +01:00
2004-03-17 13:14:44 +01:00
peer_entry http_tracker_connection::extract_peer_info(const entry& info)
2004-01-31 11:46:15 +01:00
{
peer_entry ret;
// extract peer id (if any)
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
{
2004-03-17 13:14:44 +01:00
if (i->string().length() != 20)
throw std::runtime_error("invalid response from tracker");
std::copy(i->string().begin(), i->string().end(), ret.id.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.id.begin(), 20, 0);
}
// extract ip
2004-03-17 13:14:44 +01:00
i = info.find_key("ip");
if (i == 0) throw std::runtime_error("invalid response from tracker");
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) throw std::runtime_error("invalid response from tracker");
ret.port = (unsigned short)i->integer();
2004-01-31 11:46:15 +01:00
return ret;
}
void http_tracker_connection::parse(entry const& e)
2004-01-31 11:46:15 +01:00
{
2004-09-16 03:14:16 +02:00
if (!has_requester()) return;
2004-01-31 11:46:15 +01:00
try
{
// parse the response
2004-03-05 13:04:47 +01:00
try
2004-01-31 11:46:15 +01:00
{
entry const& failure = e["failure reason"];
2004-10-03 13:39:34 +02:00
if (has_requester()) requester().tracker_request_error(
m_req, m_code, failure.string().c_str());
2004-10-03 13:39:34 +02:00
return;
2004-01-31 11:46:15 +01:00
}
catch (type_error const&) {}
2004-01-31 11:46:15 +01:00
2005-08-11 01:32:39 +02:00
try
{
entry const& warning = e["warning message"];
if (has_requester())
requester().tracker_warning(warning.string());
}
catch(type_error const&) {}
2005-03-11 18:21:56 +01:00
std::vector<peer_entry> peer_list;
if (m_req.kind == tracker_request::scrape_request)
{
std::string ih;
std::copy(m_req.info_hash.begin(), m_req.info_hash.end()
, std::back_inserter(ih));
entry scrape_data = e["files"][ih];
int complete = scrape_data["complete"].integer();
int incomplete = scrape_data["incomplete"].integer();
requester().tracker_response(m_req, peer_list, 0, complete
2005-03-11 18:21:56 +01:00
, incomplete);
return;
}
2004-01-31 11:46:15 +01:00
2005-03-11 18:21:56 +01:00
int interval = (int)e["interval"].integer();
2004-01-31 11:46:15 +01:00
2004-03-12 17:42:33 +01:00
if (e["peers"].type() == entry::string_t)
2004-01-31 11:46:15 +01:00
{
2004-03-12 17:42:33 +01:00
std::string const& peers = e["peers"].string();
for (std::string::const_iterator i = peers.begin();
i != peers.end();)
{
if (std::distance(i, peers.end()) < 6) break;
peer_entry p;
p.id.clear();
std::stringstream ip_str;
ip_str << (int)detail::read_uint8(i) << ".";
ip_str << (int)detail::read_uint8(i) << ".";
ip_str << (int)detail::read_uint8(i) << ".";
ip_str << (int)detail::read_uint8(i);
p.ip = ip_str.str();
p.port = detail::read_uint16(i);
peer_list.push_back(p);
}
}
else
{
entry::list_type const& l = e["peers"].list();
2004-03-12 17:42:33 +01:00
for(entry::list_type::const_iterator i = l.begin(); i != l.end(); ++i)
{
peer_entry p = extract_peer_info(*i);
peer_list.push_back(p);
}
2004-01-31 11:46:15 +01:00
}
// look for optional crape info
int complete = -1;
int incomplete = -1;
2005-02-23 09:57:54 +01:00
try { complete = e["complete"].integer(); }
2005-06-11 01:12:50 +02:00
catch(type_error&) {}
2005-02-23 09:57:54 +01:00
try { incomplete = e["incomplete"].integer(); }
2005-06-11 01:12:50 +02:00
catch(type_error&) {}
requester().tracker_response(m_req, peer_list, interval, complete
2005-02-23 09:57:54 +01:00
, incomplete);
2004-01-31 11:46:15 +01:00
}
catch(type_error& e)
{
requester().tracker_request_error(m_req, m_code, e.what());
2004-01-31 11:46:15 +01:00
}
catch(std::runtime_error& e)
{
requester().tracker_request_error(m_req, m_code, e.what());
2004-01-31 11:46:15 +01:00
}
}
}
2005-03-19 13:22:40 +01:00