Update to the new ncmlib random API.
This commit is contained in:
parent
0732ed5f84
commit
759b6bd831
@ -396,7 +396,7 @@ static int arp_is_query_reply(struct arpMsg am[static 1])
|
|||||||
static int arp_gen_probe_wait(struct client_state_t cs[static 1])
|
static int arp_gen_probe_wait(struct client_state_t cs[static 1])
|
||||||
{
|
{
|
||||||
// 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 + (nk_random_u32(&cs->rnd32_state) & 0x7fffffffu)
|
return arp_probe_min + (nk_random_u32(&cs->rnd_state) & 0x7fffffffu)
|
||||||
% (arp_probe_max - arp_probe_min);
|
% (arp_probe_max - arp_probe_min);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -495,7 +495,7 @@ ssize_t send_decline(struct client_state_t cs[static 1], uint32_t server)
|
|||||||
|
|
||||||
ssize_t send_release(struct client_state_t cs[static 1])
|
ssize_t send_release(struct client_state_t cs[static 1])
|
||||||
{
|
{
|
||||||
struct dhcpmsg packet = {.xid = nk_random_u32(&cs->rnd32_state)};
|
struct dhcpmsg packet = {.xid = nk_random_u32(&cs->rnd_state)};
|
||||||
init_packet(&packet, DHCPRELEASE);
|
init_packet(&packet, DHCPRELEASE);
|
||||||
packet.ciaddr = cs->clientAddr;
|
packet.ciaddr = cs->clientAddr;
|
||||||
add_option_reqip(&packet, cs->clientAddr);
|
add_option_reqip(&packet, cs->clientAddr);
|
||||||
|
@ -121,7 +121,7 @@ static int open_iaidfile_write(const uint8_t hwaddr[static 6],
|
|||||||
// RFC6355 specifies a RFC4122 UUID, but I simply use a 128-byte random
|
// RFC6355 specifies a RFC4122 UUID, but I simply use a 128-byte random
|
||||||
// value, as the complexity of RFC4122 UUID generation is completely
|
// value, as the complexity of RFC4122 UUID generation is completely
|
||||||
// unwarranted for DHCPv4.
|
// unwarranted for DHCPv4.
|
||||||
static size_t generate_duid(struct nk_random_state_u32 s[static 1],
|
static size_t generate_duid(struct nk_random_state s[static 1],
|
||||||
char dest[static 1], size_t dlen)
|
char dest[static 1], size_t dlen)
|
||||||
{
|
{
|
||||||
const size_t tlen = sizeof(uint16_t) + 4 * sizeof(uint32_t);
|
const size_t tlen = sizeof(uint16_t) + 4 * sizeof(uint32_t);
|
||||||
@ -143,7 +143,7 @@ static size_t generate_duid(struct nk_random_state_u32 s[static 1],
|
|||||||
|
|
||||||
// RFC6355 specifies the IAID as a 32-bit value that uniquely identifies
|
// RFC6355 specifies the IAID as a 32-bit value that uniquely identifies
|
||||||
// a hardware link for a given host.
|
// a hardware link for a given host.
|
||||||
static size_t generate_iaid(struct nk_random_state_u32 s[static 1],
|
static size_t generate_iaid(struct nk_random_state s[static 1],
|
||||||
char dest[static 1], size_t dlen)
|
char dest[static 1], size_t dlen)
|
||||||
{
|
{
|
||||||
if (dlen < sizeof(uint32_t))
|
if (dlen < sizeof(uint32_t))
|
||||||
@ -169,7 +169,7 @@ void get_clientid(struct client_state_t cs[static 1],
|
|||||||
|
|
||||||
int fd = open_iaidfile_read(cc->arp, sizeof cc->arp);
|
int fd = open_iaidfile_read(cc->arp, sizeof cc->arp);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
iaid_len = generate_iaid(&cs->rnd32_state, iaid, sizeof iaid);
|
iaid_len = generate_iaid(&cs->rnd_state, iaid, sizeof iaid);
|
||||||
fd = open_iaidfile_write(cc->arp, sizeof cc->arp);
|
fd = open_iaidfile_write(cc->arp, sizeof cc->arp);
|
||||||
ssize_t r = safe_write(fd, iaid, iaid_len);
|
ssize_t r = safe_write(fd, iaid, iaid_len);
|
||||||
if (r < 0 || (size_t)r != iaid_len)
|
if (r < 0 || (size_t)r != iaid_len)
|
||||||
@ -186,7 +186,7 @@ void get_clientid(struct client_state_t cs[static 1],
|
|||||||
|
|
||||||
fd = open_duidfile_read();
|
fd = open_duidfile_read();
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
duid_len = generate_duid(&cs->rnd32_state, duid, sizeof duid);
|
duid_len = generate_duid(&cs->rnd_state, duid, sizeof duid);
|
||||||
fd = open_duidfile_write();
|
fd = open_duidfile_write();
|
||||||
ssize_t r = safe_write(fd, duid, duid_len);
|
ssize_t r = safe_write(fd, duid, duid_len);
|
||||||
if (r < 0 || (size_t)r != duid_len)
|
if (r < 0 || (size_t)r != duid_len)
|
||||||
|
10
src/ndhc.c
10
src/ndhc.c
@ -357,7 +357,7 @@ static void do_ndhc_work(void)
|
|||||||
// We can't do anything while the iface is disabled, anyway.
|
// We can't do anything while the iface is disabled, anyway.
|
||||||
// Suspend might cause link state change notifications to be
|
// Suspend might cause link state change notifications to be
|
||||||
// missed, so we use a non-infinite timeout.
|
// missed, so we use a non-infinite timeout.
|
||||||
timeout = 2000 + nk_random_u32(&cs.rnd32_state) % 3000;
|
timeout = 2000 + nk_random_u32(&cs.rnd_state) % 3000;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,7 +370,7 @@ static void do_ndhc_work(void)
|
|||||||
arp_wake_ts <= nowts, sev_signal);
|
arp_wake_ts <= nowts, sev_signal);
|
||||||
|
|
||||||
if (dhcp_ok == COR_ERROR) {
|
if (dhcp_ok == COR_ERROR) {
|
||||||
timeout = 2000 + nk_random_u32(&cs.rnd32_state) % 3000;
|
timeout = 2000 + nk_random_u32(&cs.rnd_state) % 3000;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,7 +435,7 @@ static void spawn_ifch(void)
|
|||||||
close(ifchSock[0]);
|
close(ifchSock[0]);
|
||||||
close(ifchStream[0]);
|
close(ifchStream[0]);
|
||||||
// Don't share the RNG state with the master process.
|
// Don't share the RNG state with the master process.
|
||||||
nk_random_u32_init(&cs.rnd32_state);
|
nk_random_init(&cs.rnd_state);
|
||||||
ifch_main();
|
ifch_main();
|
||||||
} else if (ifch_pid > 0) {
|
} else if (ifch_pid > 0) {
|
||||||
close(ifchSock[1]);
|
close(ifchSock[1]);
|
||||||
@ -452,7 +452,7 @@ static void spawn_sockd(void)
|
|||||||
close(sockdSock[0]);
|
close(sockdSock[0]);
|
||||||
close(sockdStream[0]);
|
close(sockdStream[0]);
|
||||||
// Don't share the RNG state with the master process.
|
// Don't share the RNG state with the master process.
|
||||||
nk_random_u32_init(&cs.rnd32_state);
|
nk_random_init(&cs.rnd_state);
|
||||||
sockd_main();
|
sockd_main();
|
||||||
} else if (sockd_pid > 0) {
|
} else if (sockd_pid > 0) {
|
||||||
close(sockdSock[1]);
|
close(sockdSock[1]);
|
||||||
@ -550,7 +550,7 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
parse_cmdline(argc, argv);
|
parse_cmdline(argc, argv);
|
||||||
|
|
||||||
nk_random_u32_init(&cs.rnd32_state);
|
nk_random_init(&cs.rnd_state);
|
||||||
|
|
||||||
if (getuid())
|
if (getuid())
|
||||||
suicide("I need to be started as root.");
|
suicide("I need to be started as root.");
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
#include "nk/random.h"
|
#include "nk/random.h"
|
||||||
|
|
||||||
struct client_state_t {
|
struct client_state_t {
|
||||||
struct nk_random_state_u32 rnd32_state;
|
struct nk_random_state rnd_state;
|
||||||
long long leaseStartTime, renewTime, rebindTime;
|
long long leaseStartTime, renewTime, rebindTime;
|
||||||
long long dhcp_wake_ts;
|
long long dhcp_wake_ts;
|
||||||
int ifDeconfig; // Set if the interface has already been deconfigured.
|
int ifDeconfig; // Set if the interface has already been deconfigured.
|
||||||
|
10
src/state.c
10
src/state.c
@ -70,7 +70,7 @@ static int delay_timeout(struct client_state_t cs[static 1], size_t numpackets)
|
|||||||
if (numpackets < sizeof tot)
|
if (numpackets < sizeof tot)
|
||||||
to = tot[numpackets];
|
to = tot[numpackets];
|
||||||
// Distribution is a bit biased but it doesn't really matter.
|
// Distribution is a bit biased but it doesn't really matter.
|
||||||
return to * 1000 + (nk_random_u32(&cs->rnd32_state) & 0x7fffffffu) % 1000;
|
return to * 1000 + (nk_random_u32(&cs->rnd_state) & 0x7fffffffu) % 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reinit_shared_deconfig(struct client_state_t cs[static 1])
|
static void reinit_shared_deconfig(struct client_state_t cs[static 1])
|
||||||
@ -149,7 +149,7 @@ static int rebinding_timeout(struct client_state_t cs[static 1],
|
|||||||
client_config.interface);
|
client_config.interface);
|
||||||
return BTO_HARDFAIL;
|
return BTO_HARDFAIL;
|
||||||
}
|
}
|
||||||
long long ts0 = nowts + (50 + nk_random_u32(&cs->rnd32_state) % 20) * 1000;
|
long long ts0 = nowts + (50 + nk_random_u32(&cs->rnd_state) % 20) * 1000;
|
||||||
cs->dhcp_wake_ts = ts0 < elt ? ts0 : elt;
|
cs->dhcp_wake_ts = ts0 < elt ? ts0 : elt;
|
||||||
return BTO_WAIT;
|
return BTO_WAIT;
|
||||||
}
|
}
|
||||||
@ -172,7 +172,7 @@ static int renewing_timeout(struct client_state_t cs[static 1],
|
|||||||
client_config.interface);
|
client_config.interface);
|
||||||
return BTO_HARDFAIL;
|
return BTO_HARDFAIL;
|
||||||
}
|
}
|
||||||
long long ts0 = nowts + (50 + nk_random_u32(&cs->rnd32_state) % 20) * 1000;
|
long long ts0 = nowts + (50 + nk_random_u32(&cs->rnd_state) % 20) * 1000;
|
||||||
cs->dhcp_wake_ts = ts0 < rbt ? ts0 : rbt;
|
cs->dhcp_wake_ts = ts0 < rbt ? ts0 : rbt;
|
||||||
return BTO_WAIT;
|
return BTO_WAIT;
|
||||||
}
|
}
|
||||||
@ -354,7 +354,7 @@ static int selecting_timeout(struct client_state_t cs[static 1],
|
|||||||
suicide("%s: No lease; failing.", client_config.interface);
|
suicide("%s: No lease; failing.", client_config.interface);
|
||||||
}
|
}
|
||||||
if (cs->num_dhcp_requests == 0)
|
if (cs->num_dhcp_requests == 0)
|
||||||
cs->xid = nk_random_u32(&cs->rnd32_state);
|
cs->xid = nk_random_u32(&cs->rnd_state);
|
||||||
if (send_discover(cs) < 0) {
|
if (send_discover(cs) < 0) {
|
||||||
log_warning("%s: Failed to send a discover request packet.",
|
log_warning("%s: Failed to send a discover request packet.",
|
||||||
client_config.interface);
|
client_config.interface);
|
||||||
@ -447,7 +447,7 @@ int dhcp_handle(struct client_state_t cs[static 1], long long nowts,
|
|||||||
{
|
{
|
||||||
scrBegin;
|
scrBegin;
|
||||||
reinit:
|
reinit:
|
||||||
cs->xid = nk_random_u32(&cs->rnd32_state);
|
cs->xid = nk_random_u32(&cs->rnd_state);
|
||||||
// We're in the SELECTING state here.
|
// We're in the SELECTING state here.
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int ret = COR_SUCCESS;
|
int ret = COR_SUCCESS;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user