Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

IPv6: support link-local addresses #744

Closed
wants to merge 1 commit into from

4 participants

@bajtos

Modified uv_ip6_addr() to fill sin6_scope_id for link-local addresses (Linux and Windows only).

Fixes #271

src/uv-common.c
@@ -170,16 +173,41 @@ struct sockaddr_in uv_ip4_addr(const char* ip, int port) {
return addr;
}
-

Style: two newlines between functions (i.e. don't remove this line.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/uv-common.c
((5 lines not shown))
struct sockaddr_in6 uv_ip6_addr(const char* ip, int port) {
struct sockaddr_in6 addr;
+ char * address_part = NULL;
+ const char* zone_index = NULL;

Style: split declaration and assignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/uv-common.c
((14 lines not shown))
+
+#ifdef __linux__
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ address_part = strndup(ip, zone_index - ip);
+ if (address_part != NULL)
+ ip = address_part;
+ /* else report error, but there is no way how to signal error to the caller :( */
+
+ zone_index++; /* skip '%' */
+ addr.sin6_scope_id = if_nametoindex(zone_index);
+ /* TODO - check that result is not zero (indicating unknown interface) */
+ }
+#else
+ /* TODO - implement sin6_scope_id for other platforms
+ * Windows IPv6 stack uses numeric zone indexes, e.g., fe80::3%1. The index is determined by the interface number;

Style: long line, wrap at 80 columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/uv-common.c
((12 lines not shown))
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
+
+#ifdef __linux__
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ address_part = strndup(ip, zone_index - ip);

Don't use a dynamic allocation here, we try to avoid that wherever possible in libuv. Use an on-stack buffer that is large enough to hold the address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((26 lines not shown))
+#include <netinet/in.h>
+#include <stdio.h>
+#include <string.h>
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name, int iface_index, int scope_value);
+
+void stringify_address(char* address) {
+ int i, j;
+ ASSERT(strlen(address)==32);
+ address[40] = '\0';
+ for (i=7; i>=0; i--) {
+ for (j=0; j<4; j++)
+ address[i*5 + j] = address[i*4 + j];
+ if (i > 0)
+ address[i*5 - 1] = ':';
+ }

Style: indentation.

@bajtos
bajtos added a note

I don't understand which part is wronly indented? The function will be removed, but I'd like to know the correct style.

All of it. :-) Use two spaces, not four.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((19 lines not shown))
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "task.h"
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <string.h>
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name, int iface_index, int scope_value);
+
+void stringify_address(char* address) {
+ int i, j;
+ ASSERT(strlen(address)==32);

Style: spaces around the ==.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((17 lines not shown))
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "task.h"
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <string.h>
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name, int iface_index, int scope_value);
+
+void stringify_address(char* address) {

Style: two newlines between functions and the top declarations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((29 lines not shown))
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name, int iface_index, int scope_value);
+
+void stringify_address(char* address) {
+ int i, j;
+ ASSERT(strlen(address)==32);
+ address[40] = '\0';
+ for (i=7; i>=0; i--) {
+ for (j=0; j<4; j++)
+ address[i*5 + j] = address[i*4 + j];
+ if (i > 0)
+ address[i*5 - 1] = ':';
+ }
+}
+
+void foreach_network_interface(iface_info_cb iface_cb) {

There is a libuv function for that: uv_interface_addresses()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((74 lines not shown))
+
+ char scoped_addr[1024];
+ snprintf(scoped_addr, 1024, "%s%%%s", ip6_addr, device_name);
+
+ LOGF("Testing link-local address %s (iface_index: 0x%02x)\n", scoped_addr, iface_index);
+ struct sockaddr_in6 addr = uv_ip6_addr(scoped_addr, TEST_PORT);
+
+ LOGF("Got scope_id 0x%02x\n", addr.sin6_scope_id);
+ ASSERT(iface_index == addr.sin6_scope_id);
+}
+
+TEST_IMPL(ip6_addr_link_local) {
+ foreach_network_interface(&test_ip6_addr_scope);
+ return 0;
+}
+

Style: no newline at EOF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
build.mk
@@ -136,6 +136,7 @@ TESTS= \
test/test-udp-send-and-recv.o \
test/test-util.o \
test/test-walk-handles.o \
+ test/test-ip6-addr.o \

They're sorted alphabetically (but not in uv.gyp for some reason. You can leave that one as-is.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/uv-common.c
((16 lines not shown))
memset(&addr, 0, sizeof(struct sockaddr_in6));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
+
+#if defined(__linux__) || defined(_WIN32)
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ assert(zone_index - ip < 40);

sizeof(address_part)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/uv-common.c
((16 lines not shown))
memset(&addr, 0, sizeof(struct sockaddr_in6));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
+
+#if defined(__linux__) || defined(_WIN32)
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ assert(zone_index - ip < 40);
+ strncpy(address_part, ip, zone_index - ip);

Doesn't zero-terminate the string when the address is > sizeof(address_part). Don't rely on the assert() to catch that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((19 lines not shown))
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "task.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#ifdef __linux__
+# include <sys/socket.h>
+# include <net/if.h>
+#endif
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name,
+ unsigned iface_index);

Style: when arguments are split over multiple lines, they should line up (and one argument per line.)

@bajtos
bajtos added a note

This rule is not consistently obeyed in the code base (see e.g. uv_read2_cb in include/uv.h), but I will fix it per your suggestion.

Agree with @bajtos. I use the double-indent style for function signatures (but not function calls) too.

@bajtos
bajtos added a note

@piscisaureus: thanks :)

I'll leave it as it is for now, we can always change it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((21 lines not shown))
+
+#include "uv.h"
+#include "task.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#ifdef __linux__
+# include <sys/socket.h>
+# include <net/if.h>
+#endif
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name,
+ unsigned iface_index);
+
+void call_iface_info_cb(iface_info_cb iface_cb, char const* iface_name, struct sockaddr_in6 const* address) {

Long line, wrap at 80 columns. Happens in a few other places too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((39 lines not shown))
+
+ result = uv_inet_ntop(AF_INET6, &address->sin6_addr, string_address, INET6_ADDRSTRLEN);
+ ASSERT(result.code == UV_OK);
+
+ iface_cb(string_address, iface_name, address->sin6_scope_id);
+}
+
+void foreach_ip6_interface(iface_info_cb iface_cb) {
+ uv_err_t result;
+ int count, ix;
+ uv_interface_address_t* addresses;
+
+ result = uv_interface_addresses(&addresses, &count);
+ ASSERT(result.code == UV_OK);
+
+ for (ix=0; ix<count; ix++) {

Style: spaces around assignment/comparison operators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((41 lines not shown))
+ ASSERT(result.code == UV_OK);
+
+ iface_cb(string_address, iface_name, address->sin6_scope_id);
+}
+
+void foreach_ip6_interface(iface_info_cb iface_cb) {
+ uv_err_t result;
+ int count, ix;
+ uv_interface_address_t* addresses;
+
+ result = uv_interface_addresses(&addresses, &count);
+ ASSERT(result.code == UV_OK);
+
+ for (ix=0; ix<count; ix++) {
+ if (addresses[ix].address.address4.sin_family != AF_INET6) continue;
+ call_iface_info_cb(iface_cb, addresses[ix].name, &addresses[ix].address.address6);

Merge that function? It's only used here.

EDIT: I guess you could just inline everything into TEST_IMPL(ip6_addr_link_local).

@bajtos
bajtos added a note

I disagree on this point. I put the code in a standalone function so that the test method is more readable. The same applies for call_iface_info_cb(). I strongly believe that many small functions make better/cleaner code, which is also easier to maintain.

Why would you want to inline/merge the function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-ip6-addr.c
((31 lines not shown))
+#endif
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name,
+ unsigned iface_index);
+
+void call_iface_info_cb(iface_info_cb iface_cb, char const* iface_name, struct sockaddr_in6 const* address) {
+ char string_address[INET6_ADDRSTRLEN];
+ uv_err_t result;
+
+ result = uv_inet_ntop(AF_INET6, &address->sin6_addr, string_address, INET6_ADDRSTRLEN);
+ ASSERT(result.code == UV_OK);
+
+ iface_cb(string_address, iface_name, address->sin6_scope_id);
+}
+
+void foreach_ip6_interface(iface_info_cb iface_cb) {

Style: two newlines between functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/test-list.h
@@ -204,6 +204,9 @@
TEST_DECLARE (poll_duplex)
TEST_DECLARE (poll_unidirectional)
TEST_DECLARE (poll_close)
+#if defined(__linux__) || defined(_WIN32)

Preferably don't #ifdef things in test-list.h. Do it in the test itself and return 0 when we're on an unsupported platform (with maybe a printed message telling the user the test is skipped.)

@bajtos
bajtos added a note

OK, I'll do the ifdef in the test method. The reason for ifdefing in test-list was that there are few other tests turned on/off this way.

BTW, I don't think there is an easy way how to print "test skipped" message. Output of LOG macro is printed only when the test fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bnoordhuis bnoordhuis commented on the diff
test/test-ip6-addr.c
((36 lines not shown))
+void call_iface_info_cb(iface_info_cb iface_cb, char const* iface_name, struct sockaddr_in6 const* address) {
+ char string_address[INET6_ADDRSTRLEN];
+ uv_err_t result;
+
+ result = uv_inet_ntop(AF_INET6, &address->sin6_addr, string_address, INET6_ADDRSTRLEN);
+ ASSERT(result.code == UV_OK);
+
+ iface_cb(string_address, iface_name, address->sin6_scope_id);
+}
+
+void foreach_ip6_interface(iface_info_cb iface_cb) {
+ uv_err_t result;
+ int count, ix;
+ uv_interface_address_t* addresses;
+
+ result = uv_interface_addresses(&addresses, &count);

Call uv_free_interface_addresses() when you're done with it.

@bajtos
bajtos added a note

This has been fixed in the last commit (github did not pick the change in this diff view).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/uv-common.c
@@ -170,14 +173,38 @@ struct sockaddr_in uv_ip4_addr(const char* ip, int port) {
return addr;
}
+static unsigned uv__parse_zone_index(const char* zone_index) {
+#ifdef __linux__
+ return if_nametoindex(zone_index);
+#elif defined(_WIN32)
+ return atoi(zone_index);
+#endif
+}

I suggest you inline this into the #if defined(__linux__) || defined(_WIN32) block further down below. It generates 'unused function' and 'no return value' warnings on non-Linux/Windows platforms.

@bajtos
bajtos added a note

I don't like that idea because the zone index handling is already awfully long. I defined a new macro UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS and protected the helper function with #ifdef instead.

Perhaps we could move uv__parse_zone_index to platform-specific files (e.g. include/uv-private/uv-win.h) in order to keep src/uv-common.c cleaner, possibly defining the helper function as a macro instead?

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bajtos

Hello, what's the status of this pull request? What changes are necessary to finish this work and have the request pulled to master?

@saghul

Looks like if_nametoindex is also supported on OSX. https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/if_nametoindex.3.html not sure about other BSDs though.

@bajtos bajtos linux,darwin,win: link-local IPv6 addresses
Modified uv_ip6_addr() to fill sin6_scope_id for link-local addresses.

Fixes #271
1b2631d
@bajtos

I fixed remaining issues, added Darwin implementation and squashed the changes into a single commit.

@bnoordhuis @piscisaureus could you please review?

@bnoordhuis

Landed on July 4 in fd45f87.

@bnoordhuis bnoordhuis closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 25, 2013
  1. @bajtos

    linux,darwin,win: link-local IPv6 addresses

    bajtos authored
    Modified uv_ip6_addr() to fill sin6_scope_id for link-local addresses.
    
    Fixes #271
This page is out of date. Refresh to see the latest.
View
3  build.mk
@@ -38,6 +38,7 @@ else
include $(SRCDIR)/config-unix.mk
endif
+# Files are sorted alphabetically
BENCHMARKS= \
test/benchmark-async-pummel.o \
test/benchmark-async.o \
@@ -59,6 +60,7 @@ BENCHMARKS= \
test/dns-server.o \
test/echo-server.o \
+# Files are sorted alphabetically
TESTS= \
test/blackhole-server.o \
test/dns-server.o \
@@ -86,6 +88,7 @@ TESTS= \
test/test-getsockname.o \
test/test-hrtime.o \
test/test-idle.o \
+ test/test-ip6-addr.o \
test/test-ipc.o \
test/test-ipc-send-recv.o \
test/test-loop-handles.o \
View
2  include/uv-private/uv-darwin.h
@@ -58,4 +58,6 @@
#define UV_HAVE_KQUEUE 1
+#define UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
+
#endif /* UV_DARWIN_H */
View
2  include/uv-private/uv-linux.h
@@ -31,4 +31,6 @@
void* watchers[2]; \
int wd; \
+#define UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
+
#endif /* UV_LINUX_H */
View
2  include/uv-private/uv-win.h
@@ -582,3 +582,5 @@ int uv_utf16_to_utf8(const WCHAR* utf16Buffer, size_t utf16Size,
char* utf8Buffer, size_t utf8Size);
int uv_utf8_to_utf16(const char* utf8Buffer, WCHAR* utf16Buffer,
size_t utf16Size);
+
+#define UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
View
28 src/uv-common.c
@@ -28,6 +28,9 @@
#include <stdlib.h> /* malloc */
#include <string.h> /* memset */
+#if defined(UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS) && !defined(_WIN32)
+# include <net/if.h> /* if_nametoindex */
+#endif
#define XX(uc, lc) case UV_##uc: return sizeof(uv_##lc##_t);
@@ -173,11 +176,36 @@ struct sockaddr_in uv_ip4_addr(const char* ip, int port) {
struct sockaddr_in6 uv_ip6_addr(const char* ip, int port) {
struct sockaddr_in6 addr;
+ char address_part[40];
+ size_t address_part_size;
+ const char* zone_index;
memset(&addr, 0, sizeof(struct sockaddr_in6));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
+
+#if defined(UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS)
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ address_part_size = sizeof(address_part);
+ assert((unsigned)(zone_index - ip) < address_part_size);
+ strncpy(address_part, ip, zone_index - ip);
+ address_part[address_part_size - 1] = '\0';
+
+ ip = address_part;
+
+ zone_index++; /* skip '%' */
+ /* NOTE: unknown interface (id=0) is silently ignored */
+#ifdef _WIN32
+ addr.sin6_scope_id = atoi(zone_index);
+#else
+ addr.sin6_scope_id = if_nametoindex(zone_index);
+#endif
+ }
+#endif
+
+ /* result code is ignored - we assume ip is a valid IPv6 address */
uv_inet_pton(AF_INET6, ip, &addr.sin6_addr);
return addr;
View
108 test/test-ip6-addr.c
@@ -0,0 +1,108 @@
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "task.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#ifdef __linux__
+# include <sys/socket.h>
+# include <net/if.h>
+#endif
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name,
+ unsigned iface_index);
+
+void call_iface_info_cb(iface_info_cb iface_cb,
+ char const* iface_name,
+ struct sockaddr_in6 const* address) {
+ char string_address[INET6_ADDRSTRLEN];
+ uv_err_t result;
+
+ result = uv_inet_ntop(AF_INET6,
+ &address->sin6_addr,
+ string_address,
+ INET6_ADDRSTRLEN);
+ ASSERT(result.code == UV_OK);
+
+ iface_cb(string_address, iface_name, address->sin6_scope_id);
+}
+
+
+void foreach_ip6_interface(iface_info_cb iface_cb) {
+ uv_err_t result;
+ int count, ix;
+ uv_interface_address_t* addresses;
+
+ result = uv_interface_addresses(&addresses, &count);

Call uv_free_interface_addresses() when you're done with it.

@bajtos
bajtos added a note

This has been fixed in the last commit (github did not pick the change in this diff view).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ ASSERT(result.code == UV_OK);
+
+ for (ix = 0; ix < count; ix++) {
+ if (addresses[ix].address.address4.sin_family != AF_INET6)
+ continue;
+
+ call_iface_info_cb(iface_cb,
+ addresses[ix].name,
+ &addresses[ix].address.address6);
+ }
+
+ uv_free_interface_addresses(addresses, count);
+}
+
+
+void test_ip6_addr_scope(const char* ip6_addr,
+ const char* device_name,
+ unsigned iface_index) {
+ /* 40 bytes address, 16 bytes device name, plus reserve */
+ char scoped_addr[128];
+ struct sockaddr_in6 addr;
+
+ /* skip addresses that are not link-local */
+ if (strncmp(ip6_addr, "fe80::", 6) != 0) return;
+
+#ifdef _WIN32
+ sprintf(scoped_addr, "%s%%%d", ip6_addr, iface_index);
+#else
+ sprintf(scoped_addr, "%s%%%s", ip6_addr, device_name);
+#endif
+
+ LOGF("Testing link-local address %s (iface_index: 0x%02x, device_name: %s)\n",
+ scoped_addr,
+ iface_index,
+ device_name);
+
+ addr = uv_ip6_addr(scoped_addr, TEST_PORT);
+
+ LOGF("Got scope_id 0x%02x\n", addr.sin6_scope_id);
+ ASSERT(iface_index == addr.sin6_scope_id);
+}
+
+
+TEST_IMPL(ip6_addr_link_local) {
+#ifdef UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
+ foreach_ip6_interface(&test_ip6_addr_scope);
+ return 0;
+#else
+ RETURN_SKIP("Qualified link-local addresses are not supported.");
+#endif
+}
View
2  test/test-list.h
@@ -209,6 +209,7 @@ TEST_DECLARE (dlerror)
TEST_DECLARE (poll_duplex)
TEST_DECLARE (poll_unidirectional)
TEST_DECLARE (poll_close)
+TEST_DECLARE (ip6_addr_link_local)
#ifdef _WIN32
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
TEST_DECLARE (argument_escaping)
@@ -495,6 +496,7 @@ TASK_LIST_START
TEST_ENTRY (strlcpy)
TEST_ENTRY (strlcat)
TEST_ENTRY (dlerror)
+ TEST_ENTRY (ip6_addr_link_local)
#if 0
/* These are for testing the test runner. */
TEST_ENTRY (fail_always)
View
1  uv.gyp
@@ -364,6 +364,7 @@
'test/test-udp-multicast-join.c',
'test/test-dlerror.c',
'test/test-udp-multicast-ttl.c',
+ 'test/test-ip6-addr.c',
],
'conditions': [
[ 'OS=="win"', {
Something went wrong with that request. Please try again.