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>
|
|
|
|
#include <cctype>
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
2012-08-23 23:02:09 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2009-01-28 06:49:21 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/tracker_manager.hpp"
|
2008-05-17 16:19:34 +02:00
|
|
|
#include "libtorrent/parse_url.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/udp_tracker_connection.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
2009-01-27 07:17:55 +01:00
|
|
|
#include "libtorrent/escape_string.hpp"
|
2010-08-20 09:01:11 +02:00
|
|
|
#include "libtorrent/broadcast_socket.hpp" // for is_any
|
2011-02-26 08:55:51 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2014-10-21 02:28:51 +02:00
|
|
|
#include "libtorrent/aux_/session_settings.hpp"
|
|
|
|
#include "libtorrent/resolver_interface.hpp"
|
|
|
|
#include "libtorrent/ip_filter.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2014-10-21 04:08:46 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
|
|
|
#endif
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2010-02-06 09:14:18 +01:00
|
|
|
std::map<address, udp_tracker_connection::connection_cache_entry>
|
|
|
|
udp_tracker_connection::m_connection_cache;
|
|
|
|
|
2010-02-18 05:37:02 +01:00
|
|
|
mutex udp_tracker_connection::m_cache_mutex;
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
udp_tracker_connection::udp_tracker_connection(
|
2008-01-08 06:47:43 +01:00
|
|
|
io_service& ios
|
2006-04-25 23:04:48 +02:00
|
|
|
, tracker_manager& man
|
|
|
|
, tracker_request const& req
|
2014-10-21 02:28:51 +02:00
|
|
|
, boost::weak_ptr<request_callback> c)
|
2009-05-15 23:23:41 +02:00
|
|
|
: tracker_connection(man, req, ios, c)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_transaction_id(0)
|
2004-01-31 11:46:15 +01:00
|
|
|
, m_attempts(0)
|
2008-02-05 07:32:10 +01:00
|
|
|
, m_state(action_error)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_abort(false)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2014-10-20 22:44:05 +02:00
|
|
|
update_transaction_id();
|
2008-09-07 12:03:59 +02:00
|
|
|
}
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2008-09-07 12:03:59 +02:00
|
|
|
void udp_tracker_connection::start()
|
|
|
|
{
|
2015-02-01 15:30:43 +01:00
|
|
|
// TODO: 2 support authentication here. tracker_req().auth
|
2008-02-05 07:32:10 +01:00
|
|
|
std::string hostname;
|
2013-02-26 06:57:29 +01:00
|
|
|
std::string protocol;
|
2008-02-05 07:32:10 +01:00
|
|
|
int port;
|
2009-06-12 18:40:38 +02:00
|
|
|
error_code ec;
|
2008-02-05 07:32:10 +01:00
|
|
|
|
|
|
|
using boost::tuples::ignore;
|
2013-02-26 06:57:29 +01:00
|
|
|
boost::tie(protocol, ignore, hostname, port, ignore)
|
2009-06-12 18:40:38 +02:00
|
|
|
= parse_url_components(tracker_req().url, ec);
|
2013-02-26 06:57:29 +01:00
|
|
|
if (port == -1) port = protocol == "http" ? 80 : 443;
|
2008-05-17 16:19:34 +02:00
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
if (ec)
|
2008-05-17 16:19:34 +02:00
|
|
|
{
|
2013-11-03 00:08:26 +01:00
|
|
|
tracker_connection::fail(ec);
|
2008-05-17 16:19:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-21 02:28:51 +02:00
|
|
|
aux::session_settings const& settings = m_man.settings();
|
2008-10-22 21:40:32 +02:00
|
|
|
|
2014-10-21 02:28:51 +02:00
|
|
|
if (settings.get_bool(settings_pack::proxy_hostnames)
|
|
|
|
&& (settings.get_int(settings_pack::proxy_type) == settings_pack::socks5
|
|
|
|
|| settings.get_int(settings_pack::proxy_type) == settings_pack::socks5_pw))
|
2010-08-03 11:08:37 +02:00
|
|
|
{
|
|
|
|
m_hostname = hostname;
|
|
|
|
m_target.port(port);
|
|
|
|
start_announce();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("udp_tracker_connection::name_lookup");
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
// when stopping, pass in the prefer cache flag, because we
|
|
|
|
// don't want to get stuck on DNS lookups when shutting down
|
|
|
|
// if we can avoid it
|
2014-10-21 02:28:51 +02:00
|
|
|
m_man.host_resolver().async_resolve(hostname
|
2014-07-06 21:18:00 +02:00
|
|
|
, tracker_req().event == tracker_request::stopped
|
|
|
|
? resolver_interface::prefer_cache
|
2014-12-17 03:44:27 +01:00
|
|
|
: resolver_interface::abort_on_shutdown
|
2014-07-06 21:18:00 +02:00
|
|
|
, boost::bind(&udp_tracker_connection::name_lookup
|
2014-10-20 22:44:05 +02:00
|
|
|
, shared_from_this(), _1, _2, port));
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2010-08-03 11:08:37 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2012-09-28 01:04:51 +02:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER [ initiating name lookup: \"%s\" ]"
|
|
|
|
, hostname.c_str());
|
2010-08-03 11:08:37 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-09-07 12:03:59 +02:00
|
|
|
set_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)
|
|
|
|
, settings.get_int(settings_pack::tracker_receive_timeout));
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2012-10-01 02:13:58 +02:00
|
|
|
void udp_tracker_connection::fail(error_code const& ec, int code
|
|
|
|
, char const* msg, int interval, int min_interval)
|
|
|
|
{
|
|
|
|
// m_target failed. remove it from the endpoint list
|
2014-10-21 22:49:36 +02:00
|
|
|
std::vector<tcp::endpoint>::iterator i = std::find(m_endpoints.begin()
|
2012-10-01 02:13:58 +02:00
|
|
|
, m_endpoints.end(), tcp::endpoint(m_target.address(), m_target.port()));
|
|
|
|
|
|
|
|
if (i != m_endpoints.end()) m_endpoints.erase(i);
|
|
|
|
|
|
|
|
// if that was the last one, fail the whole announce
|
|
|
|
if (m_endpoints.empty())
|
2012-10-01 04:19:00 +02:00
|
|
|
{
|
2012-10-01 02:13:58 +02:00
|
|
|
tracker_connection::fail(ec, code, msg, interval, min_interval);
|
2012-10-01 04:19:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2012-10-01 04:19:00 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER [ host: \"%s\" ip: \"%s\" | error: \"%s\" ]"
|
|
|
|
, m_hostname.c_str(), print_endpoint(m_target).c_str(), ec.message().c_str());
|
|
|
|
#endif
|
2012-10-01 02:13:58 +02:00
|
|
|
|
|
|
|
// pick another target endpoint and try again
|
|
|
|
m_target = pick_target_endpoint();
|
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2012-10-01 04:19:00 +02:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER trying next IP [ host: \"%s\" ip: \"%s\" ]"
|
|
|
|
, m_hostname.c_str(), print_endpoint(m_target).c_str());
|
|
|
|
#endif
|
2014-10-21 02:28:51 +02:00
|
|
|
get_io_service().post(boost::bind(
|
2014-10-20 22:44:05 +02:00
|
|
|
&udp_tracker_connection::start_announce, shared_from_this()));
|
2014-12-30 10:02:20 +01:00
|
|
|
|
2014-12-30 17:56:03 +01:00
|
|
|
aux::session_settings const& settings = m_man.settings();
|
2014-12-30 10:02:20 +01:00
|
|
|
set_timeout(tracker_req().event == tracker_request::stopped
|
2014-12-30 17:56:03 +01:00
|
|
|
? settings.get_int(settings_pack::stop_tracker_timeout)
|
|
|
|
: settings.get_int(settings_pack::tracker_completion_timeout)
|
|
|
|
, settings.get_int(settings_pack::tracker_receive_timeout));
|
2012-10-01 02:13:58 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void udp_tracker_connection::name_lookup(error_code const& error
|
2014-07-06 21:18:00 +02:00
|
|
|
, std::vector<address> const& addresses, int port)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("udp_tracker_connection::name_lookup");
|
|
|
|
#endif
|
2010-05-30 03:33:03 +02:00
|
|
|
if (m_abort) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error == asio::error::operation_aborted) return;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (error || addresses.empty())
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error);
|
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();
|
2014-12-09 10:08:26 +01:00
|
|
|
#if 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
|
2010-03-02 10:04:27 +01:00
|
|
|
if (cancelled())
|
|
|
|
{
|
2010-03-02 10:12:34 +01:00
|
|
|
fail(error_code(errors::torrent_aborted));
|
2010-03-02 10:04:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
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.
|
2009-05-15 23:23:41 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<address>::const_iterator i = addresses.begin()
|
|
|
|
, end(addresses.end()); i != end; ++i)
|
|
|
|
m_endpoints.push_back(tcp::endpoint(*i, port));
|
2009-05-15 23:23:41 +02:00
|
|
|
|
2011-03-04 07:55:39 +01:00
|
|
|
if (tracker_req().apply_ip_filter)
|
2007-03-02 19:40:02 +01:00
|
|
|
{
|
2011-03-04 07:55:39 +01:00
|
|
|
// remove endpoints that are filtered by the IP filter
|
2014-10-21 22:49:36 +02:00
|
|
|
for (std::vector<tcp::endpoint>::iterator k = m_endpoints.begin();
|
2011-03-04 07:55:39 +01:00
|
|
|
k != m_endpoints.end();)
|
2010-06-06 04:31:20 +02:00
|
|
|
{
|
2014-10-21 02:28:51 +02:00
|
|
|
if (m_man.ip_filter().access(k->address()) == ip_filter::blocked)
|
2011-03-04 07:55:39 +01:00
|
|
|
{
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2012-09-28 01:04:51 +02:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER [ IP blocked by filter: %s ]"
|
|
|
|
, print_address(k->address()).c_str());
|
2010-06-06 04:31:20 +02:00
|
|
|
#endif
|
2011-03-04 07:55:39 +01:00
|
|
|
k = m_endpoints.erase(k);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++k;
|
2010-06-06 04:31:20 +02:00
|
|
|
}
|
2007-03-02 19:40:02 +01:00
|
|
|
}
|
2008-10-22 21:40:32 +02:00
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
// if all endpoints were filtered by the IP filter, we can't connect
|
2009-05-15 23:23:41 +02:00
|
|
|
if (m_endpoints.empty())
|
2008-10-22 21:40:32 +02:00
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::banned_by_ip_filter));
|
2008-10-22 21:40:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-03-02 19:40:02 +01:00
|
|
|
|
2012-10-01 02:13:58 +02:00
|
|
|
m_target = pick_target_endpoint();
|
|
|
|
|
|
|
|
start_announce();
|
|
|
|
}
|
|
|
|
|
|
|
|
udp::endpoint udp_tracker_connection::pick_target_endpoint() const
|
|
|
|
{
|
2014-10-21 22:49:36 +02:00
|
|
|
std::vector<tcp::endpoint>::const_iterator iter = m_endpoints.begin();
|
2012-10-01 02:13:58 +02:00
|
|
|
udp::endpoint target = udp::endpoint(iter->address(), iter->port());
|
2009-05-15 23:23:41 +02:00
|
|
|
|
|
|
|
if (bind_interface() != address_v4::any())
|
2007-12-30 02:57:57 +01:00
|
|
|
{
|
2009-05-15 23:23:41 +02:00
|
|
|
// find first endpoint that matches our bind interface type
|
|
|
|
for (; iter != m_endpoints.end() && iter->address().is_v4()
|
|
|
|
!= bind_interface().is_v4(); ++iter);
|
|
|
|
|
|
|
|
if (iter == m_endpoints.end())
|
|
|
|
{
|
2012-10-01 02:13:58 +02:00
|
|
|
TORRENT_ASSERT(target.address().is_v4() != bind_interface().is_v4());
|
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2009-05-15 23:23:41 +02:00
|
|
|
if (cb)
|
|
|
|
{
|
2012-10-01 02:13:58 +02:00
|
|
|
char const* tracker_address_type = target.address().is_v4() ? "IPv4" : "IPv6";
|
2009-05-15 23:23:41 +02:00
|
|
|
char const* bind_address_type = bind_interface().is_v4() ? "IPv4" : "IPv6";
|
|
|
|
char msg[200];
|
|
|
|
snprintf(msg, sizeof(msg)
|
|
|
|
, "the tracker only resolves to an %s address, and you're "
|
|
|
|
"listening on an %s socket. This may prevent you from receiving "
|
|
|
|
"incoming connections."
|
|
|
|
, tracker_address_type, bind_address_type);
|
|
|
|
|
|
|
|
cb->tracker_warning(tracker_req(), msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-01 02:13:58 +02:00
|
|
|
target = udp::endpoint(iter->address(), iter->port());
|
2009-05-15 23:23:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 02:13:58 +02:00
|
|
|
return target;
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void udp_tracker_connection::start_announce()
|
|
|
|
{
|
2010-02-18 05:37:02 +01:00
|
|
|
mutex::scoped_lock l(m_cache_mutex);
|
2010-02-06 09:14:18 +01:00
|
|
|
std::map<address, connection_cache_entry>::iterator cc
|
|
|
|
= m_connection_cache.find(m_target.address());
|
|
|
|
if (cc != m_connection_cache.end())
|
2009-05-15 23:23:41 +02:00
|
|
|
{
|
2010-02-06 09:14:18 +01:00
|
|
|
// we found a cached entry! Now, we can only
|
|
|
|
// use if if it hasn't expired
|
|
|
|
if (time_now() < cc->second.expires)
|
2009-05-15 23:23:41 +02:00
|
|
|
{
|
2010-02-06 09:14:18 +01:00
|
|
|
if (tracker_req().kind == tracker_request::announce_request)
|
|
|
|
send_udp_announce();
|
|
|
|
else if (tracker_req().kind == tracker_request::scrape_request)
|
|
|
|
send_udp_scrape();
|
2009-05-15 23:23:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-02-06 09:14:18 +01:00
|
|
|
// if it expired, remove it from the cache
|
|
|
|
m_connection_cache.erase(cc);
|
2007-12-30 02:57:57 +01:00
|
|
|
}
|
2010-02-18 05:37:02 +01:00
|
|
|
l.unlock();
|
2010-02-06 09:14:18 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
send_udp_connect();
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2010-11-18 02:06:33 +01:00
|
|
|
void udp_tracker_connection::on_timeout(error_code const& ec)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2010-11-18 02:06:33 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fail(ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2008-01-06 23:14:00 +01:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2012-09-28 01:04:51 +02:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER [ timed out url: %s ]", tracker_req().url.c_str());
|
2008-01-06 23:14:00 +01:00
|
|
|
#endif
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::timed_out));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
void udp_tracker_connection::close()
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-10-26 09:14:19 +02:00
|
|
|
tracker_connection::close();
|
|
|
|
}
|
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
bool udp_tracker_connection::on_receive_hostname(error_code const& e
|
|
|
|
, char const* hostname, char const* buf, int size)
|
|
|
|
{
|
|
|
|
// just ignore the hostname this came from, pretend that
|
|
|
|
// it's from the same endpoint we sent it to (i.e. the same
|
|
|
|
// port). We have so many other ways of confirming this packet
|
|
|
|
// comes from the tracker anyway, so it's not a big deal
|
|
|
|
return on_receive(e, m_target, buf, size);
|
|
|
|
}
|
|
|
|
|
2010-05-30 03:33:03 +02:00
|
|
|
bool udp_tracker_connection::on_receive(error_code const& e
|
2008-03-25 05:46:18 +01:00
|
|
|
, udp::endpoint const& ep, char const* buf, int size)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2015-01-13 22:34:43 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
|
|
|
#endif
|
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
// ignore resposes before we've sent any requests
|
2015-01-13 22:34:43 +01:00
|
|
|
if (m_state == action_error)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb) cb->debug_log("<== UDP_TRACKER [ m_action == error ]");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2015-01-13 22:34:43 +01:00
|
|
|
if (m_abort)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb) cb->debug_log("<== UDP_TRACKER [ aborted]");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2005-08-09 01:32:38 +02:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
// ignore packet not sent from the tracker
|
2010-08-20 09:01:11 +02:00
|
|
|
// if m_target is inaddr_any, it suggests that we
|
|
|
|
// sent the packet through a proxy only knowing
|
|
|
|
// the hostname, in which case this packet might be for us
|
2015-01-13 22:34:43 +01:00
|
|
|
if (!is_any(m_target.address()) && m_target != ep)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb) cb->debug_log("<== UDP_TRACKER [ unexpected source IP: %s "
|
|
|
|
"expected: %s ]"
|
|
|
|
, print_endpoint(ep).c_str()
|
|
|
|
, print_endpoint(m_target).c_str());
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-25 05:46:18 +01:00
|
|
|
|
2010-02-23 22:53:45 +01:00
|
|
|
if (e) fail(e);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2015-01-13 22:34:43 +01:00
|
|
|
if (cb) cb->debug_log("<== UDP_TRACKER_PACKET [ size: %d ]", size);
|
2008-02-05 07:32:10 +01:00
|
|
|
#endif
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
// ignore packets smaller than 8 bytes
|
2010-05-30 03:33:03 +02:00
|
|
|
if (size < 8) return false;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
const char* ptr = buf;
|
2006-04-25 23:04:48 +02:00
|
|
|
int action = detail::read_int32(ptr);
|
2014-10-20 22:44:05 +02:00
|
|
|
boost::uint32_t transaction = detail::read_uint32(ptr);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2015-01-13 22:34:43 +01:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER_PACKET [ action: %d ]", action);
|
2008-02-05 07:32:10 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
// ignore packets with incorrect transaction id
|
2015-01-13 22:34:43 +01:00
|
|
|
if (m_transaction_id != transaction)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER_PACKET [ tid: %x ]"
|
|
|
|
, int(transaction));
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
if (action == action_error)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::tracker_failure), -1, std::string(ptr, size - 8).c_str());
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
// ignore packets that's not a response to our message
|
2015-01-13 22:34:43 +01:00
|
|
|
if (action != m_state)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER_PACKET [ unexpected action: %d "
|
|
|
|
" expected: %d ]", action, m_state);
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-11-16 07:41:35 +01:00
|
|
|
restart_read_timeout();
|
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2007-09-14 04:54:15 +02:00
|
|
|
if (cb)
|
2012-09-28 01:04:51 +02:00
|
|
|
cb->debug_log("*** UDP_TRACKER_RESPONSE [ tid: %x ]"
|
2010-02-13 17:29:17 +01:00
|
|
|
, int(transaction));
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
switch (m_state)
|
|
|
|
{
|
|
|
|
case action_connect:
|
2010-05-30 03:33:03 +02:00
|
|
|
return on_connect_response(buf, size);
|
2008-02-05 07:32:10 +01:00
|
|
|
case action_announce:
|
2010-05-30 03:33:03 +02:00
|
|
|
return on_announce_response(buf, size);
|
2008-02-05 07:32:10 +01:00
|
|
|
case action_scrape:
|
2010-05-30 03:33:03 +02:00
|
|
|
return on_scrape_response(buf, size);
|
2008-02-05 07:32:10 +01:00
|
|
|
default: break;
|
|
|
|
}
|
2010-05-30 03:33:03 +02:00
|
|
|
return false;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2014-10-20 22:44:05 +02:00
|
|
|
|
|
|
|
void udp_tracker_connection::update_transaction_id()
|
|
|
|
{
|
|
|
|
boost::uint32_t new_tid;
|
|
|
|
|
|
|
|
// don't use 0, because that has special meaning (unintialized)
|
|
|
|
do {
|
|
|
|
new_tid = random();
|
|
|
|
} while (new_tid == 0);
|
|
|
|
|
|
|
|
if (m_transaction_id != 0)
|
|
|
|
m_man.update_transaction_id(shared_from_this(), new_tid);
|
|
|
|
m_transaction_id = new_tid;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-05-30 03:33:03 +02:00
|
|
|
bool udp_tracker_connection::on_connect_response(char const* buf, int size)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2008-02-05 07:32:10 +01:00
|
|
|
// ignore packets smaller than 16 bytes
|
2010-05-30 03:33:03 +02:00
|
|
|
if (size < 16) return false;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
restart_read_timeout();
|
|
|
|
buf += 8; // skip header
|
2006-10-03 00:19:21 +02:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
// reset transaction
|
2014-10-20 22:44:05 +02:00
|
|
|
update_transaction_id();
|
2010-02-06 09:14:18 +01:00
|
|
|
boost::uint64_t connection_id = detail::read_int64(buf);
|
2010-02-18 05:37:02 +01:00
|
|
|
|
|
|
|
mutex::scoped_lock l(m_cache_mutex);
|
2010-02-06 09:14:18 +01:00
|
|
|
connection_cache_entry& cce = m_connection_cache[m_target.address()];
|
|
|
|
cce.connection_id = connection_id;
|
2014-10-21 02:28:51 +02:00
|
|
|
cce.expires = time_now() + seconds(m_man.settings().get_int(settings_pack::udp_tracker_token_expiry));
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
if (tracker_req().kind == tracker_request::announce_request)
|
|
|
|
send_udp_announce();
|
|
|
|
else if (tracker_req().kind == tracker_request::scrape_request)
|
|
|
|
send_udp_scrape();
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
2008-02-05 07:32:10 +01:00
|
|
|
}
|
2005-04-05 09:55:27 +02:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
void udp_tracker_connection::send_udp_connect()
|
|
|
|
{
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2015-01-14 02:59:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m_abort)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2015-01-14 02:59:23 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb) cb->debug_log("==> UDP_TRACKER_CONNECT [ skipped, m_abort ]");
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2015-01-14 02:59:23 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-02-05 07:32:10 +01:00
|
|
|
|
|
|
|
char buf[16];
|
|
|
|
char* ptr = buf;
|
|
|
|
|
2014-10-20 22:44:05 +02:00
|
|
|
TORRENT_ASSERT(m_transaction_id != 0);
|
2008-02-05 07:32:10 +01:00
|
|
|
|
|
|
|
detail::write_uint32(0x417, ptr);
|
|
|
|
detail::write_uint32(0x27101980, ptr); // connection_id
|
|
|
|
detail::write_int32(action_connect, ptr); // action (connect)
|
|
|
|
detail::write_int32(m_transaction_id, ptr); // transaction_id
|
|
|
|
TORRENT_ASSERT(ptr - buf == sizeof(buf));
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2010-08-03 11:08:37 +02:00
|
|
|
if (!m_hostname.empty())
|
|
|
|
{
|
2015-02-08 01:59:54 +01:00
|
|
|
m_man.get_udp_socket().send_hostname(m_hostname.c_str()
|
2015-01-14 02:59:23 +01:00
|
|
|
, m_target.port(), buf, 16, ec);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-08 01:59:54 +01:00
|
|
|
m_man.get_udp_socket().send(m_target, buf, 16, ec);
|
2015-01-14 02:59:23 +01:00
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2015-01-14 02:59:23 +01:00
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
++m_attempts;
|
2007-12-30 02:57:57 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2015-01-14 02:59:23 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb) cb->debug_log("==> UDP_TRACKER_CONNECT [ failed: %s ]"
|
|
|
|
, ec.message().c_str());
|
|
|
|
#endif
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(ec);
|
2007-12-30 02:57:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-01-14 02:59:23 +01:00
|
|
|
|
|
|
|
#if defined TORRENT_LOGGING
|
|
|
|
if (cb)
|
|
|
|
{
|
|
|
|
char hex_ih[41];
|
|
|
|
to_hex((char const*)&tracker_req().info_hash[0], 20, hex_ih);
|
|
|
|
cb->debug_log("==> UDP_TRACKER_CONNECT [ to: %s ih: %s]"
|
|
|
|
, m_hostname.empty()
|
|
|
|
? print_endpoint(m_target).c_str()
|
|
|
|
: (m_hostname + ":" + to_string(m_target.port()).elems).c_str()
|
|
|
|
, hex_ih);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_state = action_connect;
|
|
|
|
sent_bytes(16 + 28); // assuming UDP/IP header
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2005-04-12 13:34:40 +02:00
|
|
|
void udp_tracker_connection::send_udp_scrape()
|
|
|
|
{
|
2010-05-30 03:33:03 +02:00
|
|
|
if (m_abort) return;
|
2006-10-03 00:19:21 +02:00
|
|
|
|
2010-02-06 09:14:18 +01:00
|
|
|
std::map<address, connection_cache_entry>::iterator i
|
|
|
|
= m_connection_cache.find(m_target.address());
|
|
|
|
// this isn't really supposed to happen
|
|
|
|
TORRENT_ASSERT(i != m_connection_cache.end());
|
|
|
|
if (i == m_connection_cache.end()) return;
|
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
char buf[8 + 4 + 4 + 20];
|
|
|
|
char* out = buf;
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2010-02-06 09:14:18 +01:00
|
|
|
detail::write_int64(i->second.connection_id, out); // connection_id
|
2008-02-05 07:32:10 +01:00
|
|
|
detail::write_int32(action_scrape, out); // action (scrape)
|
|
|
|
detail::write_int32(m_transaction_id, out); // transaction_id
|
2005-04-12 13:34:40 +02:00
|
|
|
// info_hash
|
2006-04-25 23:04:48 +02:00
|
|
|
std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end(), out);
|
2013-06-14 18:41:47 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-02-25 09:50:07 +01:00
|
|
|
out += 20;
|
|
|
|
TORRENT_ASSERT(out - buf == sizeof(buf));
|
2013-06-14 18:41:47 +02:00
|
|
|
#endif
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2010-08-03 11:08:37 +02:00
|
|
|
if (!m_hostname.empty())
|
|
|
|
{
|
2015-02-08 01:59:54 +01:00
|
|
|
m_man.get_udp_socket().send_hostname(m_hostname.c_str(), m_target.port(), buf, sizeof(buf), ec);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-08 01:59:54 +01:00
|
|
|
m_man.get_udp_socket().send(m_target, buf, sizeof(buf), ec);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2008-02-05 07:32:10 +01:00
|
|
|
m_state = action_scrape;
|
2008-09-22 02:15:05 +02:00
|
|
|
sent_bytes(sizeof(buf) + 28); // assuming UDP/IP header
|
2005-04-12 13:34:40 +02:00
|
|
|
++m_attempts;
|
2007-12-30 02:57:57 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(ec);
|
2007-12-30 02:57:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2005-04-12 13:34:40 +02:00
|
|
|
}
|
|
|
|
|
2010-05-30 03:33:03 +02:00
|
|
|
bool udp_tracker_connection::on_announce_response(char const* buf, int size)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2010-05-30 03:33:03 +02:00
|
|
|
if (size < 20) return false;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
buf += 8; // skip header
|
|
|
|
restart_read_timeout();
|
2014-09-28 08:36:03 +02:00
|
|
|
|
|
|
|
tracker_response resp;
|
|
|
|
|
|
|
|
resp.interval = detail::read_int32(buf);
|
|
|
|
resp.min_interval = 60;
|
|
|
|
resp.incomplete = detail::read_int32(buf);
|
|
|
|
resp.complete = detail::read_int32(buf);
|
2008-02-05 07:32:10 +01:00
|
|
|
int num_peers = (size - 20) / 6;
|
|
|
|
if ((size - 20) % 6 != 0)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::invalid_tracker_response_length));
|
2010-05-30 03:33:03 +02:00
|
|
|
return false;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2007-09-14 04:54:15 +02:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2007-09-14 04:54:15 +02:00
|
|
|
if (cb)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-12-30 18:37:38 +01:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2012-09-28 01:04:51 +02:00
|
|
|
cb->debug_log("<== UDP_TRACKER_RESPONSE [ url: %s ]", tracker_req().url.c_str());
|
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
|
|
|
{
|
2010-11-16 07:41:35 +01:00
|
|
|
close();
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
|
|
|
std::vector<peer_entry> peer_list;
|
2014-09-28 08:36:03 +02:00
|
|
|
resp.peers4.reserve(num_peers);
|
2004-01-31 11:46:15 +01:00
|
|
|
for (int i = 0; i < num_peers; ++i)
|
|
|
|
{
|
2014-09-28 08:36:03 +02:00
|
|
|
ipv4_peer_entry e;
|
|
|
|
memcpy(&e.ip[0], buf, 4);
|
|
|
|
buf += 4;
|
2004-01-31 11:46:15 +01:00
|
|
|
e.port = detail::read_uint16(buf);
|
2014-09-28 08:36:03 +02:00
|
|
|
resp.peers4.push_back(e);
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2009-05-15 23:23:41 +02:00
|
|
|
std::list<address> ip_list;
|
2014-10-21 22:49:36 +02:00
|
|
|
for (std::vector<tcp::endpoint>::const_iterator i = m_endpoints.begin()
|
2009-09-06 02:57:01 +02:00
|
|
|
, end(m_endpoints.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
ip_list.push_back(i->address());
|
|
|
|
}
|
2009-05-15 23:23:41 +02:00
|
|
|
|
|
|
|
cb->tracker_response(tracker_req(), m_target.address(), ip_list
|
2014-09-28 08:36:03 +02:00
|
|
|
, resp);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
close();
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2010-05-30 03:33:03 +02:00
|
|
|
bool udp_tracker_connection::on_scrape_response(char const* buf, int size)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
restart_read_timeout();
|
|
|
|
int action = detail::read_int32(buf);
|
2014-10-20 22:44:05 +02:00
|
|
|
boost::uint32_t transaction = detail::read_uint32(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
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::invalid_tracker_transaction_id));
|
2010-05-30 03:33:03 +02:00
|
|
|
return false;
|
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
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::tracker_failure), -1, std::string(buf, size - 8).c_str());
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
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
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::invalid_tracker_action));
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
if (size < 20)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(error_code(errors::invalid_tracker_response_length));
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
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();
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 23:46:27 +01:00
|
|
|
cb->tracker_scrape_response(tracker_req()
|
2010-09-06 06:02:15 +02:00
|
|
|
, complete, incomplete, downloaded, -1);
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
close();
|
2010-05-30 03:33:03 +02:00
|
|
|
return true;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-02-05 07:32:10 +01:00
|
|
|
|
|
|
|
void udp_tracker_connection::send_udp_announce()
|
|
|
|
{
|
2010-05-30 03:33:03 +02:00
|
|
|
if (m_abort) return;
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2012-08-23 23:02:09 +02:00
|
|
|
char buf[800];
|
2008-02-05 07:32:10 +01:00
|
|
|
char* out = buf;
|
|
|
|
|
|
|
|
tracker_request const& req = tracker_req();
|
2009-09-25 20:17:59 +02:00
|
|
|
const bool stats = req.send_stats;
|
2014-10-21 02:28:51 +02:00
|
|
|
aux::session_settings const& settings = m_man.settings();
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2010-02-06 09:14:18 +01:00
|
|
|
std::map<address, connection_cache_entry>::iterator i
|
|
|
|
= m_connection_cache.find(m_target.address());
|
|
|
|
// this isn't really supposed to happen
|
|
|
|
TORRENT_ASSERT(i != m_connection_cache.end());
|
|
|
|
if (i == m_connection_cache.end()) return;
|
|
|
|
|
|
|
|
detail::write_int64(i->second.connection_id, out); // connection_id
|
2008-02-05 07:32:10 +01:00
|
|
|
detail::write_int32(action_announce, out); // action (announce)
|
|
|
|
detail::write_int32(m_transaction_id, out); // transaction_id
|
|
|
|
std::copy(req.info_hash.begin(), req.info_hash.end(), out); // info_hash
|
2008-02-25 09:50:07 +01:00
|
|
|
out += 20;
|
2008-02-05 07:32:10 +01:00
|
|
|
std::copy(req.pid.begin(), req.pid.end(), out); // peer_id
|
2008-02-25 09:50:07 +01:00
|
|
|
out += 20;
|
2009-09-25 20:17:59 +02:00
|
|
|
detail::write_int64(stats ? req.downloaded : 0, out); // downloaded
|
|
|
|
detail::write_int64(stats ? req.left : 0, out); // left
|
|
|
|
detail::write_int64(stats ? req.uploaded : 0, out); // uploaded
|
2008-02-05 07:32:10 +01:00
|
|
|
detail::write_int32(req.event, out); // event
|
|
|
|
// ip address
|
2009-11-23 09:38:50 +01:00
|
|
|
address_v4 announce_ip;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!settings.get_bool(settings_pack::anonymous_mode)
|
|
|
|
&& !settings.get_str(settings_pack::announce_ip).empty())
|
2009-11-23 09:38:50 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
2014-07-06 21:18:00 +02:00
|
|
|
address ip = address::from_string(settings.get_str(settings_pack::announce_ip).c_str(), ec);
|
2009-11-23 09:38:50 +01:00
|
|
|
if (!ec && ip.is_v4()) announce_ip = ip.to_v4();
|
|
|
|
}
|
|
|
|
detail::write_uint32(announce_ip.to_ulong(), out);
|
2008-02-05 07:32:10 +01:00
|
|
|
detail::write_int32(req.key, out); // key
|
|
|
|
detail::write_int32(req.num_want, out); // num_want
|
|
|
|
detail::write_uint16(req.listen_port, out); // port
|
|
|
|
|
2012-08-23 23:02:09 +02:00
|
|
|
std::string request_string;
|
|
|
|
error_code ec;
|
|
|
|
using boost::tuples::ignore;
|
2015-02-01 15:30:43 +01:00
|
|
|
boost::tie(ignore, ignore, ignore, ignore, request_string)
|
|
|
|
= parse_url_components(req.url, ec);
|
2012-08-23 23:02:09 +02:00
|
|
|
if (ec) request_string.clear();
|
|
|
|
|
|
|
|
if (!request_string.empty())
|
|
|
|
{
|
2012-09-18 04:03:24 +02:00
|
|
|
int str_len = (std::min)(int(request_string.size()), 255);
|
|
|
|
request_string.resize(str_len);
|
|
|
|
|
|
|
|
detail::write_uint8(2, out);
|
|
|
|
detail::write_uint8(str_len, out);
|
2012-08-23 23:02:09 +02:00
|
|
|
detail::write_string(request_string, out);
|
|
|
|
}
|
|
|
|
|
2014-07-05 16:10:25 +02:00
|
|
|
TORRENT_ASSERT(out - buf <= int(sizeof(buf)));
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2014-12-09 10:08:26 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2008-02-05 07:32:10 +01:00
|
|
|
boost::shared_ptr<request_callback> cb = requester();
|
2009-04-24 19:04:20 +02:00
|
|
|
if (cb)
|
|
|
|
{
|
|
|
|
char hex_ih[41];
|
|
|
|
to_hex((char const*)&req.info_hash[0], 20, hex_ih);
|
2012-09-28 01:04:51 +02:00
|
|
|
cb->debug_log("==> UDP_TRACKER_ANNOUNCE [%s]", hex_ih);
|
2009-04-24 19:04:20 +02:00
|
|
|
}
|
2008-02-05 07:32:10 +01:00
|
|
|
#endif
|
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
if (!m_hostname.empty())
|
|
|
|
{
|
2015-02-08 01:59:54 +01:00
|
|
|
m_man.get_udp_socket().send_hostname(m_hostname.c_str()
|
2015-01-14 02:59:23 +01:00
|
|
|
, m_target.port(), buf, out - buf, ec);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-08 01:59:54 +01:00
|
|
|
m_man.get_udp_socket().send(m_target, buf, out - buf, ec);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2008-02-05 07:32:10 +01:00
|
|
|
m_state = action_announce;
|
2012-08-23 23:02:09 +02:00
|
|
|
sent_bytes(out - buf + 28); // assuming UDP/IP header
|
2008-02-05 07:32:10 +01:00
|
|
|
++m_attempts;
|
|
|
|
if (ec)
|
|
|
|
{
|
2010-02-23 22:53:45 +01:00
|
|
|
fail(ec);
|
2008-02-05 07:32:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-31 11:46:15 +01:00
|
|
|
}
|
2005-03-19 13:22:40 +01:00
|
|
|
|