From b25ea5329f31c60864d7c018550b5c6b61e85b7d Mon Sep 17 00:00:00 2001 From: tnextday Date: Wed, 16 Nov 2016 22:41:57 +0800 Subject: [PATCH 1/6] updates `m_last_sent` in `peer_connection::setup_send()` instand of `peer_connection::keep_alive()` --- src/peer_connection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 670b6fc97..b9ddb69d9 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -5698,6 +5698,7 @@ namespace libtorrent } m_channel_state[upload_channel] |= peer_info::bw_network; + m_last_sent = aux::time_now(); } void peer_connection::on_disk() @@ -6847,7 +6848,6 @@ namespace libtorrent peer_log(peer_log_alert::outgoing_message, "KEEPALIVE"); #endif - m_last_sent = aux::time_now(); write_keepalive(); } From f83e16f0cc1ef6708aca52e13645d53266681ba5 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 19 Nov 2016 11:22:02 -0500 Subject: [PATCH 2/6] add unit test for stack allocator --- test/Jamfile | 1 + test/Makefile.am | 2 + test/test_stack_allocator.cpp | 93 +++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 test/test_stack_allocator.cpp diff --git a/test/Jamfile b/test/Jamfile index 58b9c3f2b..da85b860c 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -120,6 +120,7 @@ test-suite libtorrent : test_stat_cache.cpp test_enum_net.cpp test_linked_list.cpp + test_stack_allocator.cpp test_file_progress.cpp ] [ run test_gzip.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index a3c966dc8..69950084f 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -21,6 +21,7 @@ test_programs = \ test_receive_buffer \ test_resume \ test_ssl \ + test_stack_allocator \ test_storage \ test_time_critical \ test_torrent \ @@ -209,6 +210,7 @@ test_receive_buffer_SOURCES = test_receive_buffer.cpp test_storage_SOURCES = test_storage.cpp test_time_critical_SOURCES = test_time_critical.cpp test_resume_SOURCES = test_resume.cpp +test_stack_allocator_SOURCES = test_stack_allocator.cpp test_ssl_SOURCES = test_ssl.cpp test_torrent_SOURCES = test_torrent.cpp test_tracker_SOURCES = test_tracker.cpp diff --git a/test/test_stack_allocator.cpp b/test/test_stack_allocator.cpp new file mode 100644 index 000000000..62b1971f2 --- /dev/null +++ b/test/test_stack_allocator.cpp @@ -0,0 +1,93 @@ +/* + +Copyright (c) 2016, 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 "test.hpp" +#include "libtorrent/stack_allocator.hpp" + +using libtorrent::aux::stack_allocator; + +TORRENT_TEST(copy_string) +{ + stack_allocator a; + int const idx1 = a.copy_string("testing"); + + // attempt to trigger a reallocation + a.allocate(100000); + + int const idx2 = a.copy_string(std::string("foobar")); + + + TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0); + TEST_CHECK(strcmp(a.ptr(idx2), "foobar") == 0); +} + +TORRENT_TEST(copy_buffer) +{ + stack_allocator a; + int const idx1 = a.copy_buffer("testing", 8); + + // attempt to trigger a reallocation + a.allocate(100000); + + TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0); +} + +TORRENT_TEST(allocate) +{ + stack_allocator a; + int const idx1 = a.allocate(100); + char* ptr = a.ptr(idx1); + for (int i = 0; i < 100; ++i) + ptr[i] = char(i % 256); + + // attempt to trigger a reallocation + a.allocate(100000); + + ptr = a.ptr(idx1); + for (int i = 0; i < 100; ++i) + TEST_CHECK(ptr[i] == char(i % 256)); +} + +TORRENT_TEST(swap) +{ + stack_allocator a1; + stack_allocator a2; + + int const idx1 = a1.copy_string("testing"); + int const idx2 = a2.copy_string("foobar"); + + a1.swap(a2); + + TEST_CHECK(strcmp(a1.ptr(idx2), "foobar") == 0); + TEST_CHECK(strcmp(a2.ptr(idx1), "testing") == 0); +} + From f201cf5eebd1634ff012b7d11ffdb5e5df2645ec Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 19 Nov 2016 14:27:26 -0500 Subject: [PATCH 3/6] fix Changelog --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index a8ae48928..fbe011249 100644 --- a/ChangeLog +++ b/ChangeLog @@ -70,6 +70,7 @@ * deprecate time functions, expose typedefs of boost::chrono in the libtorrent namespace instead * deprecate file_base feature in file_storage/torrent_info + * changed default piece and file priority to 4 (previously 1) * improve piece picker support for reverse picking (used for snubbed peers) to not cause priority inversion for regular peers * improve piece picker to better support torrents with very large pieces From 721a607593c3f04c42c1447fa837c0de46b52405 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 19 Nov 2016 14:33:04 -0500 Subject: [PATCH 4/6] update piece priority documentation --- include/libtorrent/torrent_handle.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 63e534306..f4449fcf9 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -1000,8 +1000,10 @@ namespace libtorrent // You may however change the priority of individual pieces. There are 8 // priority levels. 0 means not to download the piece at all. Otherwise, // lower priority values means less likely to be picked. Piece priority - // takes precedence over piece availability. Every priority-7 piece will - // be attempted to be picked before a priority 6 piece and so on. + // takes precedence over piece availability. Every piece with priority 7 + // will be attempted to be picked before a priority 6 piece and so on. + // + // The default priority of pieces is 4. // // Piece priorities can not be changed for torrents that have not // downloaded the metadata yet. For instance, magnet links and torrents From 4df1ecbe25f198e8f3f2c88a7f4ea7b9c1a779bf Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 19 Nov 2016 14:19:35 -0500 Subject: [PATCH 5/6] re-enable some warnings and fix code --- Jamfile | 6 +++--- include/libtorrent/aux_/disable_warnings_push.hpp | 5 +++++ include/libtorrent/stat.hpp | 3 +++ src/file.cpp | 4 ++-- src/kademlia/dos_blocker.cpp | 6 +++--- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Jamfile b/Jamfile index 5580ccf91..dd4133138 100644 --- a/Jamfile +++ b/Jamfile @@ -234,11 +234,11 @@ rule warnings ( properties * ) result += -Wno-padded ; result += -Wno-global-constructors ; result += -Wno-c++98-compat ; +# this warns on any global static object, which are used for error_category +# objects result += -Wno-exit-time-destructors ; - result += -Wno-documentation-unknown-command ; - result += -Wno-disabled-macro-expansion ; result += -Wno-unused-command-line-argument ; - result += -Wno-error=implicit-fallthrough ; + result += -Wno-implicit-fallthrough ; result += -Wno-c++11-long-long ; result += -Wno-variadic-macros ; diff --git a/include/libtorrent/aux_/disable_warnings_push.hpp b/include/libtorrent/aux_/disable_warnings_push.hpp index e124fd308..cd9792519 100644 --- a/include/libtorrent/aux_/disable_warnings_push.hpp +++ b/include/libtorrent/aux_/disable_warnings_push.hpp @@ -47,6 +47,7 @@ POSSIBILITY OF SUCH DAMAGE. #ifdef __clang__ #pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-pragmas" #pragma clang diagnostic ignored "-Wall" #pragma clang diagnostic ignored "-Weverything" #pragma clang diagnostic ignored "-Wsign-conversion" @@ -70,6 +71,10 @@ POSSIBILITY OF SUCH DAMAGE. #pragma clang diagnostic ignored "-Wunused-parameter" #pragma clang diagnostic ignored "-Wreserved-id-macro" #pragma clang diagnostic ignored "-Wunused-local-typedef" +#pragma clang diagnostic ignored "-Wdouble-promotion" +#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" +#pragma clang diagnostic ignored "-Wexit-time-destructors" +#pragma clang diagnostic ignored "-Wdocumentation-unknown-command" #endif #ifdef _MSC_VER diff --git a/include/libtorrent/stat.hpp b/include/libtorrent/stat.hpp index a1e209456..18e14e617 100644 --- a/include/libtorrent/stat.hpp +++ b/include/libtorrent/stat.hpp @@ -38,7 +38,10 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include + +#include "libtorrent/aux_/disable_warnings_push.hpp" #include +#include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/config.hpp" diff --git a/src/file.cpp b/src/file.cpp index 2ad6ec50a..13bffc180 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -37,7 +37,9 @@ POSSIBILITY OF SUCH DAMAGE. #ifdef __clang__ #pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-pragmas" #pragma clang diagnostic ignored "-Wunused-macros" +#pragma clang diagnostic ignored "-Wreserved-id-macro" #endif // these defines are just in case the system we're on needs them for 64 bit file @@ -141,8 +143,6 @@ POSSIBILITY OF SUCH DAMAGE. #endif -#undef _FILE_OFFSET_BITS - // make sure the _FILE_OFFSET_BITS define worked // on this platform. It's supposed to make file // related functions support 64-bit offsets. diff --git a/src/kademlia/dos_blocker.cpp b/src/kademlia/dos_blocker.cpp index 7221e66cd..f4808657e 100644 --- a/src/kademlia/dos_blocker.cpp +++ b/src/kademlia/dos_blocker.cpp @@ -77,16 +77,16 @@ namespace libtorrent { namespace dht if (match->count == m_message_rate_limit * 10) { #ifndef TORRENT_DISABLE_LOGGING - logger->log(dht_logger::tracker, "BANNING PEER [ ip: %s time: %f count: %d ]" + logger->log(dht_logger::tracker, "BANNING PEER [ ip: %s time: %d ms count: %d ]" , print_address(addr).c_str() - , total_milliseconds((now - match->limit) + seconds(10)) / 1000.f + , int(total_milliseconds((now - match->limit) + seconds(10))) , int(match->count)); #endif // we've received too many messages in less than 10 seconds // from this node. Ignore it until it's silent for 5 minutes match->limit = now + seconds(m_block_timeout); } - + return false; } From c1224f72c30f86a748f9d29c9f452be861720efc Mon Sep 17 00:00:00 2001 From: tnextday Date: Sun, 20 Nov 2016 23:32:20 +0800 Subject: [PATCH 6/6] add urlseed timeout test (#1340) updates m_last_sent in peer_connection::setup_send() instead of peer_connection::keep_alive() --- simulation/libsimulator | 2 +- simulation/test_web_seed.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/simulation/libsimulator b/simulation/libsimulator index 3eae1da7d..e45086495 160000 --- a/simulation/libsimulator +++ b/simulation/libsimulator @@ -1 +1 @@ -Subproject commit 3eae1da7de7d13ab26472c4a44ee4477ce24fa26 +Subproject commit e450864958668f8c2ecf8b9839fa278c9c797571 diff --git a/simulation/test_web_seed.cpp b/simulation/test_web_seed.cpp index da99e5dc8..20208c510 100644 --- a/simulation/test_web_seed.cpp +++ b/simulation/test_web_seed.cpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/settings_pack.hpp" #include "libtorrent/deadline_timer.hpp" #include "libtorrent/torrent_info.hpp" +#include "libtorrent/alert_types.hpp" #include "simulator/http_server.hpp" #include "settings.hpp" #include "libtorrent/create_torrent.hpp" @@ -161,3 +162,37 @@ TORRENT_TEST(single_file_torrent) TEST_CHECK(expected); } +TORRENT_TEST(urlseed_timeout) +{ + bool timeout = false; + run_test( + [](lt::session& ses) + { + file_storage fs; + fs.add_file("timeout_test", 0x8000); + lt::add_torrent_params params; + params.ti = ::create_torrent(fs); + params.url_seeds.push_back("http://2.2.2.2:8080/"); + params.flags &= ~lt::add_torrent_params::flag_auto_managed; + params.flags &= ~lt::add_torrent_params::flag_paused; + params.save_path = "."; + ses.async_add_torrent(params); + }, + [&timeout](lt::session& ses, lt::alert const* alert) { + const lt::peer_disconnected_alert *pda = lt::alert_cast(alert); + if (pda && pda->error == errors::timed_out_inactivity){ + timeout = true; + } + }, + [](sim::simulation& sim, lt::session& ses) + { + sim::asio::io_service web_server(sim, address_v4::from_string("2.2.2.2")); + + // listen on port 8080 + sim::http_server http(web_server, 8080); + http.register_stall_handler("/timeout_test"); + sim.run(); + } + ); + TEST_EQUAL(timeout, true); +} \ No newline at end of file