treewide: initialize properly file descriptors
[project/odhcpd.git] / src / ndp.c
index 8cae97ffd56b7c91455bb53b3ebb8fa9ecfa7c71..f9f24954fc559f6dbe2212d8ccee4ebf6c7151bd 100644 (file)
--- a/src/ndp.c
+++ b/src/ndp.c
@@ -103,7 +103,7 @@ int ndp_init(void)
        netlink_add_netevent_handler(&ndp_netevent_handler);
 
 out:
-       if (ret < 0 && ping_socket > 0) {
+       if (ret < 0 && ping_socket >= 0) {
                close(ping_socket);
                ping_socket = -1;
        }
@@ -125,7 +125,7 @@ int ndp_setup_interface(struct interface *iface, bool enable)
                goto out;
        }
 
-       if (iface->ndp_event.uloop.fd > 0) {
+       if (iface->ndp_event.uloop.fd >= 0) {
                uloop_fd_delete(&iface->ndp_event.uloop);
                close(iface->ndp_event.uloop.fd);
                iface->ndp_event.uloop.fd = -1;
@@ -203,7 +203,7 @@ int ndp_setup_interface(struct interface *iface, bool enable)
                netlink_dump_neigh_table(true);
 
  out:
-       if (ret < 0 && iface->ndp_event.uloop.fd > 0) {
+       if (ret < 0 && iface->ndp_event.uloop.fd >= 0) {
                close(iface->ndp_event.uloop.fd);
                iface->ndp_event.uloop.fd = -1;
        }
@@ -284,6 +284,7 @@ static void handle_solicit(void *addr, void *data, size_t len,
        struct ip6_hdr *ip6 = data;
        struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
        struct sockaddr_ll *ll = addr;
+       struct interface *c;
        char ipbuf[INET6_ADDRSTRLEN];
        uint8_t mac[6];
 
@@ -311,11 +312,11 @@ static void handle_solicit(void *addr, void *data, size_t len,
        if (!memcmp(ll->sll_addr, mac, sizeof(mac)))
                return; /* Looped back */
 
-       struct interface *c;
-       list_for_each_entry(c, &interfaces, head)
+       avl_for_each_element(&interfaces, c, avl) {
                if (iface != c && c->ndp == MODE_RELAY &&
                                (ns_is_dad || !c->external))
                        ping6(&req->nd_ns_target, c);
+       }
 }
 
 /* Use rtnetlink to modify kernel routes */
@@ -340,17 +341,15 @@ static void setup_addr_for_relaying(struct in6_addr *addr, struct interface *ifa
 
        inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
 
-       list_for_each_entry(c, &interfaces, head) {
-               if (iface == c || (c->ndp != MODE_RELAY && !add))
+       avl_for_each_element(&interfaces, c, avl) {
+               if (iface == c || c->ndp != MODE_RELAY)
                        continue;
 
-               bool neigh_add = (c->ndp == MODE_RELAY ? add : false);
-
-               if (netlink_setup_proxy_neigh(addr, c->ifindex, neigh_add))
+               if (netlink_setup_proxy_neigh(addr, c->ifindex, add))
                        syslog(LOG_DEBUG, "Failed to %s proxy neighbour entry %s on %s",
-                               neigh_add ? "add" : "delete", ipbuf, c->name);
+                               add ? "add" : "delete", ipbuf, c->name);
                else
                        syslog(LOG_DEBUG, "%s proxy neighbour entry %s on %s",
-                               neigh_add ? "Added" : "Deleted", ipbuf, c->name);
+                               add ? "Added" : "Deleted", ipbuf, c->name);
        }
 }