diff --git a/src/Anon.cc b/src/Anon.cc index 98d98a9f99..f8f580c1db 100644 --- a/src/Anon.cc +++ b/src/Anon.cc @@ -20,7 +20,7 @@ AnonymizeIPAddr* zeek::detail::ip_anonymizer[NUM_ADDR_ANONYMIZATION_METHODS] = { static uint32_t rand32() { - return ((bro_random() & 0xffff) << 16) | (bro_random() & 0xffff); + return ((zeek::random_number() & 0xffff) << 16) | (zeek::random_number() & 0xffff); } // From tcpdpriv. diff --git a/src/Net.cc b/src/Net.cc index 97940a792d..eaffe0b749 100644 --- a/src/Net.cc +++ b/src/Net.cc @@ -248,7 +248,7 @@ void net_packet_dispatch(double t, const Packet* pkt, iosource::PktSrc* src_ps) if ( load_freq == 0 ) load_freq = uint32_t(0xffffffff) / uint32_t(load_sample_freq); - if ( uint32_t(bro_random() & 0xffffffff) < load_freq ) + if ( uint32_t(zeek::random_number() & 0xffffffff) < load_freq ) { // Drain the queued timer events so they're not // charged against this sample. diff --git a/src/PacketFilter.cc b/src/PacketFilter.cc index 61afbe0afd..7b0ef0fbee 100644 --- a/src/PacketFilter.cc +++ b/src/PacketFilter.cc @@ -113,5 +113,5 @@ bool PacketFilter::MatchFilter(const Filter& f, const IP_Hdr& ip, return false; } - return uint32_t(bro_random()) < f.probability; + return uint32_t(zeek::random_number()) < f.probability; } diff --git a/src/util.cc b/src/util.cc index ace3638e59..cccc65db8c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1211,7 +1211,7 @@ unsigned int bro_prng(unsigned int state) return zeek::prng(state); } -long int bro_random() +long int zeek::random_number() { if ( ! bro_rand_determistic ) return random(); // Use system PRNG. @@ -1221,6 +1221,11 @@ long int bro_random() return bro_rand_state; } +long int bro_random() + { + return zeek::random_number(); + } + // Returns a 64-bit random string. uint64_t rand64bit() { @@ -1228,7 +1233,7 @@ uint64_t rand64bit() int i; for ( i = 1; i <= 4; ++i ) - base = (base<<16) | bro_random(); + base = (base<<16) | zeek::random_number(); return base; } @@ -2098,7 +2103,7 @@ uint64_t calculate_unique_id(size_t pool) gettimeofday(&unique.time, 0); unique.pool = (uint64_t) pool; unique.pid = getpid(); - unique.rnd = bro_random(); + unique.rnd = static_cast(zeek::random_number()); uid_instance = HashKey::HashBytes(&unique, sizeof(unique)); ++uid_instance; // Now it's larger than zero. diff --git a/src/util.h b/src/util.h index b29a80d64b..e30b7650e5 100644 --- a/src/util.h +++ b/src/util.h @@ -202,7 +202,7 @@ extern std::string strstrip(std::string s); extern void hmac_md5(size_t size, const unsigned char* bytes, unsigned char digest[16]); -// Initializes RNGs for bro_random() and MD5 usage. If load_file is given, +// Initializes RNGs for zeek::random_number() and MD5 usage. If load_file is given, // the seeds (both random & MD5) are loaded from that file. This takes // precedence over the "use_empty_seeds" argument, which just // zero-initializes all seed values. If write_file is given, the seeds are @@ -225,6 +225,7 @@ unsigned int bro_prng(unsigned int state); // Replacement for the system random(), to which is normally falls back // except when a seed has been given. In that case, the function bro_prng. +[[deprecated("Remove in v4.1. Use zeek::random_number()")]] long int bro_random(); // Calls the system srandom() function with the given seed if not running @@ -599,4 +600,11 @@ void set_thread_name(const char* name, pthread_t tid = pthread_self()); */ long int prng(long int state); +/** + * Wrapper for system random() in the default case, but when running in + * deterministic mode, uses the platform-independent zeek::prng() + * to obtain consistent results since implementations of rand() may vary. + */ +long int random_number(); + } // namespace zeek diff --git a/src/zeek.bif b/src/zeek.bif index f1af07ea36..253e2d8822 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -930,7 +930,7 @@ function hrw_weight%(key_digest: count, site_id: count%): count ## provided by the OS. function rand%(max: count%): count %{ - auto result = bro_uint_t(double(max) * double(bro_random()) / (RAND_MAX + 1.0)); + auto result = bro_uint_t(double(max) * double(zeek::random_number()) / (RAND_MAX + 1.0)); return zeek::val_mgr->Count(result); %} diff --git a/testing/btest/plugins/reader-plugin/src/Foo.cc b/testing/btest/plugins/reader-plugin/src/Foo.cc index 4a05420bc3..6cae03ca18 100644 --- a/testing/btest/plugins/reader-plugin/src/Foo.cc +++ b/testing/btest/plugins/reader-plugin/src/Foo.cc @@ -45,10 +45,10 @@ std::string Foo::RandomString(const int len) "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < len; ++i) - // bro_random is not thread-safe; as we are only using one simultaneous thread + // zeek::random_number() is not thread-safe; as we are only using one simultaneous thread // here, this should not matter in this case. If this test ever starts showing // random errors, this might be the culprit. - s[i] = values[bro_random() / (RAND_MAX / sizeof(values))]; + s[i] = values[zeek::random_number() / (RAND_MAX / sizeof(values))]; return s; }