saner debug output
[project/mdnsd.git] / interface.c
index 2a695a1..1584c24 100644 (file)
@@ -110,6 +110,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);
 
@@ -184,24 +189,29 @@ 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)
+       else
                dns_handle_packet(iface, buffer, len, 0);
 }
 
@@ -211,12 +221,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 +241,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,12 +255,36 @@ 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 (ifindex != iface->ifindex)
+
+       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 (inp->ipi6_ifindex != iface->ifindex)
                fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
        else
                dns_handle_packet(iface, buffer, len, 0);
@@ -335,7 +371,8 @@ reconnect_socket4(struct uloop_timeout *timeout)
        struct interface *iface = container_of(timeout, struct interface, reconnect);
        int yes = 1;
 
-       iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV4ONLY, iface->mcast_addr, "5353");
+       iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV4ONLY,
+               (iface->multicast) ? (iface->mcast_addr) : (iface->v4_addrs), "5353");
        if (iface->fd.fd < 0) {
                fprintf(stderr, "failed to add listener %s: %s\n", iface->mcast_addr, strerror(errno));
                goto retry;
@@ -350,14 +387,16 @@ reconnect_socket4(struct uloop_timeout *timeout)
        if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
                fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
 
-       if (interface_mcast_setup4(iface)) {
+       if (iface->multicast && interface_mcast_setup4(iface)) {
                iface->fd.fd = -1;
                goto retry;
        }
 
        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, 1);
+               announce_init(iface);
+       }
 
        return;
 
@@ -373,7 +412,7 @@ 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));
@@ -392,14 +431,18 @@ reconnect_socket6(struct uloop_timeout *timeout)
        if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
                fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
 
-       if (interface_socket_setup6(iface)) {
+       if (iface->multicast && interface_socket_setup6(iface)) {
                iface->fd.fd = -1;
                goto retry;
        }
 
        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, 1);
+               announce_init(iface);
+       }
+
        return;
 
 retry:
@@ -436,7 +479,7 @@ iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
        }
 }
 
-static struct interface* _interface_add(const char *name, int v6)
+static struct interface* _interface_add(const char *name, int multicast, int v6)
 {
        struct interface *iface;
        char *name_buf;
@@ -444,13 +487,14 @@ static struct interface* _interface_add(const char *name, 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);
        iface->fd.fd = -1;
+       iface->multicast = multicast;
        iface->v6 = v6;
        if (v6)
                iface->mcast_addr = MCAST_ADDR6;
@@ -470,7 +514,7 @@ error:
 
 int interface_add(const char *name)
 {
-       struct interface *v4 = NULL, *v6 = NULL;
+       struct interface *v4 = NULL, *v6 = NULL, *unicast;
        struct ifaddrs *ifap, *ifa;
 
        getifaddrs(&ifap);
@@ -484,12 +528,21 @@ int interface_add(const char *name)
                        if (cfg_proto && (cfg_proto != 4))
                                continue;
 
-                       v4 = _interface_add(name, 0);
-                       if (!v4)
+                       unicast = _interface_add(name, 0, 0);
+                       if (!unicast)
                                continue;
-
                        sa = (struct sockaddr_in *) ifa->ifa_addr;
+                       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 = _interface_add(name, 1, 0);
+                       if (!v4)
+                               continue;
                        memcpy(&v4->v4_addr, &sa->sin_addr, sizeof(v4->v4_addr));
+                       inet_ntop(AF_INET, &sa->sin_addr, v4->v4_addrs, sizeof(v4->v4_addrs));
+
+                       v4->peer = unicast;
+                       unicast->peer = v4;
                }
 
                if (ifa->ifa_addr->sa_family == AF_INET6 && !v6) {
@@ -503,10 +556,20 @@ int interface_add(const char *name)
                        if (memcmp(&sa6->sin6_addr, &ll_prefix, 2))
                                continue;
 
-                       v6 = _interface_add(name, 1);
+                       unicast = _interface_add(name, 0, 1);
+                       if (!unicast)
+                               continue;
+                       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 = _interface_add(name, 1, 1);
                        if (!v6)
                                continue;
                        memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr));
+                       inet_ntop(AF_INET6, &sa6->sin6_addr, v6->v6_addrs, sizeof(v6->v6_addrs));
+
+                       v6->peer = unicast;
+                       unicast->peer = v6;
                }
        }