factor out generate_peer_id into its own file

This commit is contained in:
Arvid Norberg 2018-07-23 09:56:09 +02:00 committed by Arvid Norberg
parent a1f2995716
commit 4282dd072a
13 changed files with 173 additions and 22 deletions

View File

@ -288,6 +288,7 @@ set(sources
error_code
file_storage
file_progress
generate_peer_id
lazy_bdecode
escape_string
string_util

View File

@ -737,6 +737,7 @@ SOURCES =
add_torrent_params
peer_info
stack_allocator
generate_peer_id
# -- extensions --
ut_pex

View File

@ -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 \

View File

@ -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

View File

@ -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

View File

@ -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 \

View File

@ -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");

55
src/generate_peer_id.cpp Normal file
View File

@ -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 <string>
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<char>(ret).subspan(print.length()));
return ret;
}
}}

View File

@ -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

View File

@ -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<char>(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)

View File

@ -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 :
<crypto>openssl:<library>/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 ]

View File

@ -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

View File

@ -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"));
}