cache: add explicit lookup for host addresses
authorFelix Fietkau <nbd@nbd.name>
Wed, 28 May 2025 20:58:33 +0000 (22:58 +0200)
committerFelix Fietkau <nbd@nbd.name>
Wed, 28 May 2025 21:11:27 +0000 (23:11 +0200)
When PTR records are received, explicitly query their addresses instead
of waiting for them to be announced.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
cache.c

diff --git a/cache.c b/cache.c
index 19577b23117871034f1583455c14083f4d48ec59..51eb707848e9856ecebd635c60ebc795feb03a7c 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -149,7 +149,12 @@ cache_update(void)
        dns_packet_question(C_DNS_SD, TYPE_ANY);
        dns_packet_question(C_DNS_SD, TYPE_PTR);
        avl_for_each_element(&services, s, avl) {
-               dns_packet_question(s->entry, TYPE_PTR);
+               if (s->host) {
+                       dns_packet_question(s->host, TYPE_A);
+                       dns_packet_question(s->host, TYPE_AAAA);
+               } else {
+                       dns_packet_question(s->entry, TYPE_PTR);
+               }
                if (++count < 16)
                        continue;
                dns_packet_broadcast();
@@ -178,7 +183,7 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
 
        s = calloc_a(sizeof(*s),
                &entry_buf, strlen(entry) + 1,
-               &host_buf, hlen ? hlen + 1 : 0);
+               &host_buf, hlen ? hlen + sizeof(".local") + 1 : 0);
 
        s->avl.key = s->entry = strcpy(entry_buf, entry);
        s->time = monotonic_time();
@@ -186,8 +191,10 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
        s->iface = iface;
        s->refresh = 50;
 
-       if (hlen)
+       if (hlen) {
                s->host = strncpy(host_buf, s->entry, hlen);
+               strcpy(host_buf + hlen, ".local");
+       }
 
        type = strstr(entry_buf, "._");
        if (type)
@@ -196,11 +203,14 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
                s->avl.key = type;
        avl_insert(&services, &s->avl);
 
-       if (hlen)
-               return s;
-
-       vlist_for_each_element(&interfaces, iface, node)
-               dns_send_question(iface, NULL, entry, TYPE_PTR, interface_multicast(iface));
+       dns_packet_init();
+       if (hlen) {
+               dns_packet_question(s->host, TYPE_A);
+               dns_packet_question(s->host, TYPE_AAAA);
+       } else {
+               dns_packet_question(entry, TYPE_PTR);
+       }
+       dns_packet_broadcast();
 
        return s;
 }