Skip to content

Commit

Permalink
dns_sd_windows: Fix discovery on IPv6 once again
Browse files Browse the repository at this point in the history
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 <paul@crapouillou.net>
  • Loading branch information
pcercuei committed Jul 20, 2023
1 parent 99cd3ac commit a231814
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions dns_sd_windows.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit a231814

Please sign in to comment.