From a231814007bb40ef607efea46b59fc16c9020606 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Wed, 19 Jul 2023 12:10:36 +0200 Subject: [PATCH] dns_sd_windows: Fix discovery on IPv6 once again The getnameinfo() function already returned the interface number appended to the IPv6 address in ip_address_to_string(); the problem was that the interface number was not properly specified in the sockaddr argument. Now, the sin6_scope_id field (which contains the interface number on Windows) is correctly set, to the interface number of the "from" sockaddr argument. From what I understand, one problem was that the IPv6 address was discovered twice; once with the mDNS socket on IPv6, and once with the mDNS socket on IPv4. The AAAA entry discovered from the IPv4 socket would override the one obtained from the IPv6 socket, but wouldn't contain information about the interface number. Address this issue by only handling AAAA entries on the IPv6 interface. Signed-off-by: Paul Cercueil --- dns_sd_windows.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/dns_sd_windows.c b/dns_sd_windows.c index a27fa33dd..534b1396e 100644 --- a/dns_sd_windows.c +++ b/dns_sd_windows.c @@ -85,15 +85,7 @@ static mdns_string_t ip_address_to_string(char *buffer, size_t capacity, if (addr->sa_family == AF_INET6) { if (addr6->sin6_port != 0 && strncmp(service, MDNS_PORT_STR, sizeof(MDNS_PORT_STR))) { - if (IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) { - len = snprintf(buffer, capacity, "[%s%%%lu]:%s", - host, addr6->sin6_scope_id, service); - } else { - len = snprintf(buffer, capacity, "[%s]:%s", host, service); - } - } else if (IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) { - len = snprintf(buffer, capacity, "%s%%%lu", - host, addr6->sin6_scope_id); + len = snprintf(buffer, capacity, "[%s]:%s", host, service); } else { len = snprintf(buffer, capacity, "%s", host); } @@ -233,6 +225,11 @@ static int query_callback(int sock, const struct sockaddr *from, size_t addrlen, rtype != MDNS_RECORDTYPE_AAAA) goto quit; +#ifdef HAVE_IPV6 + if (rtype == MDNS_RECORDTYPE_AAAA && from->sa_family != AF_INET6) + goto quit; +#endif + if (entry != MDNS_ENTRYTYPE_ANSWER) goto quit; @@ -326,6 +323,7 @@ static int query_callback(int sock, const struct sockaddr *from, size_t addrlen, struct sockaddr_in6 addr; mdns_record_parse_aaaa(data, size, record_offset, record_length, &addr); + addr.sin6_scope_id = ((struct sockaddr_in6 *)from)->sin6_scope_id; addrstr = ip_address_to_string(namebuffer, sizeof(namebuffer), (struct sockaddr *) &addr, sizeof(addr)); IIO_DEBUG("%.*s : %.*s AAAA %.*s\n", MDNS_STRING_FORMAT(fromaddrstr),