cache: improve update call by doing a full refresh probe
[project/mdnsd.git] / service.c
index af3083ec60e7792fb32c5df525723c54513dac1e..b693b3127b4cf54f08e49cb2df6d4d9550d5495b 100644 (file)
--- a/service.c
+++ b/service.c
@@ -17,6 +17,7 @@
 
 #include <resolv.h>
 #include <glob.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <time.h>
 
@@ -38,6 +39,7 @@ enum {
        SERVICE_SERVICE,
        SERVICE_PORT,
        SERVICE_TXT,
+       SERVICE_HOSTNAME,
        __SERVICE_MAX,
 };
 
@@ -60,14 +62,20 @@ static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
        [SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
        [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
        [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
+       [SERVICE_HOSTNAME] = { .name = "hostname", .type = BLOBMSG_TYPE_STRING },
 };
 
 static void
 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
               struct vlist_node *node_old);
 
+static void
+hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
+               struct vlist_node *node_old);
+
 static struct blob_buf b;
 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
+VLIST_TREE(hostnames, avl_strcmp, hostname_update, false, false);
 static int service_init_announce;
 
 /**
@@ -122,7 +130,7 @@ service_timeout(struct service *s)
        time_t t = monotonic_time();
 
        if (t - s->t <= TOUT_LOOKUP) {
-               DBG(2, "t=%lu, s->t=%lu, t - s->t = %lu\n", t, s->t, t - s->t);
+               DBG(2, "t=%" PRId64 ", s->t=%" PRId64 ", t - s->t = %" PRId64 "\n", (int64_t)t, (int64_t)s->t, (int64_t)(t - s->t));
                return 0;
        }
 
@@ -209,6 +217,44 @@ service_update(struct vlist_tree *tree, struct vlist_node *node_new,
        free(s);
 }
 
+static void
+hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
+               struct vlist_node *node_old)
+{
+       struct interface *iface;
+       struct hostname *h;
+
+       if (!node_old) {
+               h = container_of(node_new, struct hostname, node);
+               vlist_for_each_element(&interfaces, iface, node)
+                       dns_reply_a(iface, NULL, announce_ttl, h->hostname);
+               return;
+       }
+
+       h = container_of(node_old, struct hostname, node);
+       if (!node_new)
+               vlist_for_each_element(&interfaces, iface, node)
+                       dns_reply_a(iface, NULL, 0, h->hostname);
+
+       free(h);
+}
+
+static void
+service_load_hostname(struct blob_attr *b)
+{
+       struct hostname *h;
+       char *hostname, *d_hostname;
+
+       hostname = blobmsg_get_string(b);
+       h = calloc_a(sizeof(*h), &d_hostname, strlen(hostname) + 1);
+       if (!h)
+               return;
+
+       h->hostname = strcpy(d_hostname, hostname);
+
+       vlist_add(&hostnames, &h->node, h->hostname);
+}
+
 static void
 service_load_blob(struct blob_attr *b)
 {
@@ -222,6 +268,12 @@ service_load_blob(struct blob_attr *b)
 
        blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
                _tb, blobmsg_data(b), blobmsg_data_len(b));
+
+       if (_tb[SERVICE_HOSTNAME]) {
+               service_load_hostname(_tb[SERVICE_HOSTNAME]);
+               return;
+       }
+
        if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
                return;
 
@@ -229,9 +281,9 @@ service_load_blob(struct blob_attr *b)
                blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
                        txt_len += 1 + strlen(blobmsg_get_string(txt));
 
-       n = strlen(blobmsg_name(b)) + 1;
+       n = strlen(blobmsg_name(b));
        s = calloc_a(sizeof(*s),
-               &d_id, n,
+               &d_id, n + 1,
                &d_instance, _tb[SERVICE_INSTANCE] ? strlen(blobmsg_get_string(_tb[SERVICE_INSTANCE])) + 1 : 0,
                &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
                &d_txt, txt_len);
@@ -297,6 +349,7 @@ service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
        get_hostname();
 
        vlist_update(&services);
+       vlist_update(&hostnames);
        service_load("/etc/umdns/*");
 
        blob_for_each_attr(cur, msg, rem) {
@@ -341,6 +394,7 @@ service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
                }
        }
        vlist_flush(&services);
+       vlist_flush(&hostnames);
 }
 
 void