diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-11 19:10:51 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-11 19:30:14 -0700 |
commit | 2a83d76127ea848fc3ccfac29d2ca7f708de8ff2 (patch) | |
tree | f65257803f1b013b853203e9aaca405114539fca | |
parent | 70c16fec71849c15b89e0809a4ab91b98ab8c272 (diff) |
libdhcp: Be more careful with xid and time_ns_init
-rw-r--r-- | libdhcp/dhcp_client.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/libdhcp/dhcp_client.c b/libdhcp/dhcp_client.c index 687aaf1..4c4ece3 100644 --- a/libdhcp/dhcp_client.c +++ b/libdhcp/dhcp_client.c @@ -93,8 +93,8 @@ */ #include <string.h> /* for strlen(), memcpy(), memset() */ -#include <stdlib.h> /* for random() */ +#include <libmisc/rand.h> #include <libmisc/vcall.h> #include <libhw/generic/alarmclock.h> @@ -657,7 +657,6 @@ static void dhcp_client_take_lease(struct dhcp_client *client, struct dhcp_recv_ .subnet_mask = client->lease_auxdata.subnet_mask, }); } -} static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client) { assert(client); @@ -666,7 +665,7 @@ static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client for (;;) { switch (client->state) { case STATE_INIT: { - client->xid = (uint32_t)random(); /* random() returns 31 random bits; good enough */ + client->xid = rand_uint63n(UINT32_MAX); client->time_ns_init = VCALL(bootclock, get_time_ns); if (dhcp_client_send(client, DHCP_MSGTYP_DISCOVER, NULL)) @@ -748,6 +747,9 @@ static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client break; } case STATE_RENEWING: { + client->xid = rand_uint63n(UINT32_MAX); + client->time_ns_init = VCALL(bootclock, get_time_ns); + VCALL(client->sock, set_read_deadline, client->lease_time_ns_t2); struct dhcp_recv_msg msg; ssize_t r = dhcp_client_recv(client, &msg); @@ -767,6 +769,7 @@ static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client case -NET_ETIMEDOUT: if (dhcp_client_send(client, DHCP_MSGTYP_REQUEST, NULL)) break; + client->lease_server_id = net_ip4_addr_zero; client->state = STATE_REBINDING; break; default: |