silently drop unicast questions that dont originate from port 5353
[project/mdnsd.git] / interface.c
index 2b09ad3ce56892879a0d9441dbf19f6e1961c368..d26ed08ba9f54b5cea39b52134e4c448dc91e855 100644 (file)
@@ -38,6 +38,7 @@
 #include "util.h"
 #include "dns.h"
 #include "announce.h"
+#include "service.h"
 
 static int
 interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len)
@@ -110,6 +111,11 @@ interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len)
 int
 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
 {
+       if (debug > 1) {
+               fprintf(stderr, "TX ipv%d: %s\n", iface->v6 * 2 + 4, iface->name);
+               fprintf(stderr, "  multicast: %d\n", iface->multicast);
+       }
+
        if (iface->v6)
                return interface_send_packet6(iface, iov, iov_len);
 
@@ -194,8 +200,7 @@ read_socket4(struct uloop_fd *u, unsigned int events)
        if (debug > 1) {
                char buf[256];
 
-               fprintf(stderr, "iface: %s\n", iface->name);
-               fprintf(stderr, "  v6: %d\n", iface->v6);
+               fprintf(stderr, "RX ipv4: %s\n", iface->name);
                fprintf(stderr, "  multicast: %d\n", iface->multicast);
                inet_ntop(AF_INET, &from.sin_addr, buf, 256);
                fprintf(stderr, "  src %s:%d\n", buf, from.sin_port);
@@ -208,7 +213,7 @@ read_socket4(struct uloop_fd *u, unsigned int events)
        if (inp->ipi_ifindex != iface->ifindex)
                fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
        else
-               dns_handle_packet(iface, buffer, len, 0);
+               dns_handle_packet(iface, (struct sockaddr *) &from, from.sin_port, buffer, len);
 }
 
 static void
@@ -272,8 +277,7 @@ read_socket6(struct uloop_fd *u, unsigned int events)
        if (debug > 1) {
                char buf[256];
 
-               fprintf(stderr, "iface: %s\n", iface->name);
-               fprintf(stderr, "  v6: %d\n", iface->v6);
+               fprintf(stderr, "RX ipv6: %s\n", iface->name);
                fprintf(stderr, "  multicast: %d\n", iface->multicast);
                inet_ntop(AF_INET6, &from.sin6_addr, buf, 256);
                fprintf(stderr, "  src %s:%d\n", buf, from.sin6_port);
@@ -284,7 +288,7 @@ read_socket6(struct uloop_fd *u, unsigned int events)
        if (inp->ipi6_ifindex != iface->ifindex)
                fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
        else
-               dns_handle_packet(iface, buffer, len, 0);
+               dns_handle_packet(iface, (struct sockaddr *) &from, from.sin6_port, buffer, len);
 }
 
 static int
@@ -375,6 +379,9 @@ reconnect_socket4(struct uloop_timeout *timeout)
                goto retry;
        }
 
+       if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_BINDTODEVICE, iface->name, strlen(iface->name) < 0))
+               fprintf(stderr, "ioctl failed: SO_BINDTODEVICE\n");
+
        if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
                fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
 
@@ -416,6 +423,9 @@ reconnect_socket6(struct uloop_timeout *timeout)
                goto retry;
        }
 
+       if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_BINDTODEVICE, iface->name, strlen(iface->name) < 0))
+               fprintf(stderr, "ioctl failed: SO_BINDTODEVICE\n");
+
        if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
                fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n");
 
@@ -575,4 +585,17 @@ int interface_add(const char *name)
        return !v4 && !v6;
 }
 
+void interface_shutdown(void)
+{
+       struct interface *iface;
+
+       vlist_for_each_element(&interfaces, iface, node)
+               if (iface->fd.fd > 0 && iface->multicast) {
+                       service_announce(iface, 0);
+                       service_reply_a(iface, 0);
+               }
+       vlist_for_each_element(&interfaces, iface, node)
+               interface_close(iface);
+}
+
 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);