Change dns_send_question function arg from unicast to multicast
[project/mdnsd.git] / service.c
index 71d987b2c5e67c104d41cf1dae584305a77a64a3..8d514c244b9b759008d3fe280657a45b4463e8a0 100644 (file)
--- a/service.c
+++ b/service.c
@@ -21,9 +21,6 @@
 #include <stdio.h>
 #include <time.h>
 
-#include <uci.h>
-#include <uci_blob.h>
-
 #include <libubus.h>
 #include <libubox/vlist.h>
 #include <libubox/uloop.h>
@@ -110,21 +107,19 @@ service_add_srv(struct service *s, int ttl)
 
 #define TOUT_LOOKUP    60
 
-static int
+static time_t
 service_timeout(struct service *s)
 {
-       time_t t = time(NULL);
+       time_t t = monotonic_time();
 
        if (t - s->t <= TOUT_LOOKUP)
                return 0;
 
-       s->t = t;
-
-       return 1;
+       return t;
 }
 
 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 +131,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))
@@ -157,8 +152,10 @@ service_reply_single(struct interface *iface, struct service *s, const char *mat
 {
        const char *host = service_name(s->service);
        char *service = strstr(host, "._");
+       time_t t = service_timeout(s);
 
-       if (!force && (!s->active || !service || !service_timeout(s)))
+
+       if (!force && (!s->active || !service || !t))
                return;
 
        service++;
@@ -166,6 +163,8 @@ service_reply_single(struct interface *iface, struct service *s, const char *mat
        if (match && strcmp(match, s->service))
                return;
 
+       s->t = t;
+
        dns_init_answer();
        service_add_ptr(service_name(s->service), ttl);
        dns_send_answer(iface, service);
@@ -188,11 +187,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 +208,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
@@ -376,6 +378,8 @@ service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
 void
 service_init(int announce)
 {
+       get_hostname();
+
        service_init_announce = announce;
        ubus_service_list(service_init_cb);
 }