treewide: always init interface list heads during initialization
authorDainis Jonitis <dainis.jonitis@ubnt.com>
Mon, 29 Apr 2019 11:40:14 +0000 (14:40 +0300)
committerHans Dedecker <dedeckeh@gmail.com>
Fri, 3 May 2019 12:51:52 +0000 (14:51 +0200)
When allocating interface, init dhcpv4_assignments, dhcpv4_fr_ips
and ia_assignments circular list heads to point to self. Avoids
checking whether next pointer is not null all over the place.

Signed-off-by: Dainis Jonitis <dainis.jonitis@ubnt.com>
src/config.c
src/dhcpv4.c
src/dhcpv6-ia.c
src/ubus.c

index 399922b0ce7b77d29d2616a88f32462256297b28..dfeadac63fc18cdfaf61731804a155bd078a3eda 100644 (file)
@@ -440,6 +440,10 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
                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);
index b25046d4ab4f6e5771208059a2281eb2f5883c51..905f66f21104b95674e6257f16dea4e39d7b0bc2 100644 (file)
@@ -88,12 +88,6 @@ int dhcpv4_setup_interface(struct interface *iface, bool enable)
                                        {INADDR_ANY}, {0}};
                int val = 1;
 
-               if (!iface->dhcpv4_assignments.next)
-                       INIT_LIST_HEAD(&iface->dhcpv4_assignments);
-
-               if (!iface->dhcpv4_fr_ips.next)
-                       INIT_LIST_HEAD(&iface->dhcpv4_fr_ips);
-
                iface->dhcpv4_event.uloop.fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
                if (iface->dhcpv4_event.uloop.fd < 0) {
                        syslog(LOG_ERR, "socket(AF_INET): %m");
@@ -160,7 +154,7 @@ int dhcpv4_setup_interface(struct interface *iface, bool enable)
 
                iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
                odhcpd_register(&iface->dhcpv4_event);
-       } else if (iface->dhcpv4_assignments.next) {
+       } else {
                while (!list_empty(&iface->dhcpv4_assignments))
                        free_assignment(list_first_entry(&iface->dhcpv4_assignments,
                                                        struct dhcp_assignment, head));
@@ -347,10 +341,11 @@ static void valid_until_cb(struct uloop_timeout *event)
        time_t now = odhcpd_time();
 
        avl_for_each_element(&interfaces, iface, avl) {
-               if (iface->dhcpv4 != MODE_SERVER || iface->dhcpv4_assignments.next == NULL)
+               struct dhcp_assignment *a, *n;
+
+               if (iface->dhcpv4 != MODE_SERVER)
                        continue;
 
-               struct dhcp_assignment *a, *n;
                list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
                        if (!INFINITE_VALID(a->valid_until) && a->valid_until < now)
                                free_assignment(a);
index 6426a74fca4adb9e1a15925dd373e25c5ad08b1c..81c2481aef40787f5bfaf32c7438bbe44ef62fd3 100644 (file)
@@ -62,7 +62,7 @@ int dhcpv6_ia_init(void)
 
 int dhcpv6_ia_setup_interface(struct interface *iface, bool enable)
 {
-       if (!enable && iface->ia_assignments.next) {
+       if (!enable) {
                struct dhcp_assignment *c;
 
                while (!list_empty(&iface->ia_assignments)) {
@@ -74,9 +74,6 @@ int dhcpv6_ia_setup_interface(struct interface *iface, bool enable)
        if (enable && iface->dhcpv6 == MODE_SERVER) {
                struct dhcp_assignment *border;
 
-               if (!iface->ia_assignments.next)
-                       INIT_LIST_HEAD(&iface->ia_assignments);
-
                if (list_empty(&iface->ia_assignments)) {
                        border = calloc(1, sizeof(*border));
 
@@ -326,8 +323,7 @@ void dhcpv6_ia_write_statefile(void)
                                        ctxt.iface->dhcpv4 != MODE_SERVER)
                                continue;
 
-                       if (ctxt.iface->dhcpv6 == MODE_SERVER &&
-                                       ctxt.iface->ia_assignments.next) {
+                       if (ctxt.iface->dhcpv6 == MODE_SERVER) {
                                list_for_each_entry(ctxt.c, &ctxt.iface->ia_assignments, head) {
                                        if (!(ctxt.c->flags & OAF_BOUND) || ctxt.c->managed_size < 0)
                                                continue;
@@ -355,9 +351,9 @@ void dhcpv6_ia_write_statefile(void)
                                }
                        }
 
-                       if (ctxt.iface->dhcpv4 == MODE_SERVER &&
-                                       ctxt.iface->dhcpv4_assignments.next) {
+                       if (ctxt.iface->dhcpv4 == MODE_SERVER) {
                                struct dhcp_assignment *c;
+
                                list_for_each_entry(c, &ctxt.iface->dhcpv4_assignments, head) {
                                        if (!(c->flags & OAF_BOUND))
                                                continue;
@@ -771,7 +767,7 @@ static void valid_until_cb(struct uloop_timeout *event)
        avl_for_each_element(&interfaces, iface, avl) {
                struct dhcp_assignment *a, *n;
 
-               if (iface->dhcpv6 != MODE_SERVER || iface->ia_assignments.next == NULL)
+               if (iface->dhcpv6 != MODE_SERVER)
                        continue;
 
                list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
index 1bb8237eb42d9ce4b90a1d506a79cdce25f7c31c..e9c5b6fb542813fe860d3fe171d0adf423ee6385 100644 (file)
@@ -29,7 +29,7 @@ static int handle_dhcpv4_leases(struct ubus_context *ctx, _unused struct ubus_ob
        a = blobmsg_open_table(&b, "device");
 
        avl_for_each_element(&interfaces, iface, avl) {
-               if (iface->dhcpv4 != MODE_SERVER || iface->dhcpv4_assignments.next == NULL)
+               if (iface->dhcpv4 != MODE_SERVER)
                        continue;
 
                void *i = blobmsg_open_table(&b, iface->ifname);
@@ -122,7 +122,7 @@ static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct
        a = blobmsg_open_table(&b, "device");
 
        avl_for_each_element(&interfaces, iface, avl) {
-               if (iface->dhcpv6 != MODE_SERVER || iface->ia_assignments.next == NULL)
+               if (iface->dhcpv6 != MODE_SERVER)
                        continue;
 
                void *i = blobmsg_open_table(&b, iface->ifname);