move interface connect/read handling to interface.c
authorFelix Fietkau <nbd@openwrt.org>
Mon, 9 Jun 2014 19:43:06 +0000 (21:43 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 9 Jun 2014 19:43:06 +0000 (21:43 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
interface.c
interface.h
main.c

index 41e0dafd14399d50ad605e2936895ae0e665223a..b5022a724b8ba608fdb970c85daea75ea68c6dc3 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,22 +71,76 @@ 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);
-       }
+       uloop_fd_delete(&iface->fd);
+       close(iface->fd.fd);
+       iface->fd.fd = -1;
+}
+
+static void interface_free(struct interface *iface)
+{
+       interface_close(iface);
        free(iface);
 }
 
+static void
+read_socket(struct uloop_fd *u, unsigned int events)
+{
+       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;
+       }
+
+       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);
+}
+
+static void
+reconnect_socket(struct uloop_timeout *timeout)
+{
+       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)
 {
-       cur_iface = iface;
+       iface->fd.cb = read_socket;
+       iface->reconnect.cb = reconnect_socket;
+       uloop_timeout_set(&iface->reconnect, 100);
 }
 
 static void
index 05104c27ad6205eb37677a3e32555ad3273c0cd7..b9ecd8aec82ebdd40fa6ca4a533ec53f067d0633 100644 (file)
 #include <libubox/vlist.h>
 
 extern struct vlist_tree interfaces;
-extern struct interface *cur_iface;
 
 struct interface {
        struct vlist_node node;
 
        const char *name;
        struct uloop_fd fd;
+       struct uloop_timeout reconnect;
 
        int ifindex;
        const char *ip;
diff --git a/main.c b/main.c
index d0acf162e82c497686490d83123852404b9b00bb..22ad73d0e7180cb8edee7267260deeae4de98adb 100644 (file)
--- a/main.c
+++ b/main.c
@@ -28,9 +28,7 @@
 #include <asm/byteorder.h>
 
 #include <libubus.h>
-#include <libubox/usock.h>
 #include <libubox/uloop.h>
-#include <libubox/avl-cmp.h>
 
 #include "dns.h"
 #include "ubus.h"
 #include "announce.h"
 #include "interface.h"
 
-static struct uloop_timeout reconnect;
 char *iface_name = "eth0";
 
-static void
-read_socket(struct uloop_fd *u, unsigned int events)
-{
-       struct interface *iface = container_of(u, struct interface, fd);
-       static uint8_t buffer[8 * 1024];
-       int len;
-
-       if (u->eof) {
-               uloop_fd_delete(u);
-               close(u->fd);
-               u->fd = -1;
-               uloop_timeout_set(&reconnect, 1000);
-               return;
-       }
-
-       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);
-}
-
-static void
-reconnect_socket(struct uloop_timeout *timeout)
-{
-       cur_iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
-       if (cur_iface->fd.fd < 0) {
-               fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
-               uloop_timeout_set(&reconnect, 1000);
-       } else {
-               if (interface_socket_setup(cur_iface)) {
-                       uloop_timeout_set(&reconnect, 1000);
-                       cur_iface->fd.fd = -1;
-                       return;
-               }
-
-               uloop_fd_add(&cur_iface->fd, ULOOP_READ);
-               sleep(5);
-               dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
-               announce_init(cur_iface);
-       }
-}
-
 int
 main(int argc, char **argv)
 {
@@ -124,9 +76,6 @@ main(int argc, char **argv)
                return -1;
        }
 
-       if (!cur_iface)
-               return -1;
-
        signal_setup();
 
        if (cache_init())
@@ -134,10 +83,6 @@ main(int argc, char **argv)
 
        service_init();
 
-       cur_iface->fd.cb = read_socket;
-       reconnect.cb = reconnect_socket;
-
-       uloop_timeout_set(&reconnect, 100);
        ubus_startup();
        uloop_run();
        uloop_done();