mirror of
https://github.com/zeek/zeek.git
synced 2025-10-03 23:28:20 +00:00
Deprecate bro_random(), replace with zeek::random_number()
Avoiding the use of zeek::random() due to potential for confusion with random().
This commit is contained in:
parent
6bbb0a6b48
commit
bde38893ce
7 changed files with 23 additions and 10 deletions
|
@ -20,7 +20,7 @@ AnonymizeIPAddr* zeek::detail::ip_anonymizer[NUM_ADDR_ANONYMIZATION_METHODS] = {
|
||||||
|
|
||||||
static uint32_t rand32()
|
static uint32_t rand32()
|
||||||
{
|
{
|
||||||
return ((bro_random() & 0xffff) << 16) | (bro_random() & 0xffff);
|
return ((zeek::random_number() & 0xffff) << 16) | (zeek::random_number() & 0xffff);
|
||||||
}
|
}
|
||||||
|
|
||||||
// From tcpdpriv.
|
// From tcpdpriv.
|
||||||
|
|
|
@ -248,7 +248,7 @@ void net_packet_dispatch(double t, const Packet* pkt, iosource::PktSrc* src_ps)
|
||||||
if ( load_freq == 0 )
|
if ( load_freq == 0 )
|
||||||
load_freq = uint32_t(0xffffffff) / uint32_t(load_sample_freq);
|
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
|
// Drain the queued timer events so they're not
|
||||||
// charged against this sample.
|
// charged against this sample.
|
||||||
|
|
|
@ -113,5 +113,5 @@ bool PacketFilter::MatchFilter(const Filter& f, const IP_Hdr& ip,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return uint32_t(bro_random()) < f.probability;
|
return uint32_t(zeek::random_number()) < f.probability;
|
||||||
}
|
}
|
||||||
|
|
11
src/util.cc
11
src/util.cc
|
@ -1211,7 +1211,7 @@ unsigned int bro_prng(unsigned int state)
|
||||||
return zeek::prng(state);
|
return zeek::prng(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
long int bro_random()
|
long int zeek::random_number()
|
||||||
{
|
{
|
||||||
if ( ! bro_rand_determistic )
|
if ( ! bro_rand_determistic )
|
||||||
return random(); // Use system PRNG.
|
return random(); // Use system PRNG.
|
||||||
|
@ -1221,6 +1221,11 @@ long int bro_random()
|
||||||
return bro_rand_state;
|
return bro_rand_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long int bro_random()
|
||||||
|
{
|
||||||
|
return zeek::random_number();
|
||||||
|
}
|
||||||
|
|
||||||
// Returns a 64-bit random string.
|
// Returns a 64-bit random string.
|
||||||
uint64_t rand64bit()
|
uint64_t rand64bit()
|
||||||
{
|
{
|
||||||
|
@ -1228,7 +1233,7 @@ uint64_t rand64bit()
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for ( i = 1; i <= 4; ++i )
|
for ( i = 1; i <= 4; ++i )
|
||||||
base = (base<<16) | bro_random();
|
base = (base<<16) | zeek::random_number();
|
||||||
return base;
|
return base;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2098,7 +2103,7 @@ uint64_t calculate_unique_id(size_t pool)
|
||||||
gettimeofday(&unique.time, 0);
|
gettimeofday(&unique.time, 0);
|
||||||
unique.pool = (uint64_t) pool;
|
unique.pool = (uint64_t) pool;
|
||||||
unique.pid = getpid();
|
unique.pid = getpid();
|
||||||
unique.rnd = bro_random();
|
unique.rnd = static_cast<int>(zeek::random_number());
|
||||||
|
|
||||||
uid_instance = HashKey::HashBytes(&unique, sizeof(unique));
|
uid_instance = HashKey::HashBytes(&unique, sizeof(unique));
|
||||||
++uid_instance; // Now it's larger than zero.
|
++uid_instance; // Now it's larger than zero.
|
||||||
|
|
10
src/util.h
10
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,
|
extern void hmac_md5(size_t size, const unsigned char* bytes,
|
||||||
unsigned char digest[16]);
|
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
|
// the seeds (both random & MD5) are loaded from that file. This takes
|
||||||
// precedence over the "use_empty_seeds" argument, which just
|
// precedence over the "use_empty_seeds" argument, which just
|
||||||
// zero-initializes all seed values. If write_file is given, the seeds are
|
// 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
|
// 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.
|
// 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();
|
long int bro_random();
|
||||||
|
|
||||||
// Calls the system srandom() function with the given seed if not running
|
// 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);
|
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
|
} // namespace zeek
|
||||||
|
|
|
@ -930,7 +930,7 @@ function hrw_weight%(key_digest: count, site_id: count%): count
|
||||||
## provided by the OS.
|
## provided by the OS.
|
||||||
function rand%(max: count%): count
|
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);
|
return zeek::val_mgr->Count(result);
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
|
|
@ -45,10 +45,10 @@ std::string Foo::RandomString(const int len)
|
||||||
"abcdefghijklmnopqrstuvwxyz";
|
"abcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
for (int i = 0; i < len; ++i)
|
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
|
// here, this should not matter in this case. If this test ever starts showing
|
||||||
// random errors, this might be the culprit.
|
// 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;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue