ubus: assume that the service iface can be NULL
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index 0658e7e2f44c127b167db8f76bb87bff2e9686de..83249939d304de87e54378a335def6f9e5d6f45e 100644 (file)
--- a/cache.c
+++ b/cache.c
 
 static struct uloop_timeout cache_gc;
 struct avl_tree services;
-AVL_TREE(records, avl_strcmp, true, NULL);
+
+static int avl_strcasecmp(const void *k1, const void *k2, void *ptr)
+{
+       return strcasecmp(k1, k2);
+}
+
+AVL_TREE(records, avl_strcasecmp, true, NULL);
 
 static void
 cache_record_free(struct cache_record *r)
@@ -89,7 +95,7 @@ cache_gc_timer(struct uloop_timeout *timeout)
                        continue;
                }
                r->refresh += 50;
-               dns_send_question(r->iface, r->record, r->type, 0);
+               dns_send_question(r->iface, (struct sockaddr *)&r->from, r->record, r->type, 0);
        }
 
        avl_for_each_element_safe(&services, s, avl, t) {
@@ -102,7 +108,7 @@ cache_gc_timer(struct uloop_timeout *timeout)
                        continue;
                }
                s->refresh += 50;
-               dns_send_question(s->iface, s->entry, TYPE_PTR, 0);
+               dns_send_question(s->iface, NULL, s->entry, TYPE_PTR, 0);
        }
 
        uloop_timeout_set(timeout, 10000);
@@ -111,7 +117,7 @@ cache_gc_timer(struct uloop_timeout *timeout)
 int
 cache_init(void)
 {
-       avl_init(&services, avl_strcmp, true, NULL);
+       avl_init(&services, avl_strcasecmp, true, NULL);
 
        cache_gc.cb = cache_gc_timer;
        uloop_timeout_set(&cache_gc, 10000);
@@ -141,7 +147,7 @@ cache_update(void)
 
        vlist_for_each_element(&interfaces, iface, node)
                avl_for_each_element(&services, s, avl)
-                       dns_send_question(iface, s->entry, TYPE_PTR, 0);
+                       dns_send_question(iface, NULL, s->entry, TYPE_PTR, 0);
 }
 
 static struct cache_service*
@@ -181,7 +187,7 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
        avl_insert(&services, &s->avl);
 
        if (!hlen)
-               dns_send_question(iface, entry, TYPE_PTR, iface->multicast);
+               dns_send_question(iface, NULL, entry, TYPE_PTR, interface_multicast(iface));
 
        return s;
 }
@@ -191,13 +197,10 @@ cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata
 {
        struct cache_record *l = avl_find_element(&records, record, l, avl);
 
-       if (!l)
-               return NULL;
-
-       while (l && l->record && !strcmp(l->record, record)) {
+       while (l && !strcmp(l->record, record)) {
                struct cache_record *r = l;
 
-               l = avl_next_element(l, avl);
+               l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
                if (r->type != type)
                        continue;
 
@@ -227,13 +230,10 @@ cache_host_is_known(char *record)
 {
        struct cache_record *l = avl_find_element(&records, record, l, avl);
 
-       if (!l)
-               return 0;
-
-       while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
+       while (l && !strcmp(l->record, record)) {
                struct cache_record *r = l;
 
-               l = avl_next_element(l, avl);
+               l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
                if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
                        continue;
                return 1;
@@ -242,8 +242,9 @@ cache_host_is_known(char *record)
        return 0;
 }
 
-void
-cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata, int flush)
+void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
+                 int blen, char *name, struct dns_answer *a, uint8_t *rdata,
+                 int flush)
 {
        struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
        struct cache_record *r;
@@ -302,7 +303,7 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
                if (rdlength <= 2)
                        return;
 
-               memcpy(rdata_buffer, &rdata[1], rdlength);
+               memcpy(rdata_buffer, &rdata[1], rdlength-1);
                rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
                tlen = rdlength + 1;
                p = &rdata_buffer[*rdata];
@@ -362,6 +363,10 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
        r->rdlength = dlen;
        r->time = now;
        r->iface = iface;
+       if (interface_ipv6(iface))
+               memcpy(&r->from, from, sizeof(struct sockaddr_in6));
+       else
+               memcpy(&r->from, from, sizeof(struct sockaddr_in));
        r->refresh = 50;
 
        if (tlen)
@@ -377,39 +382,87 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
 }
 
 void
-cache_dump_records(struct blob_buf *buf, const char *name)
+cache_dump_records(struct blob_buf *buf, const char *name, int array,
+                  const char **hostname)
 {
        struct cache_record *r, *last, *next;
        const char *txt;
        char buffer[INET6_ADDRSTRLEN];
+       void *c = NULL;
 
        last = avl_last_element(&records, last, avl);
+       for (r = avl_find_element(&records, name, r, avl); r; r = next) {
+               switch (r->type) {
+               case TYPE_A:
+                       if (!c && array)
+                               c = blobmsg_open_array(buf, "ipv4");
+                       if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
+                               blobmsg_add_string(buf, "ipv4", buffer);
+                       break;
+               }
+
+               if (r == last)
+                       break;
+
+               next = avl_next_element(r, avl);
+               if (strcmp(r->record, next->record) != 0)
+                       break;
+       }
+
+       if (c) {
+               blobmsg_close_array(buf, c);
+               c = NULL;
+       }
+
+       for (r = avl_find_element(&records, name, r, avl); r; r = next) {
+               switch (r->type) {
+               case TYPE_AAAA:
+                       if (!c && array)
+                               c = blobmsg_open_array(buf, "ipv6");
+                       if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
+                               blobmsg_add_string(buf, "ipv6", buffer);
+                       break;
+               }
+
+               if (r == last)
+                       break;
+
+               next = avl_next_element(r, avl);
+               if (strcmp(r->record, next->record) != 0)
+                       break;
+       }
+
+       if (c) {
+               blobmsg_close_array(buf, c);
+               c = NULL;
+       }
+
        for (r = avl_find_element(&records, name, r, avl); r; r = next) {
                switch (r->type) {
                case TYPE_TXT:
                        if (r->txt && strlen(r->txt)) {
+                               if (array)
+                                       c = blobmsg_open_array(buf, "txt");
+
                                txt = r->txt;
                                do {
                                        blobmsg_add_string(buf, "txt", txt);
                                        txt = &txt[strlen(txt) + 1];
                                } while (*txt);
+                               if (array)
+                                       blobmsg_close_array(buf, c);
                        }
                        break;
 
                case TYPE_SRV:
+                       if (r->rdata) {
+                               blobmsg_add_string(buf, "host", (char *)r->rdata + sizeof(struct dns_srv_data));
+                               if (hostname)
+                                       *hostname = (char *)r->rdata + sizeof(struct dns_srv_data);
+                       }
                        if (r->port)
                                blobmsg_add_u32(buf, "port", r->port);
                        break;
-
-               case TYPE_A:
-                       if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
-                               blobmsg_add_string(buf, "ipv4", buffer);
-                       break;
-
-               case TYPE_AAAA:
-                       if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
-                               blobmsg_add_string(buf, "ipv6", buffer);
-                       break;
                }
 
                if (r == last)