From 3df3648a1ffdbe5450b6368adb216e73f8989b3f Mon Sep 17 00:00:00 2001 From: Pekka Nikander Date: Thu, 27 Jul 2017 10:24:01 +0200 Subject: [PATCH] unix: add if_indextoname if_indextoname(...) is a function defined in 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: https://github.com/libuv/libuv/pull/1443 --- docs/src/misc.rst | 6 ++++++ include/uv-unix.h | 1 + include/uv.h | 2 ++ src/inet.c | 3 +++ test/test-ip6-addr.c | 2 ++ 5 files changed, 14 insertions(+) diff --git a/docs/src/misc.rst b/docs/src/misc.rst index 87a29a845bd..f00855c00a2 100644 --- a/docs/src/misc.rst +++ b/docs/src/misc.rst @@ -267,6 +267,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. diff --git a/include/uv-unix.h b/include/uv-unix.h index 54b3123a0c8..0bff2512967 100644 --- a/include/uv-unix.h +++ b/include/uv-unix.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include diff --git a/include/uv.h b/include/uv.h index c0442c6a437..37b8705c5a8 100644 --- a/include/uv.h +++ b/include/uv.h @@ -1442,6 +1442,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); diff --git a/src/inet.c b/src/inet.c index d76f63d2dda..dabdfe4e1bf 100644 --- a/src/inet.c +++ b/src/inet.c @@ -44,6 +44,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"; diff --git a/test/test-ip6-addr.c b/test/test-ip6-addr.c index cd57f0c5249..b6b9fbc7a6b 100644 --- a/test/test-ip6-addr.c +++ b/test/test-ip6-addr.c @@ -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),