Replace the remaining calls to rand() with nk_random_u32().

This commit is contained in:
Nicholas J. Kain 2014-03-22 02:08:23 -04:00
parent 009f80b428
commit 71db577ed5
2 changed files with 9 additions and 7 deletions

View File

@ -561,10 +561,11 @@ static int arp_is_query_reply(struct arpMsg *am)
return 1; 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. // 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) 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) if (arp_ip_anon_ping(cs, arp_dhcp_packet.yiaddr) == -1)
log_warning("arp: Failed to send ARP ping in retransmission."); 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_wake_ts[AS_COLLISION_CHECK] =
arp_send_stats[ASEND_COLLISION_CHECK].ts + probe_wait_time; arp_send_stats[ASEND_COLLISION_CHECK].ts + probe_wait_time;
} }

View File

@ -77,13 +77,14 @@ static const dhcp_state_t dhcp_states[] = {
static unsigned int num_dhcp_requests; static unsigned int num_dhcp_requests;
static long long dhcp_wake_ts = -1; 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; int to = 64;
char tot[] = { 4, 8, 16, 32, 64 }; char tot[] = { 4, 8, 16, 32, 64 };
if (numpackets < sizeof tot) if (numpackets < sizeof tot)
to = tot[numpackets]; 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) 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) { if (num_dhcp_requests < 5) {
send_selecting(cs); 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++; num_dhcp_requests++;
} else } else
reinit_selecting(cs, 0); 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) if (num_dhcp_requests == 0)
cs->xid = nk_random_u32(&cs->rnd32_state); cs->xid = nk_random_u32(&cs->rnd32_state);
send_discover(cs); 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++; num_dhcp_requests++;
} }