2016-01-18 08:21:27 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2015, Steven Siloti
|
|
|
|
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 "test.hpp"
|
|
|
|
|
|
|
|
#include "simulator/simulator.hpp"
|
|
|
|
|
2017-07-21 05:19:28 +02:00
|
|
|
#include "libtorrent/aux_/listen_socket_handle.hpp"
|
2017-07-22 04:18:16 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2016-01-18 08:21:27 +01:00
|
|
|
#include "libtorrent/udp_socket.hpp"
|
|
|
|
#include "libtorrent/kademlia/dht_tracker.hpp"
|
2016-09-17 15:42:04 +02:00
|
|
|
#include "libtorrent/kademlia/dht_state.hpp"
|
2016-01-18 08:21:27 +01:00
|
|
|
#include "libtorrent/performance_counters.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
2017-09-02 23:58:10 +02:00
|
|
|
#include "libtorrent/kademlia/dht_settings.hpp"
|
2016-07-22 18:31:42 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2016-01-18 08:21:27 +01:00
|
|
|
#include "libtorrent/kademlia/dht_observer.hpp"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <cstdarg>
|
2016-11-04 05:39:48 +01:00
|
|
|
#include <cmath>
|
2016-01-18 08:21:27 +01:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2016-01-18 08:21:27 +01:00
|
|
|
using namespace sim;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2016-01-18 08:21:27 +01:00
|
|
|
|
2016-04-24 21:26:28 +02:00
|
|
|
#if !defined TORRENT_DISABLE_DHT
|
|
|
|
|
2016-01-18 08:21:27 +01:00
|
|
|
struct obs : dht::dht_observer
|
|
|
|
{
|
2017-07-21 05:19:28 +02:00
|
|
|
void set_external_address(lt::aux::listen_socket_handle const&, address const& /* addr */
|
2016-06-19 01:24:27 +02:00
|
|
|
, address const& /* source */) override
|
2016-01-18 08:21:27 +01:00
|
|
|
{}
|
2018-07-08 23:45:29 +02:00
|
|
|
int get_listen_port(lt::aux::transport, lt::aux::listen_socket_handle const& s) override
|
2019-04-22 01:50:44 +02:00
|
|
|
{ return s.get()->udp_external_port(); }
|
2016-07-10 02:10:38 +02:00
|
|
|
void get_peers(sha1_hash const&) override {}
|
|
|
|
void outgoing_get_peers(sha1_hash const& /* target */
|
2016-06-19 01:24:27 +02:00
|
|
|
, sha1_hash const& /* sent_target */, udp::endpoint const& /* ep */) override {}
|
2016-07-10 02:10:38 +02:00
|
|
|
void announce(sha1_hash const& /* ih */
|
2016-06-19 01:24:27 +02:00
|
|
|
, address const& /* addr */, int /* port */) override {}
|
2016-12-27 16:12:57 +01:00
|
|
|
bool on_dht_request(string_view /* query */
|
|
|
|
, dht::msg const& /* request */, entry& /* response */) override
|
|
|
|
{ return false; }
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-09 01:13:47 +02:00
|
|
|
bool should_log(module_t) const override { return true; }
|
2018-11-16 16:19:12 +01:00
|
|
|
void log(dht_logger::module_t, char const* fmt, ...) override
|
2016-01-18 08:21:27 +01:00
|
|
|
{
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
|
|
|
vprintf(fmt, v);
|
|
|
|
va_end(v);
|
|
|
|
puts("\n");
|
|
|
|
}
|
2016-07-10 02:10:38 +02:00
|
|
|
void log_packet(message_direction_t /* dir */
|
2016-09-28 05:21:18 +02:00
|
|
|
, span<char const> /* pkt */
|
2016-09-16 03:13:43 +02:00
|
|
|
, udp::endpoint const& /* node */) override {}
|
2016-12-27 16:12:57 +01:00
|
|
|
#endif
|
2016-01-18 08:21:27 +01:00
|
|
|
};
|
|
|
|
|
2017-07-21 05:19:28 +02:00
|
|
|
void send_packet(lt::udp_socket& sock, lt::aux::listen_socket_handle const&, udp::endpoint const& ep
|
2017-07-26 19:38:40 +02:00
|
|
|
, span<char const> p, error_code& ec, udp_send_flags_t const flags)
|
2017-04-21 06:45:43 +02:00
|
|
|
{
|
|
|
|
sock.send(ep, p, ec, flags);
|
|
|
|
}
|
|
|
|
|
2016-01-18 08:21:27 +01:00
|
|
|
#endif // #if !defined TORRENT_DISABLE_DHT
|
|
|
|
|
|
|
|
TORRENT_TEST(dht_rate_limit)
|
|
|
|
{
|
|
|
|
#if !defined TORRENT_DISABLE_DHT
|
|
|
|
|
|
|
|
default_config cfg;
|
|
|
|
simulation sim(cfg);
|
|
|
|
asio::io_service dht_ios(sim, address_v4::from_string("40.30.20.10"));
|
|
|
|
|
|
|
|
// receiver (the DHT under test)
|
|
|
|
lt::udp_socket sock(dht_ios);
|
|
|
|
obs o;
|
2017-07-22 04:18:16 +02:00
|
|
|
auto ls = std::make_shared<lt::aux::listen_socket_t>();
|
2017-10-10 15:50:49 +02:00
|
|
|
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10")
|
|
|
|
, lt::aux::session_interface::source_dht, lt::address());
|
2017-07-21 05:19:28 +02:00
|
|
|
ls->local_endpoint = tcp::endpoint(address_v4::from_string("40.30.20.10"), 8888);
|
2016-01-18 08:21:27 +01:00
|
|
|
error_code ec;
|
|
|
|
sock.bind(udp::endpoint(address_v4::from_string("40.30.20.10"), 8888), ec);
|
2019-01-11 01:43:45 +01:00
|
|
|
dht::settings dhtsett;
|
2016-01-18 08:21:27 +01:00
|
|
|
dhtsett.block_ratelimit = 100000; // disable the DOS blocker
|
|
|
|
dhtsett.ignore_dark_internet = false;
|
|
|
|
dhtsett.upload_rate_limit = 400;
|
|
|
|
float const target_upload_rate = 400;
|
|
|
|
int const num_packets = 2000;
|
|
|
|
|
|
|
|
counters cnt;
|
2016-09-17 15:42:04 +02:00
|
|
|
dht::dht_state state;
|
2016-06-05 20:07:24 +02:00
|
|
|
std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(dhtsett));
|
2016-09-17 15:42:04 +02:00
|
|
|
auto dht = std::make_shared<lt::dht::dht_tracker>(
|
2017-04-21 06:45:43 +02:00
|
|
|
&o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5)
|
2018-11-11 02:37:13 +01:00
|
|
|
, dhtsett, cnt, *dht_storage, std::move(state));
|
2017-07-21 05:19:28 +02:00
|
|
|
dht->new_socket(ls);
|
2016-04-24 21:26:28 +02:00
|
|
|
|
|
|
|
bool stop = false;
|
|
|
|
std::function<void(error_code const&, size_t)> on_read
|
2016-06-19 01:24:27 +02:00
|
|
|
= [&](error_code const& ec, size_t const /* bytes */)
|
2016-04-24 21:26:28 +02:00
|
|
|
{
|
|
|
|
if (ec) return;
|
|
|
|
udp_socket::packet p;
|
|
|
|
error_code err;
|
2016-07-22 18:31:42 +02:00
|
|
|
int const num = int(sock.read(lt::span<udp_socket::packet>(&p, 1), err));
|
2017-09-22 06:00:38 +02:00
|
|
|
if (num) dht->incoming_packet(ls, p.from, p.data);
|
2016-04-24 21:26:28 +02:00
|
|
|
if (stop || err) return;
|
|
|
|
sock.async_read(on_read);
|
|
|
|
};
|
|
|
|
sock.async_read(on_read);
|
2016-01-18 08:21:27 +01:00
|
|
|
|
|
|
|
// sender
|
|
|
|
int num_packets_sent = 0;
|
|
|
|
asio::io_service sender_ios(sim, address_v4::from_string("10.20.30.40"));
|
|
|
|
udp::socket sender_sock(sender_ios);
|
|
|
|
sender_sock.open(udp::v4());
|
|
|
|
sender_sock.bind(udp::endpoint(address_v4(), 4444));
|
2017-12-15 17:18:27 +01:00
|
|
|
sender_sock.non_blocking(true);
|
2016-01-18 08:21:27 +01:00
|
|
|
asio::high_resolution_timer timer(sender_ios);
|
2016-06-19 01:24:27 +02:00
|
|
|
std::function<void(error_code const&)> sender_tick = [&](error_code const&)
|
2016-01-18 08:21:27 +01:00
|
|
|
{
|
|
|
|
if (num_packets_sent == num_packets)
|
|
|
|
{
|
|
|
|
// we're done. shut down (a second from now, to let the dust settle)
|
|
|
|
timer.expires_from_now(chrono::seconds(1));
|
2016-06-19 01:24:27 +02:00
|
|
|
timer.async_wait([&](error_code const&)
|
2016-01-18 08:21:27 +01:00
|
|
|
{
|
|
|
|
dht->stop();
|
2016-04-24 21:26:28 +02:00
|
|
|
stop = true;
|
2016-01-18 08:21:27 +01:00
|
|
|
sender_sock.close();
|
|
|
|
sock.close();
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const packet[] = "d1:ad2:id20:ababababababababababe1:y1:q1:q4:pinge";
|
|
|
|
sender_sock.send_to(asio::const_buffers_1(packet, sizeof(packet)-1)
|
|
|
|
, udp::endpoint(address_v4::from_string("40.30.20.10"), 8888));
|
|
|
|
++num_packets_sent;
|
|
|
|
|
|
|
|
timer.expires_from_now(chrono::milliseconds(10));
|
|
|
|
timer.async_wait(sender_tick);
|
|
|
|
};
|
|
|
|
timer.expires_from_now(chrono::milliseconds(10));
|
|
|
|
timer.async_wait(sender_tick);
|
|
|
|
|
|
|
|
udp::endpoint from;
|
|
|
|
int num_bytes_received = 0;
|
|
|
|
int num_packets_received = 0;
|
|
|
|
char buffer[1500];
|
|
|
|
std::function<void(error_code const&, std::size_t)> on_receive
|
2016-06-19 01:24:27 +02:00
|
|
|
= [&](error_code const& ec, std::size_t const bytes)
|
2016-01-18 08:21:27 +01:00
|
|
|
{
|
|
|
|
if (ec) return;
|
|
|
|
|
2016-06-19 01:24:27 +02:00
|
|
|
num_bytes_received += int(bytes);
|
2016-01-18 08:21:27 +01:00
|
|
|
++num_packets_received;
|
|
|
|
|
|
|
|
sender_sock.async_receive_from(asio::mutable_buffers_1(buffer, sizeof(buffer))
|
|
|
|
, from, on_receive);
|
|
|
|
};
|
|
|
|
sender_sock.async_receive_from(asio::mutable_buffers_1(buffer, sizeof(buffer))
|
|
|
|
, from, on_receive);
|
|
|
|
|
|
|
|
// run simulation
|
|
|
|
lt::clock_type::time_point start = lt::clock_type::now();
|
|
|
|
sim.run();
|
|
|
|
lt::clock_type::time_point end = lt::clock_type::now();
|
|
|
|
|
|
|
|
// subtract one target_upload_rate here, since we initialize the quota to one
|
|
|
|
// full second worth of bandwidth
|
|
|
|
float const average_upload_rate = (num_bytes_received - target_upload_rate)
|
|
|
|
/ (duration_cast<chrono::milliseconds>(end - start).count() * 0.001f);
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("send %d packets. received %d packets (%d bytes). average rate: %f (target: %f)\n"
|
2016-01-18 08:21:27 +01:00
|
|
|
, num_packets_sent, num_packets_received, num_bytes_received
|
|
|
|
, average_upload_rate, target_upload_rate);
|
|
|
|
|
|
|
|
// the actual upload rate should be within 5% of the target
|
|
|
|
TEST_CHECK(std::abs(average_upload_rate - target_upload_rate) < target_upload_rate * 0.05);
|
|
|
|
|
|
|
|
TEST_EQUAL(cnt[counters::dht_messages_in], num_packets);
|
|
|
|
|
|
|
|
// the number of dropped packets + the number of received pings, should equal
|
|
|
|
// exactly the number of packets we sent
|
|
|
|
TEST_EQUAL(cnt[counters::dht_messages_in_dropped]
|
|
|
|
+ cnt[counters::dht_ping_in], num_packets);
|
|
|
|
|
2018-07-24 08:18:48 +02:00
|
|
|
#endif // #if !defined TORRENT_DISABLE_DHT
|
2016-01-18 08:21:27 +01:00
|
|
|
}
|
2017-07-08 17:32:24 +02:00
|
|
|
|
|
|
|
// TODO: put test here to take advantage of existing code, refactor
|
|
|
|
TORRENT_TEST(dht_delete_socket)
|
|
|
|
{
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
|
|
|
|
sim::default_config cfg;
|
|
|
|
sim::simulation sim(cfg);
|
|
|
|
sim::asio::io_service dht_ios(sim, lt::address_v4::from_string("40.30.20.10"));
|
|
|
|
|
|
|
|
lt::udp_socket sock(dht_ios);
|
|
|
|
error_code ec;
|
|
|
|
sock.bind(udp::endpoint(address_v4::from_string("40.30.20.10"), 8888), ec);
|
|
|
|
|
|
|
|
obs o;
|
2017-07-22 04:18:16 +02:00
|
|
|
auto ls = std::make_shared<lt::aux::listen_socket_t>();
|
2017-10-10 15:50:49 +02:00
|
|
|
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10")
|
|
|
|
, lt::aux::session_interface::source_dht, lt::address());
|
2017-07-21 05:19:28 +02:00
|
|
|
ls->local_endpoint = tcp::endpoint(address_v4::from_string("40.30.20.10"), 8888);
|
2019-01-11 01:43:45 +01:00
|
|
|
dht::settings dhtsett;
|
2017-07-08 17:32:24 +02:00
|
|
|
counters cnt;
|
|
|
|
dht::dht_state state;
|
|
|
|
std::unique_ptr<lt::dht::dht_storage_interface> dht_storage(dht::dht_default_storage_constructor(dhtsett));
|
|
|
|
auto dht = std::make_shared<lt::dht::dht_tracker>(
|
|
|
|
&o, dht_ios, std::bind(&send_packet, std::ref(sock), _1, _2, _3, _4, _5)
|
2018-11-11 02:37:13 +01:00
|
|
|
, dhtsett, cnt, *dht_storage, std::move(state));
|
2017-07-08 17:32:24 +02:00
|
|
|
|
|
|
|
dht->start([](std::vector<std::pair<dht::node_entry, std::string>> const&){});
|
2017-07-21 05:19:28 +02:00
|
|
|
dht->new_socket(ls);
|
2017-07-08 17:32:24 +02:00
|
|
|
|
|
|
|
// schedule the removal of the socket at exactly 2 second,
|
|
|
|
// this simulates the fact that the internal scheduled call
|
|
|
|
// to connection_timeout will be executed right after leaving
|
|
|
|
// the state of cancellable
|
|
|
|
asio::high_resolution_timer t1(dht_ios);
|
|
|
|
t1.expires_from_now(chrono::seconds(2));
|
|
|
|
t1.async_wait([&](error_code const&)
|
|
|
|
{
|
2017-07-21 05:19:28 +02:00
|
|
|
dht->delete_socket(ls);
|
2017-07-08 17:32:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// stop the DHT
|
|
|
|
asio::high_resolution_timer t2(dht_ios);
|
|
|
|
t2.expires_from_now(chrono::seconds(3));
|
|
|
|
t2.async_wait([&](error_code const&) { dht->stop(); });
|
|
|
|
|
|
|
|
sim.run();
|
|
|
|
|
|
|
|
#endif // TORRENT_DISABLE_DHT
|
|
|
|
}
|