umdnsd: Replace strerror(errno) with %m.
[project/mdnsd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 9ceeab1..7b1c811 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -59,8 +59,7 @@ umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
        blob_buf_init(&b, 0);
        avl_for_each_element(&services, s, avl) {
                char *local;
-               if (cache_service_is_host(s))
-                       continue;
+
                snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
                local = strstr(buffer, ".local");
                if (local)
@@ -96,24 +95,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;
-       char *buffer = (char *) mdns_buf;
+       struct cache_record *prev = NULL;
+       struct cache_record *r;
        void *c;
 
        blob_buf_init(&b, 0);
-       avl_for_each_element(&services, s, avl) {
-               char *local;
-               if (!cache_service_is_host(s))
+       avl_for_each_element(&records, r, avl) {
+               if (r->type != TYPE_A && r->type != TYPE_AAAA)
                        continue;
-               snprintf(buffer, MAX_NAME_LEN, "%s", s->entry);
-               local = strstr(buffer, "._");
-               if (local)
-                       *local = '\0';
-               c = blobmsg_open_table(&b, buffer);
-               strncat(buffer, ".local", MAX_NAME_LEN);
-               cache_dump_records(&b, buffer);
-               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);
 
@@ -180,7 +176,7 @@ umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
                    struct blob_attr *msg)
 {
        struct blob_attr *tb[QUERY_MAX], *c;
-       const char *question = "_services._dns-sd._udp.local";
+       const char *question = C_DNS_SD;
        const char *ifname;
        int type = TYPE_ANY;
 
@@ -205,10 +201,10 @@ umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
 
        if (!strcmp(method, "query")) {
                if (iface_v4)
-                       dns_send_question(iface_v4, question, type, 1);
+                       dns_send_question(iface_v4, NULL, question, type, 1);
 
                if (iface_v6)
-                       dns_send_question(iface_v6, question, type, 1);
+                       dns_send_question(iface_v6, NULL, question, type, 1);
 
                return UBUS_STATUS_OK;
        } else if (!strcmp(method, "fetch")) {