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
34TEST_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(0 == 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(0 == 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(0 == uv_if_indextoname(iface_index, scoped_addr, &scoped_addr_len));
76#ifndef _WIN32
77 /* This assert fails on Windows, as Windows semantics are different. */
78 ASSERT(0 == strcmp(device_name, scoped_addr));
79#endif
80
81 interface_id_len = sizeof(interface_id);
82 r = uv_if_indextoiid(iface_index, interface_id, &interface_id_len);
83 ASSERT(0 == r);
84#ifdef _WIN32
85 /* On Windows, the interface identifier is the numeric string of the index. */
86 ASSERT(strtoul(interface_id, NULL, 10) == iface_index);
87#else
88 /* On Unix/Linux, the interface identifier is the interface device name. */
89 ASSERT(0 == strcmp(device_name, interface_id));
90#endif
91
92 snprintf(scoped_addr,
93 sizeof(scoped_addr),
94 "%s%%%s",
95 string_address,
96 interface_id);
97
98 fprintf(stderr, "Testing link-local address %s "
99 "(iface_index: 0x%02x, device_name: %s)\n",
100 scoped_addr,
101 iface_index,
102 device_name);
103 fflush(stderr);
104
105 ASSERT(0 == uv_ip6_addr(scoped_addr, TEST_PORT, &addr));
106 fprintf(stderr, "Got scope_id 0x%02x\n", addr.sin6_scope_id);
107 fflush(stderr);
108 ASSERT(iface_index == addr.sin6_scope_id);
109 }
110
111 uv_free_interface_addresses(addresses, count);
112
113 scoped_addr_len = sizeof(scoped_addr);
114 ASSERT(0 != uv_if_indextoname((unsigned int)-1, scoped_addr, &scoped_addr_len));
115
116 MAKE_VALGRIND_HAPPY();
117 return 0;
118}
119
120
121#define GOOD_ADDR_LIST(X) \
122 X("::") \
123 X("::1") \
124 X("fe80::1") \
125 X("fe80::") \
126 X("fe80::2acf:daff:fedd:342a") \
127 X("fe80:0:0:0:2acf:daff:fedd:342a") \
128 X("fe80:0:0:0:2acf:daff:1.2.3.4") \
129 X("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") \
130
131#define BAD_ADDR_LIST(X) \
132 X(":::1") \
133 X("abcde::1") \
134 X("fe80:0:0:0:2acf:daff:fedd:342a:5678") \
135 X("fe80:0:0:0:2acf:daff:abcd:1.2.3.4") \
136 X("fe80:0:0:2acf:daff:1.2.3.4.5") \
137 X("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255.255") \
138
139#define TEST_GOOD(ADDR) \
140 ASSERT(0 == uv_inet_pton(AF_INET6, ADDR, &addr)); \
141 ASSERT(0 == uv_inet_pton(AF_INET6, ADDR "%en1", &addr)); \
142 ASSERT(0 == uv_inet_pton(AF_INET6, ADDR "%%%%", &addr)); \
143 ASSERT(0 == uv_inet_pton(AF_INET6, ADDR "%en1:1.2.3.4", &addr)); \
144
145#define TEST_BAD(ADDR) \
146 ASSERT(0 != uv_inet_pton(AF_INET6, ADDR, &addr)); \
147 ASSERT(0 != uv_inet_pton(AF_INET6, ADDR "%en1", &addr)); \
148 ASSERT(0 != uv_inet_pton(AF_INET6, ADDR "%%%%", &addr)); \
149 ASSERT(0 != uv_inet_pton(AF_INET6, ADDR "%en1:1.2.3.4", &addr)); \
150
151TEST_IMPL(ip6_pton) {
152 struct in6_addr addr;
153
154 GOOD_ADDR_LIST(TEST_GOOD)
155 BAD_ADDR_LIST(TEST_BAD)
156
157 MAKE_VALGRIND_HAPPY();
158 return 0;
159}
160
161#undef GOOD_ADDR_LIST
162#undef BAD_ADDR_LIST
163