replaced std::rand() with custom random generator

This commit is contained in:
Arvid Norberg 2011-02-26 07:55:51 +00:00
parent 7288f77ec9
commit ab3b82b8ee
18 changed files with 48 additions and 31 deletions

View File

@ -36,6 +36,7 @@ set(sources
piece_picker piece_picker
policy policy
puff puff
random
rss rss
session session
session_impl session_impl

View File

@ -410,6 +410,7 @@ SOURCES =
piece_picker piece_picker
policy policy
puff puff
random
rss rss
session session
session_impl session_impl

View File

@ -78,6 +78,7 @@ nobase_include_HEADERS = \
proxy_base.hpp \ proxy_base.hpp \
ptime.hpp \ ptime.hpp \
puff.hpp \ puff.hpp \
random.hpp \
rss.hpp \ rss.hpp \
session.hpp \ session.hpp \
session_settings.hpp \ session_settings.hpp \

View File

@ -63,6 +63,7 @@ libtorrent_rasterbar_la_SOURCES = \
packet_buffer.cpp \ packet_buffer.cpp \
policy.cpp \ policy.cpp \
puff.cpp \ puff.cpp \
random.cpp \
rss.cpp \ rss.cpp \
session.cpp \ session.cpp \
session_impl.cpp \ session_impl.cpp \

View File

@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/broadcast_socket.hpp" #include "libtorrent/broadcast_socket.hpp"
#include "libtorrent/escape_string.hpp" #include "libtorrent/escape_string.hpp"
#include "libtorrent/peer_info.hpp" #include "libtorrent/peer_info.hpp"
#include "libtorrent/random.hpp"
#ifndef TORRENT_DISABLE_ENCRYPTION #ifndef TORRENT_DISABLE_ENCRYPTION
#include "libtorrent/pe_crypto.hpp" #include "libtorrent/pe_crypto.hpp"
@ -427,7 +428,7 @@ namespace libtorrent
return; return;
} }
int pad_size = std::rand() % 512; int pad_size = random() % 512;
#ifdef TORRENT_VERBOSE_LOGGING #ifdef TORRENT_VERBOSE_LOGGING
peer_log(" pad size: %d", pad_size); peer_log(" pad size: %d", pad_size);
@ -472,7 +473,7 @@ namespace libtorrent
sha1_hash const& info_hash = t->torrent_file().info_hash(); sha1_hash const& info_hash = t->torrent_file().info_hash();
char const* const secret = m_dh_key_exchange->get_secret(); 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); 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(crypto_select == 0x02 || crypto_select == 0x01);
TORRENT_ASSERT(!m_sent_handshake); 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); 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; if (num_lazy_pieces < 1) num_lazy_pieces = 1;
for (int i = 0; i < num_pieces; ++i) 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; lazy_pieces[lazy_piece++] = i;
} }
TORRENT_ASSERT(lazy_piece == num_lazy_pieces); TORRENT_ASSERT(lazy_piece == num_lazy_pieces);

View File

