2004-01-31 11:46:15 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, 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 <cctype>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <tuple>
|
2009-01-28 06:49:21 +01:00
|
|
|
|
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"
|
2015-03-15 00:10:20 +01:00
|
|
|
#include "libtorrent/hex.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"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp"
|
2016-04-28 14:20:10 +02:00
|
|
|
#include "libtorrent/aux_/io.hpp"
|
2016-12-12 03:37:07 +01:00
|
|
|
#include "libtorrent/peer.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2014-10-21 04:08:46 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2010-02-06 09:14:18 +01:00
|
|
|
std::map<address, udp_tracker_connection::connection_cache_entry>
|
|
|
|
udp_tracker_connection::m_connection_cache;
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
std::mutex udp_tracker_connection::m_cache_mutex;
|
2010-02-18 05:37:02 +01:00
|
|
|
|
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
|
2016-08-31 14:27:36 +02:00
|
|
|
, std::weak_ptr<request_callback> c)
|
2018-01-11 01:35:15 +01:00
|
|
|
: tracker_connection(man, req, ios, std::move(c))
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_transaction_id(0)
|
2004-01-31 11:46:15 +01:00
|
|
|
, m_attempts(0)
|
2016-11-11 02:10:00 +01:00
|
|
|
, m_state(action_t::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
|
|
|
|
2016-12-14 20:09:18 +01:00
|
|
|
std::tie(protocol, std::ignore, hostname, port, std::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;
|
|
|
|
}
|
2016-04-28 14:20:10 +02:00
|
|
|
|
2014-10-21 02:28:51 +02:00
|
|
|
aux::session_settings const& settings = m_man.settings();
|
2008-10-22 21:40:32 +02:00
|
|
|
|
2018-07-04 14:49:51 +02:00
|
|
|
int const proxy_type = settings.get_int(settings_pack::proxy_type);
|
|
|
|
|
2014-10-21 02:28:51 +02:00
|
|
|
if (settings.get_bool(settings_pack::proxy_hostnames)
|
2018-07-04 14:49:51 +02:00
|
|
|
&& (proxy_type == settings_pack::socks5
|
|
|
|
|| proxy_type == settings_pack::socks5_pw))
|
2010-08-03 11:08:37 +02:00
|
|
|
{
|
|
|
|
m_hostname = hostname;
|
2016-11-21 16:08:26 +01:00
|
|
|
m_target.port(std::uint16_t(port));
|
2010-08-03 11:08:37 +02:00
|
|
|
start_announce();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-14 20:09:18 +01:00
|
|
|
using namespace std::placeholders;
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("udp_tracker_connection::name_lookup");
|
2017-05-25 20:58:29 +02:00
|
|
|
// when stopping, pass in the cache-only flag, because we
|
2014-07-06 21:18:00 +02:00
|
|
|
// don't want to get stuck on DNS lookups when shutting down
|
2014-10-21 02:28:51 +02:00
|
|
|
m_man.host_resolver().async_resolve(hostname
|
2017-05-28 21:01:46 +02:00
|
|
|
, (tracker_req().event == tracker_request::stopped
|
2017-07-15 02:59:20 +02:00
|
|
|
? resolver_interface::cache_only : resolver_flags{})
|
|
|
|
| resolver_interface::abort_on_shutdown
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::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
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-08-31 14:27:36 +02:00
|
|
|
std::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
|
|
|
}
|
|
|
|
|
2017-12-18 22:21:08 +01:00
|
|
|
void udp_tracker_connection::fail(error_code const& ec
|
2017-02-06 01:18:06 +01:00
|
|
|
, char const* msg, seconds32 const interval, seconds32 const min_interval)
|
2012-10-01 02:13:58 +02:00
|
|
|
{
|
|
|
|
// m_target failed. remove it from the endpoint list
|
2016-12-14 20:09:18 +01:00
|
|
|
auto const i = std::find(m_endpoints.begin()
|
2018-09-17 16:56:48 +02:00
|
|
|
, m_endpoints.end(), make_tcp(m_target));
|
2012-10-01 02:13:58 +02:00
|
|
|
|
|
|
|
if (i != m_endpoints.end()) m_endpoints.erase(i);
|
|
|
|
|
2017-07-21 05:19:28 +02:00
|
|
|
// if that was the last one, or the listen socket was closed
|
|
|
|
// fail the whole announce
|
|
|
|
if (m_endpoints.empty() || !tracker_req().outgoing_socket)
|
2012-10-01 04:19:00 +02:00
|
|
|
{
|
2017-12-18 22:21:08 +01:00
|
|
|
tracker_connection::fail(ec, msg, interval, min_interval);
|
2012-10-01 04:19:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2016-09-14 04:46:07 +02:00
|
|
|
if (cb && cb->should_log())
|
|
|
|
{
|
2018-01-11 01:35:15 +01:00
|
|
|
cb->debug_log(R"(*** UDP_TRACKER [ host: "%s" ip: "%s" | error: "%s" ])"
|
2016-09-14 04:46:07 +02:00
|
|
|
, m_hostname.c_str(), print_endpoint(m_target).c_str(), ec.message().c_str());
|
|
|
|
}
|
2012-10-01 04:19:00 +02:00
|
|
|
#endif
|
2012-10-01 02:13:58 +02:00
|
|
|
|
|
|
|
// pick another target endpoint and try again
|
2018-09-17 16:56:48 +02:00
|
|
|
m_target = make_udp(m_endpoints.front());
|
2012-10-01 02:13:58 +02:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-14 04:46:07 +02:00
|
|
|
if (cb && cb->should_log())
|
|
|
|
{
|
2018-01-11 01:35:15 +01:00
|
|
|
cb->debug_log(R"(*** UDP_TRACKER trying next IP [ host: "%s" ip: "%s" ])"
|
2016-09-14 04:46:07 +02:00
|
|
|
, m_hostname.c_str(), print_endpoint(m_target).c_str());
|
|
|
|
}
|
2012-10-01 04:19:00 +02:00
|
|
|
#endif
|
2016-05-25 06:31:52 +02:00
|
|
|
get_io_service().post(std::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
|
|
|
{
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("udp_tracker_connection::name_lookup");
|
2010-05-30 03:33:03 +02:00
|
|
|
if (m_abort) return;
|
2015-06-06 07:22:53 +02:00
|
|
|
if (error == boost::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
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_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();
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2017-07-21 05:19:28 +02:00
|
|
|
if (!tracker_req().outgoing_socket)
|
|
|
|
{
|
|
|
|
fail(error_code(errors::invalid_listen_socket));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto bind_address = bind_interface();
|
|
|
|
|
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.
|
2017-07-21 05:19:28 +02:00
|
|
|
bool is_v4 = bind_address.is_v4();
|
|
|
|
auto scope = is_v4 ? 0 : bind_address.to_v6().scope_id();
|
2016-12-14 20:09:18 +01:00
|
|
|
for (auto const& addr : addresses)
|
2017-04-28 05:34:39 +02:00
|
|
|
{
|
|
|
|
if (addr.is_v4() != is_v4) continue;
|
|
|
|
if (addr.is_v6() && addr.to_v6().scope_id() != scope)
|
|
|
|
continue;
|
2017-08-16 23:21:11 +02:00
|
|
|
m_endpoints.emplace_back(addr, std::uint16_t(port));
|
2017-04-28 05:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_endpoints.empty())
|
|
|
|
{
|
|
|
|
fail(error_code(boost::asio::error::address_family_not_supported));
|
|
|
|
return;
|
|
|
|
}
|
2009-05-15 23:23:41 +02:00
|
|
|
|
2015-05-16 08:33:37 +02:00
|
|
|
if (tracker_req().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
|
2016-12-14 20:09:18 +01:00
|
|
|
for (auto k = m_endpoints.begin(); k != m_endpoints.end();)
|
2010-06-06 04:31:20 +02:00
|
|
|
{
|
2016-10-22 17:47:24 +02:00
|
|
|
if (tracker_req().filter->access(k->address()) == ip_filter::blocked)
|
2011-03-04 07:55:39 +01:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-14 04:46:07 +02:00
|
|
|
if (cb && cb->should_log())
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2016-10-22 17:47:24 +02:00
|
|
|
|
2018-09-17 16:56:48 +02:00
|
|
|
m_target = make_udp(m_endpoints.front());
|
2012-10-01 02:13:58 +02:00
|
|
|
|
|
|
|
start_announce();
|
|
|
|
}
|
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
void udp_tracker_connection::start_announce()
|
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::unique_lock<std::mutex> l(m_cache_mutex);
|
2016-12-14 20:09:18 +01:00
|
|
|
auto const cc = m_connection_cache.find(m_target.address());
|
2010-02-06 09:14:18 +01:00
|
|
|
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
|
2015-03-12 05:34:54 +01:00
|
|
|
if (aux::time_now() < cc->second.expires)
|
2009-05-15 23:23:41 +02:00
|
|
|
{
|
2015-06-27 23:30:00 +02:00
|
|
|
if (0 == (tracker_req().kind & tracker_request::scrape_request))
|
2010-02-06 09:14:18 +01:00
|
|
|
send_udp_announce();
|
2015-06-27 23:30:00 +02:00
|
|
|
else if (0 != (tracker_req().kind & tracker_request::scrape_request))
|
2010-02-06 09:14:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-08-31 14:27:36 +02:00
|
|
|
std::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()
|
|
|
|
{
|
2016-12-11 16:56:44 +01:00
|
|
|
cancel();
|
|
|
|
m_man.remove_request(this);
|
2007-10-26 09:14:19 +02:00
|
|
|
}
|
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
bool udp_tracker_connection::on_receive_hostname(char const* hostname
|
2016-07-22 18:31:42 +02:00
|
|
|
, span<char const> buf)
|
2010-08-03 11:08:37 +02:00
|
|
|
{
|
2015-05-18 03:30:32 +02:00
|
|
|
TORRENT_UNUSED(hostname);
|
2010-08-03 11:08:37 +02:00
|
|
|
// 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
|
2016-04-28 14:20:10 +02:00
|
|
|
return on_receive(m_target, buf);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
bool udp_tracker_connection::on_receive(udp::endpoint const& ep
|
2016-07-22 18:31:42 +02:00
|
|
|
, span<char const> const buf)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2015-01-13 22:34:43 +01:00
|
|
|
#endif
|
|
|
|
|
2016-12-13 05:53:03 +01:00
|
|
|
// ignore responses before we've sent any requests
|
2016-11-11 02:10:00 +01:00
|
|
|
if (m_state == action_t::error)
|
2015-01-13 22:34:43 +01:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-01-13 22:34:43 +01:00
|
|
|
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)
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-01-13 22:34:43 +01:00
|
|
|
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)
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-14 04:46:07 +02:00
|
|
|
if (cb && cb->should_log())
|
|
|
|
{
|
|
|
|
cb->debug_log("<== UDP_TRACKER [ unexpected source IP: %s "
|
|
|
|
"expected: %s ]"
|
|
|
|
, print_endpoint(ep).c_str()
|
|
|
|
, print_endpoint(m_target).c_str());
|
|
|
|
}
|
2015-01-13 22:34:43 +01:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-04-28 14:20:10 +02:00
|
|
|
if (cb) cb->debug_log("<== UDP_TRACKER_PACKET [ size: %d ]"
|
|
|
|
, int(buf.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
|
2016-04-28 14:20:10 +02:00
|
|
|
if (buf.size() < 8) return false;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2016-07-22 18:31:42 +02:00
|
|
|
span<const char> ptr = buf;
|
2016-11-11 02:10:00 +01:00
|
|
|
auto const action = static_cast<action_t>(aux::read_int32(ptr));
|
2016-07-22 18:31:42 +02:00
|
|
|
std::uint32_t const transaction = aux::read_uint32(ptr);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-11-11 02:10:00 +01:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER_PACKET [ action: %d ]"
|
|
|
|
, static_cast<int>(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)
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-01-13 22:34:43 +01:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER_PACKET [ tid: %x ]"
|
|
|
|
, int(transaction));
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2016-11-11 02:10:00 +01:00
|
|
|
if (action == action_t::error)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2017-12-18 22:21:08 +01:00
|
|
|
fail(error_code(errors::tracker_failure)
|
2016-04-28 14:20:10 +02:00
|
|
|
, std::string(buf.data(), buf.size()).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)
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-01-13 22:34:43 +01:00
|
|
|
if (cb) cb->debug_log("*** UDP_TRACKER_PACKET [ unexpected action: %d "
|
2016-11-11 02:10:00 +01:00
|
|
|
" expected: %d ]", static_cast<int>(action), static_cast<int>(m_state));
|
2015-01-13 22:34:43 +01:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-11-16 07:41:35 +01:00
|
|
|
restart_read_timeout();
|
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_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)
|
|
|
|
{
|
2016-11-11 02:10:00 +01:00
|
|
|
case action_t::connect:
|
2016-04-28 14:20:10 +02:00
|
|
|
return on_connect_response(buf);
|
2016-11-11 02:10:00 +01:00
|
|
|
case action_t::announce:
|
2016-04-28 14:20:10 +02:00
|
|
|
return on_announce_response(buf);
|
2016-11-11 02:10:00 +01:00
|
|
|
case action_t::scrape:
|
2016-04-28 14:20:10 +02:00
|
|
|
return on_scrape_response(buf);
|
2016-11-11 02:10:00 +01:00
|
|
|
case action_t::error:
|
|
|
|
return false;
|
2008-02-05 07:32:10 +01:00
|
|
|
}
|
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()
|
|
|
|
{
|
2016-12-13 05:53:03 +01:00
|
|
|
// don't use 0, because that has special meaning (uninitialized)
|
2016-12-12 02:24:26 +01:00
|
|
|
std::uint32_t const new_tid = random(0xfffffffe) + 1;
|
2014-10-20 22:44:05 +02:00
|
|
|
|
|
|
|
if (m_transaction_id != 0)
|
|
|
|
m_man.update_transaction_id(shared_from_this(), new_tid);
|
|
|
|
m_transaction_id = new_tid;
|
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2016-07-22 18:31:42 +02:00
|
|
|
bool udp_tracker_connection::on_connect_response(span<char const> buf)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2008-02-05 07:32:10 +01:00
|
|
|
// ignore packets smaller than 16 bytes
|
2016-04-28 14:20:10 +02:00
|
|
|
if (buf.size() < 16) return false;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2008-02-05 07:32:10 +01:00
|
|
|
restart_read_timeout();
|
2016-04-28 14:20:10 +02:00
|
|
|
|
|
|
|
// skip header
|
2016-07-24 09:52:20 +02:00
|
|
|
buf = buf.subspan(8);
|
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();
|
2016-12-14 20:09:18 +01:00
|
|
|
std::int64_t const connection_id = aux::read_int64(buf);
|
2010-02-18 05:37:02 +01:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
std::lock_guard<std::mutex> 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;
|
2015-03-12 05:34:54 +01:00
|
|
|
cce.expires = aux::time_now() + seconds(m_man.settings().get_int(settings_pack::udp_tracker_token_expiry));
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2015-06-27 23:30:00 +02:00
|
|
|
if (0 == (tracker_req().kind & tracker_request::scrape_request))
|
2008-02-05 07:32:10 +01:00
|
|
|
send_udp_announce();
|
2015-06-27 23:30:00 +02:00
|
|
|
else if (0 != (tracker_req().kind & tracker_request::scrape_request))
|
2008-02-05 07:32:10 +01:00
|
|
|
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()
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-08-31 14:27:36 +02:00
|
|
|
std::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-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-01-14 02:59:23 +01:00
|
|
|
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
|
|
|
|
2018-07-24 12:16:43 +02:00
|
|
|
std::size_t const connect_packet_size = 16;
|
|
|
|
std::array<char, connect_packet_size> buf;
|
2016-07-22 18:31:42 +02:00
|
|
|
span<char> view = buf;
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2014-10-20 22:44:05 +02:00
|
|
|
TORRENT_ASSERT(m_transaction_id != 0);
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_uint32(0x417, view);
|
|
|
|
aux::write_uint32(0x27101980, view); // connection_id
|
2016-12-17 21:03:43 +01:00
|
|
|
aux::write_int32(action_t::connect, view); // action (connect)
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_int32(m_transaction_id, view); // transaction_id
|
2018-05-29 16:45:15 +02:00
|
|
|
TORRENT_ASSERT(view.empty());
|
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())
|
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
m_man.send_hostname(bind_socket(), m_hostname.c_str()
|
2016-04-28 14:20:10 +02:00
|
|
|
, m_target.port(), buf, ec
|
2015-08-25 04:18:10 +02:00
|
|
|
, udp_socket::tracker_connection);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
m_man.send(bind_socket(), m_target, buf, ec
|
2015-08-25 04:18:10 +02:00
|
|
|
, udp_socket::tracker_connection);
|
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-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-14 04:46:07 +02:00
|
|
|
if (cb && cb->should_log())
|
|
|
|
{
|
|
|
|
cb->debug_log("==> UDP_TRACKER_CONNECT [ failed: %s ]"
|
|
|
|
, ec.message().c_str());
|
|
|
|
}
|
2015-01-14 02:59:23 +01:00
|
|
|
#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
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-14 04:46:07 +02:00
|
|
|
if (cb && cb->should_log())
|
2015-01-14 02:59:23 +01:00
|
|
|
{
|
|
|
|
cb->debug_log("==> UDP_TRACKER_CONNECT [ to: %s ih: %s]"
|
|
|
|
, m_hostname.empty()
|
|
|
|
? print_endpoint(m_target).c_str()
|
2016-05-01 05:10:47 +02:00
|
|
|
: (m_hostname + ":" + to_string(m_target.port()).data()).c_str()
|
2016-12-22 16:44:36 +01:00
|
|
|
, aux::to_hex(tracker_req().info_hash).c_str());
|
2015-01-14 02:59:23 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-11 02:10:00 +01:00
|
|
|
m_state = action_t::connect;
|
2015-01-14 02:59:23 +01:00
|
|
|
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
|
|
|
|
2016-12-14 20:09:18 +01:00
|
|
|
auto const i = m_connection_cache.find(m_target.address());
|
2010-02-06 09:14:18 +01:00
|
|
|
// 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];
|
2016-07-22 18:31:42 +02:00
|
|
|
span<char> view = buf;
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_int64(i->second.connection_id, view); // connection_id
|
2016-12-17 21:03:43 +01:00
|
|
|
aux::write_int32(action_t::scrape, view); // action (scrape)
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_int32(m_transaction_id, view); // transaction_id
|
2005-04-12 13:34:40 +02:00
|
|
|
// info_hash
|
2016-04-28 14:20:10 +02:00
|
|
|
std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end()
|
|
|
|
, view.data());
|
2016-06-18 14:31:07 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2016-04-28 14:20:10 +02:00
|
|
|
TORRENT_ASSERT(view.size() == 20);
|
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())
|
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
m_man.send_hostname(bind_socket(), m_hostname.c_str(), m_target.port()
|
2016-04-28 14:20:10 +02:00
|
|
|
, buf, ec, udp_socket::tracker_connection);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
m_man.send(bind_socket(), m_target, buf, ec
|
2015-08-25 04:18:10 +02:00
|
|
|
, udp_socket::tracker_connection);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2016-11-11 02:10:00 +01:00
|
|
|
m_state = action_t::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
|
|
|
}
|
|
|
|
|
2016-07-22 18:31:42 +02:00
|
|
|
bool udp_tracker_connection::on_announce_response(span<char const> buf)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2016-04-28 14:20:10 +02:00
|
|
|
if (buf.size() < 20) return false;
|
2004-01-31 11:46:15 +01:00
|
|
|
|
2016-07-24 09:52:20 +02:00
|
|
|
buf = buf.subspan(8);
|
2008-02-05 07:32:10 +01:00
|
|
|
restart_read_timeout();
|
2014-09-28 08:36:03 +02:00
|
|
|
|
|
|
|
tracker_response resp;
|
|
|
|
|
2017-02-06 01:18:06 +01:00
|
|
|
resp.interval = seconds32(aux::read_int32(buf));
|
|
|
|
resp.min_interval = seconds32(60);
|
2016-04-28 14:20:10 +02:00
|
|
|
resp.incomplete = aux::read_int32(buf);
|
|
|
|
resp.complete = aux::read_int32(buf);
|
2017-03-11 08:46:51 +01:00
|
|
|
|
2018-08-15 00:02:37 +02:00
|
|
|
std::size_t const ip_stride = is_v6(m_target) ? 18 : 6;
|
2017-03-11 08:46:51 +01:00
|
|
|
|
2018-05-29 16:45:15 +02:00
|
|
|
std::size_t const num_peers = buf.size() / ip_stride;
|
2017-03-14 17:55:13 +01:00
|
|
|
if (buf.size() % ip_stride != 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
|
|
|
}
|
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2007-09-14 04:54:15 +02:00
|
|
|
if (cb)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
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
|
|
|
|
2018-04-01 13:48:17 +02:00
|
|
|
if (is_v6(m_target))
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2018-05-29 16:45:15 +02:00
|
|
|
resp.peers6.reserve(num_peers);
|
|
|
|
for (std::size_t i = 0; i < num_peers; ++i)
|
2017-03-11 08:46:51 +01:00
|
|
|
{
|
2018-03-28 18:32:59 +02:00
|
|
|
ipv6_peer_entry e{};
|
2017-03-14 17:55:13 +01:00
|
|
|
std::memcpy(e.ip.data(), buf.data(), 16);
|
|
|
|
buf = buf.subspan(16);
|
|
|
|
e.port = aux::read_uint16(buf);
|
2017-03-11 08:46:51 +01:00
|
|
|
resp.peers6.push_back(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-29 16:45:15 +02:00
|
|
|
resp.peers4.reserve(num_peers);
|
|
|
|
for (std::size_t i = 0; i < num_peers; ++i)
|
2017-03-11 08:46:51 +01:00
|
|
|
{
|
2018-03-28 18:32:59 +02:00
|
|
|
ipv4_peer_entry e{};
|
2017-08-16 23:21:11 +02:00
|
|
|
std::memcpy(e.ip.data(), buf.data(), 4);
|
2017-03-14 17:55:13 +01:00
|
|
|
buf = buf.subspan(4);
|
|
|
|
e.port = aux::read_uint16(buf);
|
2017-03-11 08:46:51 +01: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;
|
2016-12-14 20:09:18 +01:00
|
|
|
for (auto const& endp : m_endpoints)
|
2009-09-06 02:57:01 +02:00
|
|
|
{
|
2016-12-14 20:09:18 +01:00
|
|
|
ip_list.push_back(endp.address());
|
2009-09-06 02:57:01 +02:00
|
|
|
}
|
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
|
|
|
|
2016-07-22 18:31:42 +02:00
|
|
|
bool udp_tracker_connection::on_scrape_response(span<char const> buf)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
restart_read_timeout();
|
2016-11-11 02:10:00 +01:00
|
|
|
auto const action = static_cast<action_t>(aux::read_int32(buf));
|
2016-12-12 03:37:07 +01:00
|
|
|
std::uint32_t const transaction = aux::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
|
|
|
}
|
|
|
|
|
2016-11-11 02:10:00 +01:00
|
|
|
if (action == action_t::error)
|
2004-01-31 11:46:15 +01:00
|
|
|
{
|
2017-12-18 22:21:08 +01:00
|
|
|
fail(error_code(errors::tracker_failure)
|
2016-04-28 14:20:10 +02:00
|
|
|
, std::string(buf.data(), buf.size()).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
|
|
|
|
2016-11-11 02:10:00 +01:00
|
|
|
if (action != action_t::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
|
|
|
}
|
|
|
|
|
2016-04-28 14:20:10 +02:00
|
|
|
if (buf.size() < 12)
|
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
|
|
|
}
|
|
|
|
|
2016-04-28 14:20:10 +02:00
|
|
|
int const complete = aux::read_int32(buf);
|
|
|
|
int const downloaded = aux::read_int32(buf);
|
|
|
|
int const incomplete = aux::read_int32(buf);
|
2005-04-12 13:34:40 +02:00
|
|
|
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2007-09-14 04:54:15 +02:00
|
|
|
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
|
|
|
}
|
2016-04-28 14:20:10 +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];
|
2016-07-22 18:31:42 +02:00
|
|
|
span<char> out = buf;
|
2008-02-05 07:32:10 +01:00
|
|
|
|
|
|
|
tracker_request const& req = tracker_req();
|
2014-10-21 02:28:51 +02:00
|
|
|
aux::session_settings const& settings = m_man.settings();
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2016-12-14 20:09:18 +01:00
|
|
|
auto const i = m_connection_cache.find(m_target.address());
|
2010-02-06 09:14:18 +01:00
|
|
|
// this isn't really supposed to happen
|
|
|
|
TORRENT_ASSERT(i != m_connection_cache.end());
|
|
|
|
if (i == m_connection_cache.end()) return;
|
|
|
|
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_int64(i->second.connection_id, out); // connection_id
|
2016-12-17 21:03:43 +01:00
|
|
|
aux::write_int32(action_t::announce, out); // action (announce)
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_int32(m_transaction_id, out); // transaction_id
|
|
|
|
std::copy(req.info_hash.begin(), req.info_hash.end(), out.data()); // info_hash
|
2017-01-27 00:22:27 +01:00
|
|
|
out = out.subspan(20);
|
2016-04-28 14:20:10 +02:00
|
|
|
std::copy(req.pid.begin(), req.pid.end(), out.data()); // peer_id
|
2017-01-27 00:22:27 +01:00
|
|
|
out = out.subspan(20);
|
2016-09-22 01:54:49 +02:00
|
|
|
aux::write_int64(req.downloaded, out); // downloaded
|
|
|
|
aux::write_int64(req.left, out); // left
|
|
|
|
aux::write_int64(req.uploaded, out); // uploaded
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_int32(req.event, out); // event
|
2008-02-05 07:32:10 +01:00
|
|
|
// 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;
|
2018-01-04 10:48:22 +01:00
|
|
|
address ip = make_address(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();
|
|
|
|
}
|
2016-12-17 21:03:43 +01:00
|
|
|
aux::write_uint32(announce_ip.to_ulong(), out);
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_int32(req.key, out); // key
|
|
|
|
aux::write_int32(req.num_want, out); // num_want
|
|
|
|
aux::write_uint16(req.listen_port, out); // port
|
2008-02-05 07:32:10 +01:00
|
|
|
|
2012-08-23 23:02:09 +02:00
|
|
|
std::string request_string;
|
|
|
|
error_code ec;
|
2016-06-20 17:32:06 +02:00
|
|
|
using std::ignore;
|
|
|
|
std::tie(ignore, ignore, ignore, ignore, request_string)
|
2015-02-01 15:30:43 +01:00
|
|
|
= parse_url_components(req.url, ec);
|
2012-08-23 23:02:09 +02:00
|
|
|
if (ec) request_string.clear();
|
|
|
|
|
|
|
|
if (!request_string.empty())
|
|
|
|
{
|
2016-12-17 21:03:43 +01:00
|
|
|
std::size_t str_len = std::min(request_string.size(), std::size_t(255));
|
2012-09-18 04:03:24 +02:00
|
|
|
request_string.resize(str_len);
|
|
|
|
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_uint8(2, out);
|
2016-12-17 21:03:43 +01:00
|
|
|
aux::write_uint8(str_len, out);
|
2016-04-28 14:20:10 +02:00
|
|
|
aux::write_string(request_string, out);
|
2012-08-23 23:02:09 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<request_callback> cb = requester();
|
2016-09-14 04:46:07 +02:00
|
|
|
if (cb && cb->should_log())
|
2009-04-24 19:04:20 +02:00
|
|
|
{
|
2016-12-22 16:44:36 +01:00
|
|
|
cb->debug_log("==> UDP_TRACKER_ANNOUNCE [%s]", aux::to_hex(req.info_hash).c_str());
|
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())
|
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
m_man.send_hostname(bind_socket(), m_hostname.c_str()
|
2016-12-14 20:09:18 +01:00
|
|
|
, m_target.port(), {buf, std::size_t(sizeof(buf) - out.size())}, ec
|
2015-08-25 04:18:10 +02:00
|
|
|
, udp_socket::tracker_connection);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
m_man.send(bind_socket(), m_target, {buf, std::size_t(sizeof(buf) - out.size())}, ec
|
2015-08-25 04:18:10 +02:00
|
|
|
, udp_socket::tracker_connection);
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2016-11-11 02:10:00 +01:00
|
|
|
m_state = action_t::announce;
|
2016-05-02 18:36:21 +02:00
|
|
|
sent_bytes(int(sizeof(buf) - out.size()) + 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
|
|
|
}
|