dhcpv4: fix lease ordering by ip address
[oweals/odhcpd.git] / src / config.c
index 16f003be86b40eef9cf9cad18d6d459d29b39919..f1fe82b3312ac7cf2adf3747959e4f7788a06447 100644 (file)
@@ -20,7 +20,7 @@
 #include "odhcpd.h"
 
 static struct blob_buf b;
-static int reload_pipe[2];
+static int reload_pipe[2] = { -1, -1 };
 
 static int lease_cmp(const void *k1, const void *k2, void *ptr);
 static void lease_update(struct vlist_tree *tree, struct vlist_node *node_new,
@@ -30,7 +30,10 @@ struct vlist_tree leases = VLIST_TREE_INIT(leases, lease_cmp, lease_update, true
 AVL_TREE(interfaces, avl_strcmp, false, NULL);
 struct config config = {.legacy = false, .main_dhcpv4 = false,
                        .dhcp_cb = NULL, .dhcp_statefile = NULL,
-                       .log_level = LOG_INFO};
+                       .log_level = LOG_WARNING};
+
+#define START_DEFAULT  100
+#define LIMIT_DEFAULT  150
 
 enum {
        IFACE_ATTR_INTERFACE,
@@ -210,6 +213,8 @@ static void set_interface_defaults(struct interface *iface)
 {
        iface->learn_routes = 1;
        iface->dhcpv4_leasetime = 43200;
+       iface->dhcpv4_start.s_addr = htonl(START_DEFAULT);
+       iface->dhcpv4_end.s_addr = htonl(START_DEFAULT + LIMIT_DEFAULT - 1);
        iface->dhcpv6_assignall = true;
        iface->dhcpv6_pd = true;
        iface->dhcpv6_na = true;
@@ -433,7 +438,12 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
                iface->router_event.uloop.fd = -1;
                iface->dhcpv6_event.uloop.fd = -1;
                iface->ndp_event.uloop.fd = -1;
+               iface->ndp_ping_fd = -1;
                iface->dhcpv4_event.uloop.fd = -1;
+               INIT_LIST_HEAD(&iface->ia_assignments);
+               INIT_LIST_HEAD(&iface->dhcpv4_assignments);
+               INIT_LIST_HEAD(&iface->dhcpv4_fr_ips);
+
                set_interface_defaults(iface);
 
                avl_insert(&interfaces, &iface->avl);
@@ -499,14 +509,16 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
 
        if ((c = tb[IFACE_ATTR_START])) {
                iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
+               iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
+                                                       LIMIT_DEFAULT - 1);
 
                if (config.main_dhcpv4 && config.legacy)
                        iface->dhcpv4 = MODE_SERVER;
        }
 
        if ((c = tb[IFACE_ATTR_LIMIT]))
-               iface->dhcpv4_end.s_addr = htonl(
-                               ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
+               iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
+                                                       blobmsg_get_u32(c) - 1);
 
        if ((c = tb[IFACE_ATTR_MASTER]))
                iface->master = blobmsg_get_bool(c);
@@ -953,19 +965,27 @@ void odhcpd_reload(void)
        struct uci_package *dhcp = NULL;
        if (!uci_load(uci, "dhcp", &dhcp)) {
                struct uci_element *e;
+
+               /* 1. Global settings */
                uci_foreach_element(&dhcp->sections, e) {
                        struct uci_section *s = uci_to_section(e);
-                       if (!strcmp(s->type, "host"))
-                               set_lease(s);
-                       else if (!strcmp(s->type, "odhcpd"))
+                       if (!strcmp(s->type, "odhcpd"))
                                set_config(s);
                }
 
+               /* 2. DHCP pools */
                uci_foreach_element(&dhcp->sections, e) {
                        struct uci_section *s = uci_to_section(e);
                        if (!strcmp(s->type, "dhcp"))
                                set_interface(s);
                }
+
+               /* 3. Static leases */
+               uci_foreach_element(&dhcp->sections, e) {
+                       struct uci_section* s = uci_to_section(e);
+                       if (!strcmp(s->type, "host"))
+                               set_lease(s);
+               }
        }
 
        if (config.dhcp_statefile) {
@@ -1079,7 +1099,7 @@ static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
        odhcpd_reload();
 }
 
-static struct uloop_fd reload_fd = { .cb = reload_cb };
+static struct uloop_fd reload_fd = { .fd = -1, .cb = reload_cb };
 
 void odhcpd_run(void)
 {