2014-02-23 02:32:55 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2014, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/address.hpp"
|
|
|
|
#include "libtorrent/io_service.hpp"
|
|
|
|
#include "libtorrent/error_code.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/socket_io.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp"
|
2014-02-23 02:32:55 +01:00
|
|
|
#include "udp_tracker.hpp"
|
2015-04-24 05:48:08 +02:00
|
|
|
#include "test_utils.hpp"
|
2014-02-23 02:32:55 +01:00
|
|
|
|
|
|
|
#include <boost/detail/atomic_count.hpp>
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2016-05-01 00:54:23 +02:00
|
|
|
#include <thread>
|
2016-09-01 03:42:18 +02:00
|
|
|
#include <memory>
|
2016-05-01 00:54:23 +02:00
|
|
|
|
2014-02-23 02:32:55 +01:00
|
|
|
using namespace libtorrent;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2014-02-23 02:32:55 +01:00
|
|
|
|
|
|
|
struct udp_tracker
|
|
|
|
{
|
|
|
|
|
2015-07-12 05:01:27 +02:00
|
|
|
libtorrent::io_service m_ios;
|
2014-02-23 02:32:55 +01:00
|
|
|
boost::detail::atomic_count m_udp_announces;
|
|
|
|
udp::socket m_socket;
|
|
|
|
int m_port;
|
2016-04-29 05:13:49 +02:00
|
|
|
bool m_abort;
|
2014-02-23 02:32:55 +01:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<std::thread> m_thread;
|
2014-02-23 02:32:55 +01:00
|
|
|
|
|
|
|
void on_udp_receive(error_code const& ec, size_t bytes_transferred, udp::endpoint* from, char* buffer, int size)
|
|
|
|
{
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP tracker, read failed: %s\n", time_now_string(), ec.message().c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_transferred < 16)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP message too short (from: %s)\n", time_now_string(), print_endpoint(*from).c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-29 05:13:49 +02:00
|
|
|
if (m_abort)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP message %d bytes\n", time_now_string(), int(bytes_transferred));
|
2014-02-23 02:32:55 +01:00
|
|
|
|
|
|
|
char* ptr = buffer;
|
|
|
|
detail::read_uint64(ptr);
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t const action = detail::read_uint32(ptr);
|
|
|
|
std::uint32_t const transaction_id = detail::read_uint32(ptr);
|
2014-02-23 02:32:55 +01:00
|
|
|
|
|
|
|
error_code e;
|
|
|
|
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case 0: // connect
|
|
|
|
|
2017-01-27 00:22:27 +01:00
|
|
|
if (bytes_transferred < 16)
|
|
|
|
{
|
|
|
|
std::printf("invalid connect message: %d Bytes, expected 16 Bytes\n"
|
|
|
|
, int(bytes_transferred));
|
|
|
|
return;
|
|
|
|
}
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP connect from %s\n", time_now_string()
|
2015-01-13 22:34:43 +01:00
|
|
|
, print_endpoint(*from).c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
ptr = buffer;
|
|
|
|
detail::write_uint32(0, ptr); // action = connect
|
|
|
|
detail::write_uint32(transaction_id, ptr); // transaction_id
|
|
|
|
detail::write_uint64(10, ptr); // connection_id
|
2015-06-06 07:22:53 +02:00
|
|
|
m_socket.send_to(boost::asio::buffer(buffer, 16), *from, 0, e);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (e) std::printf("%s: UDP send_to failed. ERROR: %s\n"
|
2015-04-24 05:48:08 +02:00
|
|
|
, time_now_string(), e.message().c_str());
|
2016-10-10 02:23:45 +02:00
|
|
|
else std::printf("%s: UDP sent response to: %s\n"
|
2015-04-24 05:48:08 +02:00
|
|
|
, time_now_string(), print_endpoint(*from).c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // announce
|
|
|
|
|
2017-01-27 00:22:27 +01:00
|
|
|
if (bytes_transferred < 84)
|
|
|
|
{
|
|
|
|
std::printf("invalid announce message: %d Bytes, expected 84 Bytes\n"
|
|
|
|
, int(bytes_transferred));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-23 02:32:55 +01:00
|
|
|
++m_udp_announces;
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP announce [%d]\n", time_now_string()
|
2015-01-13 22:34:43 +01:00
|
|
|
, int(m_udp_announces));
|
2014-02-23 02:32:55 +01:00
|
|
|
ptr = buffer;
|
|
|
|
detail::write_uint32(1, ptr); // action = announce
|
|
|
|
detail::write_uint32(transaction_id, ptr); // transaction_id
|
|
|
|
detail::write_uint32(1800, ptr); // interval
|
|
|
|
detail::write_uint32(1, ptr); // incomplete
|
|
|
|
detail::write_uint32(1, ptr); // complete
|
|
|
|
// 0 peers
|
2015-06-06 07:22:53 +02:00
|
|
|
m_socket.send_to(boost::asio::buffer(buffer, 20), *from, 0, e);
|
2016-10-10 02:23:45 +02:00
|
|
|
if (e) std::printf("%s: UDP send_to failed. ERROR: %s\n"
|
2015-04-24 05:48:08 +02:00
|
|
|
, time_now_string(), e.message().c_str());
|
2016-10-10 02:23:45 +02:00
|
|
|
else std::printf("%s: UDP sent response to: %s\n"
|
2015-04-24 05:48:08 +02:00
|
|
|
, time_now_string(), print_endpoint(*from).c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// ignore scrapes
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP scrape (ignored)\n", time_now_string());
|
2014-02-23 02:32:55 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP unknown message: %d\n", time_now_string()
|
2015-01-13 22:34:43 +01:00
|
|
|
, action);
|
2014-02-23 02:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_socket.async_receive_from(
|
2015-06-06 07:22:53 +02:00
|
|
|
boost::asio::buffer(buffer, size), *from, 0
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&udp_tracker::on_udp_receive, this, _1, _2, from, buffer, size));
|
2014-02-23 02:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
udp_tracker()
|
|
|
|
: m_udp_announces(0)
|
|
|
|
, m_socket(m_ios)
|
|
|
|
, m_port(0)
|
2016-04-29 05:13:49 +02:00
|
|
|
, m_abort(false)
|
2014-02-23 02:32:55 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
m_socket.open(udp::v4(), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("UDP Error opening listen UDP tracker socket: %s\n", ec.message().c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_socket.bind(udp::endpoint(address_v4::any(), 0), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("UDP Error binding UDP tracker socket to port 0: %s\n", ec.message().c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_port = m_socket.local_endpoint(ec).port();
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("UDP Error getting local endpoint of UDP tracker socket: %s\n", ec.message().c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: UDP tracker initialized on port %d\n", time_now_string(), m_port);
|
2014-02-23 02:32:55 +01:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
m_thread = std::make_shared<std::thread>(&udp_tracker::thread_fun, this);
|
2014-02-23 02:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-04-29 05:13:49 +02:00
|
|
|
void stop()
|
2014-02-23 02:32:55 +01:00
|
|
|
{
|
2016-04-29 05:13:49 +02:00
|
|
|
m_abort = true;
|
2014-02-23 02:32:55 +01:00
|
|
|
m_socket.cancel();
|
|
|
|
m_socket.close();
|
2016-04-29 05:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~udp_tracker()
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_ios.post(std::bind(&udp_tracker::stop, this));
|
2014-02-23 02:32:55 +01:00
|
|
|
if (m_thread) m_thread->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
int port() const { return m_port; }
|
|
|
|
|
|
|
|
int num_hits() const { return m_udp_announces; }
|
|
|
|
|
|
|
|
static void incoming_packet(error_code const& ec, size_t bytes_transferred, size_t *ret, error_code* error, bool* done)
|
|
|
|
{
|
|
|
|
*ret = bytes_transferred;
|
|
|
|
*error = ec;
|
|
|
|
*done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_fun()
|
|
|
|
{
|
|
|
|
char buffer[2000];
|
2015-06-03 04:37:59 +02:00
|
|
|
|
2014-02-23 02:32:55 +01:00
|
|
|
error_code ec;
|
|
|
|
udp::endpoint from;
|
|
|
|
m_socket.async_receive_from(
|
2016-04-25 23:22:09 +02:00
|
|
|
boost::asio::buffer(buffer, int(sizeof(buffer))), from, 0
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&udp_tracker::on_udp_receive, this, _1, _2, &from, &buffer[0], int(sizeof(buffer))));
|
2014-02-23 02:32:55 +01:00
|
|
|
|
|
|
|
m_ios.run(ec);
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("UDP Error running UDP tracker service: %s\n", ec.message().c_str());
|
2014-02-23 02:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("UDP exiting UDP tracker thread\n");
|
2014-02-23 02:32:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<udp_tracker> g_udp_tracker;
|
2014-02-23 02:32:55 +01:00
|
|
|
|
|
|
|
int start_udp_tracker()
|
|
|
|
{
|
|
|
|
g_udp_tracker.reset(new udp_tracker);
|
|
|
|
return g_udp_tracker->port();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the number of UDP tracker announces received
|
|
|
|
int num_udp_announces()
|
|
|
|
{
|
|
|
|
if (g_udp_tracker) return g_udp_tracker->num_hits();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_udp_tracker()
|
|
|
|
{
|
|
|
|
g_udp_tracker.reset();
|
|
|
|
}
|
|
|
|
|