From 71db577ed53adb2a5c006b013e3ca0e005a1fb08 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sat, 22 Mar 2014 02:08:23 -0400 Subject: [PATCH] Replace the remaining calls to rand() with nk_random_u32(). --- ndhc/arp.c | 7 ++++--- ndhc/state.c | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ndhc/arp.c b/ndhc/arp.c index b6689ac..ec50cda 100644 --- a/ndhc/arp.c +++ b/ndhc/arp.c @@ -561,10 +561,11 @@ static int arp_is_query_reply(struct arpMsg *am) return 1; } -static int arp_gen_probe_wait(void) +static int arp_gen_probe_wait(struct client_state_t *cs) { // This is not a uniform distribution but it doesn't matter here. - return arp_probe_min + rand() % (arp_probe_max - arp_probe_min); + return arp_probe_min + (nk_random_u32(&cs->rnd32_state) & 0x7fffffffu) + % (arp_probe_max - arp_probe_min); } static void arp_defense_timeout(struct client_state_t *cs, long long nowts) @@ -642,7 +643,7 @@ static void arp_collision_timeout(struct client_state_t *cs, long long nowts) } if (arp_ip_anon_ping(cs, arp_dhcp_packet.yiaddr) == -1) log_warning("arp: Failed to send ARP ping in retransmission."); - probe_wait_time = arp_gen_probe_wait(); + probe_wait_time = arp_gen_probe_wait(cs); arp_wake_ts[AS_COLLISION_CHECK] = arp_send_stats[ASEND_COLLISION_CHECK].ts + probe_wait_time; } diff --git a/ndhc/state.c b/ndhc/state.c index 71408b4..4978243 100644 --- a/ndhc/state.c +++ b/ndhc/state.c @@ -77,13 +77,14 @@ static const dhcp_state_t dhcp_states[] = { static unsigned int num_dhcp_requests; static long long dhcp_wake_ts = -1; -static int delay_timeout(size_t numpackets) +static int delay_timeout(struct client_state_t *cs, size_t numpackets) { int to = 64; char tot[] = { 4, 8, 16, 32, 64 }; if (numpackets < sizeof tot) to = tot[numpackets]; - return to * 1000 + rand() % 1000; + // Distribution is a bit biased but it doesn't really matter. + return to * 1000 + (nk_random_u32(&cs->rnd32_state) & 0x7fffffffu) % 1000; } static void reinit_shared_deconfig(struct client_state_t *cs) @@ -123,7 +124,7 @@ static void requesting_timeout(struct client_state_t *cs, long long nowts) { if (num_dhcp_requests < 5) { send_selecting(cs); - dhcp_wake_ts = nowts + delay_timeout(num_dhcp_requests); + dhcp_wake_ts = nowts + delay_timeout(cs, num_dhcp_requests); num_dhcp_requests++; } else reinit_selecting(cs, 0); @@ -309,7 +310,7 @@ static void selecting_timeout(struct client_state_t *cs, long long nowts) if (num_dhcp_requests == 0) cs->xid = nk_random_u32(&cs->rnd32_state); send_discover(cs); - dhcp_wake_ts = nowts + delay_timeout(num_dhcp_requests); + dhcp_wake_ts = nowts + delay_timeout(cs, num_dhcp_requests); num_dhcp_requests++; }