treewide: simplify dhcp leasetime checking
authorHans Dedecker <dedeckeh@gmail.com>
Sun, 19 Feb 2017 21:00:38 +0000 (22:00 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Sun, 19 Feb 2017 21:15:57 +0000 (22:15 +0100)
Set dhcp leasetim to 43200 seconds for all interfaces independant
if dhcpv4 is enabled for an interface. Minimum allowed leasetime
is 60 seconds when parsing leastime configuration; this allows to
clean up the awkward leasetime checks on different places in the code

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

index b621c75..df0cbc4 100644 (file)
@@ -192,6 +192,7 @@ static void set_interface_defaults(struct interface *iface)
 {
        iface->managed = 1;
        iface->learn_routes = 1;
+       iface->dhcpv4_leasetime = 43200;
        iface->ra_maxinterval = 600;
        iface->ra_mininterval = iface->ra_maxinterval/3;
        iface->ra_lifetime = -1;
@@ -288,10 +289,10 @@ static double parse_leasetime(struct blob_attr *c) {
                        goto err;
        }
 
-       if (time >= 60)
-               return time;
+       if (time < 60)
+               time = 60;
 
-       return 0;
+       return time;
 
 err:
        return -1;
@@ -351,8 +352,7 @@ static int set_lease(struct uci_section *s)
                if (time < 0)
                        goto err;
 
-               if (time >= 60)
-                       lease->dhcpv4_leasetime = time;
+               lease->dhcpv4_leasetime = time;
        }
 
        list_add(&lease->head, &leases);
@@ -425,8 +425,7 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
                if (time < 0)
                        goto err;
 
-               if (time >= 60)
-                       iface->dhcpv4_leasetime = time;
+               iface->dhcpv4_leasetime = time;
        }
 
        if ((c = tb[IFACE_ATTR_START])) {
index 5ef99b1..81e537d 100644 (file)
@@ -153,8 +153,7 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                                return -1;
                        }
 
-                       if (lease->dhcpv4_leasetime >= 60)
-                               a->leasetime = lease->dhcpv4_leasetime;
+                       a->leasetime = lease->dhcpv4_leasetime;
 
                        a->addr = ntohl(lease->ipaddr.s_addr);
                        memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
@@ -194,10 +193,6 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                                free_dhcpv4_assignment(a);
                }
 
-
-               if (iface->dhcpv4_leasetime < 60)
-                       iface->dhcpv4_leasetime = 43200;
-
                iface->dhcpv4_event.uloop.fd = sock;
                iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
                odhcpd_register(&iface->dhcpv4_event);
@@ -627,7 +622,7 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                        assigned = dhcpv4_assign(iface, a, raddr);
                }
 
-               if (a->leasetime >= 60)
+               if (a->leasetime)
                        my_leasetime = a->leasetime;
                else
                        my_leasetime = iface->dhcpv4_leasetime;
index 84f368e..888634f 100644 (file)
@@ -88,8 +88,7 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
                                return -1;
                        }
 
-                       if (lease->dhcpv4_leasetime > 0)
-                               a->leasetime = lease->dhcpv4_leasetime;
+                       a->leasetime = lease->dhcpv4_leasetime;
 
                        a->clid_len = duid_len;
                        a->length = 128;
@@ -719,16 +718,11 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
        } else {
                if (a) {
                        uint32_t leasetime;
-                       if (a->leasetime > 0)
+                       if (a->leasetime)
                                leasetime = a->leasetime;
                        else
                                leasetime = iface->dhcpv4_leasetime;
 
-                       if (leasetime == 0)
-                               leasetime = 3600;
-                       else if (leasetime < 60)
-                               leasetime = 60;
-
                        uint32_t pref = leasetime;
                        uint32_t valid = leasetime;