Skip to content

Commit

Permalink
avahi: Fix detection of contexts at IPv6 addresses
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pcercuei committed Jul 10, 2023
1 parent eebff52 commit 8904ef7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion dns_sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@

#ifdef _WIN32
#include <winsock2.h>
#include <ntddndis.h>
#include <netioapi.h>
#else
#include <net/if.h>
#include <sys/param.h>
#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 */
Expand Down
17 changes: 15 additions & 2 deletions dns_sd_avahi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 8904ef7

Please sign in to comment.