From a2dd8d6bd918fb211db491c7dcb1810032f3f8ca Mon Sep 17 00:00:00 2001 From: Dainis Jonitis Date: Mon, 29 Apr 2019 14:40:14 +0300 Subject: [PATCH] treewide: always init interface list heads during initialization 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 --- src/config.c | 4 ++++ src/dhcpv4.c | 13 ++++--------- src/dhcpv6-ia.c | 14 +++++--------- src/ubus.c | 4 ++-- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/config.c b/src/config.c index 399922b..dfeadac 100644 --- a/src/config.c +++ b/src/config.c @@ -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); diff --git a/src/dhcpv4.c b/src/dhcpv4.c index b25046d..905f66f 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -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); diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c index 6426a74..81c2481 100644 --- a/src/dhcpv6-ia.c +++ b/src/dhcpv6-ia.c @@ -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) { diff --git a/src/ubus.c b/src/ubus.c index 1bb8237..e9c5b6f 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -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); -- 2.25.1