odhcpd: Replace strerror(errno) with %m format
[project/odhcpd.git] / src / odhcpd.c
index 13697b7..71b803d 100644 (file)
@@ -187,8 +187,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);
@@ -371,12 +371,6 @@ static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int even
                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,10 +380,26 @@ 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);
        }
 }
 
@@ -466,6 +476,16 @@ void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len)
        *dst = 0;
 }
 
+const char *odhcpd_print_mac(const uint8_t *mac, const size_t len)
+{
+       static char buf[32];
+
+       snprintf(buf, sizeof(buf), "%02x", mac[0]);
+       for (size_t i = 1, j = 2; i < len && j < sizeof(buf); i++, j += 3)
+               snprintf(buf + j, sizeof(buf) - j, ":%02x", mac[i]);
+
+       return buf;
+}
 
 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits)
 {