From 3406f12ab3469a1155d2b3ab7041c147ed3f24d6 Mon Sep 17 00:00:00 2001 From: airium Date: Mon, 19 Nov 2018 19:12:42 +0800 Subject: [PATCH 1/2] Include &ipv4= for private trackers --- include/libtorrent/tracker_manager.hpp | 2 ++ src/http_tracker_connection.cpp | 11 +++++++++++ src/torrent.cpp | 12 +++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 25d80effb..b54d4d0bf 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -161,6 +161,8 @@ namespace libtorrent boost::uint32_t key; int num_want; + + address_v4 ipv4; #if TORRENT_USE_IPV6 address_v6 ipv6; #endif diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index e13d6b40f..21700a9fa 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -183,6 +183,17 @@ namespace libtorrent } } + if (tracker_req().ipv4 != address_v4() && !i2p) + { + error_code err; + std::string const ip = tracker_req().ipv4.to_string(err); + if (!err) + { + url += "&ipv4="; + url += escape_string(ip.c_str(), ip.size()); + } + } + #if TORRENT_USE_IPV6 if (tracker_req().ipv6 != address_v6() && !i2p) { diff --git a/src/torrent.cpp b/src/torrent.cpp index fc6f18aef..3f343ec5e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3246,17 +3246,19 @@ namespace { req.event = e; -#if TORRENT_USE_IPV6 - // since sending our IPv6 address to the tracker may be sensitive. Only + // since sending our IPv4/v6 address to the tracker may be sensitive. Only // do that if we're not in anonymous mode and if it's a private torrent if (!settings().get_bool(settings_pack::anonymous_mode) && m_torrent_file && m_torrent_file->priv()) { - boost::optional ep = m_ses.get_ipv6_interface(); - if (ep) req.ipv6 = ep->address().to_v6(); - } + boost::optional ep4 = m_ses.get_ipv4_interface(); + if (ep4) req.ipv4 = ep4->address().to_v4(); +#if TORRENT_USE_IPV6 + boost::optional ep6 = m_ses.get_ipv6_interface(); + if (ep6) req.ipv6 = ep6->address().to_v6(); #endif + } // if we are aborting. we don't want any new peers req.num_want = (req.event == tracker_request::stopped) From cdf66409a4bd9e4d8399729082f6813e9cae5094 Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 23 Nov 2018 02:08:57 +0100 Subject: [PATCH 2/2] don't use page aligned disk buffers --- CMakeLists.txt | 1 - Jamfile | 1 - include/libtorrent/Makefile.am | 1 - include/libtorrent/allocator.hpp | 59 ------- include/libtorrent/disk_buffer_pool.hpp | 5 +- include/libtorrent/disk_io_thread.hpp | 1 - include/libtorrent/storage.hpp | 1 - src/Makefile.am | 1 - src/allocator.cpp | 211 ------------------------ src/disk_buffer_pool.cpp | 11 +- src/file.cpp | 1 - 11 files changed, 6 insertions(+), 287 deletions(-) delete mode 100644 include/libtorrent/allocator.hpp delete mode 100644 src/allocator.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b5dca7068..4f79b184c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,6 @@ set(sources web_connection_base alert alert_manager - allocator announce_entry assert bandwidth_limit diff --git a/Jamfile b/Jamfile index fb8c6fa0a..b81241fcd 100644 --- a/Jamfile +++ b/Jamfile @@ -632,7 +632,6 @@ lib iphlpapi : : iphlpapi shared ; SOURCES = alert alert_manager - allocator announce_entry assert bandwidth_limit diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 14bb3a7eb..b495d7a67 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -7,7 +7,6 @@ nobase_include_HEADERS = \ alert_manager.hpp \ alert_types.hpp \ alloca.hpp \ - allocator.hpp \ announce_entry.hpp \ assert.hpp \ bandwidth_limit.hpp \ diff --git a/include/libtorrent/allocator.hpp b/include/libtorrent/allocator.hpp deleted file mode 100644 index 43b5888aa..000000000 --- a/include/libtorrent/allocator.hpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - -Copyright (c) 2009-2018, 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. - -*/ - -#ifndef TORRENT_ALLOCATOR_HPP_INCLUDED -#define TORRENT_ALLOCATOR_HPP_INCLUDED - -#include -#include "libtorrent/config.hpp" - -namespace libtorrent -{ - - TORRENT_EXTRA_EXPORT int page_size(); - - struct TORRENT_EXTRA_EXPORT page_aligned_allocator - { - typedef int size_type; - typedef std::ptrdiff_t difference_type; - - static char* malloc(size_type bytes); - static void free(char* block); -#ifdef TORRENT_DEBUG_BUFFERS - static bool in_use(char const* block); -#endif - }; - -} - -#endif - diff --git a/include/libtorrent/disk_buffer_pool.hpp b/include/libtorrent/disk_buffer_pool.hpp index 4c012ac32..b8b134f74 100644 --- a/include/libtorrent/disk_buffer_pool.hpp +++ b/include/libtorrent/disk_buffer_pool.hpp @@ -43,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #ifndef TORRENT_DISABLE_POOL_ALLOCATOR -#include "libtorrent/allocator.hpp" // for page_aligned_allocator #include #endif @@ -158,7 +157,7 @@ namespace libtorrent #ifndef TORRENT_DISABLE_POOL_ALLOCATOR // if this is true, all buffers are allocated // from m_pool. If this is false, all buffers - // are allocated using page_aligned_allocator. + // are allocated using malloc. // if the settings change to prefer the other // allocator, this bool will not switch over // to match the settings until all buffers have @@ -174,7 +173,7 @@ namespace libtorrent // memory pool for read and write operations // and disk cache - boost::pool m_pool; + boost::pool<> m_pool; #endif // this is specifically exempt from release_asserts diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index d0c1d027e..69194f125 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -35,7 +35,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/storage.hpp" -#include "libtorrent/allocator.hpp" #include "libtorrent/io_service.hpp" #include "libtorrent/sliding_average.hpp" #include "libtorrent/disk_io_job.hpp" diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 25ccd8697..2c79bf29b 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -57,7 +57,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/disk_buffer_holder.hpp" #include "libtorrent/thread.hpp" #include "libtorrent/storage_defs.hpp" -#include "libtorrent/allocator.hpp" #include "libtorrent/file_pool.hpp" // pool_file_status #include "libtorrent/part_file.hpp" #include "libtorrent/stat_cache.hpp" diff --git a/src/Makefile.am b/src/Makefile.am index 70ffc0bd4..9fc844032 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -41,7 +41,6 @@ libtorrent_rasterbar_la_SOURCES = \ web_connection_base.cpp \ alert.cpp \ alert_manager.cpp \ - allocator.cpp \ announce_entry.cpp \ assert.cpp \ bandwidth_limit.cpp \ diff --git a/src/allocator.cpp b/src/allocator.cpp deleted file mode 100644 index 38460acc8..000000000 --- a/src/allocator.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/* - -Copyright (c) 2009-2018, 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/allocator.hpp" -#include "libtorrent/config.hpp" -#include "libtorrent/assert.hpp" // for print_backtrace -#include - -#if defined TORRENT_BEOS -#include -#include // malloc/free -#elif !defined TORRENT_WINDOWS -#include // posix_memalign/free -#include // _SC_PAGESIZE -#endif - -#if TORRENT_USE_MEMALIGN || TORRENT_USE_POSIX_MEMALIGN || defined TORRENT_WINDOWS -#include // memalign and _aligned_malloc -#include // _aligned_malloc on mingw -#endif - -#ifdef TORRENT_WINDOWS -// windows.h must be included after stdlib.h under mingw -#include -#endif - -#ifdef TORRENT_MINGW -#define _aligned_malloc __mingw_aligned_malloc -#define _aligned_free __mingw_aligned_free -#endif - -#ifdef TORRENT_DEBUG_BUFFERS -#ifndef TORRENT_WINDOWS -#include -#endif - -struct alloc_header -{ - boost::int64_t size; - int magic; - char stack[3072]; -}; - -#endif - -namespace libtorrent -{ - - int page_size() - { - static int s = 0; - if (s != 0) return s; - -#ifdef TORRENT_BUILD_SIMULATOR - s = 4096; -#elif defined TORRENT_WINDOWS - SYSTEM_INFO si; - GetSystemInfo(&si); - s = si.dwPageSize; -#elif defined TORRENT_BEOS - s = B_PAGE_SIZE; -#else - s = int(sysconf(_SC_PAGESIZE)); -#endif - // assume the page size is 4 kiB if we - // fail to query it - if (s <= 0) s = 4096; - return s; - } - - char* page_aligned_allocator::malloc(page_aligned_allocator::size_type bytes) - { - TORRENT_ASSERT(bytes > 0); - // just sanity check (this needs to be pretty high - // for cases where the cache size is several gigabytes) - TORRENT_ASSERT(bytes < 0x30000000); - - TORRENT_ASSERT(int(bytes) >= page_size()); -#ifdef TORRENT_DEBUG_BUFFERS - const int page = page_size(); - const int num_pages = (bytes + (page-1)) / page + 2; - const int orig_bytes = bytes; - bytes = num_pages * page; -#endif - - void* ret; -#if TORRENT_USE_POSIX_MEMALIGN - if (posix_memalign(&ret, page_size(), bytes) - != 0) ret = NULL; -#elif TORRENT_USE_MEMALIGN - ret = memalign(page_size(), bytes); -#elif defined TORRENT_WINDOWS - ret = _aligned_malloc(bytes, page_size()); -#elif defined TORRENT_BEOS - area_id id = create_area("", &ret, B_ANY_ADDRESS - , (bytes + page_size() - 1) & (page_size()-1), B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); - if (id < B_OK) return NULL; -#else - ret = valloc(size_t(bytes)); -#endif - if (ret == NULL) return NULL; - -#ifdef TORRENT_DEBUG_BUFFERS - // make the two surrounding pages non-readable and -writable - alloc_header* h = static_cast(ret); - h->size = orig_bytes; - h->magic = 0x1337; - print_backtrace(h->stack, sizeof(h->stack)); - -#ifdef TORRENT_WINDOWS -#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, NULL) -#define PROT_READ PAGE_READONLY -#endif - mprotect(ret, page, PROT_READ); - mprotect(static_cast(ret) + (num_pages-1) * page, page, PROT_READ); - -#ifdef TORRENT_WINDOWS -#undef mprotect -#undef PROT_READ -#endif -// fprintf(stderr, "malloc: %p head: %p tail: %p size: %d\n", ret + page, ret, ret + page + bytes, int(bytes)); - - return static_cast(ret) + page; -#else - return static_cast(ret); -#endif // TORRENT_DEBUG_BUFFERS - } - - void page_aligned_allocator::free(char* block) - { - if (block == 0) return; - -#ifdef TORRENT_DEBUG_BUFFERS - -#ifdef TORRENT_WINDOWS -#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, NULL) -#define PROT_READ PAGE_READONLY -#define PROT_WRITE PAGE_READWRITE -#endif - const int page = page_size(); - // make the two surrounding pages non-readable and -writable - mprotect(block - page, page, PROT_READ | PROT_WRITE); - alloc_header* h = reinterpret_cast(block - page); - const int num_pages = (h->size + (page-1)) / page + 2; - TORRENT_ASSERT(h->magic == 0x1337); - mprotect(block + (num_pages-2) * page, page, PROT_READ | PROT_WRITE); -// fprintf(stderr, "free: %p head: %p tail: %p size: %d\n", block, block - page, block + h->size, int(h->size)); - h->magic = 0; - block -= page; - -#ifdef TORRENT_WINDOWS -#undef mprotect -#undef PROT_READ -#undef PROT_WRITE -#endif - - print_backtrace(h->stack, sizeof(h->stack)); - -#endif // TORRENT_DEBUG_BUFFERS - -#ifdef TORRENT_WINDOWS - _aligned_free(block); -#elif defined TORRENT_BEOS - area_id id = area_for(block); - if (id < B_OK) return; - delete_area(id); -#else - ::free(block); -#endif // TORRENT_WINDOWS - } - -#ifdef TORRENT_DEBUG_BUFFERS - bool page_aligned_allocator::in_use(char const* block) - { - const int page = page_size(); - alloc_header const* h = reinterpret_cast(block - page); - return h->magic == 0x1337; - } -#endif - -} - diff --git a/src/disk_buffer_pool.cpp b/src/disk_buffer_pool.cpp index d3956fd1d..969ad2702 100644 --- a/src/disk_buffer_pool.cpp +++ b/src/disk_buffer_pool.cpp @@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/disk_buffer_pool.hpp" #include "libtorrent/assert.hpp" -#include "libtorrent/allocator.hpp" #include "libtorrent/aux_/session_settings.hpp" #include "libtorrent/io_service.hpp" #include "libtorrent/alert.hpp" @@ -185,8 +184,6 @@ namespace libtorrent #if defined TORRENT_DEBUG return m_buffers_in_use.count(buffer) == 1; -#elif defined TORRENT_DEBUG_BUFFERS - return page_aligned_allocator::in_use(buffer); #elif defined TORRENT_DISABLE_POOL_ALLOCATOR return true; #else @@ -290,7 +287,7 @@ namespace libtorrent { #if defined TORRENT_DISABLE_POOL_ALLOCATOR - ret = page_aligned_allocator::malloc(m_block_size); + ret = static_cast(std::malloc(m_block_size)); #else if (m_using_pool_allocator) @@ -306,7 +303,7 @@ namespace libtorrent } else { - ret = page_aligned_allocator::malloc(m_block_size); + ret = static_cast(std::malloc(m_block_size)); } #endif if (ret == NULL) @@ -549,13 +546,13 @@ namespace libtorrent { #if defined TORRENT_DISABLE_POOL_ALLOCATOR - page_aligned_allocator::free(buf); + std::free(buf); #else if (m_using_pool_allocator) m_pool.free(buf); else - page_aligned_allocator::free(buf); + std::free(buf); #endif // TORRENT_DISABLE_POOL_ALLOCATOR } diff --git a/src/file.cpp b/src/file.cpp index c2a08bc9e..a792f6a6e 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -64,7 +64,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/alloca.hpp" -#include "libtorrent/allocator.hpp" // page_size #include "libtorrent/file.hpp" #include #include