dhcpv4: avoid setting lifetime to infinite for static assignments
authorHans Dedecker <dedeckeh@gmail.com>
Sun, 16 Feb 2020 20:15:28 +0000 (21:15 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Sun, 16 Feb 2020 20:27:16 +0000 (21:27 +0100)
Don't set the valid lifetime to infinite for static assignments
but rather set it to the leasetime given to the client.
This makes it possible to display the leasetime for static assigments
and simplifies the code in several places

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
src/dhcpv4.c

index 24a4fea08bb0df787142dae6574671a7258a44cd..51236d2488bdbab5b709736f518e962e7c7d3a5f 100644 (file)
@@ -1081,8 +1081,7 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac,
                                a->flags &= ~OAF_BOUND;
 
                                *incl_fr_opt = accept_fr_nonce;
-                               if (!(a->flags & OAF_STATIC))
-                                       a->valid_until = now;
+                               a->valid_until = now;
                        } else {
                                if ((!(a->flags & OAF_STATIC) || !a->hostname) && hostname_len > 0) {
                                        a->hostname = realloc(a->hostname, hostname_len + 1);
@@ -1113,8 +1112,7 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac,
                                } else
                                        *incl_fr_opt = false;
 
-                               if (!(a->flags & OAF_STATIC))
-                                       a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
+                               a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
                        }
                } else if (!assigned && a) {
                        /* Cleanup failed assignment */
@@ -1124,17 +1122,16 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac,
 
        } else if (msg == DHCPV4_MSG_RELEASE && a) {
                a->flags &= ~OAF_BOUND;
-
-               if (!(a->flags & OAF_STATIC))
-                       a->valid_until = now - 1;
+               a->valid_until = now - 1;
 
        } else if (msg == DHCPV4_MSG_DECLINE && a) {
                a->flags &= ~OAF_BOUND;
 
-               if (!(a->flags & OAF_STATIC)) {
+               if (!(a->flags & OAF_STATIC) || a->lease->ipaddr != a->addr) {
                        memset(a->hwaddr, 0, sizeof(a->hwaddr));
                        a->valid_until = now + 3600; /* Block address for 1h */
-               }
+               } else
+                       a->valid_until = now - 1;
        }
 
        dhcpv6_ia_write_statefile();