2013-06-12 19:03:56 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2013, 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"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp"
|
2018-11-18 12:59:52 +01:00
|
|
|
#include "libtorrent/bdecode.hpp"
|
2013-06-24 05:34:45 +02:00
|
|
|
#include "dht_server.hpp"
|
2015-04-24 05:48:08 +02:00
|
|
|
#include "test_utils.hpp"
|
2013-06-12 19:03:56 +02:00
|
|
|
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_IOSTREAM
|
2013-09-09 09:06:12 +02:00
|
|
|
#include <iostream>
|
|
|
|
#endif
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
#include <thread>
|
2017-04-02 20:22:17 +02:00
|
|
|
#include <atomic>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2016-09-01 03:42:18 +02:00
|
|
|
#include <memory>
|
2016-05-01 00:54:23 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2013-06-12 19:03:56 +02:00
|
|
|
|
|
|
|
struct dht_server
|
|
|
|
{
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
lt::io_service m_ios;
|
2017-04-02 20:22:17 +02:00
|
|
|
std::atomic<int> m_dht_requests;
|
2013-06-12 19:03:56 +02:00
|
|
|
udp::socket m_socket;
|
|
|
|
int m_port;
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<std::thread> m_thread;
|
2013-06-12 19:03:56 +02:00
|
|
|
|
|
|
|
dht_server()
|
|
|
|
: m_dht_requests(0)
|
|
|
|
, m_socket(m_ios)
|
2014-01-18 07:53:47 +01:00
|
|
|
, m_port(0)
|
2013-06-12 19:03:56 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
m_socket.open(udp::v4(), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("Error opening listen DHT socket: %s\n", ec.message().c_str());
|
2013-06-12 19:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_socket.bind(udp::endpoint(address_v4::any(), 0), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("Error binding DHT socket to port 0: %s\n", ec.message().c_str());
|
2013-06-12 19:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_port = m_socket.local_endpoint(ec).port();
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("Error getting local endpoint of DHT socket: %s\n", ec.message().c_str());
|
2013-06-12 19:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%s: DHT initialized on port %d\n", time_now_string(), m_port);
|
2013-06-12 19:03:56 +02:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
m_thread = std::make_shared<std::thread>(&dht_server::thread_fun, this);
|
2013-06-12 19:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~dht_server()
|
|
|
|
{
|
2013-06-17 07:18:56 +02:00
|
|
|
m_socket.cancel();
|
2013-06-12 19:03:56 +02:00
|
|
|
m_socket.close();
|
|
|
|
if (m_thread) m_thread->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
int port() const { return m_port; }
|
|
|
|
|
|
|
|
int num_hits() const { return m_dht_requests; }
|
|
|
|
|
2016-11-12 19:50:49 +01:00
|
|
|
static void incoming_packet(error_code const& ec, size_t bytes_transferred
|
|
|
|
, size_t *ret, error_code* error, bool* done)
|
2013-06-17 07:18:56 +02:00
|
|
|
{
|
|
|
|
*ret = bytes_transferred;
|
|
|
|
*error = ec;
|
|
|
|
*done = true;
|
|
|
|
}
|
|
|
|
|
2013-06-12 19:03:56 +02:00
|
|
|
void thread_fun()
|
|
|
|
{
|
2018-11-18 12:59:52 +01:00
|
|
|
std::array<char, 2000> buffer;
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2013-06-12 19:03:56 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
udp::endpoint from;
|
2013-06-17 07:18:56 +02:00
|
|
|
size_t bytes_transferred;
|
|
|
|
bool done = false;
|
|
|
|
m_socket.async_receive_from(
|
2018-11-18 12:59:52 +01:00
|
|
|
boost::asio::buffer(buffer.data(), buffer.size()), from, 0
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&incoming_packet, _1, _2, &bytes_transferred, &ec, &done));
|
2013-06-17 07:18:56 +02:00
|
|
|
while (!done)
|
|
|
|
{
|
2015-07-12 05:01:27 +02:00
|
|
|
m_ios.poll_one();
|
2013-06-17 07:18:56 +02:00
|
|
|
m_ios.reset();
|
|
|
|
}
|
|
|
|
|
2013-06-13 08:32:13 +02:00
|
|
|
if (ec == boost::asio::error::operation_aborted
|
|
|
|
|| ec == boost::asio::error::bad_descriptor) return;
|
2013-06-12 19:03:56 +02:00
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("Error receiving on DHT socket: %s\n", ec.message().c_str());
|
2013-06-12 19:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-11-18 12:59:52 +01:00
|
|
|
entry msg = bdecode(span<char const>(buffer).first(int(bytes_transferred)));
|
2013-06-12 19:03:56 +02:00
|
|
|
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_IOSTREAM
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << msg << std::endl;
|
2013-06-12 19:03:56 +02:00
|
|
|
#endif
|
|
|
|
++m_dht_requests;
|
|
|
|
}
|
2016-09-25 15:50:48 +02:00
|
|
|
catch (std::exception const& e)
|
2013-06-12 19:03:56 +02:00
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("failed to decode DHT message: %s\n", e.what());
|
2013-06-12 19:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-02 20:22:17 +02:00
|
|
|
namespace {
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<dht_server> g_dht;
|
2017-04-02 20:22:17 +02:00
|
|
|
}
|
2013-06-12 19:03:56 +02:00
|
|
|
|
|
|
|
int start_dht()
|
|
|
|
{
|
|
|
|
g_dht.reset(new dht_server);
|
|
|
|
return g_dht->port();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the number of DHT messages received
|
|
|
|
int num_dht_hits()
|
|
|
|
{
|
|
|
|
if (g_dht) return g_dht->num_hits();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_dht()
|
|
|
|
{
|
|
|
|
g_dht.reset();
|
|
|
|
}
|
|
|
|
|