|
160
|
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 <stdlib.h>
|
|
|
27 #include <string.h>
|
|
|
28
|
|
|
29 #if defined(__FreeBSD__) || defined(__NetBSD__)
|
|
|
30 #include <sys/sysctl.h>
|
|
|
31 #endif
|
|
|
32
|
|
|
33 #define CHECK_HANDLE(handle) \
|
|
|
34 ASSERT_NE((uv_udp_t*)(handle) == &server \
|
|
|
35 || (uv_udp_t*)(handle) == &client \
|
|
|
36 || (uv_timer_t*)(handle) == &timeout, 0)
|
|
|
37
|
|
|
38 #define CHECK_REQ(req) \
|
|
|
39 ASSERT_PTR_EQ((req), &req_);
|
|
|
40
|
|
|
41 static uv_udp_t client;
|
|
|
42 static uv_udp_t server;
|
|
|
43 static uv_udp_send_t req_;
|
|
|
44 static char data[10];
|
|
|
45 static uv_timer_t timeout;
|
|
|
46
|
|
|
47 static int send_cb_called;
|
|
|
48 static int recv_cb_called;
|
|
|
49 static int close_cb_called;
|
|
|
50 static uint16_t client_port;
|
|
|
51
|
|
|
52 #if defined(__FreeBSD__) || defined(__NetBSD__)
|
|
|
53 static int can_ipv6_ipv4_dual(void) {
|
|
|
54 int v6only;
|
|
|
55 size_t size = sizeof(int);
|
|
|
56
|
|
|
57 if (sysctlbyname("net.inet6.ip6.v6only", &v6only, &size, NULL, 0))
|
|
|
58 return 0;
|
|
|
59
|
|
|
60 return v6only != 1;
|
|
|
61 }
|
|
|
62 #endif
|
|
|
63
|
|
|
64
|
|
|
65 static void alloc_cb(uv_handle_t* handle,
|
|
|
66 size_t suggested_size,
|
|
|
67 uv_buf_t* buf) {
|
|
|
68 static char slab[65536];
|
|
|
69 CHECK_HANDLE(handle);
|
|
|
70 buf->base = slab;
|
|
|
71 buf->len = sizeof(slab);
|
|
|
72 }
|
|
|
73
|
|
|
74
|
|
|
75 static void close_cb(uv_handle_t* handle) {
|
|
|
76 CHECK_HANDLE(handle);
|
|
|
77 close_cb_called++;
|
|
|
78 }
|
|
|
79
|
|
|
80
|
|
|
81 static void send_cb(uv_udp_send_t* req, int status) {
|
|
|
82 CHECK_REQ(req);
|
|
|
83 CHECK_HANDLE(req->handle);
|
|
|
84 ASSERT_OK(status);
|
|
|
85 send_cb_called++;
|
|
|
86 }
|
|
|
87
|
|
|
88 static int is_from_client(const struct sockaddr* addr) {
|
|
|
89 const struct sockaddr_in6* addr6;
|
|
|
90 char dst[256];
|
|
|
91 int r;
|
|
|
92
|
|
|
93 /* Debugging output, and filter out unwanted network traffic */
|
|
|
94 if (addr != NULL) {
|
|
|
95 ASSERT_EQ(addr->sa_family, AF_INET6);
|
|
|
96 addr6 = (struct sockaddr_in6*) addr;
|
|
|
97 r = uv_inet_ntop(addr->sa_family, &addr6->sin6_addr, dst, sizeof(dst));
|
|
|
98 if (r == 0)
|
|
|
99 printf("from [%.*s]:%d\n", (int) sizeof(dst), dst, addr6->sin6_port);
|
|
|
100 if (addr6->sin6_port != client_port)
|
|
|
101 return 0;
|
|
|
102 if (r != 0 || strcmp(dst, "::ffff:127.0.0.1"))
|
|
|
103 return 0;
|
|
|
104 }
|
|
|
105 return 1;
|
|
|
106 }
|
|
|
107
|
|
|
108
|
|
|
109 static void ipv6_recv_fail(uv_udp_t* handle,
|
|
|
110 ssize_t nread,
|
|
|
111 const uv_buf_t* buf,
|
|
|
112 const struct sockaddr* addr,
|
|
|
113 unsigned flags) {
|
|
|
114 printf("got %d %.*s\n", (int) nread, nread > 0 ? (int) nread : 0, buf->base);
|
|
|
115 if (!is_from_client(addr) || (nread == 0 && addr == NULL))
|
|
|
116 return;
|
|
|
117 ASSERT(0 && "this function should not have been called");
|
|
|
118 }
|
|
|
119
|
|
|
120
|
|
|
121 static void ipv6_recv_ok(uv_udp_t* handle,
|
|
|
122 ssize_t nread,
|
|
|
123 const uv_buf_t* buf,
|
|
|
124 const struct sockaddr* addr,
|
|
|
125 unsigned flags) {
|
|
|
126 CHECK_HANDLE(handle);
|
|
|
127
|
|
|
128 printf("got %d %.*s\n", (int) nread, nread > 0 ? (int) nread : 0, buf->base);
|
|
|
129 if (!is_from_client(addr) || (nread == 0 && addr == NULL))
|
|
|
130 return;
|
|
|
131
|
|
|
132 ASSERT_EQ(9, nread);
|
|
|
133 ASSERT(!memcmp(buf->base, data, 9));
|
|
|
134 recv_cb_called++;
|
|
|
135 }
|
|
|
136
|
|
|
137
|
|
|
138 static void timeout_cb(uv_timer_t* timer) {
|
|
|
139 uv_close((uv_handle_t*)&server, close_cb);
|
|
|
140 uv_close((uv_handle_t*)&client, close_cb);
|
|
|
141 uv_close((uv_handle_t*)&timeout, close_cb);
|
|
|
142 }
|
|
|
143
|
|
|
144
|
|
|
145 static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) {
|
|
|
146 struct sockaddr_in6 addr6;
|
|
|
147 struct sockaddr_in addr;
|
|
|
148 int addr6_len;
|
|
|
149 int addr_len;
|
|
|
150 uv_buf_t buf;
|
|
|
151 char dst[256];
|
|
|
152 int r;
|
|
|
153
|
|
|
154 ASSERT_OK(uv_ip6_addr("::0", TEST_PORT, &addr6));
|
|
|
155
|
|
|
156 r = uv_udp_init(uv_default_loop(), &server);
|
|
|
157 ASSERT_OK(r);
|
|
|
158
|
|
|
159 r = uv_udp_bind(&server, (const struct sockaddr*) &addr6, bind_flags);
|
|
|
160 ASSERT_OK(r);
|
|
|
161
|
|
|
162 addr6_len = sizeof(addr6);
|
|
|
163 ASSERT_OK(uv_udp_getsockname(&server,
|
|
|
164 (struct sockaddr*) &addr6,
|
|
|
165 &addr6_len));
|
|
|
166 ASSERT_OK(uv_inet_ntop(addr6.sin6_family,
|
|
|
167 &addr6.sin6_addr,
|
|
|
168 dst,
|
|
|
169 sizeof(dst)));
|
|
|
170 printf("on [%.*s]:%d\n", (int) sizeof(dst), dst, addr6.sin6_port);
|
|
|
171
|
|
|
172 r = uv_udp_recv_start(&server, alloc_cb, recv_cb);
|
|
|
173 ASSERT_OK(r);
|
|
|
174
|
|
|
175 r = uv_udp_init(uv_default_loop(), &client);
|
|
|
176 ASSERT_OK(r);
|
|
|
177
|
|
|
178 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
|
|
179 ASSERT_OK(uv_inet_ntop(addr.sin_family, &addr.sin_addr, dst, sizeof(dst)));
|
|
|
180 printf("to [%.*s]:%d\n", (int) sizeof(dst), dst, addr.sin_port);
|
|
|
181
|
|
|
182 /* Create some unique data to send */
|
|
|
183 ASSERT_EQ(9, snprintf(data,
|
|
|
184 sizeof(data),
|
|
|
185 "PING%5u",
|
|
|
186 uv_os_getpid() & 0xFFFF));
|
|
|
187 buf = uv_buf_init(data, 9);
|
|
|
188 printf("sending %s\n", data);
|
|
|
189
|
|
|
190 r = uv_udp_send(&req_,
|
|
|
191 &client,
|
|
|
192 &buf,
|
|
|
193 1,
|
|
|
194 (const struct sockaddr*) &addr,
|
|
|
195 send_cb);
|
|
|
196 ASSERT_OK(r);
|
|
|
197
|
|
|
198 addr_len = sizeof(addr);
|
|
|
199 ASSERT_OK(uv_udp_getsockname(&client, (struct sockaddr*) &addr, &addr_len));
|
|
|
200 ASSERT_OK(uv_inet_ntop(addr.sin_family, &addr.sin_addr, dst, sizeof(dst)));
|
|
|
201 printf("from [%.*s]:%d\n", (int) sizeof(dst), dst, addr.sin_port);
|
|
|
202 client_port = addr.sin_port;
|
|
|
203
|
|
|
204 r = uv_timer_init(uv_default_loop(), &timeout);
|
|
|
205 ASSERT_OK(r);
|
|
|
206
|
|
|
207 r = uv_timer_start(&timeout, timeout_cb, 500, 0);
|
|
|
208 ASSERT_OK(r);
|
|
|
209
|
|
|
210 ASSERT_OK(close_cb_called);
|
|
|
211 ASSERT_OK(send_cb_called);
|
|
|
212 ASSERT_OK(recv_cb_called);
|
|
|
213
|
|
|
214 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
|
215
|
|
|
216 ASSERT_EQ(3, close_cb_called);
|
|
|
217
|
|
|
218 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
219 }
|
|
|
220
|
|
|
221
|
|
|
222 TEST_IMPL(udp_dual_stack) {
|
|
|
223 #if defined(__CYGWIN__) || defined(__MSYS__)
|
|
|
224 /* FIXME: Does Cygwin support this? */
|
|
|
225 RETURN_SKIP("FIXME: This test needs more investigation on Cygwin");
|
|
|
226 #endif
|
|
|
227
|
|
|
228 if (!can_ipv6())
|
|
|
229 RETURN_SKIP("IPv6 not supported");
|
|
|
230
|
|
|
231 #if defined(__FreeBSD__) || defined(__NetBSD__)
|
|
|
232 if (!can_ipv6_ipv4_dual())
|
|
|
233 RETURN_SKIP("IPv6-IPv4 dual stack not supported");
|
|
|
234 #elif defined(__OpenBSD__)
|
|
|
235 RETURN_SKIP("IPv6-IPv4 dual stack not supported");
|
|
|
236 #endif
|
|
|
237
|
|
|
238 do_test(ipv6_recv_ok, 0);
|
|
|
239
|
|
|
240 printf("recv_cb_called %d\n", recv_cb_called);
|
|
|
241 printf("send_cb_called %d\n", send_cb_called);
|
|
|
242 ASSERT_EQ(1, recv_cb_called);
|
|
|
243 ASSERT_EQ(1, send_cb_called);
|
|
|
244
|
|
|
245 return 0;
|
|
|
246 }
|
|
|
247
|
|
|
248
|
|
|
249 TEST_IMPL(udp_ipv6_only) {
|
|
|
250 if (!can_ipv6())
|
|
|
251 RETURN_SKIP("IPv6 not supported");
|
|
|
252
|
|
|
253 do_test(ipv6_recv_fail, UV_UDP_IPV6ONLY);
|
|
|
254
|
|
|
255 ASSERT_OK(recv_cb_called);
|
|
|
256 ASSERT_EQ(1, send_cb_called);
|
|
|
257
|
|
|
258 return 0;
|
|
|
259 }
|