ubus: also allow dumping ipv4/6 as an array
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index fea9035609253b67172e332e2aca3ac6a70e51a3..046e8b91f1828577cc4789f6da0b533254e7f774 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -89,7 +89,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 +102,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);
@@ -141,7 +141,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 +181,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, iface->multicast);
 
        return s;
 }
@@ -194,7 +194,7 @@ cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata
        if (!l)
                return NULL;
 
-       while (l && l->record && !strcmp(l->record, record)) {
+       while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
                struct cache_record *r = l;
 
                l = avl_next_element(l, avl);
@@ -303,7 +303,7 @@ void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
                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];
@@ -382,22 +382,74 @@ void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
 }
 
 void
-cache_dump_records(struct blob_buf *buf, const char *name)
+cache_dump_records(struct blob_buf *buf, const char *name, int array)
 {
        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;
 
@@ -405,16 +457,6 @@ cache_dump_records(struct blob_buf *buf, const char *name)
                        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)