1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining a copy 4 * of this software and associated documentation files (the "Software"), to 5 * deal in the Software without restriction, including without limitation the 6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 * sell copies of the Software, and to permit persons to whom the Software is 8 * furnished to do so, subject to the following conditions: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 * IN THE SOFTWARE. 20 */ 21 22 #include "uv.h" 23 #include "task.h" 24 25 #include <stdio.h> 26 #include <string.h> 27 28 #ifdef __linux__ 29 # include <sys/socket.h> 30 # include <net/if.h> 31 #endif 32 33 34 TEST_IMPL(ip6_addr_link_local) { 35 #if defined(__CYGWIN__) || defined(__MSYS__) 36 /* FIXME: Does Cygwin support this? */ 37 RETURN_SKIP("FIXME: This test needs more investigation on Cygwin"); 38 #endif 39 char string_address[INET6_ADDRSTRLEN]; 40 uv_interface_address_t* addresses; 41 uv_interface_address_t* address; 42 struct sockaddr_in6 addr; 43 unsigned int iface_index; 44 const char* device_name; 45 /* 40 bytes address, 16 bytes device name, plus reserve. */ 46 char scoped_addr[128]; 47 size_t scoped_addr_len; 48 char interface_id[UV_IF_NAMESIZE]; 49 size_t interface_id_len; 50 int count; 51 int ix; 52 int r; 53 54 ASSERT_OK(uv_interface_addresses(&addresses, &count)); 55 56 for (ix = 0; ix < count; ix++) { 57 address = addresses + ix; 58 59 if (address->address.address6.sin6_family != AF_INET6) 60 continue; 61 62 ASSERT_OK(uv_inet_ntop(AF_INET6, 63 &address->address.address6.sin6_addr, 64 string_address, 65 sizeof(string_address))); 66 67 /* Skip addresses that are not link-local. */ 68 if (strncmp(string_address, "fe80::", 6) != 0) 69 continue; 70 71 iface_index = address->address.address6.sin6_scope_id; 72 device_name = address->name; 73 74 scoped_addr_len = sizeof(scoped_addr); 75 ASSERT_OK(uv_if_indextoname(iface_index, 76 scoped_addr, 77 &scoped_addr_len)); 78 #ifndef _WIN32 79 /* This assert fails on Windows, as Windows semantics are different. */ 80 ASSERT_OK(strcmp(device_name, scoped_addr)); 81 #endif 82 83 interface_id_len = sizeof(interface_id); 84 r = uv_if_indextoiid(iface_index, interface_id, &interface_id_len); 85 ASSERT_OK(r); 86 #ifdef _WIN32 87 /* On Windows, the interface identifier is the numeric string of the index. */ 88 ASSERT_EQ(strtoul(interface_id, NULL, 10), iface_index); 89 #else 90 /* On Unix/Linux, the interface identifier is the interface device name. */ 91 ASSERT_OK(strcmp(device_name, interface_id)); 92 #endif 93 94 snprintf(scoped_addr, 95 sizeof(scoped_addr), 96 "%s%%%s", 97 string_address, 98 interface_id); 99 100 fprintf(stderr, "Testing link-local address %s " 101 "(iface_index: 0x%02x, device_name: %s)\n", 102 scoped_addr, 103 iface_index, 104 device_name); 105 fflush(stderr); 106 107 ASSERT_OK(uv_ip6_addr(scoped_addr, TEST_PORT, &addr)); 108 fprintf(stderr, "Got scope_id 0x%2x\n", (unsigned)addr.sin6_scope_id); 109 fflush(stderr); 110 ASSERT_EQ(iface_index, addr.sin6_scope_id); 111 } 112 113 uv_free_interface_addresses(addresses, count); 114 115 scoped_addr_len = sizeof(scoped_addr); 116 ASSERT_NE(0, uv_if_indextoname((unsigned int)-1, 117 scoped_addr, 118 &scoped_addr_len)); 119 120 MAKE_VALGRIND_HAPPY(uv_default_loop()); 121 return 0; 122 } 123 124 125 #define GOOD_ADDR_LIST(X) \ 126 X("::") \ 127 X("::1") \ 128 X("fe80::1") \ 129 X("fe80::") \ 130 X("fe80::2acf:daff:fedd:342a") \ 131 X("fe80:0:0:0:2acf:daff:fedd:342a") \ 132 X("fe80:0:0:0:2acf:daff:1.2.3.4") \ 133 X("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") \ 134 135 #define BAD_ADDR_LIST(X) \ 136 X(":::1") \ 137 X("abcde::1") \ 138 X("fe80:0:0:0:2acf:daff:fedd:342a:5678") \ 139 X("fe80:0:0:0:2acf:daff:abcd:1.2.3.4") \ 140 X("fe80:0:0:2acf:daff:1.2.3.4.5") \ 141 X("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255.255") \ 142 143 #define TEST_GOOD(ADDR) \ 144 ASSERT_OK(uv_inet_pton(AF_INET6, ADDR, &addr)); \ 145 ASSERT_OK(uv_inet_pton(AF_INET6, ADDR "%en1", &addr)); \ 146 ASSERT_OK(uv_inet_pton(AF_INET6, ADDR "%%%%", &addr)); \ 147 ASSERT_OK(uv_inet_pton(AF_INET6, ADDR "%en1:1.2.3.4", &addr)); \ 148 149 #define TEST_BAD(ADDR) \ 150 ASSERT_NE(0, uv_inet_pton(AF_INET6, ADDR, &addr)); \ 151 ASSERT_NE(0, uv_inet_pton(AF_INET6, ADDR "%en1", &addr)); \ 152 ASSERT_NE(0, uv_inet_pton(AF_INET6, ADDR "%%%%", &addr)); \ 153 ASSERT_NE(0, uv_inet_pton(AF_INET6, ADDR "%en1:1.2.3.4", &addr)); \ 154 155 TEST_IMPL(ip6_pton) { 156 struct in6_addr addr; 157 158 GOOD_ADDR_LIST(TEST_GOOD) 159 BAD_ADDR_LIST(TEST_BAD) 160 161 MAKE_VALGRIND_HAPPY(uv_default_loop()); 162 return 0; 163 } 164 165 #undef GOOD_ADDR_LIST 166 #undef BAD_ADDR_LIST 167 168 TEST_IMPL(ip6_sin6_len) { 169 struct sockaddr_in6 s; 170 ASSERT_OK(uv_ip6_addr("::", 0, &s)); 171 #ifdef SIN6_LEN 172 ASSERT_EQ(s.sin6_len, sizeof(s)); 173 #endif 174 return 0; 175 } 176