diff --git a/CMakeLists.txt b/CMakeLists.txt index fe4ffbc22..43528ffff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ set(sources piece_picker policy puff + random rss session session_impl diff --git a/Jamfile b/Jamfile index 8075f0d0b..8fc01bb23 100755 --- a/Jamfile +++ b/Jamfile @@ -410,6 +410,7 @@ SOURCES = piece_picker policy puff + random rss session session_impl diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 671a296e6..1213db6a6 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -78,6 +78,7 @@ nobase_include_HEADERS = \ proxy_base.hpp \ ptime.hpp \ puff.hpp \ + random.hpp \ rss.hpp \ session.hpp \ session_settings.hpp \ diff --git a/src/Makefile.am b/src/Makefile.am index 568217778..ce2f5199d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,6 +63,7 @@ libtorrent_rasterbar_la_SOURCES = \ packet_buffer.cpp \ policy.cpp \ puff.cpp \ + random.cpp \ rss.cpp \ session.cpp \ session_impl.cpp \ diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index a3e92dca6..5c0fe1749 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/broadcast_socket.hpp" #include "libtorrent/escape_string.hpp" #include "libtorrent/peer_info.hpp" +#include "libtorrent/random.hpp" #ifndef TORRENT_DISABLE_ENCRYPTION #include "libtorrent/pe_crypto.hpp" @@ -427,7 +428,7 @@ namespace libtorrent return; } - int pad_size = std::rand() % 512; + int pad_size = random() % 512; #ifdef TORRENT_VERBOSE_LOGGING peer_log(" pad size: %d", pad_size); @@ -472,7 +473,7 @@ namespace libtorrent sha1_hash const& info_hash = t->torrent_file().info_hash(); char const* const secret = m_dh_key_exchange->get_secret(); - int pad_size = rand() % 512; + int pad_size = random() % 512; TORRENT_ASSERT(!m_rc4_encrypted || send_buffer_size() == m_encrypted_bytes); @@ -549,7 +550,7 @@ namespace libtorrent TORRENT_ASSERT(crypto_select == 0x02 || crypto_select == 0x01); TORRENT_ASSERT(!m_sent_handshake); - int pad_size = rand() % 512; + int pad_size = random() % 512; TORRENT_ASSERT(!m_rc4_encrypted || send_buffer_size() == m_encrypted_bytes); @@ -2032,7 +2033,7 @@ namespace libtorrent if (num_lazy_pieces < 1) num_lazy_pieces = 1; for (int i = 0; i < num_pieces; ++i) { - if (rand() % (num_pieces - i) >= num_lazy_pieces - lazy_piece) continue; + if (int(random() % (num_pieces - i)) >= num_lazy_pieces - lazy_piece) continue; lazy_pieces[lazy_piece++] = i; } TORRENT_ASSERT(lazy_piece == num_lazy_pieces); diff --git a/src/escape_string.cpp b/src/escape_string.cpp index a09e69497..bca8a09c0 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/escape_string.hpp" #include "libtorrent/parse_url.hpp" +#include "libtorrent/random.hpp" #ifdef TORRENT_WINDOWS #ifndef WIN32_LEAN_AND_MEAN @@ -114,7 +115,7 @@ namespace libtorrent // the random number while (begin != end) - *begin++ = printable[rand() % (sizeof(printable)-1)]; + *begin++ = printable[random() % (sizeof(printable)-1)]; } char to_lower(char c) diff --git a/src/natpmp.cpp b/src/natpmp.cpp index d0d516474..f1ca72ba0 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/enum_net.hpp" #include "libtorrent/socket_io.hpp" #include "libtorrent/io_service.hpp" +//#include "libtorrent/random.hpp" #if BOOST_VERSION < 103500 #include @@ -434,7 +435,7 @@ void natpmp::on_reply(error_code const& e // simulate packet loss /* - if ((rand() % 2) == 0) + if ((random() % 2) == 0) { log(" simulating drop", l); return; diff --git a/src/pe_crypto.cpp b/src/pe_crypto.cpp index 51f3ef651..ef5a4791c 100644 --- a/src/pe_crypto.cpp +++ b/src/pe_crypto.cpp @@ -39,7 +39,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #elif defined TORRENT_USE_OPENSSL #include -#include +#include "libtorrent/random.hpp" #elif defined TORRENT_USE_TOMMATH extern "C" { #include "libtorrent/tommath.h" @@ -110,7 +110,7 @@ get_out: #elif defined TORRENT_USE_OPENSSL // create local key for (int i = 0; i < sizeof(m_dh_local_secret); ++i) - m_dh_local_secret[i] = rand(); + m_dh_local_secret[i] = random(); BIGNUM* prime = 0; BIGNUM* secret = 0; @@ -145,7 +145,7 @@ get_out: #elif defined TORRENT_USE_TOMMATH // create local key for (int i = 0; i < int(sizeof(m_dh_local_secret)); ++i) - m_dh_local_secret[i] = rand(); + m_dh_local_secret[i] = random(); mp_int prime; mp_int secret; diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 07149ad46..cba780ce8 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/piece_picker.hpp" #include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/bitfield.hpp" +#include "libtorrent/random.hpp" #ifdef TORRENT_DEBUG #include "libtorrent/peer_connection.hpp" @@ -539,7 +540,7 @@ namespace libtorrent priority_range(priority, &range_start, &range_end); int new_index; if (range_end == range_start) new_index = range_start; - else new_index = rand() % (range_end - range_start + 1) + range_start; + else new_index = random() % (range_end - range_start + 1) + range_start; #ifdef TORRENT_PICKER_LOG std::cerr << "add " << index << " (" << priority << ")" << std::endl; @@ -750,7 +751,7 @@ namespace libtorrent int range_start, range_end; priority_range(priority, &range_start, &range_end); TORRENT_ASSERT(range_start < range_end); - int other_index = rand() % (range_end - range_start) + range_start; + int other_index = random() % (range_end - range_start) + range_start; if (other_index == elem_index) return; @@ -1513,7 +1514,7 @@ namespace libtorrent // we're not using rarest first (only for the first // bucket, since that's where the currently downloading // pieces are) - int start_piece = std::rand() % m_piece_map.size(); + int start_piece = random() % m_piece_map.size(); int piece = start_piece; while (num_blocks > 0) @@ -1605,7 +1606,7 @@ namespace libtorrent // pick one random block from the first busy piece we encountered // none of these blocks have more than one request to them - if (!temp.empty()) interesting_blocks.push_back(temp[rand() % temp.size()]); + if (!temp.empty()) interesting_blocks.push_back(temp[random() % temp.size()]); #ifdef TORRENT_DEBUG // make sure that we at this point have added requests to all unrequested blocks diff --git a/src/policy.cpp b/src/policy.cpp index 25afe020a..ae2d302f8 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -55,6 +55,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/piece_picker.hpp" #include "libtorrent/broadcast_socket.hpp" #include "libtorrent/peer_info.hpp" +#include "libtorrent/random.hpp" #ifdef TORRENT_DEBUG #include "libtorrent/bt_peer_connection.hpp" @@ -468,7 +469,7 @@ namespace libtorrent TORRENT_ASSERT(m_finished == m_torrent->is_finished()); - int round_robin = rand() % m_peers.size(); + int round_robin = random() % m_peers.size(); for (int iterations = (std::min)(int(m_peers.size()), 300); iterations > 0; --iterations) @@ -586,7 +587,7 @@ namespace libtorrent // set external_ip to a random value, to // radomize which peers we prefer address_v4::bytes_type bytes; - std::generate(bytes.begin(), bytes.end(), &std::rand); + std::generate(bytes.begin(), bytes.end(), &random); external_ip = address_v4(bytes); } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index c04c71831..10b56af15 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -80,6 +80,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/settings.hpp" #include "libtorrent/build_config.hpp" #include "libtorrent/extensions.hpp" +#include "libtorrent/random.hpp" #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING #endif @@ -196,7 +197,7 @@ namespace aux { { seed_random_generator() { - std::srand((unsigned int)total_microseconds(time_now_hires() - min_time())); + random_seed((unsigned int)total_microseconds(time_now_hires() - min_time())); } }; @@ -847,7 +848,7 @@ namespace aux { // ---- generate a peer id ---- static seed_random_generator seeder; - m_key = rand() + (rand() << 15) + (rand() << 30); + m_key = random() + (random() << 15) + (random() << 30); std::string print = cl_fprint.to_string(); TORRENT_ASSERT_VAL(print.length() <= 20, print.length()); @@ -1937,7 +1938,7 @@ namespace aux { socks5_stream& s = *m_socks_listen_socket->get(); s.set_command(2); // 2 means BIND (as opposed to CONNECT) m_socks_listen_port = m_listen_interface.port(); - if (m_socks_listen_port == 0) m_socks_listen_port = 2000 + rand() % 60000; + if (m_socks_listen_port == 0) m_socks_listen_port = 2000 + random() % 60000; s.async_connect(tcp::endpoint(address_v4::any(), m_socks_listen_port) , boost::bind(&session_impl::on_socks_accept, this, m_socks_listen_socket, _1)); } @@ -4889,7 +4890,7 @@ namespace aux { if (m_external_addresses.size() > 20) { - if (rand() < RAND_MAX / 2) + if (random() < RAND_MAX / 2) { #if defined TORRENT_VERBOSE_LOGGING (*m_logger) << time_now_string() << ": More than 20 slots, dopped\n"; diff --git a/src/smart_ban.cpp b/src/smart_ban.cpp index 88c19487f..f6a967ce8 100644 --- a/src/smart_ban.cpp +++ b/src/smart_ban.cpp @@ -59,6 +59,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/peer_connection.hpp" #include "libtorrent/peer_info.hpp" +#include "libtorrent/random.hpp" namespace libtorrent { @@ -71,7 +72,7 @@ namespace { smart_ban_plugin(torrent& t) : m_torrent(t) - , m_salt(rand()) + , m_salt(random()) { } diff --git a/src/torrent.cpp b/src/torrent.cpp index 17d088f3c..d855f17ed 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -80,6 +80,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/enum_net.hpp" #include "libtorrent/http_connection.hpp" #include "libtorrent/gzip.hpp" // for inflate_gzip +#include "libtorrent/random.hpp" #ifdef TORRENT_USE_OPENSSL #include "libtorrent/ssl_stream.hpp" @@ -3078,7 +3079,7 @@ namespace libtorrent return -1; } - return avail_vec[rand() % avail_vec.size()]; + return avail_vec[random() % avail_vec.size()]; } void torrent::on_files_deleted(int ret, disk_io_job const& j) @@ -6651,7 +6652,7 @@ namespace libtorrent // it num_interested == 0, we need to pick a new piece // now, pick one of the rarest pieces to download - int pick = rand() % rarest_pieces.size(); + int pick = random() % rarest_pieces.size(); bool was_finished = is_finished(); m_picker->set_piece_priority(rarest_pieces[pick], 1); update_peer_interest(was_finished); diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index 4dba776bf..d04d9e2d2 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/escape_string.hpp" #include "libtorrent/broadcast_socket.hpp" // for is_any +#include "libtorrent/random.hpp" namespace libtorrent { @@ -402,7 +403,7 @@ namespace libtorrent char* ptr = buf; if (m_transaction_id == 0) - m_transaction_id = std::rand() ^ (std::rand() << 16); + m_transaction_id = random() ^ (random() << 16); detail::write_uint32(0x417, ptr); detail::write_uint32(0x27101980, ptr); // connection_id @@ -432,7 +433,7 @@ namespace libtorrent void udp_tracker_connection::send_udp_scrape() { if (m_transaction_id == 0) - m_transaction_id = std::rand() ^ (std::rand() << 16); + m_transaction_id = random() ^ (random() << 16); if (m_abort) return; @@ -590,7 +591,7 @@ namespace libtorrent void udp_tracker_connection::send_udp_announce() { if (m_transaction_id == 0) - m_transaction_id = std::rand() ^ (std::rand() << 16); + m_transaction_id = random() ^ (random() << 16); if (m_abort) return; diff --git a/src/upnp.cpp b/src/upnp.cpp index 83b15c052..6547509cc 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/connection_queue.hpp" #include "libtorrent/enum_net.hpp" #include "libtorrent/escape_string.hpp" +#include "libtorrent/random.hpp" #if defined TORRENT_ASIO_DEBUGGING #include "libtorrent/debug.hpp" @@ -1296,7 +1297,7 @@ void upnp::on_upnp_map_response(error_code const& e { // The external port cannot be wildcarder // pick a random port - m.external_port = 40000 + (std::rand() % 10000); + m.external_port = 40000 + (random() % 10000); m.action = mapping_t::action_add; ++m.failcount; update_map(d, mapping, l); diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index efd6bb1d5..144a654fa 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/extensions.hpp" #include "libtorrent/extensions/ut_metadata.hpp" #include "libtorrent/alert_types.hpp" +#include "libtorrent/random.hpp" #ifdef TORRENT_STATS #include "libtorrent/aux_/session_impl.hpp" #endif @@ -390,7 +391,7 @@ namespace libtorrent { namespace void failed_hash_check(ptime const& now) { - m_request_limit = now + seconds(20 + (rand() * 50) / RAND_MAX); + m_request_limit = now + seconds(20 + (random() * 50) / RAND_MAX); } private: diff --git a/src/utp_socket_manager.cpp b/src/utp_socket_manager.cpp index b502bee61..2cd3c365b 100644 --- a/src/utp_socket_manager.cpp +++ b/src/utp_socket_manager.cpp @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/instantiate_connection.hpp" #include "libtorrent/socket_io.hpp" #include "libtorrent/broadcast_socket.hpp" // for is_teredo +#include "libtorrent/random.hpp" // #define TORRENT_DEBUG_MTU 1135 @@ -314,7 +315,7 @@ namespace libtorrent } else { - send_id = rand(); + send_id = random(); recv_id = send_id - 1; } utp_socket_impl* impl = construct_utp_impl(recv_id, send_id, str, this); diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index fc8d72088..807f93862 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alloca.hpp" #include "libtorrent/timestamp_history.hpp" #include "libtorrent/error.hpp" +#include "libtorrent/random.hpp" #include #define TORRENT_UTP_LOG 0 @@ -1121,7 +1122,7 @@ void utp_socket_impl::detach() void utp_socket_impl::send_syn() { - m_seq_nr = rand(); + m_seq_nr = random(); m_acked_seq_nr = (m_seq_nr - 1) & ACK_MASK; m_loss_seq_nr = m_acked_seq_nr; m_ack_nr = 0; @@ -1259,7 +1260,7 @@ void utp_socket_impl::send_reset(utp_header* ph) h.connection_id = m_send_id; h.timestamp_difference_microseconds = m_reply_micro; h.wnd_size = 0; - h.seq_nr = rand(); + h.seq_nr = random(); h.ack_nr = ph->seq_nr; ptime now = time_now_hires(); h.timestamp_microseconds = boost::uint32_t(total_microseconds(now - min_time())); @@ -2414,7 +2415,7 @@ bool utp_socket_impl::incoming_packet(char const* buf, int size , this, socket_state_names[m_state]); #endif m_ack_nr = ph->seq_nr; - m_seq_nr = rand(); + m_seq_nr = random(); m_acked_seq_nr = (m_seq_nr - 1) & ACK_MASK; m_loss_seq_nr = m_acked_seq_nr;