test-ip6-addr.c
Go to the documentation of this file.
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(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 
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 
112 
113  scoped_addr_len = sizeof(scoped_addr);
114  ASSERT(0 != uv_if_indextoname((unsigned int)-1, scoped_addr, &scoped_addr_len));
115 
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 
151 TEST_IMPL(ip6_pton) {
152  struct in6_addr addr;
153 
156 
158  return 0;
159 }
160 
161 #undef GOOD_ADDR_LIST
162 #undef BAD_ADDR_LIST
163 
164 #ifdef SIN6_LEN
165 TEST_IMPL(ip6_sin6_len) {
166  struct sockaddr_in6 s;
167  ASSERT(uv_ip6_addr("::", 0, &s) < 0);
168  ASSERT(s.sin6_len == sizeof(s));
169  return 0;
170 }
171 #endif
uv_if_indextoname
UV_EXTERN int uv_if_indextoname(unsigned int ifindex, char *buffer, size_t *size)
Definition: unix/getaddrinfo.c:229
uv_ip6_addr
UV_EXTERN int uv_ip6_addr(const char *ip, int port, struct sockaddr_in6 *addr)
Definition: uv-common.c:232
task.h
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
TEST_BAD
#define TEST_BAD(ADDR)
Definition: test-ip6-addr.c:145
string.h
UV_IF_NAMESIZE
#define UV_IF_NAMESIZE
Definition: uv.h:1655
ASSERT
#define ASSERT(expr)
Definition: task.h:102
absl::FormatConversionChar::s
@ s
GOOD_ADDR_LIST
#define GOOD_ADDR_LIST(X)
Definition: test-ip6-addr.c:121
uv_interface_addresses
UV_EXTERN int uv_interface_addresses(uv_interface_address_t **addresses, int *count)
Definition: aix.c:1042
TEST_PORT
#define TEST_PORT
Definition: task.h:53
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
uv_interface_address_s::name
char * name
Definition: uv.h:1086
TEST_GOOD
#define TEST_GOOD(ADDR)
Definition: test-ip6-addr.c:139
sockaddr_in6
Definition: ares_ipv6.h:25
TEST_IMPL
TEST_IMPL(ip6_addr_link_local)
Definition: test-ip6-addr.c:34
uv_free_interface_addresses
UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t *addresses, int count)
Definition: aix.c:1210
uv_if_indextoiid
UV_EXTERN int uv_if_indextoiid(unsigned int ifindex, char *buffer, size_t *size)
Definition: unix/getaddrinfo.c:253
sockaddr_in6::sin6_family
unsigned short sin6_family
Definition: ares_ipv6.h:27
uv_interface_address_s
Definition: uv.h:1085
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_inet_ntop
UV_EXTERN int uv_inet_ntop(int af, const void *src, char *dst, size_t size)
Definition: inet.c:40
sockaddr_in6::sin6_addr
struct ares_in6_addr sin6_addr
Definition: ares_ipv6.h:30
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
uv_interface_address_s::address
union uv_interface_address_s::@400 address
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
BAD_ADDR_LIST
#define BAD_ADDR_LIST(X)
Definition: test-ip6-addr.c:131
sockaddr_in6::sin6_scope_id
unsigned int sin6_scope_id
Definition: ares_ipv6.h:31
uv_interface_address_s::address6
struct sockaddr_in6 address6
Definition: uv.h:1091


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26