Rename project to umdns
[project/mdnsd.git] / interface.c
index bb4282f..587b188 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);
 
@@ -133,6 +139,25 @@ static void interface_free(struct interface *iface)
        free(iface);
 }
 
+static int
+interface_valid_src(void *ip1, void *mask, void *ip2, int len)
+{
+       uint8_t *i1 = ip1;
+       uint8_t *i2 = ip2;
+       uint8_t *m = mask;
+       int i;
+
+       if (cfg_no_subnet)
+               return 0;
+
+       for (i = 0; i < len; i++, i1++, i2++, m++) {
+               if ((*i1 & *m) != (*i2 & *m))
+                       return -1;
+       }
+
+       return 0;
+}
+
 static void
 read_socket4(struct uloop_fd *u, unsigned int events)
 {
@@ -184,25 +209,30 @@ read_socket4(struct uloop_fd *u, unsigned int events)
 
                default:
                        fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
-                       break;
+                       return;
                }
        }
 
-       if (0) {
+       if (ttl != 255)
+               return;
+
+       if (debug > 1) {
                char buf[256];
 
+               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, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port);
+               fprintf(stderr, "  src %s:%d\n", buf, from.sin_port);
                inet_ntop(AF_INET, &inp->ipi_spec_dst, buf, 256);
-               fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port);
+               fprintf(stderr, "  dst %s\n", buf);
                inet_ntop(AF_INET, &inp->ipi_addr, buf, 256);
-               fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port);
+               fprintf(stderr, "  real %s\n", buf);
        }
 
        if (inp->ipi_ifindex != iface->ifindex)
                fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
-       else if (ttl == 255)
-               dns_handle_packet(iface, buffer, len, 0);
+       else if (!interface_valid_src((void *) &iface->v4_addr, (void *) &iface->v4_netmask, (void *) &from.sin_addr, 4))
+               dns_handle_packet(iface, (struct sockaddr *) &from, from.sin_port, buffer, len);
 }
 
 static void
@@ -211,12 +241,14 @@ read_socket6(struct uloop_fd *u, unsigned int events)
        struct interface *iface = container_of(u, struct interface, fd);
        static uint8_t buffer[8 * 1024];
        struct iovec iov[1];
-       char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + 1];
+       char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
        struct cmsghdr *cmsgptr;
        struct msghdr msg;
        socklen_t len;
        struct sockaddr_in6 from;
        int flags = 0, ifindex = -1;
+       int ttl = 0;
+       struct in6_pktinfo *inp = NULL;
 
        if (u->eof) {
                interface_close(iface);
@@ -229,7 +261,7 @@ read_socket6(struct uloop_fd *u, unsigned int events)
 
        memset(&msg, 0, sizeof(msg));
        msg.msg_name = (struct sockaddr *) &from;
-       msg.msg_namelen = (iface->v6) ? (sizeof(struct sockaddr_in6)) : (sizeof(struct sockaddr_in));
+       msg.msg_namelen = sizeof(struct sockaddr_in6);
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
        msg.msg_control = &cmsg6;
@@ -243,15 +275,39 @@ read_socket6(struct uloop_fd *u, unsigned int events)
        for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
                void *c = CMSG_DATA(cmsgptr);
 
-               if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_PKTINFO)
-                       ifindex = ((struct in_pktinfo *) c)->ipi_ifindex;
-               else if (cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_PKTINFO)
-                       ifindex = ((struct in6_pktinfo *) c)->ipi6_ifindex;
+               switch (cmsgptr->cmsg_type) {
+               case IPV6_PKTINFO:
+                       inp = ((struct in6_pktinfo *) c);
+                       break;
+
+               case IPV6_HOPLIMIT:
+                       ttl = (uint8_t) *((int *) c);
+                       break;
+
+               default:
+                       fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
+                       return;
+               }
+       }
+
+       if (ttl != 255)
+               return;
+
+       if (debug > 1) {
+               char buf[256];
+
+               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);
+               inet_ntop(AF_INET6, &inp->ipi6_addr, buf, 256);
+               fprintf(stderr, "  dst %s\n", buf);
        }
-       if (ifindex != iface->ifindex)
+
+       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);
+       else if (!interface_valid_src((void *) &iface->v6_addr, (void *) &iface->v6_netmask, (void *) &from.sin6_addr, 16))
+               dns_handle_packet(iface, (struct sockaddr *) &from, from.sin6_port, buffer, len);
 }
 
 static int
@@ -342,6 +398,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");
 
@@ -357,8 +416,10 @@ reconnect_socket4(struct uloop_timeout *timeout)
        }
 
        uloop_fd_add(&iface->fd, ULOOP_READ);
-       dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
-       announce_init(iface);
+       if (iface->multicast) {
+               dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0);
+               announce_init(iface);
+       }
 
        return;
 
@@ -374,13 +435,16 @@ reconnect_socket6(struct uloop_timeout *timeout)
        int ttl = 255;
        int yes = 1;
 
