premiere-libtorrent/src/udp_tracker_connection.cpp

437 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 <vector>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <sstream>
#include "zlib.h"
#include "libtorrent/tracker_manager.hpp"
#include "libtorrent/udp_tracker_connection.hpp"
#include "libtorrent/io.hpp"
namespace
{
enum
{
udp_connection_retries = 4,
udp_announce_retries = 15,
udp_connect_timeout = 15,
udp_announce_timeout = 10,
udp_buffer_size = 2048
};
}
2004-10-14 03:17:04 +02:00
using namespace boost::posix_time;
2004-01-31 11:46:15 +01:00
namespace libtorrent
{
udp_tracker_connection::udp_tracker_connection(
tracker_request const& req
, std::string const& hostname
, unsigned short port
2004-09-16 03:14:16 +02:00
, boost::weak_ptr<request_callback> c
2004-01-31 11:46:15 +01:00
, const http_settings& stn)
: tracker_connection(c)
2004-10-14 03:17:04 +02:00
, m_request_time(second_clock::universal_time())
2004-01-31 11:46:15 +01:00
, m_request(req)
, m_transaction_id(0)
, m_connection_id(0)
, m_settings(stn)
, m_attempts(0)
{
// TODO: this is a problem. DNS-lookup is blocking!
// (may block up to 5 seconds)
2004-02-26 01:27:06 +01:00
address a(hostname.c_str(), port);
2004-09-16 03:14:16 +02:00
if (has_requester()) requester().m_tracker_address = a;
2004-01-31 11:46:15 +01:00
m_socket.reset(new socket(socket::udp, false));
m_socket->connect(a);
send_udp_connect();
}
bool udp_tracker_connection::send_finished() const
{
using namespace boost::posix_time;
2004-10-14 03:17:04 +02:00
time_duration d = second_clock::universal_time() - m_request_time;
2004-01-31 11:46:15 +01:00
return (m_transaction_id != 0
&& m_connection_id != 0)
|| d > seconds(m_settings.tracker_timeout);
}
bool udp_tracker_connection::tick()
{
using namespace boost::posix_time;
2004-10-14 03:17:04 +02:00
time_duration d = second_clock::universal_time() - m_request_time;
2004-01-31 11:46:15 +01:00
if (m_connection_id == 0
&& d > seconds(udp_connect_timeout))
{
if (m_attempts >= udp_connection_retries)
{
if (has_requester())
requester().tracker_request_timed_out(m_request);
2004-01-31 11:46:15 +01:00
return true;
}
send_udp_connect();
return false;
}
if (m_connection_id != 0
&& d > seconds(udp_announce_timeout))
{
if (m_attempts >= udp_announce_retries)
{
if (has_requester())
requester().tracker_request_timed_out(m_request);
2004-01-31 11:46:15 +01:00
return true;
}
2005-04-12 13:34:40 +02:00
if (m_request.kind == tracker_request::announce_request)
send_udp_announce();
else if (m_request.kind == tracker_request::scrape_request)
send_udp_scrape();
2004-01-31 11:46:15 +01:00
return false;
}
char buf[udp_buffer_size];
int ret = m_socket->receive(buf, udp_buffer_size);
// if there was nothing to receive, return
if (ret == 0) return false;
if (ret < 0)
{
socket::error_code err = m_socket->last_error();
if (err == socket::would_block) return false;
throw network_error(m_socket->last_error());
}
if (ret == udp_buffer_size)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().tracker_request_error(
m_request, -1, "tracker reply too big");
2004-01-31 11:46:15 +01:00
return true;
}
if (m_connection_id == 0)
{
return parse_connect_response(buf, ret);
}
2005-04-12 13:34:40 +02:00
else if (m_request.kind == tracker_request::announce_request)
2004-01-31 11:46:15 +01:00
{
return parse_announce_response(buf, ret);
}
2005-04-12 13:34:40 +02:00
else if (m_request.kind == tracker_request::scrape_request)
{
return parse_scrape_response(buf, ret);
}
assert(false);
return false;
2004-01-31 11:46:15 +01:00
}
void udp_tracker_connection::send_udp_announce()
{
if (m_transaction_id == 0)
m_transaction_id = rand() | (rand() << 16);
2005-04-04 18:04:40 +02:00
std::vector<char> buf;
std::back_insert_iterator<std::vector<char> > out(buf);
2004-01-31 11:46:15 +01:00
// connection_id
2005-04-04 18:04:40 +02:00
detail::write_int64(m_connection_id, out);
2004-01-31 11:46:15 +01:00
// action (announce)
2005-04-04 18:04:40 +02:00
detail::write_int32(announce, out);
2004-01-31 11:46:15 +01:00
// transaction_id
2005-04-04 18:04:40 +02:00
detail::write_int32(m_transaction_id, out);
2004-01-31 11:46:15 +01:00
// info_hash
2005-04-04 18:04:40 +02:00
std::copy(m_request.info_hash.begin(), m_request.info_hash.end(), out);
2004-01-31 11:46:15 +01:00
// peer_id
2005-04-04 18:04:40 +02:00
std::copy(m_request.id.begin(), m_request.id.end(), out);
2004-01-31 11:46:15 +01:00
// downloaded
2005-04-04 18:04:40 +02:00
detail::write_int64(m_request.downloaded, out);
2004-01-31 11:46:15 +01:00
// left
2005-04-04 18:04:40 +02:00
detail::write_int64(m_request.left, out);
2004-01-31 11:46:15 +01:00
// uploaded
2005-04-04 18:04:40 +02:00
detail::write_int64(m_request.uploaded, out);
2004-01-31 11:46:15 +01:00
// event
2005-04-04 18:04:40 +02:00
detail::write_int32(m_request.event, out);
2004-01-31 11:46:15 +01:00
// ip address
2005-04-04 18:04:40 +02:00
detail::write_int32(0, out);
// key
detail::write_int32(m_request.key, out);
2004-01-31 11:46:15 +01:00
// num_want
2005-04-04 18:04:40 +02:00
detail::write_int32(m_request.num_want, out);
2004-01-31 11:46:15 +01:00
// port
2005-04-04 18:04:40 +02:00
detail::write_uint16(m_request.listen_port, out);
2005-04-05 09:55:27 +02:00
2005-04-04 18:04:40 +02:00
m_socket->send(&buf[0], buf.size());
2004-10-14 03:17:04 +02:00
m_request_time = second_clock::universal_time();
2004-01-31 11:46:15 +01:00
++m_attempts;
}
2005-04-12 13:34:40 +02:00
void udp_tracker_connection::send_udp_scrape()
{
if (m_transaction_id == 0)
m_transaction_id = rand() | (rand() << 16);
std::vector<char> buf;
std::back_insert_iterator<std::vector<char> > out(buf);
// connection_id
detail::write_int64(m_connection_id, out);
// action (scrape)
detail::write_int32(scrape, out);
// transaction_id
detail::write_int32(m_transaction_id, out);
// info_hash
std::copy(m_request.info_hash.begin(), m_request.info_hash.end(), out);
m_socket->send(&buf[0], buf.size());
m_request_time = second_clock::universal_time();
++m_attempts;
}
2004-01-31 11:46:15 +01:00
void udp_tracker_connection::send_udp_connect()
{
char send_buf[16];
char* ptr = send_buf;
if (m_transaction_id == 0)
m_transaction_id = rand() | (rand() << 16);
// connection_id
detail::write_int64(0, ptr);
// action (connect)
detail::write_int32(connect, ptr);
// transaction_id
detail::write_int32(m_transaction_id, ptr);
m_socket->send(send_buf, 16);
2004-10-14 03:17:04 +02:00
m_request_time = second_clock::universal_time();
2004-01-31 11:46:15 +01:00
++m_attempts;
}
bool udp_tracker_connection::parse_announce_response(const char* buf, int len)
{
assert(buf != 0);
assert(len > 0);
if (len < 8)
{
2005-03-19 13:22:40 +01:00
#ifdef TORRENT_VERBOSE_LOGGING
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().debug_log("udp_tracker_connection: "
2004-01-31 11:46:15 +01:00
"got a message with size < 8, ignoring");
#endif
return false;
}
int action = detail::read_int32(buf);
int transaction = detail::read_int32(buf);
if (transaction != m_transaction_id)
{
return false;
}
if (action == error)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().tracker_request_error(
m_request, -1, std::string(buf, buf + len - 8));
2004-01-31 11:46:15 +01:00
return true;
}
if (action != announce) return false;
2005-04-12 13:34:40 +02:00
if (len < 20)
2004-01-31 11:46:15 +01:00
{
2005-03-19 13:22:40 +01:00
#ifdef TORRENT_VERBOSE_LOGGING
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().debug_log("udp_tracker_connection: "
2005-04-12 13:34:40 +02:00
"got a message with size < 20, ignoring");
2004-01-31 11:46:15 +01:00
#endif
return false;
}
int interval = detail::read_int32(buf);
int incomplete = detail::read_int32(buf);
int complete = detail::read_int32(buf);
2005-04-05 19:39:36 +02:00
int num_peers = (len - 20) / 6;
if ((len - 20) % 6 != 0)
2004-01-31 11:46:15 +01:00
{
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().tracker_request_error(
m_request, -1, "invalid tracker response");
2004-01-31 11:46:15 +01:00
return true;
}
2004-09-16 03:14:16 +02:00
if (!has_requester()) return true;
2004-01-31 11:46:15 +01:00
std::vector<peer_entry> peer_list;
for (int i = 0; i < num_peers; ++i)
{
peer_entry e;
std::stringstream s;
s << (int)detail::read_uint8(buf) << ".";
s << (int)detail::read_uint8(buf) << ".";
s << (int)detail::read_uint8(buf) << ".";
s << (int)detail::read_uint8(buf);
e.ip = s.str();
e.port = detail::read_uint16(buf);
e.id.clear();
peer_list.push_back(e);
}
requester().tracker_response(m_request, peer_list, interval
, complete, incomplete);
2004-01-31 11:46:15 +01:00
return true;
}
2005-04-12 13:34:40 +02:00
bool udp_tracker_connection::parse_scrape_response(const char* buf, int len)
{
assert(buf != 0);
assert(len > 0);
if (len < 8)
{
#ifdef TORRENT_VERBOSE_LOGGING
if (has_requester())
requester().debug_log("udp_tracker_connection: "
"got a message with size < 8, ignoring");
#endif
return false;
}
int action = detail::read_int32(buf);
int transaction = detail::read_int32(buf);
if (transaction != m_transaction_id)
{
return false;
}
if (action == error)
{
if (has_requester())
requester().tracker_request_error(
m_request, -1, std::string(buf, buf + len - 8));
return true;
}
if (action != scrape) return false;
if (len < 20)
{
#ifdef TORRENT_VERBOSE_LOGGING
if (has_requester())
requester().debug_log("udp_tracker_connection: "
"got a message with size < 20, ignoring");
#endif
return false;
}
int complete = detail::read_int32(buf);
int completed = detail::read_int32(buf);
int incomplete = detail::read_int32(buf);
if (!has_requester()) return true;
std::vector<peer_entry> peer_list;
requester().tracker_response(m_request, peer_list, 0
, complete, incomplete);
return true;
}
2004-01-31 11:46:15 +01:00
bool udp_tracker_connection::parse_connect_response(const char* buf, int len)
{
assert(buf != 0);
assert(len > 0);
if (len < 8)
{
2005-03-19 13:22:40 +01:00
#ifdef TORRENT_VERBOSE_LOGGING
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().debug_log("udp_tracker_connection: "
2004-01-31 11:46:15 +01:00
"got a message with size < 8, ignoring");
#endif
return false;
}
const char* ptr = buf;
int action = detail::read_int32(ptr);
int transaction = detail::read_int32(ptr);
if (action == error)
{
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().tracker_request_error(
m_request, -1, std::string(ptr, buf + len));
2004-01-31 11:46:15 +01:00
return true;
}
if (action != connect) return false;
if (m_transaction_id != transaction)
{
2005-03-19 13:22:40 +01:00
#ifdef TORRENT_VERBOSE_LOGGING
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().debug_log("udp_tracker_connection: "
2004-01-31 11:46:15 +01:00
"got a message with incorrect transaction id, ignoring");
#endif
return false;
}
if (len < 16)
{
2005-03-19 13:22:40 +01:00
#ifdef TORRENT_VERBOSE_LOGGING
2004-09-16 03:14:16 +02:00
if (has_requester())
requester().debug_log("udp_tracker_connection: "
2004-01-31 11:46:15 +01:00
"got a connection message size < 16, ignoring");
#endif
return false;
}
// reset transaction
m_transaction_id = 0;
m_attempts = 0;
m_connection_id = detail::read_int64(ptr);
2005-04-12 13:34:40 +02:00
if (m_request.kind == tracker_request::announce_request)
send_udp_announce();
else if (m_request.kind == tracker_request::scrape_request)
send_udp_scrape();
2004-01-31 11:46:15 +01:00
return false;
}
}
2005-03-19 13:22:40 +01:00