@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
#include "libtorrent/escape_string.hpp" #include "libtorrent/escape_string.hpp"
#include "libtorrent/parse_url.hpp" #include "libtorrent/parse_url.hpp"
#include "libtorrent/random.hpp"
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
#ifndef WIN32_LEAN_AND_MEAN #ifndef WIN32_LEAN_AND_MEAN
@ -114,7 +115,7 @@ namespace libtorrent
// the random number // the random number
while (begin != end) while (begin != end)
*begin++ = printable[rand() % (sizeof(printable)-1)]; *begin++ = printable[random() % (sizeof(printable)-1)];
} }
char to_lower(char c) char to_lower(char c)

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/enum_net.hpp" #include "libtorrent/enum_net.hpp"
#include "libtorrent/socket_io.hpp" #include "libtorrent/socket_io.hpp"
#include "libtorrent/io_service.hpp" #include "libtorrent/io_service.hpp"
//#include "libtorrent/random.hpp"
#if BOOST_VERSION < 103500 #if BOOST_VERSION < 103500
#include <asio/ip/host_name.hpp> #include <asio/ip/host_name.hpp>
@ -434,7 +435,7 @@ void natpmp::on_reply(error_code const& e
// simulate packet loss // simulate packet loss
/* /*
if ((rand() % 2) == 0) if ((random() % 2) == 0)
{ {
log(" simulating drop", l); log(" simulating drop", l);
return; return;

View File

@ -39,7 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <gcrypt.h> #include <gcrypt.h>
#elif defined TORRENT_USE_OPENSSL #elif defined TORRENT_USE_OPENSSL
#include <openssl/bn.h> #include <openssl/bn.h>
#include <openssl/rand.h> #include "libtorrent/random.hpp"
#elif defined TORRENT_USE_TOMMATH #elif defined TORRENT_USE_TOMMATH
extern "C" { extern "C" {
#include "libtorrent/tommath.h" #include "libtorrent/tommath.h"
@ -110,7 +110,7 @@ get_out:
#elif defined TORRENT_USE_OPENSSL #elif defined TORRENT_USE_OPENSSL
// create local key // create local key
for (int i = 0; i < sizeof(m_dh_local_secret); ++i) 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* prime = 0;
BIGNUM* secret = 0; BIGNUM* secret = 0;
@ -145,7 +145,7 @@ get_out:
#elif defined TORRENT_USE_TOMMATH #elif defined TORRENT_USE_TOMMATH
// create local key // create local key
for (int i = 0; i < int(sizeof(m_dh_local_secret)); ++i) 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 prime;
mp_int secret; mp_int secret;

View File

@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/piece_picker.hpp" #include "libtorrent/piece_picker.hpp"
#include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/bitfield.hpp" #include "libtorrent/bitfield.hpp"
#include "libtorrent/random.hpp"
#ifdef TORRENT_DEBUG #ifdef TORRENT_DEBUG
#include "libtorrent/peer_connection.hpp" #include "libtorrent/peer_connection.hpp"
@ -539,7 +540,7 @@ namespace libtorrent
priority_range(priority, &range_start, &range_end); priority_range(priority, &range_start, &range_end);
int new_index; int new_index;
if (range_end == range_start) new_index = range_start; 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 #ifdef TORRENT_PICKER_LOG
std::cerr << "add " << index << " (" << priority << ")" << std::endl; std::cerr << "add " << index << " (" << priority << ")" << std::endl;
@ -750,7 +751,7 @@ namespace libtorrent
int range_start, range_end; int range_start, range_end;
priority_range(priority, &range_start, &range_end); priority_range(priority, &range_start, &range_end);
TORRENT_ASSERT(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; if (other_index == elem_index) return;
@ -1513,7 +1514,7 @@ namespace libtorrent
// we're not using rarest first (only for the first // we're not using rarest first (only for the first
// bucket, since that's where the currently downloading // bucket, since that's where the currently downloading
// pieces are) // pieces are)
int start_piece = std::rand() % m_piece_map.size(); int start_piece = random() % m_piece_map.size();
int piece = start_piece; int piece = start_piece;
while (num_blocks > 0) while (num_blocks > 0)
@ -1605,7 +1606,7 @@ namespace libtorrent
// pick one random block from the first busy piece we encountered // pick one random block from the first busy piece we encountered
// none of these blocks have more than one request to them // 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 #ifdef TORRENT_DEBUG
// make sure that we at this point have added requests to all unrequested blocks // make sure that we at this point have added requests to all unrequested blocks

View File

@ -55,6 +55,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/piece_picker.hpp" #include "libtorrent/piece_picker.hpp"
#include "libtorrent/broadcast_socket.hpp" #include "libtorrent/broadcast_socket.hpp"
#include "libtorrent/peer_info.hpp" #include "libtorrent/peer_info.hpp"
#include "libtorrent/random.hpp"
#ifdef TORRENT_DEBUG #ifdef TORRENT_DEBUG
#include "libtorrent/bt_peer_connection.hpp" #include "libtorrent/bt_peer_connection.hpp"
@ -468,7 +469,7 @@ namespace libtorrent
TORRENT_ASSERT(m_finished == m_torrent->is_finished()); 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); for (int iterations = (std::min)(int(m_peers.size()), 300);
iterations > 0; --iterations) iterations > 0; --iterations)
@ -586,7 +587,7 @@ namespace libtorrent
// set external_ip to a random value, to // set external_ip to a random value, to
// radomize which peers we prefer // radomize which peers we prefer
address_v4::bytes_type bytes; 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); external_ip = address_v4(bytes);
} }

View File

@ -80,6 +80,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/settings.hpp" #include "libtorrent/settings.hpp"
#include "libtorrent/build_config.hpp" #include "libtorrent/build_config.hpp"
#include "libtorrent/extensions.hpp" #include "libtorrent/extensions.hpp"
#include "libtorrent/random.hpp"
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
#endif #endif
@ -196,7 +197,7 @@ namespace aux {
{ {
seed_random_generator() 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 ---- // ---- generate a peer id ----
static seed_random_generator seeder; 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(); std::string print = cl_fprint.to_string();
TORRENT_ASSERT_VAL(print.length() <= 20, print.length()); TORRENT_ASSERT_VAL(print.length() <= 20, print.length());
@ -1937,7 +1938,7 @@ namespace aux {
socks5_stream& s = *m_socks_listen_socket->get<socks5_stream>(); socks5_stream& s = *m_socks_listen_socket->get<socks5_stream>();
s.set_command(2); // 2 means BIND (as opposed to CONNECT) s.set_command(2); // 2 means BIND (as opposed to CONNECT)
m_socks_listen_port = m_listen_interface.port(); 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) 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)); , 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 (m_external_addresses.size() > 20)
{ {
if (rand() < RAND_MAX / 2) if (random() < RAND_MAX / 2)
{ {
#if defined TORRENT_VERBOSE_LOGGING #if defined TORRENT_VERBOSE_LOGGING
(*m_logger) << time_now_string() << ": More than 20 slots, dopped\n"; (*m_logger) << time_now_string() << ": More than 20 slots, dopped\n";

View File

@ -59,6 +59,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/peer_connection.hpp" #include "libtorrent/peer_connection.hpp"
#include "libtorrent/peer_info.hpp" #include "libtorrent/peer_info.hpp"
#include "libtorrent/random.hpp"
namespace libtorrent { namespace libtorrent {
@ -71,7 +72,7 @@ namespace
{ {
smart_ban_plugin(torrent& t) smart_ban_plugin(torrent& t)
: m_torrent(t) : m_torrent(t)
, m_salt(rand()) , m_salt(random())
{ {
} }

View File

@ -80,6 +80,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/enum_net.hpp" #include "libtorrent/enum_net.hpp"
#include "libtorrent/http_connection.hpp" #include "libtorrent/http_connection.hpp"
#include "libtorrent/gzip.hpp" // for inflate_gzip #include "libtorrent/gzip.hpp" // for inflate_gzip
#include "libtorrent/random.hpp"
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
#include "libtorrent/ssl_stream.hpp" #include "libtorrent/ssl_stream.hpp"
@ -3078,7 +3079,7 @@ namespace libtorrent
return -1; 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) 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 // it num_interested == 0, we need to pick a new piece
// now, pick one of the rarest pieces to download // 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(); bool was_finished = is_finished();
m_picker->set_piece_priority(rarest_pieces[pick], 1); m_picker->set_piece_priority(rarest_pieces[pick], 1);
update_peer_interest(was_finished); update_peer_interest(was_finished);

View File

@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/escape_string.hpp" #include "libtorrent/escape_string.hpp"
#include "libtorrent/broadcast_socket.hpp" // for is_any #include "libtorrent/broadcast_socket.hpp" // for is_any
#include "libtorrent/random.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -402,7 +403,7 @@ namespace libtorrent
char* ptr = buf; char* ptr = buf;
if (m_transaction_id == 0) 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(0x417, ptr);
detail::write_uint32(0x27101980, ptr); // connection_id detail::write_uint32(0x27101980, ptr); // connection_id
@ -432,7 +433,7 @@ namespace libtorrent
void udp_tracker_connection::send_udp_scrape() void udp_tracker_connection::send_udp_scrape()
{ {
if (m_transaction_id == 0) if (m_transaction_id == 0)
m_transaction_id = std::rand() ^ (std::rand() << 16); m_transaction_id = random() ^ (random() << 16);
if (m_abort) return; if (m_abort) return;
@ -590,7 +591,7 @@ namespace libtorrent
void udp_tracker_connection::send_udp_announce() void udp_tracker_connection::send_udp_announce()
{ {
if (m_transaction_id == 0) if (m_transaction_id == 0)
m_transaction_id = std::rand() ^ (std::rand() << 16); m_transaction_id = random() ^ (random() << 16);
if (m_abort) return; if (m_abort) return;

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/connection_queue.hpp" #include "libtorrent/connection_queue.hpp"
#include "libtorrent/enum_net.hpp" #include "libtorrent/enum_net.hpp"
#include "libtorrent/escape_string.hpp" #include "libtorrent/escape_string.hpp"
#include "libtorrent/random.hpp"
#if defined TORRENT_ASIO_DEBUGGING #if defined TORRENT_ASIO_DEBUGGING
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
@ -1296,7 +1297,7 @@ void upnp::on_upnp_map_response(error_code const& e
{ {
// The external port cannot be wildcarder // The external port cannot be wildcarder
// pick a random port // pick a random port
m.external_port = 40000 + (std::rand() % 10000); m.external_port = 40000 + (random() % 10000);
m.action = mapping_t::action_add; m.action = mapping_t::action_add;
++m.failcount; ++m.failcount;
update_map(d, mapping, l); update_map(d, mapping, l);

View File

@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/extensions.hpp" #include "libtorrent/extensions.hpp"
#include "libtorrent/extensions/ut_metadata.hpp" #include "libtorrent/extensions/ut_metadata.hpp"
#include "libtorrent/alert_types.hpp" #include "libtorrent/alert_types.hpp"
#include "libtorrent/random.hpp"
#ifdef TORRENT_STATS #ifdef TORRENT_STATS
#include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_impl.hpp"
#endif #endif
@ -390,7 +391,7 @@ namespace libtorrent { namespace
void failed_hash_check(ptime const& now) 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: private:

View File

@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/instantiate_connection.hpp" #include "libtorrent/instantiate_connection.hpp"
#include "libtorrent/socket_io.hpp" #include "libtorrent/socket_io.hpp"
#include "libtorrent/broadcast_socket.hpp" // for is_teredo #include "libtorrent/broadcast_socket.hpp" // for is_teredo
#include "libtorrent/random.hpp"
// #define TORRENT_DEBUG_MTU 1135 // #define TORRENT_DEBUG_MTU 1135
@ -314,7 +315,7 @@ namespace libtorrent
} }
else else
{ {
send_id = rand(); send_id = random();
recv_id = send_id - 1; recv_id = send_id - 1;
} }
utp_socket_impl* impl = construct_utp_impl(recv_id, send_id, str, this); utp_socket_impl* impl = construct_utp_impl(recv_id, send_id, str, this);

View File

@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/alloca.hpp" #include "libtorrent/alloca.hpp"
#include "libtorrent/timestamp_history.hpp" #include "libtorrent/timestamp_history.hpp"
#include "libtorrent/error.hpp" #include "libtorrent/error.hpp"
#include "libtorrent/random.hpp"
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
#define TORRENT_UTP_LOG 0 #define TORRENT_UTP_LOG 0
@ -1121,7 +1122,7 @@ void utp_socket_impl::detach()
void utp_socket_impl::send_syn() 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_acked_seq_nr = (m_seq_nr - 1) & ACK_MASK;
m_loss_seq_nr = m_acked_seq_nr; m_loss_seq_nr = m_acked_seq_nr;
m_ack_nr = 0; m_ack_nr = 0;
@ -1259,7 +1260,7 @@ void utp_socket_impl::send_reset(utp_header* ph)
h.connection_id = m_send_id; h.connection_id = m_send_id;
h.timestamp_difference_microseconds = m_reply_micro; h.timestamp_difference_microseconds = m_reply_micro;
h.wnd_size = 0; h.wnd_size = 0;
h.seq_nr = rand(); h.seq_nr = random();
h.ack_nr = ph->seq_nr; h.ack_nr = ph->seq_nr;
ptime now = time_now_hires(); ptime now = time_now_hires();
h.timestamp_microseconds = boost::uint32_t(total_microseconds(now - min_time())); 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]); , this, socket_state_names[m_state]);
#endif #endif
m_ack_nr = ph->seq_nr; 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_acked_seq_nr = (m_seq_nr - 1) & ACK_MASK;
m_loss_seq_nr = m_acked_seq_nr; m_loss_seq_nr = m_acked_seq_nr;