ubus: also allow dumping ipv4/6 as an array
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index 3f3f617d4dc85a7b6ca3edf4e9e2cfdaaf10db84..046e8b91f1828577cc4789f6da0b533254e7f774 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -75,9 +75,22 @@ cache_gc_timer(struct uloop_timeout *timeout)
        struct cache_record *r, *p;
        struct cache_service *s, *t;
 
-       avl_for_each_element_safe(&records, r, avl, p)
-               if (cache_is_expired(r->time, r->ttl, 100))
+       avl_for_each_element_safe(&records, r, avl, p) {
+               if (!cache_is_expired(r->time, r->ttl, r->refresh))
+                       continue;
+               /* Records other than A(AAA) are handled as services */
+               if (r->type != TYPE_A && r->type != TYPE_AAAA) {
+                       if (cache_is_expired(r->time, r->ttl, 100))
+                               cache_record_free(r);
+                       continue;
+               }
+               if (r->refresh >= 100) {
                        cache_record_free(r);
+                       continue;
+               }
+               r->refresh += 50;
+               dns_send_question(r->iface, (struct sockaddr *)&r->from, r->record, r->type, 0);
+       }
 
        avl_for_each_element_safe(&services, s, avl, t) {
                if (!s->host)
@@ -89,12 +102,7 @@ cache_gc_timer(struct uloop_timeout *timeout)
                        continue;
                }
                s->refresh += 50;
-               if (cache_service_is_host(s)) {
-                       dns_send_question(s->iface, s->entry, TYPE_A, 0);
-                       dns_send_question(s->iface, s->entry, TYPE_AAAA, 0);
-               } else {
-                       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);
@@ -133,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*
@@ -173,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;
 }
@@ -186,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);
@@ -234,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;
@@ -267,7 +276,8 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
                    nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
                        host_len = rdlength - nlen - 1;
 
-               cache_service(iface, rdata_buffer, host_len, a->ttl);
+               if (name[0] == '_')
+                       cache_service(iface, rdata_buffer, host_len, a->ttl);
 
                dlen = strlen(rdata_buffer) + 1;
                rdata = (uint8_t*)rdata_buffer;
@@ -293,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];
@@ -308,14 +318,12 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
                break;
 
        case TYPE_A:
-               cache_service(iface, name, strlen(name), a->ttl);
                if (a->rdlength != 4)
                        return;
                dlen = 4;
                break;
 
        case TYPE_AAAA:
-               cache_service(iface, name, strlen(name), a->ttl);
                if (a->rdlength != 16)
                        return;
                dlen = 16;
@@ -330,9 +338,11 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
                if (!a->ttl) {
                        DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
                        r->time = now + 1 - r->ttl;
+                       r->refresh = 100;
                } else {
                        r->ttl = a->ttl;
                        r->time = now;
+                       r->refresh = 50;
                        DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
                }
                return;
@@ -353,6 +363,11 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
        r->rdlength = dlen;
        r->time = now;
        r->iface = iface;
+       if (iface->v6)
+               memcpy(&r->from, from, sizeof(struct sockaddr_in6));
+       else
+               memcpy(&r->from, from, sizeof(struct sockaddr_in));
+       r->refresh = 50;
 
        if (tlen)
                r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
@@ -367,22 +382,74 @@ 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)
 {
        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;
 
@@ -390,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)