ubus: assume that the service iface can be NULL
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index 8c851a39a9cce59e3020cb926e13c90d3df822d1..83249939d304de87e54378a335def6f9e5d6f45e 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -187,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;
 }
@@ -197,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;
 
@@ -233,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;
@@ -369,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));
@@ -388,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;
@@ -460,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;