diff --git a/CMakeLists.txt b/CMakeLists.txt index 62b84f732..6a959bd5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -288,6 +288,7 @@ set(sources error_code file_storage file_progress + generate_peer_id lazy_bdecode escape_string string_util diff --git a/Jamfile b/Jamfile index 7a86ea6f2..6eca252e7 100644 --- a/Jamfile +++ b/Jamfile @@ -737,6 +737,7 @@ SOURCES = add_torrent_params peer_info stack_allocator + generate_peer_id # -- extensions -- ut_pex diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 3be632873..9a8df6104 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -175,6 +175,7 @@ nobase_include_HEADERS = \ aux_/deque.hpp \ aux_/escape_string.hpp \ aux_/export.hpp \ + aux_/generate_peer_id.hpp \ aux_/io.hpp \ aux_/listen_socket_handle.hpp \ aux_/max_path.hpp \ diff --git a/include/libtorrent/aux_/generate_peer_id.hpp b/include/libtorrent/aux_/generate_peer_id.hpp new file mode 100644 index 000000000..8c4c3ebd6 --- /dev/null +++ b/include/libtorrent/aux_/generate_peer_id.hpp @@ -0,0 +1,48 @@ +/* + +Copyright (c) 2003-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_GENERATE_PEER_ID_HPP_INCLUDED +#define TORRENT_GENERATE_PEER_ID_HPP_INCLUDED + +#include "libtorrent/peer_id.hpp" +#include "libtorrent/aux_/export.hpp" + +namespace libtorrent { namespace aux { + +struct session_settings; + +TORRENT_EXTRA_EXPORT peer_id generate_peer_id(session_settings const& sett); + +}} + +#endif + diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 746c1e08a..0c4795213 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -104,8 +104,6 @@ namespace libtorrent { struct tracker_request; class bt_peer_connection; - peer_id generate_peer_id(aux::session_settings const& sett); - enum class waste_reason { piece_timed_out, piece_cancelled, piece_unknown, piece_seed diff --git a/src/Makefile.am b/src/Makefile.am index b5b29b251..ad918ba10 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -80,6 +80,7 @@ libtorrent_rasterbar_la_SOURCES = \ file_pool.cpp \ file_storage.cpp \ fingerprint.cpp \ + generate_peer_id.cpp \ gzip.cpp \ hasher.cpp \ hex.cpp \ diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 923ea10fa..ccc363e56 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -62,6 +62,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/alert_manager.hpp" // for alert_manager #include "libtorrent/string_util.hpp" // for search +#include "libtorrent/aux_/generate_peer_id.hpp" #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) #include "libtorrent/pe_crypto.hpp" @@ -154,7 +155,7 @@ namespace { , m_rc4_encrypted(false) , m_recv_buffer(peer_connection::m_recv_buffer) #endif - , m_our_peer_id(generate_peer_id(*pack.sett)) + , m_our_peer_id(aux::generate_peer_id(*pack.sett)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "CONSTRUCT", "bt_peer_connection"); diff --git a/src/generate_peer_id.cpp b/src/generate_peer_id.cpp new file mode 100644 index 000000000..17104b328 --- /dev/null +++ b/src/generate_peer_id.cpp @@ -0,0 +1,55 @@ +/* + +Copyright (c) 2003-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/aux_/generate_peer_id.hpp" +#include "libtorrent/aux_/session_settings.hpp" +#include "libtorrent/peer_id.hpp" +#include "libtorrent/string_util.hpp" // for url_random + +#include + +namespace libtorrent { namespace aux { + +peer_id generate_peer_id(session_settings const& sett) +{ + peer_id ret; + std::string print = sett.get_str(settings_pack::peer_fingerprint); + if (print.size() > ret.size()) print.resize(ret.size()); + + // the client's fingerprint + std::copy(print.begin(), print.end(), ret.begin()); + if (print.length() < ret.size()) + url_random(span(ret).subspan(print.length())); + return ret; +} + +}} diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 7fc8ef1b5..a86ff3d97 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -88,6 +88,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/hex.hpp" // to_hex, from_hex #include "libtorrent/aux_/scope_end.hpp" #include "libtorrent/aux_/set_socket_buffer.hpp" +#include "libtorrent/aux_/generate_peer_id.hpp" #ifndef TORRENT_DISABLE_LOGGING @@ -3002,7 +3003,7 @@ namespace aux { #if TORRENT_ABI_VERSION == 1 peer_id session_impl::deprecated_get_peer_id() const { - return generate_peer_id(m_settings); + return aux::generate_peer_id(m_settings); } #endif diff --git a/src/torrent.cpp b/src/torrent.cpp index e633b3311..1b5ac12b9 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -100,6 +100,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/numeric_cast.hpp" #include "libtorrent/aux_/path.hpp" #include "libtorrent/aux_/set_socket_buffer.hpp" +#include "libtorrent/aux_/generate_peer_id.hpp" #ifndef TORRENT_DISABLE_LOGGING #include "libtorrent/aux_/session_impl.hpp" // for tracker_logger @@ -147,19 +148,6 @@ bool is_downloading_state(int const st) peer_info.web_seed = true; } - peer_id generate_peer_id(aux::session_settings const& sett) - { - peer_id ret; - std::string print = sett.get_str(settings_pack::peer_fingerprint); - if (print.size() > ret.size()) print.resize(ret.size()); - - // the client's fingerprint - std::copy(print.begin(), print.end(), ret.begin()); - if (print.length() < ret.size()) - url_random(span(ret).subspan(print.length())); - return ret; - } - torrent_hot_members::torrent_hot_members(aux::session_interface& ses , add_torrent_params const& p, bool const session_paused) : m_ses(ses) @@ -200,7 +188,7 @@ bool is_downloading_state(int const st) , m_info_hash(p.info_hash) , m_error_file(torrent_status::error_file_none) , m_sequence_number(-1) - , m_peer_id(generate_peer_id(settings())) + , m_peer_id(aux::generate_peer_id(settings())) , m_announce_to_trackers(!(p.flags & torrent_flags::paused)) , m_announce_to_lsd(!(p.flags & torrent_flags::paused)) , m_has_incoming(false) diff --git a/test/Jamfile b/test/Jamfile index 9769f8c1f..45ff6676a 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -139,6 +139,7 @@ test-suite libtorrent : test_linked_list.cpp test_stack_allocator.cpp test_file_progress.cpp + test_generate_peer_id.cpp test_alloca.cpp ] [ run test_listen_socket.cpp @@ -156,9 +157,7 @@ test-suite libtorrent : openssl:/torrent//crypto ] - [ run test_string.cpp - test_utf8.cpp - ] + [ run test_string.cpp test_utf8.cpp ] [ run test_sha1_hash.cpp ] [ run test_span.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index 0fcfa7a30..a57b1a082 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -199,7 +199,8 @@ test_primitives_SOURCES = \ test_fence.cpp \ test_dos_blocker.cpp \ test_upnp.cpp \ - test_flags.cpp + test_flags.cpp \ + test_generate_peer_id.cpp test_recheck_SOURCES = test_recheck.cpp test_stat_cache_SOURCES = test_stat_cache.cpp diff --git a/test/test_generate_peer_id.cpp b/test/test_generate_peer_id.cpp new file mode 100644 index 000000000..b32ca70f9 --- /dev/null +++ b/test/test_generate_peer_id.cpp @@ -0,0 +1,56 @@ +/* + +Copyright (c) 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 "test.hpp" +#include "libtorrent/aux_/generate_peer_id.hpp" +#include "libtorrent/aux_/session_settings.hpp" +#include "libtorrent/aux_/escape_string.hpp" +#include "libtorrent/settings_pack.hpp" + +TORRENT_TEST(generate_peer_id) +{ + lt::aux::session_settings sett; + sett.set_str(lt::settings_pack::peer_fingerprint, "abc"); + lt::peer_id const id = lt::aux::generate_peer_id(sett); + + TEST_CHECK(std::equal(id.begin(), id.begin() + 3, "abc")); + TEST_CHECK(!lt::need_encoding(id.data(), int(id.size()))); +} + +TORRENT_TEST(generate_peer_id_truncate) +{ + lt::aux::session_settings sett; + sett.set_str(lt::settings_pack::peer_fingerprint, "abcdefghijklmnopqrstuvwxyz"); + lt::peer_id const id = lt::aux::generate_peer_id(sett); + + TEST_CHECK(std::equal(id.begin(), id.end(), "abcdefghijklmnopqrst")); +}