statefiles: don't write expired leases
authorDavid Härdeman <[email protected]>
Sat, 8 Nov 2025 17:17:34 +0000 (18:17 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Tue, 11 Nov 2025 07:31:14 +0000 (08:31 +0100)
This also aligns the behaviour of state and host files.

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/302
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/statefiles.c

index 40c0c29c8fa8ad0a1e8cfffc0715474e4e553eb2..a2d39af32503da1bef9504a457f89525232302e2 100644 (file)
@@ -177,10 +177,8 @@ static void statefiles_write_state6(struct write_ctxt *ctxt, struct dhcpv6_lease
 {
        char duidbuf[DUID_HEXSTRLEN];
 
-       ctx->buf_idx = 0;
-
-       if (INFINITE_VALID(lease->valid_until) || lease->valid_until > ctxt->now)
-               odhcpd_enum_addr6(ctxt->iface, lease, ctxt->now, statefiles_write_state6_addr, ctxt);
+       ctxt->buf_idx = 0;
+       odhcpd_enum_addr6(ctxt->iface, lease, ctxt->now, statefiles_write_state6_addr, ctxt);
 
        odhcpd_hexlify(duidbuf, lease->clid_data, lease->clid_len);
 
@@ -197,7 +195,7 @@ static void statefiles_write_state6(struct write_ctxt *ctxt, struct dhcpv6_lease
                 lease->assigned_host_id :
                 (uint64_t)lease->assigned_subnet_id),
                lease->length,
-               ctx->buf);
+               ctxt->buf);
 }
 
 static void statefiles_write_state4(struct write_ctxt *ctxt, struct dhcpv4_lease *lease)
@@ -269,17 +267,29 @@ static bool statefiles_write_state(time_t now)
                if (ctxt.iface->dhcpv6 == MODE_SERVER) {
                        struct dhcpv6_lease *lease;
 
-                       list_for_each_entry(lease, &ctxt.iface->ia_assignments, head)
-                               if (lease->flags & OAF_BOUND)
-                                       statefiles_write_state6(&ctxt, lease);
+                       list_for_each_entry(lease, &ctxt.iface->ia_assignments, head) {
+                               if (!(lease->flags & OAF_BOUND))
+                                       continue;
+
+                               if (!INFINITE_VALID(lease->valid_until) && lease->valid_until <= now)
+                                       continue;
+
+                               statefiles_write_state6(&ctxt, lease);
+                       }
                }
 
                if (ctxt.iface->dhcpv4 == MODE_SERVER) {
                        struct dhcpv4_lease *lease;
 
-                       list_for_each_entry(lease, &ctxt.iface->dhcpv4_leases, head)
-                               if (lease->flags & OAF_BOUND)
-                                       statefiles_write_state4(&ctxt, lease);
+                       list_for_each_entry(lease, &ctxt.iface->dhcpv4_leases, head) {
+                               if (!(lease->flags & OAF_BOUND))
+                                       continue;
+
+                               if (!INFINITE_VALID(lease->valid_until) && lease->valid_until <= now)
+                                       continue;
+
+                               statefiles_write_state4(&ctxt, lease);
+                       }
                }
        }