treewide: switch to C-code style comments
[project/odhcpd.git] / src / odhcpd.c
index 97a6de92f34620fa79f2d8516d354e957a4b3f08..0e0002d95159e402e4c004b76021312ec58c667c 100644 (file)
@@ -120,7 +120,7 @@ int main(int argc, char **argv)
 }
 
 
-// Read IPv6 MTU for interface
+/* Read IPv6 MTU for interface */
 int odhcpd_get_interface_config(const char *ifname, const char *what)
 {
        char buf[64];
@@ -139,25 +139,27 @@ int odhcpd_get_interface_config(const char *ifname, const char *what)
 }
 
 
-// Read IPv6 MAC for interface
+/* Read IPv6 MAC for interface */
 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
 {
        struct ifreq ifr;
+
        memset(&ifr, 0, sizeof(ifr));
-       strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
+       strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name) - 1);
        if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
                return -1;
+
        memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
        return 0;
 }
 
 
-// Forwards a packet on a specific interface
+/* Forwards a packet on a specific interface */
 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
                struct iovec *iov, size_t iov_len,
                const struct interface *iface)
 {
-       // Construct headers
+       /* Construct headers */
        uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
        struct msghdr msg = {
                .msg_name = (void *) dest,
@@ -169,7 +171,7 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
                .msg_flags = 0
        };
 
-       // Set control data (define destination interface)
+       /* Set control data (define destination interface) */
        struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
        chdr->cmsg_level = IPPROTO_IPV6;
        chdr->cmsg_type = IPV6_PKTINFO;
@@ -177,7 +179,7 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
        struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
        pktinfo->ipi6_ifindex = iface->ifindex;
 
-       // Also set scope ID if link-local
+       /* Also set scope ID if link-local */
        if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
                        || IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
                dest->sin6_scope_id = iface->ifindex;
@@ -187,8 +189,8 @@ 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 (%s)",
-                               ipbuf, iface->ifname, strerror(errno));
+               syslog(LOG_NOTICE, "Failed to send to %s%%%s (%m)",
+                               ipbuf, iface->ifname);
        else
                syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
                                (long)sent, ipbuf, iface->ifname);
@@ -198,20 +200,27 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
 
 static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
 {
-       int status = -1;
-       struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, ifindex};
+       int ret = -1;
+       struct sockaddr_in6 addr;
        socklen_t alen = sizeof(addr);
        int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
+       if (sock < 0)
+               return -1;
+
+       memset(&addr, 0, sizeof(addr));
+       addr.sin6_family = AF_INET6;
+       inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &addr.sin6_addr);
+       addr.sin6_scope_id = ifindex;
+
        if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
                        !getsockname(sock, (struct sockaddr*)&addr, &alen)) {
                *lladdr = addr.sin6_addr;
-               status = 0;
+               ret = 0;
        }
 
        close(sock);
-
-       return status;
+       return ret;
 }
 
 /*
@@ -292,7 +301,7 @@ struct interface* odhcpd_get_master_interface(void)
 }
 
 
-// Convenience function to receive and do basic validation of packets
+/* Convenience function to receive and do basic validation of packets */
 static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int events)
 {
        struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
@@ -340,7 +349,7 @@ static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int even
                }
 
 
-               // Extract destination interface
+               /* Extract destination interface */
                int destiface = 0;
                int *hlim = NULL;
                void *dest = NULL;
@@ -363,20 +372,14 @@ static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int even
                        }
                }
 
-               // Check hoplimit if received
+               /* Check hoplimit if received */
                if (hlim && *hlim != 255)
                        continue;
 
-               // Detect interface for packet sockets
+               /* Detect interface for packet sockets */
                if (addr.ll.sll_family == AF_PACKET)
                        destiface = addr.ll.sll_ifindex;
 
-               struct interface *iface =
-                               odhcpd_get_interface_by_index(destiface);
-
-               if (!iface && addr.nl.nl_family != AF_NETLINK)
-                       continue;
-
                char ipbuf[INET6_ADDRSTRLEN] = "kernel";
                if (addr.ll.sll_family == AF_PACKET &&
                                len >= (ssize_t)sizeof(struct ip6_hdr))
@@ -386,14 +389,30 @@ static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int even
                else if (addr.in.sin_family == AF_INET)
                        inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
 
-               syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
-                               ipbuf, (iface) ? iface->ifname : "netlink");
+               /* From netlink */
+               if (addr.nl.nl_family == AF_NETLINK) {
+                       syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
+                                       ipbuf, "netlink");
+                       e->handle_dgram(&addr, data_buf, len, NULL, dest);
+                       return;
+               } else if (destiface != 0) {
+                       struct interface *iface;
+                       list_for_each_entry(iface, &interfaces, head) {
+                               if (iface->ifindex != destiface)
+                                       continue;
+
+                               syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
+                                               ipbuf, iface->ifname);
+
+                               e->handle_dgram(&addr, data_buf, len, iface, dest);
+                       }
+               }
+
 
-               e->handle_dgram(&addr, data_buf, len, iface, dest);
        }
 }
 
-// Register events for the multiplexer
+/* Register events for the multiplexer */
 int odhcpd_register(struct odhcpd_event *event)
 {
        event->uloop.cb = odhcpd_receive_packets;