Skip to content

Commit

Permalink
unix: add if_indextoname
Browse files Browse the repository at this point in the history
if_indextoname(...) is a function defined in <net/if.h>

It is used to convert an IPv6 scope_id into an interface
identifier string such as %eth0 or %lo

There is an identical function in Windows, but
that has not been tested.

Adds documentation and an ASSERT to the relevant test case.

PR-URL: libuv#1445
  • Loading branch information
pekkanikander committed Jul 27, 2017
1 parent 19e51ae commit 9196eea
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/src/misc.rst
Expand Up @@ -271,6 +271,12 @@ API
and :man:`inet_pton(3)`. On success they return 0. In case of error
the target `dst` pointer is unmodified.
.. c:function:: char* uv_if_indextoname(unsigned int ifindex, char* ifname)
Cross-platform IPv6-capable implementation of :man:`if_indextoname(3)`.
Writes the result to `ifname`, which must be at least `IFNAMSIZ` long.
On success returns the generated string. In case of error returns `NULL`.
.. c:function:: int uv_exepath(char* buffer, size_t* size)
Gets the executable path.
Expand Down
1 change: 1 addition & 0 deletions include/uv-unix.h
Expand Up @@ -32,6 +32,7 @@
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>

#include <termios.h>
#include <pwd.h>
Expand Down
2 changes: 2 additions & 0 deletions include/uv.h
Expand Up @@ -1392,6 +1392,8 @@ UV_EXTERN int uv_ip6_name(const struct sockaddr_in6* src, char* dst, size_t size
UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size);
UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst);

UV_EXTERN char* uv_if_indextoname(unsigned int ifindex, char* ifname);

UV_EXTERN int uv_exepath(char* buffer, size_t* size);

UV_EXTERN int uv_cwd(char* buffer, size_t* size);
Expand Down
3 changes: 3 additions & 0 deletions src/inet.c
Expand Up @@ -49,6 +49,9 @@ int uv_inet_ntop(int af, const void* src, char* dst, size_t size) {
/* NOTREACHED */
}

char* uv_if_indextoname(unsigned int ifindex, char* ifname) {
return if_indextoname(ifindex, ifname);
}

static int inet_ntop4(const unsigned char *src, char *dst, size_t size) {
static const char fmt[] = "%u.%u.%u.%u";
Expand Down
2 changes: 2 additions & 0 deletions test/test-ip6-addr.c
Expand Up @@ -67,6 +67,8 @@ TEST_IMPL(ip6_addr_link_local) {
iface_index = address->address.address6.sin6_scope_id;
device_name = address->name;

ASSERT(0 == strcmp(device_name, uv_if_indextoname(iface_index, scoped_addr)));

#ifdef _WIN32
snprintf(scoped_addr,
sizeof(scoped_addr),
Expand Down

0 comments on commit 9196eea

Please sign in to comment.