Skip to contentSkip to conversation

dhcp-lease: allow shorter lease times

PATCH New James Prestwood 1 participant 1 message Nov 5, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dhcp-lease: allow shorter lease times

In practice a DHCP server shouldn't be handing out such short leases, but for testing purposes its convenient having short leases for testing renews.

The existing math for t1/t2 had integer rounding limitations which prevented any leases under 8 seconds. Intead we can use floating point math then case back to a uint32_t to allow leases of 2 seconds or more.

---
 ell/dhcp-lease.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
ell/dhcp-lease.c +3 -3
index 59de3e3..8f73b0b 100644
@@ -142,7 +142,7 @@ struct l_dhcp_lease *_dhcp_lease_parse_options(struct dhcp_message_iter *iter)
 	if (!lease->server_address || !lease->lifetime)
 		goto error;
 
-	if (lease->lifetime < 10)
+	if (lease->lifetime < 2)
 		goto error;
 
 	/*
@@ -157,10 +157,10 @@ struct l_dhcp_lease *_dhcp_lease_parse_options(struct dhcp_message_iter *iter)
 		return lease;
 
 	if (!lease->t1)
-		lease->t1 = lease->lifetime / 2;
+		lease->t1 = (uint32_t)((float)lease->lifetime * 0.5);
 
 	if (!lease->t2)
-		lease->t2 = lease->lifetime / 8 * 7;
+		lease->t2 = (uint32_t)((float)lease->lifetime * 0.875);
 
 	if (lease->t1 > lease->t2)
 		goto error;
Signature · 1 line
2.26.2