Keep source sockaddr for every cached DNS record
[project/mdnsd.git] / announce.c
index a8a9540..fed1b21 100644 (file)
@@ -22,6 +22,7 @@
 #include "util.h"
 #include "service.h"
 #include "announce.h"
+#include "interface.h"
 
 #define TTL_TIMEOUT    75
 
@@ -34,51 +35,53 @@ enum {
        STATE_ANNOUNCE,
 };
 
-static struct uloop_timeout announce;
-struct uloop_fd *announce_fd;
-static int announce_state;
 int announce_ttl = 75 * 60;
 
 static void
 announce_timer(struct uloop_timeout *timeout)
 {
-       char host[256];
+       struct interface *iface = container_of(timeout, struct interface, announce_timer);
 
-       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(announce_fd, host, TYPE_ANY);
+                       dns_send_question(iface, mdns_hostname_local, TYPE_ANY, 1);
                        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:
-                       if (cache_host_is_known(host)) {
-                               fprintf(stderr, "the host %s already exists. stopping announce service\n", host);
+                       if (cache_host_is_known(mdns_hostname_local)) {
+                               fprintf(stderr, "the host %s already exists. stopping announce service\n", mdns_hostname_local);
                                return;
                        }
-                       announce_state++;
+                       iface->announce_state++;
+                       /* Fall through */
 
                case STATE_ANNOUNCE:
-                       service_announce(announce_fd);
+                       dns_reply_a(iface, NULL, announce_ttl);
+                       service_announce_services(iface, NULL, announce_ttl);
                        uloop_timeout_set(timeout, announce_ttl * 800);
                        break;
        }
 }
 
 void
-announce_init(struct uloop_fd *u)
+announce_init(struct interface *iface)
+{
+       iface->announce_state = STATE_PROBE1;
+       iface->announce_timer.cb = announce_timer;
+       uloop_timeout_set(&iface->announce_timer, 100);
+}
+
+void
+announce_free(struct interface *iface)
 {
-       announce_state = STATE_PROBE1;
-       announce.cb = announce_timer;
-       announce_fd = u;
-       uloop_timeout_set(&announce, 100);
+       uloop_timeout_cancel(&iface->announce_timer);
 }