display more srv attributes in output of ubus browse function
authorMohd Husaam Mehdi <h.mehdi@gxgroup.eu>
Sun, 14 Jan 2024 14:48:32 +0000 (20:18 +0530)
committerJohn Crispin <john@phrozen.org>
Wed, 11 Dec 2024 11:42:41 +0000 (12:42 +0100)
In the output of ubus call umdns browse, the following srv params
have been exposed with this commit:

* domain,
* ttl,
* last_update,
* priority
* weight

The new output looks as follows:

"_http._tcp": {
"Apache service": {
"iface": "br-lan",
"host": "user831.local",
"domain": "local",
"port": 80,
"ttl": 120,
"last_update": "2024-01-16T02:41:02Z",
"priority": 0,
"weight": 0,
"ipv4": "192.168.1.245",
"ipv6": "9999:1234:830::1f0",
"ipv6": "fdc5:c8b6:f412::1f0",
"ipv6": "fdc5:c8b6:f412:0:a800:aaff:fe53:101",
"ipv6": "9999:1234:830:0:a800:aaff:fe53:101"
}
}

Signed-off-by: Mohd Husaam <husaam.mehdi@iopsys.eu>
cache.c

diff --git a/cache.c b/cache.c
index 384487aa3d8b7a604acae5c637bfd9703ca6803f..2412ed2c8b733ba5c9be03b6a4a068a05bc31f58 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -460,8 +460,47 @@ cache_dump_records(struct blob_buf *buf, const char *name, int array,
                                if (hostname)
                                        *hostname = (char *)r->rdata + sizeof(struct dns_srv_data);
                        }
-                       if (r->port)
-                               blobmsg_add_u32(buf, "port", r->port);
+
+
+                       if (r->record) {
+                               const struct dns_srv_data *dsd;
+                               char *domain = NULL;
+                               
+                               domain = strstr(r->record, "._udp.");
+
+                               if (!domain)
+                                       domain = strstr(r->record, "._tcp.");
+
+                               if (!domain)
+                                       break;
+
+                               domain = domain + strlen("._udp.");
+                               blobmsg_add_string(buf, "domain", domain);
+
+                               if (r->port)
+                                       blobmsg_add_u32(buf, "port", r->port);
+
+                               if (r->ttl)
+                                       blobmsg_add_u32(buf, "ttl", r->ttl);
+
+                               if (r->time) {
+                                       struct tm *local_time;
+                                       char str_tm[32] = {0};
+                                       time_t last_update = time(NULL) - (monotonic_time() - r->time);
+                                       local_time = localtime(&last_update);
+                                       strftime(str_tm, sizeof(str_tm), "%Y-%m-%dT%H:%M:%SZ", local_time);
+
+                                       blobmsg_add_string(buf, "last_update", str_tm);
+                               }
+
+                               dsd = (const struct dns_srv_data*)r->rdata;
+
+                               if (r->rdlength > sizeof(*dsd)) {
+                                       blobmsg_add_u32(buf, "priority", be16_to_cpu(dsd->priority));
+                                       blobmsg_add_u32(buf, "weight", be16_to_cpu(dsd->weight));
+                               }
+                       }
+
                        break;
                }