util: randomize hashing of IP addresses

Include a random (constant) value in the hash in UTI_IPToHash() to
randomize the order in which NTP sources are stored in the hash table
and polled on start. This change also randomizes the order of clientlog
records.
This commit is contained in:
Miroslav Lichvar 2016-03-14 18:06:56 +01:00
parent 9749a1c6fc
commit b45f53dd20

10
util.c
View file

@ -362,6 +362,7 @@ UTI_IPToRefid(IPAddr *ip)
uint32_t
UTI_IPToHash(IPAddr *ip)
{
static uint32_t seed = 0;
unsigned char *addr;
unsigned int i, len;
uint32_t hash;
@ -379,10 +380,15 @@ UTI_IPToHash(IPAddr *ip)
return 0;
}
for (i = 0, hash = 0; i < len; i++)
/* Include a random seed in the hash to randomize collisions
and order of addresses in hash tables */
while (!seed)
UTI_GetRandomBytes(&seed, sizeof (seed));
for (i = 0, hash = seed; i < len; i++)
hash = 71 * hash + addr[i];
return hash;
return hash + seed;
}
/* ================================================== */