mirror of
https://github.com/zeek/zeek.git
synced 2025-10-14 12:38:20 +00:00
A new bif unique_id(prefix) that returns a string that's unique across
Bro instaces with high probablity. "prefix" is a string that will be prepended to the returned ID.
This commit is contained in:
parent
4bdb94955d
commit
8266709e20
10 changed files with 96 additions and 59 deletions
52
src/Conn.cc
52
src/Conn.cc
|
@ -216,56 +216,6 @@ Connection::~Connection()
|
|||
--external_connections;
|
||||
}
|
||||
|
||||
uint64 Connection::uid_counter = 0;
|
||||
uint64 Connection::uid_instance = 0;
|
||||
|
||||
uint64 Connection::CalculateNextUID()
|
||||
{
|
||||
if ( uid_instance == 0 )
|
||||
{
|
||||
// This is the first time we need a UID.
|
||||
|
||||
if ( ! have_random_seed() )
|
||||
{
|
||||
// If we don't need deterministic output (as
|
||||
// indicated by a set seed), we calculate the
|
||||
// instance ID by hashing something likely to be
|
||||
// globally unique.
|
||||
struct {
|
||||
char hostname[128];
|
||||
struct timeval time;
|
||||
pid_t pid;
|
||||
int rnd;
|
||||
} unique;
|
||||
|
||||
gethostname(unique.hostname, 128);
|
||||
unique.hostname[sizeof(unique.hostname)-1] = '\0';
|
||||
gettimeofday(&unique.time, 0);
|
||||
unique.pid = getpid();
|
||||
unique.rnd = bro_random();
|
||||
|
||||
uid_instance = HashKey::HashBytes(&unique, sizeof(unique));
|
||||
++uid_instance; // Now it's larger than zero.
|
||||
}
|
||||
|
||||
else
|
||||
// Generate determistic UIDs.
|
||||
uid_instance = 1;
|
||||
}
|
||||
|
||||
// Now calculate the unique ID for this connection.
|
||||
struct {
|
||||
uint64 counter;
|
||||
hash_t instance;
|
||||
} key;
|
||||
|
||||
key.counter = ++uid_counter;
|
||||
key.instance = uid_instance;
|
||||
|
||||
uint64_t h = HashKey::HashBytes(&key, sizeof(key));
|
||||
return h;
|
||||
}
|
||||
|
||||
void Connection::Done()
|
||||
{
|
||||
finished = 1;
|
||||
|
@ -417,7 +367,7 @@ RecordVal* Connection::BuildConnVal()
|
|||
conn_val->Assign(8, new StringVal("")); // history
|
||||
|
||||
if ( ! uid )
|
||||
uid = CalculateNextUID();
|
||||
uid = calculate_unique_id();
|
||||
|
||||
char tmp[20];
|
||||
conn_val->Assign(9, new StringVal(uitoa_n(uid, tmp, sizeof(tmp), 62)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue