From 8904ef7d8c84c568f552277661ecb2e04fc03e91 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Thu, 6 Jul 2023 16:56:06 +0200 Subject: [PATCH] avahi: Fix detection of contexts at IPv6 addresses The IPv6 addresses at which a IIO context has been detected were translated to Libiio URIs, so that the final part can create a Libiio context to verify that the contexts actually exist. However, those URIs were built without the interface specifier, which caused the contexts at IPv6 link-local addresses to being skipped as the Libiio context wouldn't create properly. Signed-off-by: Paul Cercueil --- dns_sd.h | 6 +++++- dns_sd_avahi.c | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/dns_sd.h b/dns_sd.h index 7906837e2..a49f0127d 100644 --- a/dns_sd.h +++ b/dns_sd.h @@ -15,11 +15,15 @@ #ifdef _WIN32 #include +#include +#include #else +#include #include #endif -#define DNS_SD_ADDRESS_STR_MAX (40) /* IPv6 Max = 4*8 + 7 + 1 for NUL */ +/* IPv6 Max = 4*8 + 7 + 1 for '%' + interface length */ +#define DNS_SD_ADDRESS_STR_MAX (40 + IF_NAMESIZE) #define FQDN_LEN (255) /* RFC 1035 */ /* MacOS doesn't include ENOMEDIUM (No medium found) like Linux does */ diff --git a/dns_sd_avahi.c b/dns_sd_avahi.c index c8d917726..6535d3f80 100644 --- a/dns_sd_avahi.c +++ b/dns_sd_avahi.c @@ -51,10 +51,13 @@ static struct dns_sd_discovery_data *new_discovery_data(void) } static void avahi_process_resolved(struct dns_sd_discovery_data *ddata, + AvahiIfIndex iface, const AvahiAddress *addr, const char *host_name, const uint16_t port) { + char *ptr; + /* Avahi is multi-threaded, so lock the list */ iio_mutex_lock(ddata->lock); ddata->resolved++; @@ -83,6 +86,16 @@ static void avahi_process_resolved(struct dns_sd_discovery_data *ddata, } iio_mutex_unlock(ddata->lock); + ptr = ddata->addr_str + strnlen(ddata->addr_str, DNS_SD_ADDRESS_STR_MAX); + + if (addr->proto == AVAHI_PROTO_INET6 + && addr->data.ipv6.address[0] == 0xfe + && addr->data.ipv6.address[1] == 0x80 + && iface != AVAHI_IF_UNSPEC + && if_indextoname((unsigned int)iface, ptr + 1)) { + *ptr = '%'; + } + IIO_DEBUG("\t\t%s:%u (%s)\n", host_name, port, ddata->addr_str); } @@ -126,7 +139,7 @@ static void __avahi_resolver_cb(AvahiServiceResolver *resolver, avahi_strerror(err)); break; case AVAHI_RESOLVER_FOUND: { - avahi_process_resolved(ddata, address, host_name, port); + avahi_process_resolved(ddata, iface, address, host_name, port); IIO_DEBUG("Avahi Resolver : service '%s' of type '%s' in domain '%s':\n", name, type, domain); break; @@ -157,7 +170,7 @@ static void avahi_host_resolver(AvahiHostNameResolver *resolver, host_name, avahi_strerror(err)); break; case AVAHI_RESOLVER_FOUND: - avahi_process_resolved(ddata, address, host_name, IIOD_PORT); + avahi_process_resolved(ddata, iface, address, host_name, IIOD_PORT); break; }