make the peer fingerprint a regular setting and remove it from the main session constructor argument list

This commit is contained in:
Arvid Norberg 2014-12-31 15:51:45 +00:00
parent 1039d4ae86
commit 764b09d6b5
19 changed files with 91 additions and 66 deletions

View File

@ -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<sha1_hash, boost::shared_ptr<torrent> > 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();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<torrent_info>* 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<torrent_info> 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);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<aux::session_impl> s = boost::make_shared<aux::session_impl>(
fingerprint("TT", 0, 0, 0 ,0));
boost::shared_ptr<aux::session_impl> s = boost::make_shared<aux::session_impl>();
settings_pack p;
s->start_session(p);

View File

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

View File

@ -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<torrent_info>(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<torrent_info>(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<torrent_info>(boost::cref(*t));

View File

@ -54,7 +54,7 @@ void test_running_torrent(boost::shared_ptr<torrent_info> 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<boost::uint8_t> zeroes;
zeroes.resize(1000, 0);