ubus: assume that the service iface can be NULL
[project/mdnsd.git] / service.c
index 67e8f40a53b8c50417ba419e042863a226845011..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>
 
 #include "announce.h"
 
 enum {
+       SERVICE_INSTANCE,
        SERVICE_SERVICE,
        SERVICE_PORT,
        SERVICE_TXT,
+       SERVICE_HOSTNAME,
        __SERVICE_MAX,
 };
 
@@ -55,17 +58,24 @@ struct service {
 };
 
 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
+       [SERVICE_INSTANCE] = { .name = "instance", .type = BLOBMSG_TYPE_STRING },
        [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;
 
 /**
@@ -119,8 +129,10 @@ service_timeout(struct service *s)
 {
        time_t t = monotonic_time();
 
-       if (t - s->t <= TOUT_LOOKUP)
+       if (t - s->t <= TOUT_LOOKUP) {
+               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;
+       }
 
        return t;
 }
@@ -205,18 +217,63 @@ 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)
 {
        struct blob_attr *txt, *_tb[__SERVICE_MAX];
        struct service *s;
-       char *d_service, *d_id;
+       char *d_instance, *d_service, *d_id;
        uint8_t *d_txt;
        int rem2;
        int txt_len = 0;
+       unsigned int n;
 
        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;
 
@@ -224,16 +281,21 @@ 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));
        s = calloc_a(sizeof(*s),
-               &d_id, strlen(blobmsg_name(b)) + 1,
+               &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);
        if (!s)
                return;
 
        s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
-       s->id = strcpy(d_id, blobmsg_name(b));
-       s->instance = umdns_host_label;
+       s->id = strncpy(d_id, blobmsg_name(b), n);
+       if (_tb[SERVICE_INSTANCE])
+               s->instance = strcpy(d_instance, blobmsg_get_string(_tb[SERVICE_INSTANCE]));
+       else
+               s->instance = umdns_host_label;
        s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
        s->active = 1;
        s->t = 0;
@@ -287,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) {
@@ -331,6 +394,7 @@ service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
                }
        }
        vlist_flush(&services);
+       vlist_flush(&hostnames);
 }
 
 void