From 764b09d6b55f5d03849fd30c0249439b296b80b8 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 31 Dec 2014 15:51:45 +0000 Subject: [PATCH] make the peer fingerprint a regular setting and remove it from the main session constructor argument list --- include/libtorrent/aux_/session_impl.hpp | 4 ++-- include/libtorrent/fingerprint.hpp | 2 +- include/libtorrent/session.hpp | 24 +++++++++++--------- include/libtorrent/settings_pack.hpp | 6 ++++- set_version.py | 11 +++++++++ src/session.cpp | 10 +++++--- src/session_impl.cpp | 29 +++++++++++++----------- src/settings_pack.cpp | 3 ++- test/setup_transfer.cpp | 27 +++++++++++----------- test/swarm_suite.cpp | 6 ++--- test/test_auto_unchoke.cpp | 6 ++--- test/test_checking.cpp | 2 +- test/test_lsd.cpp | 4 ++-- test/test_pex.cpp | 6 ++--- test/test_privacy.cpp | 2 +- test/test_rss.cpp | 3 +-- test/test_session.cpp | 2 +- test/test_storage.cpp | 8 +++---- test/test_torrent.cpp | 2 +- 19 files changed, 91 insertions(+), 66 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index f60240e69..ba9140e54 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -112,7 +112,6 @@ namespace libtorrent class upnp; class natpmp; class lsd; - struct fingerprint; class torrent; class alert; struct cache_info; @@ -194,7 +193,7 @@ namespace libtorrent typedef std::map > torrent_map; #endif - session_impl(fingerprint const& cl_fprint); + session_impl(); virtual ~session_impl(); void init(); @@ -636,6 +635,7 @@ namespace libtorrent void update_lsd(); void update_dht(); void update_count_slow(); + void update_peer_fingerprint(); void on_trigger_auto_manage(); diff --git a/include/libtorrent/fingerprint.hpp b/include/libtorrent/fingerprint.hpp index 2527d5be8..880a1289b 100644 --- a/include/libtorrent/fingerprint.hpp +++ b/include/libtorrent/fingerprint.hpp @@ -97,7 +97,7 @@ namespace libtorrent { char s[100]; snprintf(s, 100, "-%c%c%c%c%c%c-" - , name[0], name[1] + , name[0], name[1] , version_to_char(major_version) , version_to_char(minor_version) , version_to_char(revision_version) diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 5cd26e512..c04e35c62 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -201,15 +201,11 @@ namespace libtorrent // The ``alert_mask`` is the same mask that you would send to // set_alert_mask(). - // TODO: 3 could the fingerprint be a setting as well? And should the - // settings_pack be optional? session(settings_pack const& pack - , fingerprint const& print = fingerprint("LT" - , LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0) , int flags = start_default_features | add_default_plugins) { TORRENT_CFG(); - init(print); + init(); start(flags, pack); } session(fingerprint const& print = fingerprint("LT" @@ -220,6 +216,7 @@ namespace libtorrent TORRENT_CFG(); settings_pack pack; pack.set_int(settings_pack::alert_mask, alert_mask); + pack.set_str(settings_pack::peer_fingerprint, print.to_string()); if ((flags & start_default_features) == 0) { pack.set_bool(settings_pack::enable_upnp, false); @@ -228,7 +225,7 @@ namespace libtorrent pack.set_bool(settings_pack::enable_dht, false); } - init(print); + init(); start(flags, pack); } session(fingerprint const& print @@ -244,6 +241,7 @@ namespace libtorrent settings_pack pack; pack.set_int(settings_pack::alert_mask, alert_mask); pack.set_int(settings_pack::max_retry_port_bind, listen_port_range.second - listen_port_range.first); + pack.set_str(settings_pack::peer_fingerprint, print.to_string()); char if_string[100]; snprintf(if_string, sizeof(if_string), "%s:%d", listen_interface, listen_port_range.first); pack.set_str(settings_pack::listen_interfaces, if_string); @@ -255,7 +253,7 @@ namespace libtorrent pack.set_bool(settings_pack::enable_lsd, false); pack.set_bool(settings_pack::enable_dht, false); } - init(print); + init(); start(flags, pack); } @@ -744,9 +742,13 @@ namespace libtorrent // anti-virus software by connecting to SMTP, FTP ports. void set_port_filter(port_filter const& f); - // sets and gets the raw peer ID used by libtorrent. When anonymous - // mode is set the peer ID is randomized per peer anyway. - void set_peer_id(peer_id const& pid); +#ifndef TORRENT_NO_DEPRECATE + // deprecated in 1.1, use settings_pack::peer_fingerprint instead + TORRENT_DEPRECATED_PREFIX + void set_peer_id(peer_id const& pid) TORRENT_DEPRECATED; +#endif + // returns the raw peer ID used by libtorrent. When anonymous mode is set + // the peer ID is randomized per peer. peer_id id() const; // sets the key sent to trackers. If it's not set, it is initialized @@ -1218,7 +1220,7 @@ namespace libtorrent private: - void init(fingerprint const& id); + void init(); void start(int flags, settings_pack const& pack); // data shared between the main thread diff --git a/include/libtorrent/settings_pack.hpp b/include/libtorrent/settings_pack.hpp index fc11d2f87..c010fa1f8 100644 --- a/include/libtorrent/settings_pack.hpp +++ b/include/libtorrent/settings_pack.hpp @@ -68,7 +68,6 @@ namespace libtorrent #endif // TODO: 2 add an API to query a settings_pack as well - // TODO: 2 maybe convert all bool types into int-types as well // The ``settings_pack`` struct, contains the names of all settings as // enum values. These values are passed in to the ``set_str()``, @@ -194,6 +193,11 @@ namespace libtorrent // .. _i2p: http://www.i2p2.de i2p_hostname, + // this is the fingerprint for the client. It will be used as the + // prefix to the peer_id. If this is 20 bytes (or longer) it will + // be used as the peer-id + peer_fingerprint, + max_string_setting_internal, num_string_settings = max_string_setting_internal - string_type_base }; diff --git a/set_version.py b/set_version.py index f6192bada..1153fa6a1 100644 --- a/set_version.py +++ b/set_version.py @@ -2,9 +2,17 @@ import os import sys import glob +import re version = (int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])) +def v(version): + ret = () + for i in version: + if i < 9: ret = ret + (chr(ord('0') + i),) + else: ret = ret + (chr(ord('A') + i - 10),) + return ret + def substitute_file(name): subst = '' f = open(name) @@ -29,6 +37,8 @@ def substitute_file(name): l = "\tversion = '%d.%d.%d',\n" % (version[0], version[1], version[2]) elif "version = '" in l and name.endswith('setup.py'): l = "\tversion = '%d.%d.%d',\n" % (version[0], version[1], version[2]) + elif '"-LT' in l and name.endswith('settings_pack.cpp'): + l = re.sub('"-LT[0-9A-Za-z]{4}-"', '"-LT%c%c%c%c-"' % v(version), l) subst += l @@ -41,6 +51,7 @@ substitute_file('CMakeLists.txt') substitute_file('configure.ac') substitute_file('bindings/python/setup.py') substitute_file('docs/gen_reference_doc.py') +substitute_file('src/settings_pack.cpp') for i in glob.glob('docs/*.rst'): substitute_file(i) substitute_file('Jamfile') diff --git a/src/session.cpp b/src/session.cpp index 08ef53138..540e4adc4 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -391,7 +391,7 @@ namespace libtorrent { throw; } #endif - void session::init(fingerprint const& id) + void session::init() { #if defined _MSC_VER && defined TORRENT_DEBUG // workaround for microsofts @@ -400,7 +400,7 @@ namespace libtorrent ::_set_se_translator(straight_to_debugger); #endif - m_impl.reset(new session_impl(id)); + m_impl.reset(new session_impl()); } void session::start(int flags, settings_pack const& pack) @@ -534,10 +534,14 @@ namespace libtorrent TORRENT_ASYNC_CALL1(set_port_filter, f); } +#ifndef TORRENT_NO_DEPRECATE void session::set_peer_id(peer_id const& id) { - TORRENT_ASYNC_CALL1(set_peer_id, id); + settings_pack p; + p.set_str(settings_pack::peer_fingerprint, id.to_string()); + apply_settings(p); } +#endif peer_id session::id() const { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 268ee7d30..6c0bc91dd 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -337,7 +337,7 @@ namespace aux { } #endif - session_impl::session_impl(fingerprint const& cl_fprint) + session_impl::session_impl() : #ifndef TORRENT_DISABLE_POOL_ALLOCATOR m_send_buffers(send_buffer_size()) @@ -445,18 +445,6 @@ namespace aux { error_code ec; m_listen_interface = tcp::endpoint(address_v4::any(), 0); TORRENT_ASSERT_VAL(!ec, ec); - - // ---- generate a peer id ---- - std::string print = cl_fprint.to_string(); - TORRENT_ASSERT_VAL(print.length() <= 20, print.length()); - - // the client's fingerprint - std::copy( - print.begin() - , print.begin() + print.length() - , m_peer_id.begin()); - - url_random((char*)&m_peer_id[print.length()], (char*)&m_peer_id[0] + 20); } void session_impl::start_session(settings_pack const& pack) @@ -588,6 +576,7 @@ namespace aux { update_natpmp(); update_lsd(); update_dht(); + update_peer_fingerprint(); if (m_listen_sockets.empty()) { @@ -5040,6 +5029,20 @@ retry: #endif } + void session_impl::update_peer_fingerprint() + { + // ---- generate a peer id ---- + std::string print = m_settings.get_str(settings_pack::peer_fingerprint); + if (print.size() > 20) print.resize(20); + + // the client's fingerprint + std::copy(print.begin(), print.begin() + print.length(), m_peer_id.begin()); + if (print.length() < 20) + { + url_random((char*)&m_peer_id[print.length()], (char*)&m_peer_id[0] + 20); + } + } + void session_impl::update_count_slow() { error_code ec; diff --git a/src/settings_pack.cpp b/src/settings_pack.cpp index 2ef5fd0e5..9fca8b244 100644 --- a/src/settings_pack.cpp +++ b/src/settings_pack.cpp @@ -127,7 +127,8 @@ namespace libtorrent SET_NOPREV(proxy_hostname, "", &session_impl::update_proxy), SET_NOPREV(proxy_username, "", &session_impl::update_proxy), SET_NOPREV(proxy_password, "", &session_impl::update_proxy), - SET_NOPREV(i2p_hostname, "", &session_impl::update_i2p_bridge) + SET_NOPREV(i2p_hostname, "", &session_impl::update_i2p_bridge), + SET_NOPREV(peer_fingerprint, "-LT1100-", &session_impl::update_peer_fingerprint) }; bool_setting_entry_t bool_settings[settings_pack::num_bool_settings] = diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 9d0f5d8a4..6f3c37b20 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -673,8 +673,8 @@ setup_transfer(lt::session* ses1, lt::session* ses2, lt::session* ses3 , add_torrent_params const* p, bool stop_lsd, bool use_ssl_ports , boost::shared_ptr* torrent2) { - assert(ses1); - assert(ses2); + TORRENT_ASSERT(ses1); + TORRENT_ASSERT(ses2); if (stop_lsd) { @@ -700,23 +700,24 @@ setup_transfer(lt::session* ses1, lt::session* ses2, lt::session* ses3 if (ses3) pack.set_bool(settings_pack::allow_multiple_connections_per_ip, true); pack.set_int(settings_pack::mixed_mode_algorithm, settings_pack::prefer_tcp); pack.set_int(settings_pack::max_failcount, 1); - ses1->apply_settings(pack); - ses2->apply_settings(pack); - if (ses3) ses3->apply_settings(pack); - peer_id pid; std::generate(&pid[0], &pid[0] + 20, random_byte); - ses1->set_peer_id(pid); + pack.set_str(settings_pack::peer_fingerprint, pid.to_string()); + ses1->apply_settings(pack); + std::generate(&pid[0], &pid[0] + 20, random_byte); - ses2->set_peer_id(pid); - assert(ses1->id() != ses2->id()); + pack.set_str(settings_pack::peer_fingerprint, pid.to_string()); + ses2->apply_settings(pack); if (ses3) { std::generate(&pid[0], &pid[0] + 20, random_byte); - ses3->set_peer_id(pid); - assert(ses3->id() != ses2->id()); + pack.set_str(settings_pack::peer_fingerprint, pid.to_string()); + ses3->apply_settings(pack); } + TORRENT_ASSERT(ses1->id() != ses2->id()); + if (ses3) TORRENT_ASSERT(ses3->id() != ses2->id()); + boost::shared_ptr t; if (torrent == 0) { @@ -792,8 +793,8 @@ setup_transfer(lt::session* ses1, lt::session* ses2, lt::session* ses3 tor2 = ses2->add_torrent(param, ec); TEST_CHECK(!ses2->get_torrents().empty()); - assert(ses1->get_torrents().size() == 1); - assert(ses2->get_torrents().size() == 1); + TORRENT_ASSERT(ses1->get_torrents().size() == 1); + TORRENT_ASSERT(ses2->get_torrents().size() == 1); // test_sleep(100); diff --git a/test/swarm_suite.cpp b/test/swarm_suite.cpp index b94b4af55..1a2ba3091 100644 --- a/test/swarm_suite.cpp +++ b/test/swarm_suite.cpp @@ -105,14 +105,14 @@ void test_swarm(int flags) pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced); pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced); - lt::session ses1(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses1(pack); ses1.apply_settings(pack); pack.set_int(settings_pack::download_rate_limit, rate_limit / 2); pack.set_int(settings_pack::upload_rate_limit, rate_limit); - lt::session ses2(pack, fingerprint("LT", 0, 1, 0, 0)); - lt::session ses3(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses2(pack); + lt::session ses3(pack); torrent_handle tor1; torrent_handle tor2; diff --git a/test/test_auto_unchoke.cpp b/test/test_auto_unchoke.cpp index eaf1a8a23..eefad1385 100644 --- a/test/test_auto_unchoke.cpp +++ b/test/test_auto_unchoke.cpp @@ -74,7 +74,7 @@ void test_swarm() pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced); pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced); - lt::session ses1(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses1(pack); pack.set_int(settings_pack::upload_rate_limit, rate_limit / 10); pack.set_int(settings_pack::download_rate_limit, rate_limit / 5); @@ -82,11 +82,11 @@ void test_swarm() pack.set_int(settings_pack::choking_algorithm, settings_pack::fixed_slots_choker); pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:49010"); - lt::session ses2(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses2(pack); pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:49010"); - lt::session ses3(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses3(pack); torrent_handle tor1; torrent_handle tor2; diff --git a/test/test_checking.cpp b/test/test_checking.cpp index e74f56c19..60b11cd80 100644 --- a/test/test_checking.cpp +++ b/test/test_checking.cpp @@ -175,7 +175,7 @@ void test_checking(int flags = read_only_files) pack.set_int(settings_pack::alert_mask, alert::all_categories); pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48000"); pack.set_int(settings_pack::max_retry_port_bind, 1000); - lt::session ses1(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses1(pack); add_torrent_params p; p.save_path = "tmp1_checking"; diff --git a/test/test_lsd.cpp b/test/test_lsd.cpp index 9b46c44d5..9681821af 100644 --- a/test/test_lsd.cpp +++ b/test/test_lsd.cpp @@ -59,10 +59,10 @@ void test_lsd() pack.set_bool(settings_pack::enable_natpmp, false); pack.set_str(settings_pack::listen_interfaces, "127.0.0.1:48100"); - lt::session ses1(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses1(pack); pack.set_str(settings_pack::listen_interfaces, "127.0.0.1:49100"); - lt::session ses2(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses2(pack); torrent_handle tor1; torrent_handle tor2; diff --git a/test/test_pex.cpp b/test/test_pex.cpp index f36baef72..983a2e3eb 100644 --- a/test/test_pex.cpp +++ b/test/test_pex.cpp @@ -78,16 +78,16 @@ void test_pex() pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced); pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced); - lt::session ses1(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses1(pack); pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:49200"); - lt::session ses3(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses3(pack); // make the peer connecting the two worthless to transfer // data, to force peer 3 to connect directly to peer 1 through pex pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:50200"); - lt::session ses2(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses2(pack); ses1.add_extension(create_ut_pex_plugin); ses2.add_extension(create_ut_pex_plugin); diff --git a/test/test_privacy.cpp b/test/test_privacy.cpp index c6500c482..95ba8bff8 100644 --- a/test/test_privacy.cpp +++ b/test/test_privacy.cpp @@ -128,7 +128,7 @@ session_proxy test_proxy(settings_pack::proxy_type_t proxy_type, int flags) sett.set_int(settings_pack::proxy_type, (settings_pack::proxy_type_t)proxy_type); sett.set_int(settings_pack::proxy_port, 4444); - lt::session* s = new lt::session(sett, fingerprint("LT", 0, 1, 0, 0)); + lt::session* s = new lt::session(sett); error_code ec; remove_all("tmp1_privacy", ec); diff --git a/test/test_rss.cpp b/test/test_rss.cpp index 61692be73..7e224fd27 100644 --- a/test/test_rss.cpp +++ b/test/test_rss.cpp @@ -97,8 +97,7 @@ void test_feed(std::string const& filename, rss_expect const& expect) settings_pack pack; pack.set_int(settings_pack::max_retry_port_bind, 100); pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:100"); - boost::shared_ptr s = boost::make_shared( - fingerprint("TT", 0, 0, 0 ,0)); + boost::shared_ptr s = boost::make_shared(); settings_pack p; s->start_session(p); diff --git a/test/test_session.cpp b/test/test_session.cpp index 8fe9fb308..5f4414458 100644 --- a/test/test_session.cpp +++ b/test/test_session.cpp @@ -43,7 +43,7 @@ int test_main() { settings_pack p; p.set_int(settings_pack::alert_mask, ~0); - lt::session ses(p, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses(p); settings_pack sett; sett.set_int(settings_pack::cache_size, 100); diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 09953aaf2..a8fda8fa5 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -551,7 +551,7 @@ void test_fastresume(std::string const& test_path) { settings_pack pack; pack.set_int(settings_pack::alert_mask, alert::all_categories); - lt::session ses(pack, fingerprint(" ", 0,0,0,0)); + lt::session ses(pack); error_code ec; @@ -602,7 +602,7 @@ void test_fastresume(std::string const& test_path) { settings_pack pack; pack.set_int(settings_pack::alert_mask, alert::all_categories); - lt::session ses(pack, fingerprint(" ", 0,0,0,0)); + lt::session ses(pack); add_torrent_params p; p.ti = boost::make_shared(boost::cref(*t)); @@ -660,7 +660,7 @@ void test_rename_file_in_fastresume(std::string const& test_path) { settings_pack pack; pack.set_int(settings_pack::alert_mask, alert::all_categories); - lt::session ses(pack, fingerprint(" ", 0,0,0,0)); + lt::session ses(pack); add_torrent_params p; p.ti = boost::make_shared(boost::cref(*t)); @@ -700,7 +700,7 @@ void test_rename_file_in_fastresume(std::string const& test_path) { settings_pack pack; pack.set_int(settings_pack::alert_mask, alert::all_categories); - lt::session ses(pack, fingerprint(" ", 0,0,0,0)); + lt::session ses(pack); add_torrent_params p; p.ti = boost::make_shared(boost::cref(*t)); diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index 916bee66b..81cb782ad 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -54,7 +54,7 @@ void test_running_torrent(boost::shared_ptr info, boost::int64_t f pack.set_int(settings_pack::alert_mask, alert::storage_notification); pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48130"); pack.set_int(settings_pack::max_retry_port_bind, 10); - lt::session ses(pack, fingerprint("LT", 0, 1, 0, 0)); + lt::session ses(pack); std::vector zeroes; zeroes.resize(1000, 0);