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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cctype>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "zlib.h"
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
#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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
using boost::bind;
|
|
|
|
using boost::lexical_cast;
|
2004-10-14 03:17:04 +02:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
udp_tracker_connection::udp_tracker_connection(
|
2006-12-15 18:47:21 +01:00
|
|
|
asio::strand& str
|
2006-04-25 23:04:48 +02:00
|
|
|
, tracker_manager& man
|
|
|
|
, tracker_request const& req
|
2004-01-31 11:46:15 +01:00
|
|
|
, std::string const& hostname
|
|
|
|
, unsigned short port
|
2007-03-02 19:40:02 +01:00
|
|
|
, address bind_infc
|
2004-09-16 03:14:16 +02:00
|
|
|
, boost::weak_ptr<request_callback> c
|
2006-05-21 01:24:19 +02:00
|
|
|
, session_settings const& stn)
|
2007-03-02 19:40:02 +01:00
|
|
|
: tracker_connection(man, req, str, bind_infc, c)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_man(man)
|
2006-12-15 18:47:21 +01:00
|
|
|
, m_strand(str)
|
|
|
|
, m_name_lookup(m_strand.io_service())
|
2007-10-22 06:17:26 +02:00
|
|
|
, m_socket(m_strand.io_service())
|
2004-01-31 11:46:15 +01:00
|
|
|
, m_transaction_id(0)
|
|
|
|
, m_connection_id(0)
|
2005-08-25 15:11:39 +02:00
|
|
|
, m_settings(stn)
|
2004-01-31 11:46:15 +01:00
|
|
|
, m_attempts(0)
|
|
|
|
{
|
2007-03-02 19:40:02 +01:00
|
|
|
udp::resolver::query q(hostname, boost::lexical_cast<std::string>(port));
|
2006-05-20 17:30:40 +02:00
|
|
|
m_name_lookup.async_resolve(q
|
2006-12-15 18:47:21 +01:00
|
|
|
, m_strand.wrap(boost::bind(
|
|
|
|
&udp_tracker_connection::name_lookup, self(), _1, _2)));
|
2007-10-26 09:14:19 +02:00
|
|
|
set_timeout(req.event == tracker_request::stopped
|
|
|
|
? m_settings.stop_tracker_timeout
|
|
|
|
: m_settings.tracker_completion_timeout
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_settings.tracker_receive_timeout);
|
2008-01-06 23:14:00 +01:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb) cb->debug_log(("*** UDP_TRACKER [ initiating name lookup: " + hostname + " ]").c_str());
|
|
|
|
#endif
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void udp_tracker_connection::name_lookup(asio::error_code const& error
|
2007-12-30 02:57:57 +01:00
|
|
|
, udp::resolver::iterator i)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error == asio::error::operation_aborted) return;
|
2007-03-02 19:40:02 +01:00
|
|
|
if (error || i == udp::resolver::iterator())
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
fail(-1, error.message().c_str());
|
2006-04-25 23:04:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2006-04-25 23:04:48 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2008-01-06 23:14:00 +01:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER [ name lookup successful ]");
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
restart_read_timeout();
|
2007-03-02 19:40:02 +01:00
|
|
|
|
|
|
|
// look for an address that has the same kind as the one
|
|
|
|
// we're listening on. To make sure the tracker get our
|
|
|
|
// correct listening address.
|
|
|
|
udp::resolver::iterator target = i;
|
|
|
|
udp::resolver::iterator end;
|
|
|
|
udp::endpoint target_address = *i;
|
|
|
|
for (; target != end && target->endpoint().address().is_v4()
|
|
|
|
!= bind_interface().is_v4(); ++target);
|
|
|
|
if (target == end)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(target_address.address().is_v4() != bind_interface().is_v4());
|
2007-09-14 04:54:15 +02:00
|
|
|
if (cb)
|
2007-03-02 19:40:02 +01:00
|
|
|
{
|
|
|
|
std::string tracker_address_type = target_address.address().is_v4() ? "IPv4" : "IPv6";
|
|
|
|
std::string bind_address_type = bind_interface().is_v4() ? "IPv4" : "IPv6";
|
2007-09-14 04:54:15 +02:00
|
|
|
cb->tracker_warning("the tracker only resolves to an "
|
2007-03-03 01:49:15 +01:00
|
|
|
+ tracker_address_type + " address, and you're listening on an "
|
|
|
|
+ bind_address_type + " socket. This may prevent you from receiving incoming connections.");
|
2007-03-02 19:40:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target_address = *target;
|
|
|
|
}
|
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
if (cb) cb->m_tracker_address = tcp::endpoint(target_address.address(), target_address.port());
|
2007-03-02 19:40:02 +01:00
|
|
|
m_target = target_address;
|
2007-12-30 02:57:57 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_socket.open(target_address.protocol(), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fail(-1, ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_socket.bind(udp::endpoint(bind_interface(), 0), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fail(-1, ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_socket.connect(target_address, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fail(-1, ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
send_udp_connect();
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void udp_tracker_connection::on_timeout()
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2008-01-06 23:14:00 +01:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER [ timed out ]");
|
|
|
|
#endif
|
2007-10-26 09:14:19 +02:00
|
|
|
asio::error_code ec;
|
|
|
|
m_socket.close(ec);
|
2006-04-25 23:04:48 +02:00
|
|
|
m_name_lookup.cancel();
|
|
|
|
fail_timeout();
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
void udp_tracker_connection::close()
|
|
|
|
{
|
|
|
|
asio::error_code ec;
|
|
|
|
m_socket.close(ec);
|
|
|
|
m_name_lookup.cancel();
|
|
|
|
tracker_connection::close();
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void udp_tracker_connection::send_udp_connect()
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb)
|
2005-08-09 01:32:38 +02:00
|
|
|
{
|
2007-09-14 04:54:15 +02:00
|
|
|
cb->debug_log("==> UDP_TRACKER_CONNECT ["
|
2006-04-25 23:04:48 +02:00
|
|
|
+ lexical_cast<std::string>(tracker_req().info_hash) + "]");
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-22 06:17:26 +02:00
|
|
|
if (!m_socket.is_open()) return; // the operation was aborted
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
char send_buf[16];
|
|
|
|
char* ptr = send_buf;
|
2005-08-09 01:32:38 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_transaction_id == 0)
|
|
|
|
m_transaction_id = rand() ^ (rand() << 16);
|
2005-08-09 01:32:38 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// connection_id
|
|
|
|
detail::write_uint32(0x417, ptr);
|
|
|
|
detail::write_uint32(0x27101980, ptr);
|
|
|
|
// action (connect)
|
|
|
|
detail::write_int32(action_connect, ptr);
|
|
|
|
// transaction_id
|
|
|
|
detail::write_int32(m_transaction_id, ptr);
|
2005-08-09 01:32:38 +02:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_socket.send(asio::buffer((void*)send_buf, 16), 0, ec);
|
2006-04-25 23:04:48 +02:00
|
|
|
++m_attempts;
|
2007-12-30 02:57:57 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fail(-1, ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
m_buffer.resize(udp_buffer_size);
|
2007-10-22 06:17:26 +02:00
|
|
|
m_socket.async_receive_from(asio::buffer(m_buffer), m_sender
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::bind(&udp_tracker_connection::connect_response, self(), _1, _2));
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void udp_tracker_connection::connect_response(asio::error_code const& error
|
2007-12-30 02:57:57 +01:00
|
|
|
, std::size_t bytes_transferred)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
if (error == asio::error::operation_aborted) return;
|
2007-10-22 06:17:26 +02:00
|
|
|
if (!m_socket.is_open()) return; // the operation was aborted
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
fail(-1, error.message().c_str());
|
2006-04-25 23:04:48 +02:00
|
|
|
return;
|
2005-08-09 01:32:38 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_target != m_sender)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
// this packet was not received from the tracker
|
2007-10-22 06:17:26 +02:00
|
|
|
m_socket.async_receive_from(asio::buffer(m_buffer), m_sender
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::bind(&udp_tracker_connection::connect_response, self(), _1, _2));
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (bytes_transferred >= udp_buffer_size)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "udp response too big");
|
|
|
|
return;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (bytes_transferred < 8)
|
|
|
|
{
|
|
|
|
fail(-1, "got a message with size < 8");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
restart_read_timeout();
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
const char* ptr = &m_buffer[0];
|
|
|
|
int action = detail::read_int32(ptr);
|
|
|
|
int transaction = detail::read_int32(ptr);
|
|
|
|
|
|
|
|
if (action == action_error)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, std::string(ptr, bytes_transferred - 8).c_str());
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (action != action_connect)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "invalid action in connect reply");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_transaction_id != transaction)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "incorrect transaction id");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (bytes_transferred < 16)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "udp_tracker_connection: "
|
|
|
|
"got a message with size < 16");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
// reset transaction
|
|
|
|
m_transaction_id = 0;
|
|
|
|
m_attempts = 0;
|
|
|
|
m_connection_id = detail::read_int64(ptr);
|
|
|
|
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb)
|
2005-04-12 13:34:40 +02:00
|
|
|
{
|
2007-12-30 02:57:57 +01:00
|
|
|
std::stringstream msg;
|
2008-01-06 23:14:00 +01:00
|
|
|
msg << "<== UDP_TRACKER_CONNECT_RESPONSE [ cid: " << m_connection_id << " ]";
|
2007-12-30 02:57:57 +01:00
|
|
|
cb->debug_log(msg.str());
|
2005-04-12 13:34:40 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (tracker_req().kind == tracker_request::announce_request)
|
|
|
|
send_udp_announce();
|
|
|
|
else if (tracker_req().kind == tracker_request::scrape_request)
|
|
|
|
send_udp_scrape();
|
|
|
|
}
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
void udp_tracker_connection::send_udp_announce()
|
|
|
|
{
|
|
|
|
if (m_transaction_id == 0)
|
2005-04-13 18:55:19 +02:00
|
|
|
m_transaction_id = rand() ^ (rand() << 16);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-10-22 06:17:26 +02:00
|
|
|
if (!m_socket.is_open()) return; // the operation was aborted
|
2006-10-03 00:19:21 +02:00
|
|
|
|
2005-04-04 18:04:40 +02:00
|
|
|
std::vector<char> buf;
|
|
|
|
std::back_insert_iterator<std::vector<char> > out(buf);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
tracker_request const& req = tracker_req();
|
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)
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int32(action_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
|
2006-04-25 23:04:48 +02:00
|
|
|
std::copy(req.info_hash.begin(), req.info_hash.end(), out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// peer_id
|
2006-04-25 23:04:48 +02:00
|
|
|
std::copy(req.pid.begin(), req.pid.end(), out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// downloaded
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int64(req.downloaded, out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// left
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int64(req.left, out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// uploaded
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int64(req.uploaded, out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// event
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int32(req.event, out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// ip address
|
2007-06-13 02:20:06 +02:00
|
|
|
if (m_settings.announce_ip != address() && m_settings.announce_ip.is_v4())
|
|
|
|
detail::write_uint32(m_settings.announce_ip.to_v4().to_ulong(), out);
|
|
|
|
else
|
|
|
|
detail::write_int32(0, out);
|
2005-04-04 18:04:40 +02:00
|
|
|
// key
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int32(req.key, out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// num_want
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int32(req.num_want, out);
|
2004-01-31 11:46:15 +01:00
|
|
|
// port
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_uint16(req.listen_port, out);
|
2005-04-13 23:23:26 +02:00
|
|
|
// extensions
|
|
|
|
detail::write_uint16(0, out);
|
2005-04-05 09:55:27 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-09-14 04:54:15 +02:00
|
|
|
cb->debug_log("==> UDP_TRACKER_ANNOUNCE ["
|
2006-04-25 23:04:48 +02:00
|
|
|
+ lexical_cast<std::string>(req.info_hash) + "]");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_socket.send(asio::buffer(buf), 0, ec);
|
2004-01-31 11:46:15 +01:00
|
|
|
++m_attempts;
|
2007-12-30 02:57:57 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fail(-1, ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-22 06:17:26 +02:00
|
|
|
m_socket.async_receive_from(asio::buffer(m_buffer), m_sender
|
2006-04-25 23:04:48 +02:00
|
|
|
, bind(&udp_tracker_connection::announce_response, self(), _1, _2));
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2005-04-12 13:34:40 +02:00
|
|
|
void udp_tracker_connection::send_udp_scrape()
|
|
|
|
{
|
|
|
|
if (m_transaction_id == 0)
|
2005-04-13 18:55:19 +02:00
|
|
|
m_transaction_id = rand() ^ (rand() << 16);
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2007-10-22 06:17:26 +02:00
|
|
|
if (!m_socket.is_open()) return; // the operation was aborted
|
2006-10-03 00:19:21 +02:00
|
|
|
|
2005-04-12 13:34:40 +02:00
|
|
|
std::vector<char> buf;
|
|
|
|
std::back_insert_iterator<std::vector<char> > out(buf);
|
|
|
|
|
|
|
|
// connection_id
|
|
|
|
detail::write_int64(m_connection_id, out);
|
|
|
|
// action (scrape)
|
2006-04-25 23:04:48 +02:00
|
|
|
detail::write_int32(action_scrape, out);
|
2005-04-12 13:34:40 +02:00
|
|
|
// transaction_id
|
|
|
|
detail::write_int32(m_transaction_id, out);
|
|
|
|
// info_hash
|
2006-04-25 23:04:48 +02:00
|
|
|
std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end(), out);
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2007-12-30 02:57:57 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
m_socket.send(asio::buffer(&buf[0], buf.size()), 0, ec);
|
2005-04-12 13:34:40 +02:00
|
|
|
++m_attempts;
|
2007-12-30 02:57:57 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fail(-1, ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-22 06:17:26 +02:00
|
|
|
m_socket.async_receive_from(asio::buffer(m_buffer), m_sender
|
2006-04-25 23:04:48 +02:00
|
|
|
, bind(&udp_tracker_connection::scrape_response, self(), _1, _2));
|
2005-04-12 13:34:40 +02:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void udp_tracker_connection::announce_response(asio::error_code const& error
|
2007-12-30 02:57:57 +01:00
|
|
|
, std::size_t bytes_transferred)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error == asio::error::operation_aborted) return;
|
2007-10-22 06:17:26 +02:00
|
|
|
if (!m_socket.is_open()) return; // the operation was aborted
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
fail(-1, error.message().c_str());
|
2006-04-25 23:04:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_target != m_sender)
|
|
|
|
{
|
|
|
|
// this packet was not received from the tracker
|
2007-10-22 06:17:26 +02:00
|
|
|
m_socket.async_receive_from(asio::buffer(m_buffer), m_sender
|
2006-04-25 23:04:48 +02:00
|
|
|
, bind(&udp_tracker_connection::connect_response, self(), _1, _2));
|
|
|
|
return;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (bytes_transferred >= udp_buffer_size)
|
|
|
|
{
|
|
|
|
fail(-1, "udp response too big");
|
|
|
|
return;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (bytes_transferred < 8)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "got a message with size < 8");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
restart_read_timeout();
|
|
|
|
char* buf = &m_buffer[0];
|
2004-01-31 11:46:15 +01:00
|
|
|
int action = detail::read_int32(buf);
|
|
|
|
int transaction = detail::read_int32(buf);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
if (transaction != m_transaction_id)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "incorrect transaction id");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (action == action_error)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, std::string(buf, bytes_transferred - 8).c_str());
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (action != action_announce)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "invalid action in announce response");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_transferred < 20)
|
|
|
|
{
|
|
|
|
fail(-1, "got a message with size < 20");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
int interval = detail::read_int32(buf);
|
2005-02-21 14:59:24 +01:00
|
|
|
int incomplete = detail::read_int32(buf);
|
|
|
|
int complete = detail::read_int32(buf);
|
2006-04-25 23:04:48 +02:00
|
|
|
int num_peers = (bytes_transferred - 20) / 6;
|
|
|
|
if ((bytes_transferred - 20) % 6 != 0)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "invalid udp tracker response length");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2006-04-25 23:04:48 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2007-09-14 04:54:15 +02:00
|
|
|
if (cb)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-09-14 04:54:15 +02:00
|
|
|
cb->debug_log("<== UDP_TRACKER_ANNOUNCE_RESPONSE");
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
if (!cb)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
m_man.remove_request(this);
|
|
|
|
return;
|
|
|
|
}
|
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);
|
2006-04-25 23:04:48 +02:00
|
|
|
e.pid.clear();
|
2004-01-31 11:46:15 +01:00
|
|
|
peer_list.push_back(e);
|
|
|
|
}
|
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
cb->tracker_response(tracker_req(), peer_list, interval
|
2005-03-24 13:13:47 +01:00
|
|
|
, complete, incomplete);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
m_man.remove_request(this);
|
2007-10-26 09:14:19 +02:00
|
|
|
close();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void udp_tracker_connection::scrape_response(asio::error_code const& error
|
2007-12-30 02:57:57 +01:00
|
|
|
, std::size_t bytes_transferred)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
if (error == asio::error::operation_aborted) return;
|
2007-10-22 06:17:26 +02:00
|
|
|
if (!m_socket.is_open()) return; // the operation was aborted
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error)
|
2005-04-12 13:34:40 +02:00
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
fail(-1, error.message().c_str());
|
2006-04-25 23:04:48 +02:00
|
|
|
return;
|
2005-04-12 13:34:40 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_target != m_sender)
|
2005-04-12 13:34:40 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
// this packet was not received from the tracker
|
2007-10-22 06:17:26 +02:00
|
|
|
m_socket.async_receive_from(asio::buffer(m_buffer), m_sender
|
2006-04-25 23:04:48 +02:00
|
|
|
, bind(&udp_tracker_connection::connect_response, self(), _1, _2));
|
|
|
|
return;
|
2005-04-12 13:34:40 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (bytes_transferred >= udp_buffer_size)
|
2005-04-12 13:34:40 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "udp response too big");
|
|
|
|
return;
|
2005-04-12 13:34:40 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (bytes_transferred < 8)
|
2005-04-12 13:34:40 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "got a message with size < 8");
|
|
|
|
return;
|
2005-04-12 13:34:40 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
restart_read_timeout();
|
|
|
|
char* buf = &m_buffer[0];
|
|
|
|
int action = detail::read_int32(buf);
|
|
|
|
int transaction = detail::read_int32(buf);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (transaction != m_transaction_id)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "incorrect transaction id");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (action == action_error)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, std::string(buf, bytes_transferred - 8).c_str());
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (action != action_scrape)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "invalid action in announce response");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (bytes_transferred < 20)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
fail(-1, "got a message with size < 20");
|
|
|
|
return;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
int complete = detail::read_int32(buf);
|
2007-11-20 23:46:27 +01:00
|
|
|
int downloaded = detail::read_int32(buf);
|
2006-04-25 23:04:48 +02:00
|
|
|
int incomplete = detail::read_int32(buf);
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (!cb)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-10-26 09:14:19 +02:00
|
|
|
close();
|
2006-04-25 23:04:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-20 23:46:27 +01:00
|
|
|
cb->tracker_scrape_response(tracker_req()
|
|
|
|
, complete, incomplete, downloaded);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
m_man.remove_request(this);
|
2007-10-26 09:14:19 +02:00
|
|
|
close();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2005-03-19 13:22:40 +01:00
|
|
|
|