From 2a83d76127ea848fc3ccfac29d2ca7f708de8ff2 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Mon, 11 Nov 2024 19:10:51 -0700 Subject: libdhcp: Be more careful with xid and time_ns_init --- libdhcp/dhcp_client.c | 9 ++++++--- 1 file 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 /* for strlen(), memcpy(), memset() */ -#include /* for random() */ +#include #include #include @@ -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: -- cgit v1.2.3-2-g168b