ubus: assume that the service iface can be NULL
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index 046e8b91f1828577cc4789f6da0b533254e7f774..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)
@@ -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);
@@ -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, NULL, 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 && !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 +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;
@@ -363,7 +363,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,7 +382,8 @@ void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
 }
 
 void
-cache_dump_records(struct blob_buf *buf, const char *name, int 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;
@@ -454,6 +455,11 @@ cache_dump_records(struct blob_buf *buf, const char *name, int array)
                        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;