dns: add support for reverse address mapping queries
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index 9de0fa03cd56d40d2079a2c8b037254bbae9fce4..36fee9cc073b9384b2f6f4fe9d94dd381822c509 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)
@@ -69,6 +75,18 @@ cache_is_expired(time_t t, uint32_t ttl, int frac)
        return 0;
 }
 
+static void
+cache_refresh_service(struct cache_service *s)
+{
+       dns_query(s->entry, TYPE_PTR);
+
+       if (!s->host)
+               return;
+
+       dns_query(s->host, TYPE_A);
+       dns_query(s->host, TYPE_AAAA);
+}
+
 static void
 cache_gc_timer(struct uloop_timeout *timeout)
 {
@@ -102,7 +120,7 @@ cache_gc_timer(struct uloop_timeout *timeout)
                        continue;
                }
                s->refresh += 50;
-               dns_send_question(s->iface, NULL, s->entry, TYPE_PTR, 0);
+               cache_refresh_service(s);
        }
 
        uloop_timeout_set(timeout, 10000);
@@ -111,7 +129,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);
@@ -136,12 +154,12 @@ void cache_cleanup(struct interface *iface)
 void
 cache_update(void)
 {
-       struct interface *iface;
        struct cache_service *s;
 
-       vlist_for_each_element(&interfaces, iface, node)
-               avl_for_each_element(&services, s, avl)
-                       dns_send_question(iface, NULL, s->entry, TYPE_PTR, 0);
+       dns_query(C_DNS_SD, TYPE_ANY);
+       dns_query(C_DNS_SD, TYPE_PTR);
+       avl_for_each_element(&services, s, avl)
+               cache_refresh_service(s);
 }
 
 static struct cache_service*
@@ -162,7 +180,7 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
 
        s = calloc_a(sizeof(*s),
                &entry_buf, strlen(entry) + 1,
-               &host_buf, hlen ? hlen + 1 : 0);
+               &host_buf, hlen ? hlen + sizeof(".local") + 1 : 0);
 
        s->avl.key = s->entry = strcpy(entry_buf, entry);
        s->time = monotonic_time();
@@ -170,8 +188,10 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
        s->iface = iface;
        s->refresh = 50;
 
-       if (hlen)
+       if (hlen) {
                s->host = strncpy(host_buf, s->entry, hlen);
+               strcpy(host_buf + hlen, ".local");
+       }
 
        type = strstr(entry_buf, "._");
        if (type)
@@ -180,8 +200,7 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
                s->avl.key = type;
        avl_insert(&services, &s->avl);
 
-       if (!hlen)
-               dns_send_question(iface, NULL, entry, TYPE_PTR, iface->multicast);
+       cache_refresh_service(s);
 
        return s;
 }
@@ -191,13 +210,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 && !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)
                        continue;
 
@@ -227,13 +243,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;
@@ -363,7 +376,7 @@ void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
        r->rdlength = dlen;
        r->time = now;
        r->iface = iface;
-       if (iface->v6)
+       if (interface_ipv6(iface))
                memcpy(&r->from, from, sizeof(struct sockaddr_in6));
        else
                memcpy(&r->from, from, sizeof(struct sockaddr_in));
@@ -382,20 +395,66 @@ void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
 }
 
 void
-cache_dump_records(struct blob_buf *buf, const char *name, int txt_array)
+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)) {
-                               void *c = NULL;
-
-                               if (txt_array)
+                               if (array)
                                        c = blobmsg_open_array(buf, "txt");
 
                                txt = r->txt;
@@ -403,24 +462,58 @@ cache_dump_records(struct blob_buf *buf, const char *name, int txt_array)
                                        blobmsg_add_string(buf, "txt", txt);
                                        txt = &txt[strlen(txt) + 1];
                                } while (*txt);
-                               if (c)
+                               if (array)
                                        blobmsg_close_array(buf, c);
                        }
                        break;
 
                case TYPE_SRV:
-                       if (r->port)
-                               blobmsg_add_u32(buf, "port", r->port);
-                       break;
+                       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);
+                       }
 
-               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);
+                       if (r->record) {
+                               const struct dns_srv_data *dsd;
+                               char *domain = NULL;
+                               
+                               domain = strstr(r->record, "._udp.");
+
+                               if (!domain)
+                                       domain = strstr(r->record, "._tcp.");
+
+                               if (!domain)
+                                       break;
+
+                               domain = domain + strlen("._udp.");
+                               blobmsg_add_string(buf, "domain", domain);
+
+                               if (r->port)
+                                       blobmsg_add_u32(buf, "port", r->port);
+
+                               if (r->ttl)
+                                       blobmsg_add_u32(buf, "ttl", r->ttl);
+
+                               if (r->time) {
+                                       struct tm *local_time;
+                                       char str_tm[32] = {0};
+                                       time_t last_update = time(NULL) - (monotonic_time() - r->time);
+                                       local_time = localtime(&last_update);
+                                       strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%SZ", local_time);
+
+                                       blobmsg_add_string(buf, "last_update", str_tm);
+                               }
+
+                               dsd = (const struct dns_srv_data*)r->rdata;
+
+                               if (r->rdlength > sizeof(*dsd)) {
+                                       blobmsg_add_u32(buf, "priority", be16_to_cpu(dsd->priority));
+                                       blobmsg_add_u32(buf, "weight", be16_to_cpu(dsd->weight));
+                               }
+                       }
+
                        break;
                }