From 7f2a78d0c9591f56ee41e7e716de62a4379347e8 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 18 Feb 2019 00:23:19 +0100 Subject: [PATCH] make random_shuffle() take a range instead of two iterators. use random_bytes() instead of std::generate() and random_byte(). Remove unused hasher.hpp includes --- appveyor.yml | 2 +- examples/make_torrent.cpp | 1 - include/libtorrent/random.hpp | 33 +++++++++++++++++---------------- include/libtorrent/torrent.hpp | 1 - simulation/setup_dht.cpp | 2 +- simulation/setup_swarm.cpp | 3 ++- src/http_connection.cpp | 2 +- src/peer_connection.cpp | 1 + src/piece_picker.cpp | 4 ++-- src/torrent.cpp | 8 ++++---- src/torrent_info.cpp | 17 ++++------------- src/ut_metadata.cpp | 5 ++++- src/write_resume_data.cpp | 1 - test/bittorrent_peer.cpp | 4 ++-- test/setup_transfer.cpp | 5 ----- test/setup_transfer.hpp | 1 - test/swarm_suite.cpp | 1 - test/test_auto_unchoke.cpp | 1 - test/test_dht.cpp | 8 ++++---- test/test_lsd.cpp | 1 - test/test_pe_crypto.cpp | 7 ++----- test/test_pex.cpp | 1 - test/test_recheck.cpp | 1 - test/test_storage.cpp | 3 ++- test/test_transfer.cpp | 1 - test/test_utp.cpp | 1 - test/test_web_seed_redirect.cpp | 7 ++++--- test/web_seed_suite.cpp | 1 - 28 files changed, 51 insertions(+), 72 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 890532cc6..bbb68ab5e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -92,7 +92,7 @@ build_script: - if defined cmake ( set "PATH=c:\Python27-x64;%PATH%" && cmake -DCMAKE_CXX_STANDARD=11 -Dbuild_tests=ON -Dbuild_examples=ON -Dbuild_tools=ON -Dpython-bindings=%python% -Dboost-python-module-name="python" -Dskip-python-runtime-test=true -DPython_ADDITIONAL_VERSIONS="2.7" -G "Visual Studio 14 2015 Win64" .. && - cmake --build . --config Release -- -verbosity:quiet + cmake --build . --config Release -- -verbosity:minimal ) test_script: diff --git a/examples/make_torrent.cpp b/examples/make_torrent.cpp index f3063987a..37fbd1527 100644 --- a/examples/make_torrent.cpp +++ b/examples/make_torrent.cpp @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bencode.hpp" #include "libtorrent/torrent_info.hpp" #include "libtorrent/storage.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/create_torrent.hpp" #include diff --git a/include/libtorrent/random.hpp b/include/libtorrent/random.hpp index eaa74a4b2..2fc6bb2be 100644 --- a/include/libtorrent/random.hpp +++ b/include/libtorrent/random.hpp @@ -40,26 +40,27 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -namespace libtorrent { namespace aux { +namespace libtorrent { +namespace aux { - TORRENT_EXTRA_EXPORT std::mt19937& random_engine(); + TORRENT_EXTRA_EXPORT std::mt19937& random_engine(); - template - void random_shuffle(RandomIt first, RandomIt last) - { - std::shuffle(first, last, random_engine()); - } - - // Fills the buffer with random bytes. - // - // This functions perform differently under different setups - // For Windows and all platforms when compiled with libcrypto, it - // generates cryptographically random bytes. - // If the above conditions are not true, then a standard - // fill of bytes is used. - TORRENT_EXTRA_EXPORT void random_bytes(span buffer); + template + void random_shuffle(Range& range) + { + std::shuffle(range.data(), range.data() + range.size(), random_engine()); } + // Fills the buffer with random bytes. + // + // This functions perform differently under different setups + // For Windows and all platforms when compiled with libcrypto, it + // generates cryptographically random bytes. + // If the above conditions are not true, then a standard + // fill of bytes is used. + TORRENT_EXTRA_EXPORT void random_bytes(span buffer); +} + TORRENT_EXTRA_EXPORT std::uint32_t random(std::uint32_t m); } diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index f60df9212..d321a6d3d 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -57,7 +57,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bandwidth_limit.hpp" #include "libtorrent/bandwidth_queue_entry.hpp" #include "libtorrent/storage_defs.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/aux_/session_interface.hpp" #include "libtorrent/aux_/time.hpp" diff --git a/simulation/setup_dht.cpp b/simulation/setup_dht.cpp index b179ffb3f..9b2481d64 100644 --- a/simulation/setup_dht.cpp +++ b/simulation/setup_dht.cpp @@ -270,7 +270,7 @@ dht_network::dht_network(sim::simulation& sim, int num_nodes, std::uint32_t flag { // every now and then, shuffle all_nodes to make the // routing tables more randomly distributed - lt::aux::random_shuffle(all_nodes.begin(), all_nodes.end()); + lt::aux::random_shuffle(all_nodes); cnt = 0; } } diff --git a/simulation/setup_swarm.cpp b/simulation/setup_swarm.cpp index 9f490cc9e..50b9643e7 100644 --- a/simulation/setup_swarm.cpp +++ b/simulation/setup_swarm.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/ip_filter.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/aux_/path.hpp" +#include "libtorrent/random.hpp" #include #include "settings.hpp" @@ -294,7 +295,7 @@ void setup_swarm(int num_nodes // make sure the sessions have different peer ids lt::peer_id pid; - std::generate(&pid[0], &pid[0] + 20, &random_byte); + lt::aux::random_bytes(pid); pack.set_str(lt::settings_pack::peer_fingerprint, pid.to_string()); if (i == 0) new_session(pack); diff --git a/src/http_connection.cpp b/src/http_connection.cpp index 3ae0a2493..c15cfef55 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -520,7 +520,7 @@ void http_connection::on_resolve(error_code const& e return; } - aux::random_shuffle(m_endpoints.begin(), m_endpoints.end()); + aux::random_shuffle(m_endpoints); // if we have been told to bind to a particular address // only connect to addresses of the same family diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index b504bf0c4..dcd3245a3 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/session_interface.hpp" #include "libtorrent/peer_list.hpp" #include "libtorrent/aux_/socket_type.hpp" +#include "libtorrent/hasher.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/broadcast_socket.hpp" #include "libtorrent/torrent.hpp" diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index eb528982f..aff0087ae 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -1509,8 +1509,8 @@ namespace libtorrent { for (auto b : m_priority_boundaries) { if (start == b) continue; - auto r = range(m_pieces, start, b); - aux::random_shuffle(r.begin(), r.end()); + span r(&m_pieces[start], static_cast(b - start)); + aux::random_shuffle(r); start = b; } diff --git a/src/torrent.cpp b/src/torrent.cpp index 517377743..97a7a3f44 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -279,7 +279,7 @@ bool is_downloading_state(int const st) for (auto const& e : p.http_seeds) ws.emplace_back(e, web_seed_entry::http_seed); - aux::random_shuffle(ws.begin(), ws.end()); + aux::random_shuffle(ws); for (auto& w : ws) m_web_seeds.emplace_back(std::move(w)); // --- TRACKERS --- @@ -486,7 +486,7 @@ bool is_downloading_state(int const st) // add the web seeds from the .torrent file std::vector const& web_seeds = m_torrent_file->web_seeds(); std::vector ws(web_seeds.begin(), web_seeds.end()); - aux::random_shuffle(ws.begin(), ws.end()); + aux::random_shuffle(ws); for (auto& w : ws) m_web_seeds.push_back(std::move(w)); #if !defined TORRENT_DISABLE_ENCRYPTION @@ -6265,7 +6265,7 @@ bool is_downloading_state(int const st) // if we didn't save 100 peers, fill in with second choice peers if (int(ret.peers.size()) < 100) { - aux::random_shuffle(deferred_peers.begin(), deferred_peers.end()); + aux::random_shuffle(deferred_peers); for (auto const p : deferred_peers) { ret.peers.push_back(p->ip()); @@ -9210,7 +9210,7 @@ bool is_downloading_state(int const st) std::copy_if(m_connections.begin(), m_connections.end(), std::back_inserter(seeds) , [](peer_connection const* p) { return p->is_seed(); }); - aux::random_shuffle(seeds.begin(), seeds.end()); + aux::random_shuffle(seeds); TORRENT_ASSERT(to_disconnect <= seeds.end_index()); for (auto const& p : span(seeds).first(to_disconnect)) p->disconnect(errors::upload_upload_connection, operation_t::bittorrent); diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 92fe58484..0d1f024cc 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -1320,19 +1320,10 @@ namespace { if (!m_urls.empty()) { // shuffle each tier - auto start = m_urls.begin(); - std::vector::iterator stop; - int current_tier = m_urls.front().tier; - for (stop = m_urls.begin(); stop != m_urls.end(); ++stop) - { - if (stop->tier != current_tier) - { - aux::random_shuffle(start, stop); - start = stop; - current_tier = stop->tier; - } - } - aux::random_shuffle(start, stop); + aux::random_shuffle(m_urls); + std::stable_sort(m_urls.begin(), m_urls.end() + , [](announce_entry const& lhs, announce_entry const& rhs) + { return lhs.tier < rhs.tier; }); } } diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 4a47db3f9..4a55181a8 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -41,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_connection.hpp" #include "libtorrent/bt_peer_connection.hpp" #include "libtorrent/peer_connection_handle.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/torrent.hpp" #include "libtorrent/torrent_handle.hpp" @@ -53,6 +52,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/aux_/time.hpp" +#if TORRENT_USE_ASSERTS +#include "libtorrent/hasher.hpp" +#endif + namespace libtorrent {namespace { enum diff --git a/src/write_resume_data.cpp b/src/write_resume_data.cpp index 538329102..92f6b62a4 100644 --- a/src/write_resume_data.cpp +++ b/src/write_resume_data.cpp @@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/write_resume_data.hpp" #include "libtorrent/add_torrent_params.hpp" #include "libtorrent/socket_io.hpp" // for write_*_endpoint() -#include "libtorrent/hasher.hpp" #include "libtorrent/torrent_info.hpp" #include "libtorrent/aux_/numeric_cast.hpp" #include "libtorrent/torrent.hpp" // for default_piece_priority diff --git a/test/bittorrent_peer.cpp b/test/bittorrent_peer.cpp index 695738581..872895963 100644 --- a/test/bittorrent_peer.cpp +++ b/test/bittorrent_peer.cpp @@ -394,7 +394,7 @@ void peer_conn::on_message(error_code const& ec, size_t bytes_transferred) pieces.clear(); for (int i = 0; i < int(pieces.size()); ++i) pieces.push_back(i); - aux::random_shuffle(pieces.begin(), pieces.end()); + aux::random_shuffle(pieces); } else if (msg == 4) // have { @@ -418,7 +418,7 @@ void peer_conn::on_message(error_code const& ec, size_t bytes_transferred) } ++ptr; } - aux::random_shuffle(pieces.begin(), pieces.end()); + aux::random_shuffle(pieces); } else if (msg == 7) // piece { diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index b1868489b..8714e2eb8 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include "libtorrent/session.hpp" #include "libtorrent/hasher.hpp" @@ -51,7 +50,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/hex.hpp" // to_hex #include "libtorrent/aux_/vector.hpp" #include "libtorrent/aux_/path.hpp" -#include "libtorrent/random.hpp" #include "test.hpp" #include "test_utils.hpp" @@ -601,9 +599,6 @@ std::shared_ptr clone_ptr(std::shared_ptr const& ptr) return std::make_shared(*ptr); } -std::uint8_t random_byte() -{ return static_cast(lt::random(0xff)); } - std::vector generate_piece(piece_index_t const idx, int const piece_size) { using namespace lt; diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index f7a95b8b7..dc8f2b0ae 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -43,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. EXPORT std::shared_ptr generate_torrent(); EXPORT int print_failures(); -EXPORT unsigned char random_byte(); EXPORT int load_file(std::string const& filename, std::vector& v , lt::error_code& ec, int limit = 8000000); diff --git a/test/swarm_suite.cpp b/test/swarm_suite.cpp index d111cd8d5..453cc06f1 100644 --- a/test/swarm_suite.cpp +++ b/test/swarm_suite.cpp @@ -32,7 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/session_settings.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/time.hpp" #include "libtorrent/random.hpp" diff --git a/test/test_auto_unchoke.cpp b/test/test_auto_unchoke.cpp index b3ef46285..5e01916f6 100644 --- a/test/test_auto_unchoke.cpp +++ b/test/test_auto_unchoke.cpp @@ -32,7 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/session_settings.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/ip_filter.hpp" #include "libtorrent/aux_/path.hpp" diff --git a/test/test_dht.cpp b/test/test_dht.cpp index cc99ec579..92fe4d517 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -1710,7 +1710,7 @@ void test_routing_table(address(&rand_addr)()) std::vector temp; - std::generate(tmp.begin(), tmp.end(), random_byte); + aux::random_bytes(tmp); table.find_node(tmp, temp, 0, int(nodes.size()) * 2); std::printf("returned-all: %d\n", int(temp.size())); TEST_EQUAL(temp.size(), nodes.size()); @@ -1723,7 +1723,7 @@ void test_routing_table(address(&rand_addr)()) for (int r = 0; r < reps; ++r) { - std::generate(tmp.begin(), tmp.end(), random_byte); + aux::random_bytes(tmp); table.find_node(tmp, temp, 0, bucket_size * 2); std::printf("returned: %d\n", int(temp.size())); TEST_EQUAL(int(temp.size()), std::min(bucket_size * 2, int(nodes.size()))); @@ -3065,7 +3065,7 @@ TORRENT_TEST(routing_table_extended) std::vector node_id_prefix; node_id_prefix.reserve(256); for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i & 0xff); - aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end()); + aux::random_shuffle(node_id_prefix); routing_table tbl(id, udp::v4(), 8, sett, &observer); for (std::size_t i = 0; i < 256; ++i) @@ -3099,7 +3099,7 @@ TORRENT_TEST(routing_table_set_id) std::vector node_id_prefix; node_id_prefix.reserve(256); for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i & 0xff); - aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end()); + aux::random_shuffle(node_id_prefix); routing_table tbl(id, udp::v4(), 8, sett, &observer); for (std::size_t i = 0; i < 256; ++i) { diff --git a/test/test_lsd.cpp b/test/test_lsd.cpp index e7f439eb1..c61d9866a 100644 --- a/test/test_lsd.cpp +++ b/test/test_lsd.cpp @@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/session_settings.hpp" #include "libtorrent/torrent_status.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/aux_/path.hpp" #include diff --git a/test/test_pe_crypto.cpp b/test/test_pe_crypto.cpp index 017684cdc..4aee4a807 100644 --- a/test/test_pe_crypto.cpp +++ b/test/test_pe_crypto.cpp @@ -35,12 +35,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/hasher.hpp" #include "libtorrent/pe_crypto.hpp" -#include "libtorrent/session.hpp" #include "libtorrent/random.hpp" #include "libtorrent/span.hpp" -#include "libtorrent/buffer.hpp" -#include "setup_transfer.hpp" #include "test.hpp" #if !defined TORRENT_DISABLE_ENCRYPTION @@ -52,11 +49,11 @@ void test_enc_handler(lt::crypto_plugin& a, lt::crypto_plugin& b) int const repcount = 128; for (int rep = 0; rep < repcount; ++rep) { - std::ptrdiff_t const buf_len = rand() % (512 * 1024); + std::ptrdiff_t const buf_len = lt::random(512 * 1024); std::vector buf(static_cast(buf_len)); std::vector cmp_buf(static_cast(buf_len)); - std::generate(buf.begin(), buf.end(), &std::rand); + lt::aux::random_bytes(buf); std::copy(buf.begin(), buf.end(), cmp_buf.begin()); using namespace lt::aux; diff --git a/test/test_pex.cpp b/test/test_pex.cpp index 38c7cc404..fea8fa594 100644 --- a/test/test_pex.cpp +++ b/test/test_pex.cpp @@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/session_settings.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/extensions/ut_pex.hpp" #include "libtorrent/ip_filter.hpp" #include "libtorrent/torrent_status.hpp" diff --git a/test/test_recheck.cpp b/test/test_recheck.cpp index f62610784..8c8939573 100644 --- a/test/test_recheck.cpp +++ b/test/test_recheck.cpp @@ -32,7 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/session_settings.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/time.hpp" diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 114c19202..35241071a 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/write_resume_data.hpp" #include "libtorrent/aux_/path.hpp" #include "libtorrent/aux_/storage_utils.hpp" +#include "libtorrent/random.hpp" #include #include // for bind @@ -185,7 +186,7 @@ std::shared_ptr setup_torrent(file_storage& fs std::vector new_piece(std::size_t const size) { std::vector ret(size); - std::generate(ret.begin(), ret.end(), random_byte); + aux::random_bytes(ret); return ret; } diff --git a/test/test_transfer.cpp b/test/test_transfer.cpp index d1ce33c2f..a61b54838 100644 --- a/test/test_transfer.cpp +++ b/test/test_transfer.cpp @@ -32,7 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/session_settings.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/time.hpp" diff --git a/test/test_utp.cpp b/test/test_utp.cpp index 8eaf689be..0709a05a9 100644 --- a/test/test_utp.cpp +++ b/test/test_utp.cpp @@ -32,7 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session.hpp" #include "libtorrent/session_settings.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/time.hpp" diff --git a/test/test_web_seed_redirect.cpp b/test/test_web_seed_redirect.cpp index 75724311f..505f287b4 100644 --- a/test/test_web_seed_redirect.cpp +++ b/test/test_web_seed_redirect.cpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "setup_transfer.hpp" #include "web_seed_suite.hpp" #include "settings.hpp" +#include "libtorrent/random.hpp" #include "libtorrent/create_torrent.hpp" #include "libtorrent/torrent_info.hpp" @@ -48,8 +49,8 @@ TORRENT_TEST(web_seed_redirect) file_storage fs; int piece_size = 0x4000; - char random_data[16000]; - std::generate(random_data, random_data + sizeof(random_data), random_byte); + std::array random_data; + aux::random_bytes(random_data); file f("test_file", open_mode::write_only, ec); if (ec) { @@ -58,7 +59,7 @@ TORRENT_TEST(web_seed_redirect) TEST_ERROR("failed to create file"); return; } - iovec_t b = { random_data, size_t(16000)}; + iovec_t b = random_data; f.writev(0, b, ec); fs.add_file("test_file", 16000); diff --git a/test/web_seed_suite.cpp b/test/web_seed_suite.cpp index 0b40f3f59..46739d32f 100644 --- a/test/web_seed_suite.cpp +++ b/test/web_seed_suite.cpp @@ -31,7 +31,6 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/session.hpp" -#include "libtorrent/hasher.hpp" #include "libtorrent/file_pool.hpp" #include "libtorrent/aux_/path.hpp" #include "libtorrent/storage.hpp"