netlink: rework handling of netlink messages
[oweals/odhcpd.git] / src / odhcpd.c
index c1d51e7c717f5c1a1d655098add19b949915f667..fafa3f68642781e82fcfeb459c443fe14ad81a90 100644 (file)
@@ -53,7 +53,6 @@ static void sighandler(_unused int signal)
        uloop_end();
 }
 
-
 static void print_usage(const char *app)
 {
        printf(
@@ -64,6 +63,17 @@ static void print_usage(const char *app)
        );
 }
 
+static bool ipv6_enabled(void)
+{
+       int fd = socket(AF_INET6, SOCK_DGRAM, 0);
+
+       if (fd < 0)
+               return false;
+
+       close(fd);
+
+       return true;
+}
 
 int main(int argc, char **argv)
 {
@@ -104,16 +114,20 @@ int main(int argc, char **argv)
        if (netlink_init())
                return 4;
 
-       if (router_init())
-               return 4;
+       if (ipv6_enabled()) {
+               if (router_init())
+                       return 4;
 
-       if (dhcpv6_init())
-               return 4;
+               if (dhcpv6_init())
+                       return 4;
 
-       if (ndp_init())
+               if (ndp_init())
+                       return 4;
+       }
+#ifndef DHCPV4_SUPPORT
+       else
                return 4;
-
-#ifdef DHCPV4_SUPPORT
+#else
        if (dhcpv4_init())
                return 4;
 #endif
@@ -195,11 +209,11 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
 
        ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
        if (sent < 0)
-               syslog(LOG_NOTICE, "Failed to send to %s%%%s (%m)",
-                               ipbuf, iface->ifname);
+               syslog(LOG_NOTICE, "Failed to send to %s%%%s@%s (%m)",
+                               ipbuf, iface->name, iface->ifname);
        else
-               syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
-                               (long)sent, ipbuf, iface->ifname);
+               syslog(LOG_DEBUG, "Sent %zd bytes to %s%%%s@%s",
+                               sent, ipbuf, iface->name, iface->ifname);
        return sent;
 }
 
@@ -277,31 +291,23 @@ int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr
 struct interface* odhcpd_get_interface_by_index(int ifindex)
 {
        struct interface *iface;
-       list_for_each_entry(iface, &interfaces, head)
-               if (iface->ifindex == ifindex)
-                       return iface;
 
-       return NULL;
-}
-
-
-struct interface* odhcpd_get_interface_by_name(const char *name)
-{
-       struct interface *iface;
-       list_for_each_entry(iface, &interfaces, head)
-               if (!strcmp(iface->ifname, name))
+       avl_for_each_element(&interfaces, iface, avl) {
+               if (iface->ifindex == ifindex)
                        return iface;
+       }
 
        return NULL;
 }
 
-
 struct interface* odhcpd_get_master_interface(void)
 {
        struct interface *iface;
-       list_for_each_entry(iface, &interfaces, head)
+
+       avl_for_each_element(&interfaces, iface, avl) {
                if (iface->master)
                        return iface;
+       }
 
        return NULL;
 }
@@ -397,18 +403,19 @@ static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int even
 
                /* From netlink */
                if (addr.nl.nl_family == AF_NETLINK) {
-                       syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
-                                       ipbuf, "netlink");
+                       syslog(LOG_DEBUG, "Received %zd Bytes from %s%%netlink", len,
+                                       ipbuf);
                        e->handle_dgram(&addr, data_buf, len, NULL, dest);
                        return;
                } else if (destiface != 0) {
                        struct interface *iface;
-                       list_for_each_entry(iface, &interfaces, head) {
+
+                       avl_for_each_element(&interfaces, iface, avl) {
                                if (iface->ifindex != destiface)
                                        continue;
 
-                               syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
-                                               ipbuf, iface->ifname);
+                               syslog(LOG_DEBUG, "Received %zd Bytes from %s%%%s@%s", len,
+                                               ipbuf, iface->name, iface->ifname);
 
                                e->handle_dgram(&addr, data_buf, len, iface, dest);
                        }