From 816c6ec88343b6e79be5c61ed3af1c0880775190 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 10:59:53 +0900 Subject: [PATCH] sd-dhcp-client: use event_reset_time_relative() Fixes CID#1548073. --- src/libsystemd-network/sd-dhcp-client.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 566da47638e..9df4a3dd3fd 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -740,10 +740,9 @@ static void client_stop(sd_dhcp_client *client, int error) { * for retransmission delays, timeout should start at 4s then double * each attempt with max of 64s, with -1 to +1 sec of random 'fuzz' added. * This assumes the first call will be using attempt 1. */ -static usec_t client_compute_request_timeout(usec_t now, uint64_t attempt) { +static usec_t client_compute_request_timeout(uint64_t attempt) { usec_t timeout = (UINT64_C(1) << MIN(attempt + 1, UINT64_C(6))) * USEC_PER_SEC; - - return usec_sub_signed(usec_add(now, timeout), RFC2131_RANDOM_FUZZ); + return usec_sub_signed(timeout, RFC2131_RANDOM_FUZZ); } /* RFC2131 section 4.4.5: @@ -758,8 +757,8 @@ static usec_t client_compute_request_timeout(usec_t now, uint64_t attempt) { * of 60 seconds. * Note that while the default T1/T2 initial times do have random 'fuzz' applied, * the RFC sec 4.4.5 does not mention adding any fuzz to retries. */ -static usec_t client_compute_reacquisition_timeout(usec_t now, usec_t expire) { - return now + MAX(usec_sub_unsigned(expire, now) / 2, 60 * USEC_PER_SEC); +static usec_t client_compute_reacquisition_timeout(usec_t now_usec, usec_t expire) { + return MAX(usec_sub_unsigned(expire, now_usec) / 2, 60 * USEC_PER_SEC); } static int cmp_uint8(const uint8_t *a, const uint8_t *b) { @@ -1242,7 +1241,7 @@ static int client_timeout_resend( goto error; client->discover_attempt++; - next_timeout = client_compute_request_timeout(time_now, client->discover_attempt); + next_timeout = client_compute_request_timeout(client->discover_attempt); break; case DHCP_STATE_REQUESTING: case DHCP_STATE_BOUND: @@ -1250,7 +1249,7 @@ static int client_timeout_resend( goto error; client->request_attempt++; - next_timeout = client_compute_request_timeout(time_now, client->request_attempt); + next_timeout = client_compute_request_timeout(client->request_attempt); break; case DHCP_STATE_STOPPED: @@ -1261,11 +1260,11 @@ static int client_timeout_resend( assert_not_reached(); } - r = event_reset_time(client->event, &client->timeout_resend, - CLOCK_BOOTTIME, - next_timeout, 10 * USEC_PER_MSEC, - client_timeout_resend, client, - client->event_priority, "dhcp4-resend-timer", true); + r = event_reset_time_relative( + client->event, &client->timeout_resend, + CLOCK_BOOTTIME, next_timeout, 10 * USEC_PER_MSEC, + client_timeout_resend, client, + client->event_priority, "dhcp4-resend-timer", true); if (r < 0) goto error; -- 2.47.3