Skip to content

Commit

Permalink
Initialize srand with hostname and PID
Browse files Browse the repository at this point in the history
  • Loading branch information
FalacerSelene authored and orgads committed Sep 15, 2024
1 parent 4393d6e commit 5e17500
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/sipp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ void connect_local_twin_socket(char *);
void close_peer_sockets();
void close_local_sockets();
void free_peer_addr_map();
void randomseed();

/********************* Reset global kludge *******************/

Expand Down
44 changes: 43 additions & 1 deletion src/sipp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,48 @@ static void setup_media_sockets()
}
}

/* Seed the random number generator from a combination of
* - Nanoseconds since epoch
* - Process ID
* - Hostname
*/
void randomseed(void)
{
struct timespec ts;
unsigned int seed = time(nullptr);
char hostname[256] = { 0 };
pid_t p = getpid();
int index;

// If the system clock can provide us with a nanosecond count, then
// include that in the seed.
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
seed ^= (unsigned int)ts.tv_nsec;
}

// The system clock might not have nanosecond resolution, and so we still
// want to combine the seed with other values to avoid two
// near-simultaneous launches getting the same random seed.

if (p != 0) {
// We don't XOR with the PID because if we launch twice successively
// it's possible the PID increment could precisely counter the time
// increment.
seed *= p;
}

// Might truncate, or might leave 0s after the end.
gethostname(hostname, 256);

for (index = 0; index < 256; index++) {
// Bitflip with successive bytes of the hostname. Once we get to the
// 0s at the end it's a noop.
seed ^= (hostname[index] << (8 * (index % sizeof(seed))));
}

srand(seed);
}

/* Main */
int main(int argc, char *argv[])
{
Expand All @@ -1346,7 +1388,7 @@ int main(int argc, char *argv[])
rtp_errors = 0;
echo_errors = 0;

srand(time(nullptr));
randomseed();

/* At least one argument is needed */
if (argc < 2) {
Expand Down

0 comments on commit 5e17500

Please sign in to comment.