add good bye messages
authorJohn Crispin <blogic@openwrt.org>
Tue, 2 Sep 2014 18:37:24 +0000 (20:37 +0200)
committerJohn Crispin <blogic@openwrt.org>
Tue, 2 Sep 2014 19:17:46 +0000 (21:17 +0200)
Signed-off-by: John Crispin <blogic@openwrt.org>
announce.c
dns.c
interface.c
interface.h
main.c
service.c
service.h

index 10389b8c662e3ac07deeaf7d5f66509084fb64d1..3ff64b26d23abd967296d30403810498048dcfb7 100644 (file)
@@ -64,7 +64,7 @@ announce_timer(struct uloop_timeout *timeout)
                        iface->announce_state++;
 
                case STATE_ANNOUNCE:
-                       service_announce(iface);
+                       service_announce(iface, announce_ttl);
                        uloop_timeout_set(timeout, announce_ttl * 800);
                        break;
        }
diff --git a/dns.c b/dns.c
index 1ff5d530f9e4f7d61775ec487b958eb25c57dbcc..d8c152948b86cf3e42479385f709e9ca9035ad51 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -331,7 +331,7 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
                break;
 
        case TYPE_PTR:
-               service_announce_services(iface, name);
+               service_announce_services(iface, name, announce_ttl);
                service_reply(iface, name, announce_ttl);
                break;
 
@@ -341,7 +341,7 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
                if (host)
                        *host = '\0';
                if (!strcmp(mdns_hostname, name))
-                       service_reply_a(iface, q->type, announce_ttl);
+                       service_reply_a(iface, announce_ttl);
                break;
        };
 }
index 6651fd834e7024253629d79b5b8958516ad04b1b..1b846d12aa00d3c840ba134bd28f43268156d20d 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)
@@ -584,4 +585,17 @@ 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) {
+                       service_announce(iface, 0);
+                       service_reply_a(iface, 0);
+               }
+       vlist_for_each_element(&interfaces, iface, node)
+               interface_free(iface);
+}
+
 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);
index 545369da431b3c89e05a66a0bf5d907530717012..4a3c67eb2f7c4f51a8a772d96af6b996b3c90a10 100644 (file)
@@ -50,6 +50,7 @@ struct interface {
 };
 
 int interface_add(const char *name);
+void interface_shutdown(void);
 int interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len);
 
 #endif
diff --git a/main.c b/main.c
index 09185b147fc4de92054c0e4bab505aa589d1f465..c6593c7aa4cfc6847a4b4257fdb87ef84d81d878 100644 (file)
--- a/main.c
+++ b/main.c
@@ -92,6 +92,7 @@ main(int argc, char **argv)
        uloop_run();
        uloop_done();
 
+       interface_shutdown();
        cache_cleanup();
        service_cleanup();
        vlist_flush(&interfaces);
index 32c3d620e420828c346e0562a95a2e339abd5689..db4694ebc7309991ac8e8fe5e8b8cf1a9cd65b06 100644 (file)
--- a/service.c
+++ b/service.c
@@ -124,7 +124,7 @@ service_timeout(struct service *s)
 }
 
 void
-service_reply_a(struct interface *iface, int type, int ttl)
+service_reply_a(struct interface *iface, int ttl)
 {
        struct ifaddrs *ifap, *ifa;
        struct sockaddr_in *sa;
@@ -136,11 +136,11 @@ service_reply_a(struct interface *iface, int type, int ttl)
        for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
                if (strcmp(ifa->ifa_name, iface->name))
                        continue;
-               if (ifa->ifa_addr->sa_family==AF_INET) {
+               if (ifa->ifa_addr->sa_family == AF_INET) {
                        sa = (struct sockaddr_in *) ifa->ifa_addr;
                        dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
                }
-               if (ifa->ifa_addr->sa_family==AF_INET6) {
+               if (ifa->ifa_addr->sa_family == AF_INET6) {
                        uint8_t ll_prefix[] = {0xfe, 0x80 };
                        sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
                        if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
@@ -188,11 +188,12 @@ service_reply(struct interface *iface, const char *match, int ttl)
        if (match)
                return;
 
-       service_reply_a(iface, TYPE_A, ttl);
+       if (ttl)
+               service_reply_a(iface, ttl);
 }
 
 void
-service_announce_services(struct interface *iface, const char *service)
+service_announce_services(struct interface *iface, const char *service, int ttl)
 {
        struct service *s;
        int tcp = 1;
@@ -208,21 +209,23 @@ service_announce_services(struct interface *iface, const char *service)
                if (!strstr(s->service, "._udp") && !tcp)
                        continue;
                s->t = 0;
-               dns_init_answer();
-               service_add_ptr(s->service, announce_ttl);
-               if (tcp)
-                       dns_send_answer(iface, sdtcp);
-               else
-                       dns_send_answer(iface, sdudp);
-               service_reply(iface, s->service, announce_ttl);
+               if (ttl) {
+                       dns_init_answer();
+                       service_add_ptr(s->service, ttl);
+                       if (tcp)
+                               dns_send_answer(iface, sdtcp);
+                       else
+                               dns_send_answer(iface, sdudp);
+               }
+               service_reply(iface, s->service, ttl);
        }
 }
 
 void
-service_announce(struct interface *iface)
+service_announce(struct interface *iface, int ttl)
 {
-       service_announce_services(iface, sdudp);
-       service_announce_services(iface, sdtcp);
+       service_announce_services(iface, sdudp, ttl);
+       service_announce_services(iface, sdtcp, ttl);
 }
 
 static void
index e17c8607ee2697caf3df3f346ef248ae5c0dbaee..49c2aedb5067fd6b3a85ecd8c8a338925c041c7b 100644 (file)
--- a/service.h
+++ b/service.h
@@ -16,9 +16,9 @@
 
 extern void service_init(int announce);
 extern void service_cleanup(void);
-extern void service_announce(struct interface *iface);
-extern void service_announce_services(struct interface *iface, const char *service);
+extern void service_announce(struct interface *iface, int ttl);
+extern void service_announce_services(struct interface *iface, const char *service, int ttl);
 extern void service_reply(struct interface *iface, const char *match, int ttl);
-extern void service_reply_a(struct interface *iface, int type, int ttl);
+extern void service_reply_a(struct interface *iface, int ttl);
 
 #endif