IPv6: support link-local addresses #744

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
4 participants
Contributor

bajtos commented Mar 14, 2013

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;
}
-
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

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

src/uv-common.c
struct sockaddr_in6 uv_ip6_addr(const char* ip, int port) {
struct sockaddr_in6 addr;
+ char * address_part = NULL;
+ const char* zone_index = NULL;
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

Style: split declaration and assignment.

src/uv-common.c
+ }
+#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;
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

Style: long line, wrap at 80 columns.

src/uv-common.c
+#ifdef __linux__
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ address_part = strndup(ip, zone_index - ip);
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

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.

test/test-ip6-addr.c
+ address[i*5 + j] = address[i*4 + j];
+ if (i > 0)
+ address[i*5 - 1] = ':';
+ }
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

Style: indentation.

@bajtos

bajtos Mar 15, 2013

Contributor

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

@bnoordhuis

bnoordhuis Mar 15, 2013

Contributor

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

test/test-ip6-addr.c
+
+void stringify_address(char* address) {
+ int i, j;
+ ASSERT(strlen(address)==32);
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

Style: spaces around the ==.

test/test-ip6-addr.c
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name, int iface_index, int scope_value);
+
+void stringify_address(char* address) {
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

Style: two newlines between functions and the top declarations.

test/test-ip6-addr.c
+ }
+}
+
+void foreach_network_interface(iface_info_cb iface_cb) {
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

There is a libuv function for that: uv_interface_addresses()

test/test-ip6-addr.c
+ foreach_network_interface(&test_ip6_addr_scope);
+ return 0;
+}
+
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

Style: no newline at EOF.

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 \
@bnoordhuis

bnoordhuis Mar 14, 2013

Contributor

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

src/uv-common.c
+#if defined(__linux__) || defined(_WIN32)
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ assert(zone_index - ip < 40);
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

sizeof(address_part)

src/uv-common.c
+ zone_index = strchr(ip, '%');
+ if (zone_index != NULL) {
+ assert(zone_index - ip < 40);
+ strncpy(address_part, ip, zone_index - ip);
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

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

test/test-ip6-addr.c
+#endif
+
+typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name,
+ unsigned iface_index);
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

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

@bajtos

bajtos Mar 19, 2013

Contributor

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.

@piscisaureus

piscisaureus Mar 19, 2013

Member

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

@bajtos

bajtos Mar 19, 2013

Contributor

@piscisaureus: thanks :)

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

test/test-ip6-addr.c
+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) {
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

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

test/test-ip6-addr.c
+ result = uv_interface_addresses(&addresses, &count);
+ ASSERT(result.code == UV_OK);
+
+ for (ix=0; ix<count; ix++) {
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

Style: spaces around assignment/comparison operators.

test/test-ip6-addr.c
+
+ 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);
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

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 Mar 18, 2013

Contributor

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?

test/test-ip6-addr.c
+ iface_cb(string_address, iface_name, address->sin6_scope_id);
+}
+
+void foreach_ip6_interface(iface_info_cb iface_cb) {
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

Style: two newlines between functions.

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)
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

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 Mar 18, 2013

Contributor

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.

+ int count, ix;
+ uv_interface_address_t* addresses;
+
+ result = uv_interface_addresses(&addresses, &count);
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

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

@bajtos

bajtos Mar 19, 2013

Contributor

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

src/uv-common.c
+#elif defined(_WIN32)
+ return atoi(zone_index);
+#endif
+}
@bnoordhuis

bnoordhuis Mar 17, 2013

Contributor

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 Mar 19, 2013

Contributor

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?

Contributor

bnoordhuis commented Mar 17, 2013

Contributor

bajtos commented Mar 27, 2013

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

Contributor

saghul commented Apr 28, 2013

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.

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

Fixes #271
Contributor

bajtos commented May 25, 2013

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

@bnoordhuis @piscisaureus could you please review?

Contributor

bnoordhuis commented Jul 25, 2013

Landed on July 4 in fd45f87.

@bnoordhuis bnoordhuis closed this Jul 25, 2013

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