Skip to content

Commit

Permalink
network: only resolve interface name on ipv6 ll
Browse files Browse the repository at this point in the history
It turns out that resolving interface name from
sockaddr_in6->sin6_scope_id only works on link-local addresses. This
resulted in errors when connecting to iiod over global ipv6.

The man page on sin6_scope_id states that: "Linux supports it only for
link-local addresses, in that case sin6_scope_id contains the interface
index."

The macro IN6_IS_ADDR_LINKLOCAL seems appropriate to test this.

I have not been able to test this on WIN32, but I have found multiple
pieces of win32 code using this macro, which all seem to pull it from
<ws2tcpip.h> which is already included in network.c so I'm confident it
will work.

fixes analogdevicesinc#296

Signed-off-by: Jorik Jonker <[email protected]>
  • Loading branch information
jonkerj committed Jul 31, 2019
1 parent c4833c5 commit 0fbb31f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,15 +1537,17 @@ struct iio_context * network_create_context(const char *host)
inet_ntop(AF_INET6, &in->sin6_addr,
description, INET6_ADDRSTRLEN);

ptr = if_indextoname(in->sin6_scope_id, description +
strlen(description) + 1);
if (!ptr) {
ret = -errno;
ERROR("Unable to lookup interface of IPv6 address\n");
goto err_free_description;
}
if (IN6_IS_ADDR_LINKLOCAL(&in->sin6_addr)) {
ptr = if_indextoname(in->sin6_scope_id, description +
strlen(description) + 1);
if (!ptr) {
ret = -errno;
ERROR("Unable to lookup interface of IPv6 address\n");
goto err_free_description;
}

*(ptr - 1) = '%';
*(ptr - 1) = '%';
}
}
#endif
if (res->ai_family == AF_INET) {
Expand Down

0 comments on commit 0fbb31f

Please sign in to comment.