From b45f53dd2030456bbcc14bc0fd5f47c712e59d2d Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Mon, 14 Mar 2016 18:06:56 +0100 Subject: [PATCH] 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. --- util.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/util.c b/util.c index 6967a9e..b654e63 100644 --- a/util.c +++ b/util.c @@ -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; } /* ================================================== */