Access cached records (instead of services) to read list of hosts
authorRafał Miłecki <rafal@milecki.pl>
Fri, 10 Mar 2017 07:59:21 +0000 (08:59 +0100)
committerRafał Miłecki <rafal@milecki.pl>
Tue, 14 Mar 2017 10:45:43 +0000 (11:45 +0100)
We don't need to access hosts stored as services. It's enough to look
for cached A and AAAA DNS records.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
cache.c
cache.h
ubus.c

diff --git a/cache.c b/cache.c
index 756ae6859983c16653ddb7b26ca20101e72e57bb..3f3f617d4dc85a7b6ca3edf4e9e2cfdaaf10db84 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -42,7 +42,7 @@
 
 static struct uloop_timeout cache_gc;
 struct avl_tree services;
-static AVL_TREE(records, avl_strcmp, true, NULL);
+AVL_TREE(records, avl_strcmp, true, NULL);
 
 static void
 cache_record_free(struct cache_record *r)
diff --git a/cache.h b/cache.h
index 7830b4fd96f1b06c11f150cebe499f4698a07c6a..cfd63680518080236aef4c43511f96d69331d607 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -47,6 +47,7 @@ struct cache_record {
 };
 
 extern struct avl_tree services;
+extern struct avl_tree records;
 
 int cache_init(void);
 void cache_update(void);
diff --git a/ubus.c b/ubus.c
index 4120fd509cd3585fbb8a5e5fdfbdf055ae5adea8..cf2c05ba5856c413639318353e26e9f0dd27af1e 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -96,16 +96,21 @@ umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       struct cache_service *s;
+       struct cache_record *prev = NULL;
+       struct cache_record *r;
        void *c;
 
        blob_buf_init(&b, 0);
-       avl_for_each_element(&services, s, avl) {
-               if (!cache_service_is_host(s))
+       avl_for_each_element(&records, r, avl) {
+               if (r->type != TYPE_A && r->type != TYPE_AAAA)
                        continue;
-               c = blobmsg_open_table(&b, s->entry);
-               cache_dump_records(&b, s->entry);
-               blobmsg_close_table(&b, c);
+               /* Query each domain just once */
+               if (!prev || strcmp(r->record, prev->record)) {
+                       c = blobmsg_open_table(&b, r->record);
+                       cache_dump_records(&b, r->record);
+                       blobmsg_close_table(&b, c);
+               }
+               prev = r;
        }
        ubus_send_reply(ctx, req, b.head);