-       snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", iface->mcast_addr, iface->name);
+       snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", (iface->multicast) ? (iface->mcast_addr) : (iface->v6_addrs), iface->name);
        iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV6ONLY, mcast_addr, "5353");
        if (iface->fd.fd < 0) {
                fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno));
                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");
 
@@ -399,8 +463,12 @@ reconnect_socket6(struct uloop_timeout *timeout)
        }
 
        uloop_fd_add(&iface->fd, ULOOP_READ);
-       dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
-       announce_init(iface);
+
+       if (iface->multicast) {
+               dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0);
+               announce_init(iface);
+       }
+
        return;
 
 retry:
@@ -445,9 +513,9 @@ static struct interface* _interface_add(const char *name, int multicast, int v6)
 
        iface = calloc_a(sizeof(*iface),
                &name_buf, strlen(name) + 1,
-               &id_buf, strlen(name) + 3);
+               &id_buf, strlen(name) + 5);
 
-       sprintf(id_buf, "%d_%s", v6, name);
+       sprintf(id_buf, "%d_%d_%s", multicast, v6, name);
        iface->name = strcpy(name_buf, name);
        iface->id = id_buf;
        iface->ifindex = if_nametoindex(name);
@@ -486,20 +554,26 @@ int interface_add(const char *name)
                        if (cfg_proto && (cfg_proto != 4))
                                continue;
 
+                       unicast = _interface_add(name, 0, 0);
+                       if (!unicast)
+                               continue;
                        v4 = _interface_add(name, 1, 0);
                        if (!v4)
                                continue;
 
                        sa = (struct sockaddr_in *) ifa->ifa_addr;
                        memcpy(&v4->v4_addr, &sa->sin_addr, sizeof(v4->v4_addr));
+                       memcpy(&unicast->v4_addr, &sa->sin_addr, sizeof(unicast->v4_addr));
+
                        inet_ntop(AF_INET, &sa->sin_addr, v4->v4_addrs, sizeof(v4->v4_addrs));
+                       inet_ntop(AF_INET, &sa->sin_addr, unicast->v4_addrs, sizeof(unicast->v4_addrs));
 
-                       unicast = _interface_add(name, 0, 0);
-                       if (!unicast)
-                               continue;
+                       sa = (struct sockaddr_in *) ifa->ifa_netmask;
+                       memcpy(&unicast->v4_netmask, &sa->sin_addr, sizeof(unicast->v4_netmask));
+                       memcpy(&v4->v4_netmask, &sa->sin_addr, sizeof(v4->v4_netmask));
 
-                       memcpy(&unicast->v4_addr, &sa->sin_addr, sizeof(unicast->v4_addr));
-                       inet_ntop(AF_INET, &sa->sin_addr, unicast->v4_addrs, sizeof(unicast->v4_addrs));
+                       v4->peer = unicast;
+                       unicast->peer = v4;
                }
 
                if (ifa->ifa_addr->sa_family == AF_INET6 && !v6) {
@@ -513,18 +587,25 @@ int interface_add(const char *name)
                        if (memcmp(&sa6->sin6_addr, &ll_prefix, 2))
                                continue;
 
+                       unicast = _interface_add(name, 0, 1);
+                       if (!unicast)
+                               continue;
                        v6 = _interface_add(name, 1, 1);
                        if (!v6)
                                continue;
+
                        memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr));
+                       memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_addr));
+
                        inet_ntop(AF_INET6, &sa6->sin6_addr, v6->v6_addrs, sizeof(v6->v6_addrs));
+                       inet_ntop(AF_INET6, &sa6->sin6_addr, unicast->v6_addrs, sizeof(unicast->v6_addrs));
 
-                       unicast = _interface_add(name, 0, 1);
-                       if (!unicast)
-                               continue;
+                       sa6 = (struct sockaddr_in6 *) ifa->ifa_netmask;
+                       memcpy(&v6->v6_netmask, &sa6->sin6_addr, sizeof(v6->v6_netmask));
+                       memcpy(&unicast->v6_netmask, &sa6->sin6_addr, sizeof(unicast->v6_netmask));
 
-                       memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_addr));
-                       inet_ntop(AF_INET6, &sa6->sin6_addr, unicast->v6_addrs, sizeof(unicast->v6_addrs));
+                       v6->peer = unicast;
+                       unicast->peer = v6;
                }
        }
 
@@ -533,4 +614,26 @@ 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) {
+                       dns_reply_a(iface, 0);
+                       service_announce_services(iface, 0);
+               }
+       vlist_for_each_element(&interfaces, iface, node)
+               interface_close(iface);
+}
+
+struct interface*
+interface_get(const char *name, int v6, int multicast)
+{
+       char id_buf[32];
+       snprintf(id_buf, sizeof(id_buf), "%d_%d_%s", multicast, v6, name);
+       struct interface *iface = vlist_find(&interfaces, id_buf, iface, node);
+       return iface;
+}
+
 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);