move announce state to struct interface
authorFelix Fietkau <nbd@openwrt.org>
Mon, 9 Jun 2014 19:13:38 +0000 (21:13 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 9 Jun 2014 19:13:38 +0000 (21:13 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
announce.c
announce.h
interface.h
main.c

index 4971c4e3e70eedbca333612a926d83c582caf7cf..4706cc2b435fb1ebc5fb7cbfedee4c37dc26a875 100644 (file)
@@ -35,29 +35,28 @@ enum {
        STATE_ANNOUNCE,
 };
 
-static struct uloop_timeout announce;
-static int announce_state;
 int announce_ttl = 75 * 60;
 
 static void
 announce_timer(struct uloop_timeout *timeout)
 {
+       struct interface *iface = container_of(timeout, struct interface, announce_timer);
        char host[256];
 
        snprintf(host, sizeof(host), "%s.local", hostname);
 
-       switch (announce_state) {
+       switch (iface->announce_state) {
                case STATE_PROBE1:
                case STATE_PROBE2:
                case STATE_PROBE3:
-                       dns_send_question(cur_iface, host, TYPE_ANY);
+                       dns_send_question(iface, host, TYPE_ANY);
                        uloop_timeout_set(timeout, 250);
-                       announce_state++;
+                       iface->announce_state++;
                        break;
 
                case STATE_PROBE_WAIT:
                        uloop_timeout_set(timeout, 500);
-                       announce_state++;
+                       iface->announce_state++;
                        break;
 
                case STATE_PROBE_END:
@@ -65,19 +64,25 @@ announce_timer(struct uloop_timeout *timeout)
                                fprintf(stderr, "the host %s already exists. stopping announce service\n", host);
                                return;
                        }
-                       announce_state++;
+                       iface->announce_state++;
 
                case STATE_ANNOUNCE:
-                       service_announce(cur_iface);
+                       service_announce(iface);
                        uloop_timeout_set(timeout, announce_ttl * 800);
                        break;
        }
 }
 
 void
-announce_init(void)
+announce_init(struct interface *iface)
 {
-       announce_state = STATE_PROBE1;
-       announce.cb = announce_timer;
-       uloop_timeout_set(&announce, 100);
+       iface->announce_state = STATE_PROBE1;
+       iface->announce_timer.cb = announce_timer;
+       uloop_timeout_set(&iface->announce_timer, 100);
+}
+
+void
+announce_free(struct interface *iface)
+{
+       uloop_timeout_cancel(&iface->announce_timer);
 }
index efd06ffe4b43a0b1097be4b2de7fba54509d07f1..d645d6d28645c469301ff0fa4b4cb4bd7bfd9ff5 100644 (file)
 #ifndef _ANNOUNCE_H__
 #define _ANNOUNCE_H__
 
+struct interface;
 extern int announce_ttl;
-extern void announce_init(void);
+
+void announce_init(struct interface *iface);
+void announce_free(struct interface *iface);
 
 #endif
index fe709aaddf32db4a9ba4eb307d9302066808e33b..05104c27ad6205eb37677a3e32555ad3273c0cd7 100644 (file)
@@ -32,6 +32,9 @@ struct interface {
 
        int ifindex;
        const char *ip;
+
+       struct uloop_timeout announce_timer;
+       int announce_state;
 };
 
 int interface_add(const char *name);
diff --git a/main.c b/main.c
index baadda151957a49f10bd549150befef22b1273e5..505fb7d7667955478effb61642db02122a9aaa26 100644 (file)
--- a/main.c
+++ b/main.c
@@ -184,7 +184,7 @@ reconnect_socket(struct uloop_timeout *timeout)
                uloop_fd_add(&cur_iface->fd, ULOOP_READ);
                sleep(5);
                dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
-               announce_init();
+               announce_init(cur_iface);
        }
 }