ubus: make it possible to return txt records as an array
authorJohn Crispin <john@phrozen.org>
Sun, 15 Jan 2023 10:04:00 +0000 (11:04 +0100)
committerJohn Crispin <john@phrozen.org>
Sun, 15 Jan 2023 12:06:01 +0000 (13:06 +0100)
Signed-off-by: John Crispin <john@phrozen.org>
cache.c
cache.h
ubus.c

diff --git a/cache.c b/cache.c
index ea6a4c8ab656699b2855fede48742e5e8f73d18b..9de0fa03cd56d40d2079a2c8b037254bbae9fce4 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -382,7 +382,7 @@ void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
 }
 
 void
-cache_dump_records(struct blob_buf *buf, const char *name)
+cache_dump_records(struct blob_buf *buf, const char *name, int txt_array)
 {
        struct cache_record *r, *last, *next;
        const char *txt;
@@ -393,11 +393,18 @@ cache_dump_records(struct blob_buf *buf, const char *name)
                switch (r->type) {
                case TYPE_TXT:
                        if (r->txt && strlen(r->txt)) {
+                               void *c = NULL;
+
+                               if (txt_array)
+                                       c = blobmsg_open_array(buf, "txt");
+
                                txt = r->txt;
                                do {
                                        blobmsg_add_string(buf, "txt", txt);
                                        txt = &txt[strlen(txt) + 1];
                                } while (*txt);
+                               if (c)
+                                       blobmsg_close_array(buf, c);
                        }
                        break;
 
diff --git a/cache.h b/cache.h
index 897d01b2bbc353cce7560d795386734e08e2210b..44d407abe68af61e1126674faef44a7ce41151ff 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -58,7 +58,7 @@ void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
                  int blen, char *name, struct dns_answer *a, uint8_t *rdata,
                  int flush);
 int cache_host_is_known(char *record);
-void cache_dump_records(struct blob_buf *buf, const char *name);
+void cache_dump_records(struct blob_buf *buf, const char *name, int txt_array);
 void cache_dump_recursive(struct blob_buf *b, const char *name, uint16_t type, struct interface *iface);
 
 #endif
diff --git a/ubus.c b/ubus.c
index bb639127d9ea4f090d5c03911d3e7e56be0c02c9..ff62e9a6dca44203c0e99df7b32e199e2e0551de 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -49,11 +49,13 @@ umdns_update(struct ubus_context *ctx, struct ubus_object *obj,
 
 enum {
        BROWSE_SERVICE,
+       BROWSE_TXT_ARRAY,
        BROWSE_MAX
 };
 
 static const struct blobmsg_policy browse_policy[] = {
        [BROWSE_SERVICE]        = { "service", BLOBMSG_TYPE_STRING },
+       [BROWSE_TXT_ARRAY]      = { "txt_array", BLOBMSG_TYPE_BOOL },
 };
 
 static int
@@ -66,10 +68,13 @@ umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
        struct blob_attr *data[BROWSE_MAX];
        void *c1 = NULL, *c2;
        char *service = NULL;
+       int txt_array = 0;
 
        blobmsg_parse(browse_policy, BROWSE_MAX, data, blob_data(msg), blob_len(msg));
        if (data[BROWSE_SERVICE])
                service = blobmsg_get_string(data[BROWSE_SERVICE]);
+       if (data[BROWSE_TXT_ARRAY])
+               txt_array = blobmsg_get_u8(data[BROWSE_TXT_ARRAY]);
 
        blob_buf_init(&b, 0);
        avl_for_each_element(&services, s, avl) {
@@ -92,8 +97,8 @@ umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
                        *local = '\0';
                c2 = blobmsg_open_table(&b, buffer);
                strncat(buffer, ".local", MAX_NAME_LEN);
-               cache_dump_records(&b, buffer);
-               cache_dump_records(&b, s->entry);
+               cache_dump_records(&b, buffer, txt_array);
+               cache_dump_records(&b, s->entry, txt_array);
                blobmsg_close_table(&b, c2);
                q = avl_next_element(s, avl);
                if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
@@ -122,7 +127,7 @@ umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
                /* 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);
+                       cache_dump_records(&b, r->record, false);
                        blobmsg_close_table(&b, c);
                }
                prev = r;