Fix sending replies to PTR questions
authorRafał Miłecki <rafal@milecki.pl>
Mon, 13 Feb 2017 15:42:35 +0000 (16:42 +0100)
committerRafał Miłecki <rafal@milecki.pl>
Tue, 14 Feb 2017 21:05:08 +0000 (22:05 +0100)
When we receive PTR question it includes hostname (instance), e.g.:
mdnsd: parse_question (391): Q -> PTR lede._http._tcp.local

First of all we should check if it matches hostname we use before trying
to reply. Secondly service_reply expects service with domain appended
(without hostname/instance) so we need to strip received string out of
hostname before passing it.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
dns.c

diff --git a/dns.c b/dns.c
index 91434f20b184d2696129ecb2a3038483a9ee204e..dac6f2c167aeb3f6244621ce0c0b6e2db4d018b4 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -367,9 +367,19 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
                break;
 
        case TYPE_PTR:
-               if (!strcmp(name, sdudp))
+               if (!strcmp(name, sdudp)) {
                        service_announce_services(iface, announce_ttl);
-               service_reply(iface, name, announce_ttl);
+               } else {
+                       /* First dot separates instance name from the rest */
+                       char *dot = strchr(name, '.');
+                       /* Length of queried instance */
+                       size_t len = dot ? dot - name : 0;
+
+                       /* Make sure it's query for the instance name we use */
+                       if (len && len == strlen(mdns_hostname) &&
+                           !strncmp(name, mdns_hostname, len))
+                               service_reply(iface, dot + 1, announce_ttl);
+               }
                break;
 
        case TYPE_AAAA: