From 3afe6c23a07b7663ebca7860e4a9b434ba25a673 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 31 Aug 2013 21:06:43 +0000 Subject: [PATCH] merged tracker key patch from RC_0_16 --- include/libtorrent/hasher.hpp | 4 ++-- include/libtorrent/torrent.hpp | 4 ++++ include/libtorrent/tracker_manager.hpp | 2 +- src/hasher.cpp | 3 ++- src/session_impl.cpp | 2 +- src/torrent.cpp | 17 ++++++++++++++++- 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/include/libtorrent/hasher.hpp b/include/libtorrent/hasher.hpp index c234a7160..2c4bd1495 100644 --- a/include/libtorrent/hasher.hpp +++ b/include/libtorrent/hasher.hpp @@ -101,8 +101,8 @@ namespace libtorrent hasher& operator=(hasher const& h); #endif - void update(std::string const& data) { update(data.c_str(), data.size()); } - void update(const char* data, int len); + hasher& update(std::string const& data) { update(data.c_str(), data.size()); return *this; } + hasher& update(const char* data, int len); sha1_hash final(); void reset(); diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index e67808cd5..888d3c1e8 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -467,6 +467,10 @@ namespace libtorrent // base64 encoding). std::string tracker_login() const; + // generate the tracker key for this torrent. + // The key is passed to http trackers as ``&key=``. + boost::uint32_t tracker_key() const; + // if we need a connect boost, connect some peers // immediately void do_connect_boost(); diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 0027e3f07..885490a00 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -125,7 +125,7 @@ namespace libtorrent event_t event; std::string url; std::string trackerid; - int key; + boost::uint32_t key; int num_want; address bind_ip; bool send_stats; diff --git a/src/hasher.cpp b/src/hasher.cpp index 4d1322770..a6a4a1ed3 100644 --- a/src/hasher.cpp +++ b/src/hasher.cpp @@ -80,7 +80,7 @@ namespace libtorrent } #endif - void hasher::update(const char* data, int len) + hasher& hasher::update(const char* data, int len) { TORRENT_ASSERT(data != 0); TORRENT_ASSERT(len > 0); @@ -93,6 +93,7 @@ namespace libtorrent #else SHA1_update(&m_context, reinterpret_cast(data), len); #endif + return *this; } sha1_hash hasher::final() diff --git a/src/session_impl.cpp b/src/session_impl.cpp index ad3729d7e..739147b10 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -628,6 +628,7 @@ namespace aux { , m_tracker_manager(*this, m_proxy) , m_num_active_downloading(0) , m_num_active_finished(0) + , m_key(0) , m_listen_port_retries(listen_port_range.second - listen_port_range.first) #if TORRENT_USE_I2P , m_i2p_conn(m_io_service) @@ -725,7 +726,6 @@ namespace aux { // ---- generate a peer id ---- static seed_random_generator seeder; - m_key = random() + (random() << 15) + (random() << 30); std::string print = cl_fprint.to_string(); TORRENT_ASSERT_VAL(print.length() <= 20, print.length()); diff --git a/src/torrent.cpp b/src/torrent.cpp index 0e3f3e07c..d76b7b47d 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2358,7 +2358,10 @@ namespace libtorrent else #endif req.listen_port = m_ses.listen_port(); - req.key = m_ses.m_key; + if (m_ses.m_key) + req.key = m_ses.m_key; + else + req.key = tracker_key(); ptime now = time_now_hires(); @@ -3780,6 +3783,18 @@ namespace libtorrent return m_username + ":" + m_password; } + boost::uint32_t torrent::tracker_key() const + { + uintptr_t self = (uintptr_t)this; + uintptr_t ses = (uintptr_t)&m_ses; + sha1_hash h = hasher((char*)&self, sizeof(self)) + .update((char*)&m_storage, sizeof(m_storage)) + .update((char*)&ses, sizeof(ses)) + .final(); + unsigned char const* ptr = &h[0]; + return detail::read_uint32(ptr); + } + void torrent::set_piece_deadline(int piece, int t, int flags) { INVARIANT_CHECK;