Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

IPv6: support link-local addresses #744

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.mk
Expand Up @@ -38,6 +38,7 @@ else
include $(SRCDIR)/config-unix.mk include $(SRCDIR)/config-unix.mk
endif endif


# Files are sorted alphabetically
BENCHMARKS= \ BENCHMARKS= \
test/benchmark-async-pummel.o \ test/benchmark-async-pummel.o \
test/benchmark-async.o \ test/benchmark-async.o \
Expand All @@ -59,6 +60,7 @@ BENCHMARKS= \
test/dns-server.o \ test/dns-server.o \
test/echo-server.o \ test/echo-server.o \


# Files are sorted alphabetically
TESTS= \ TESTS= \
test/blackhole-server.o \ test/blackhole-server.o \
test/dns-server.o \ test/dns-server.o \
Expand Down Expand Up @@ -86,6 +88,7 @@ TESTS= \
test/test-getsockname.o \ test/test-getsockname.o \
test/test-hrtime.o \ test/test-hrtime.o \
test/test-idle.o \ test/test-idle.o \
test/test-ip6-addr.o \
test/test-ipc.o \ test/test-ipc.o \
test/test-ipc-send-recv.o \ test/test-ipc-send-recv.o \
test/test-loop-handles.o \ test/test-loop-handles.o \
Expand Down
2 changes: 2 additions & 0 deletions include/uv-private/uv-darwin.h
Expand Up @@ -58,4 +58,6 @@


#define UV_HAVE_KQUEUE 1 #define UV_HAVE_KQUEUE 1


#define UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS

#endif /* UV_DARWIN_H */ #endif /* UV_DARWIN_H */
2 changes: 2 additions & 0 deletions include/uv-private/uv-linux.h
Expand Up @@ -31,4 +31,6 @@
void* watchers[2]; \ void* watchers[2]; \
int wd; \ int wd; \


#define UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS

#endif /* UV_LINUX_H */ #endif /* UV_LINUX_H */
2 changes: 2 additions & 0 deletions include/uv-private/uv-win.h
Expand Up @@ -582,3 +582,5 @@ int uv_utf16_to_utf8(const WCHAR* utf16Buffer, size_t utf16Size,
char* utf8Buffer, size_t utf8Size); char* utf8Buffer, size_t utf8Size);
int uv_utf8_to_utf16(const char* utf8Buffer, WCHAR* utf16Buffer, int uv_utf8_to_utf16(const char* utf8Buffer, WCHAR* utf16Buffer,
size_t utf16Size); size_t utf16Size);

#define UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
28 changes: 28 additions & 0 deletions src/uv-common.c
Expand Up @@ -28,6 +28,9 @@
#include <stdlib.h> /* malloc */ #include <stdlib.h> /* malloc */
#include <string.h> /* memset */ #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); #define XX(uc, lc) case UV_##uc: return sizeof(uv_##lc##_t);


Expand Down Expand Up @@ -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 uv_ip6_addr(const char* ip, int port) {
struct sockaddr_in6 addr; struct sockaddr_in6 addr;
char address_part[40];
size_t address_part_size;
const char* zone_index;


memset(&addr, 0, sizeof(struct sockaddr_in6)); memset(&addr, 0, sizeof(struct sockaddr_in6));


addr.sin6_family = AF_INET6; addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port); 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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); uv_inet_pton(AF_INET6, ip, &addr.sin6_addr);


return addr; return addr;
Expand Down
108 changes: 108 additions & 0 deletions 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: two newlines between functions.

uv_err_t result;
int count, ix;
uv_interface_address_t* addresses;

result = uv_interface_addresses(&addresses, &count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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
}
2 changes: 2 additions & 0 deletions test/test-list.h
Expand Up @@ -209,6 +209,7 @@ TEST_DECLARE (dlerror)
TEST_DECLARE (poll_duplex) TEST_DECLARE (poll_duplex)
TEST_DECLARE (poll_unidirectional) TEST_DECLARE (poll_unidirectional)
TEST_DECLARE (poll_close) TEST_DECLARE (poll_close)
TEST_DECLARE (ip6_addr_link_local)
#ifdef _WIN32 #ifdef _WIN32
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows) TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
TEST_DECLARE (argument_escaping) TEST_DECLARE (argument_escaping)
Expand Down Expand Up @@ -495,6 +496,7 @@ TASK_LIST_START
TEST_ENTRY (strlcpy) TEST_ENTRY (strlcpy)
TEST_ENTRY (strlcat) TEST_ENTRY (strlcat)
TEST_ENTRY (dlerror) TEST_ENTRY (dlerror)
TEST_ENTRY (ip6_addr_link_local)
#if 0 #if 0
/* These are for testing the test runner. */ /* These are for testing the test runner. */
TEST_ENTRY (fail_always) TEST_ENTRY (fail_always)
Expand Down
1 change: 1 addition & 0 deletions uv.gyp
Expand Up @@ -364,6 +364,7 @@
'test/test-udp-multicast-join.c', 'test/test-udp-multicast-join.c',
'test/test-dlerror.c', 'test/test-dlerror.c',
'test/test-udp-multicast-ttl.c', 'test/test-udp-multicast-ttl.c',
'test/test-ip6-addr.c',
], ],
'conditions': [ 'conditions': [
[ 'OS=="win"', { [ 'OS=="win"', {
Expand Down