store ip address as struct in_addr instead of a string
[project/mdnsd.git] / interface.c
index 41e0daf..bcd6f13 100644 (file)
@@ -27,6 +27,8 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include <libubox/usock.h>
+#include <libubox/uloop.h>
 #include <libubox/avl-cmp.h>
 #include <libubox/utils.h>
 #include "interface.h"
@@ -34,8 +36,6 @@
 #include "dns.h"
 #include "announce.h"
 
-struct interface *cur_iface = NULL;
-
 int
 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
 {
@@ -71,59 +71,61 @@ interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
        return sendmsg(fd, &m, 0);
 }
 
-static void interface_free(struct interface *iface)
+static void interface_close(struct interface *iface)
 {
-       if (cur_iface == iface)
-               cur_iface = NULL;
+       if (iface->fd.fd < 0)
+               return;
 
        announce_free(iface);
-       if (iface->fd.fd >= 0) {
-               uloop_fd_delete(&iface->fd);
-               close(iface->fd.fd);
-       }
-       free(iface);
+       uloop_fd_delete(&iface->fd);
+       close(iface->fd.fd);
+       iface->fd.fd = -1;
 }
 
-static void interface_start(struct interface *iface)
+static void interface_free(struct interface *iface)
 {
-       cur_iface = iface;
+       interface_close(iface);
+       free(iface);
 }
 
 static void
-iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
-               struct vlist_node *node_old)
+read_socket(struct uloop_fd *u, unsigned int events)
 {
-       struct interface *iface;
-
-       if (node_old) {
-               iface = container_of(node_old, struct interface, node);
-               interface_free(iface);
+       struct interface *iface = container_of(u, struct interface, fd);
+       static uint8_t buffer[8 * 1024];
+       int len;
+
+       if (u->eof) {
+               interface_close(iface);
+               uloop_timeout_set(&iface->reconnect, 1000);
+               return;
        }
 
-       if (node_new) {
-               iface = container_of(node_new, struct interface, node);
-               interface_start(iface);
+       len = read(u->fd, buffer, sizeof(buffer));
+       if (len < 1) {
+               fprintf(stderr, "read failed: %s\n", strerror(errno));
+               return;
        }
+
+       dns_handle_packet(iface, buffer, len);
 }
 
-int interface_socket_setup(struct interface *iface)
+static int
+interface_socket_setup(struct interface *iface)
 {
        struct ip_mreqn mreq;
        uint8_t ttl = 255;
        int yes = 1;
        int no = 0;
        struct sockaddr_in sa = { 0 };
-       struct in_addr in;
        int fd = iface->fd.fd;
 
-       inet_aton(iface->ip, &in);
-
        sa.sin_family = AF_INET;
        sa.sin_port = htons(MCAST_PORT);
        inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
 
        memset(&mreq, 0, sizeof(mreq));
-       mreq.imr_address.s_addr = in.s_addr;
+       mreq.imr_address.s_addr = iface->v4_addr.s_addr;
        mreq.imr_multiaddr = sa.sin_addr;
        mreq.imr_ifindex = iface->ifindex;
 
@@ -159,52 +161,100 @@ int interface_socket_setup(struct interface *iface)
        return 0;
 }
 
-static const char*
-get_iface_ipv4(const char *ifname)
+static void
+reconnect_socket(struct uloop_timeout *timeout)
 {
-       static char buffer[INET_ADDRSTRLEN];
+       struct interface *iface = container_of(timeout, struct interface, reconnect);
+
+       iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
+       if (iface->fd.fd < 0) {
+               fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
+               goto retry;
+       }
+
+       if (interface_socket_setup(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);
+       return;
+
+retry:
+       uloop_timeout_set(timeout, 1000);
+}
+
+
+static void interface_start(struct interface *iface)
+{
+       iface->fd.cb = read_socket;
+       iface->reconnect.cb = reconnect_socket;
+       uloop_timeout_set(&iface->reconnect, 100);
+}
+
+static void
+iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
+               struct vlist_node *node_old)
+{
+       struct interface *iface;
+
+       if (node_old) {
+               iface = container_of(node_old, struct interface, node);
+               interface_free(iface);
+       }
+
+       if (node_new) {
+               iface = container_of(node_new, struct interface, node);
+               interface_start(iface);
+       }
+}
+
+static int
+get_iface_ipv4(struct interface *iface)
+{
+       struct sockaddr_in *sin;
        struct ifreq ir;
-       const char *ret;
-       int sock;
+       int sock, ret = -1;
 
        sock = socket(AF_INET, SOCK_DGRAM, 0);
        if (sock < 0)
-               return NULL;
+               return -1;
 
        memset(&ir, 0, sizeof(struct ifreq));
-       strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name));
+       strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name));
 
-       if (ioctl(sock, SIOCGIFADDR, &ir) < 0)
-               return NULL;
+       ret = ioctl(sock, SIOCGIFADDR, &ir);
+       if (ret < 0)
+               goto out;
 
-       ret = inet_ntop(AF_INET, &((struct sockaddr_in *) &ir.ifr_addr)->sin_addr, buffer, sizeof(buffer));
-       close(sock);
+       sin = (struct sockaddr_in *) &ir.ifr_addr;
+       memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr));
 
+out:
+       close(sock);
        return ret;
 }
 
 int interface_add(const char *name)
 {
        struct interface *iface;
-       const char *ip_str;
-       char *name_buf, *ip_buf;
-
-       ip_str = get_iface_ipv4(name);
-       if (!ip_str)
-               return -1;
+       char *name_buf;
 
        iface = calloc_a(sizeof(*iface),
-               &name_buf, strlen(name) + 1,
-               &ip_buf, strlen(ip_str) + 1);
+               &name_buf, strlen(name) + 1);
 
        iface->name = strcpy(name_buf, name);
-       iface->ip = strcpy(ip_buf, ip_str);
        iface->ifindex = if_nametoindex(name);
        iface->fd.fd = -1;
 
        if (iface->ifindex <= 0)
                goto error;
 
+       if (get_iface_ipv4(iface))
+               goto error;
+
        vlist_add(&interfaces, &iface->node, name);
        return 0;