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
30#define CHECK_HANDLE(handle) \
31 ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
32
33static uv_udp_t server;
34static uv_udp_t client;
35
36static int cl_recv_cb_called;
37
38static int sv_send_cb_called;
39
40static int close_cb_called;
41
42static void alloc_cb(uv_handle_t* handle,
43 size_t suggested_size,
44 uv_buf_t* buf) {
45 static char slab[65536];
46 CHECK_HANDLE(handle);
47 ASSERT(suggested_size <= sizeof(slab));
48 buf->base = slab;
49 buf->len = sizeof(slab);
50}
51
52
53static void close_cb(uv_handle_t* handle) {
54 CHECK_HANDLE(handle);
55 close_cb_called++;
56}
57
58
59static void sv_send_cb(uv_udp_send_t* req, int status) {
60 ASSERT(req != NULL);
61 ASSERT(status == 0);
62 CHECK_HANDLE(req->handle);
63
64 sv_send_cb_called++;
65
66 uv_close((uv_handle_t*) req->handle, close_cb);
67}
68
69
70static void cl_recv_cb(uv_udp_t* handle,
71 ssize_t nread,
72 const uv_buf_t* buf,
73 const struct sockaddr* addr,
74 unsigned flags) {
75 CHECK_HANDLE(handle);
76 ASSERT(flags == 0);
77
78 cl_recv_cb_called++;
79
80 if (nread < 0) {
81 ASSERT(0 && "unexpected error");
82 }
83
84 if (nread == 0) {
85 /* Returning unused buffer. Don't count towards cl_recv_cb_called */
86 ASSERT(addr == NULL);
87 return;
88 }
89
90 ASSERT(addr != NULL);
91 ASSERT(nread == 4);
92 ASSERT(!memcmp("PING", buf->base, nread));
93
94 /* we are done with the client handle, we can close it */
95 uv_close((uv_handle_t*) &client, close_cb);
96}
97
98
99TEST_IMPL(udp_multicast_join6) {
100 int r;
101 uv_udp_send_t req;
102 uv_buf_t buf;
103 struct sockaddr_in6 addr;
104
105 if (!can_ipv6())
106 RETURN_SKIP("IPv6 not supported");
107
108 ASSERT(0 == uv_ip6_addr("::1", TEST_PORT, &addr));
109
110 r = uv_udp_init(uv_default_loop(), &server);
111 ASSERT(r == 0);
112
113 r = uv_udp_init(uv_default_loop(), &client);
114 ASSERT(r == 0);
115
116 /* bind to the desired port */
117 r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
118 ASSERT(r == 0);
119
120 /* join the multicast channel */
121#if defined(__APPLE__) || \
122 defined(_AIX) || \
123 defined(__MVS__) || \
124 defined(__FreeBSD_kernel__) || \
125 defined(__NetBSD__) || \
126 defined(__OpenBSD__)
127 r = uv_udp_set_membership(&client, "ff02::1", "::1%lo0", UV_JOIN_GROUP);
128#else
129 r = uv_udp_set_membership(&client, "ff02::1", NULL, UV_JOIN_GROUP);
130#endif
131 if (r == UV_ENODEV) {
132 MAKE_VALGRIND_HAPPY();
133 RETURN_SKIP("No ipv6 multicast route");
134 }
135
136 ASSERT(r == 0);
137
138 r = uv_udp_recv_start(&client, alloc_cb, cl_recv_cb);
139 ASSERT(r == 0);
140
141 buf = uv_buf_init("PING", 4);
142
143 /* server sends "PING" */
144 r = uv_udp_send(&req,
145 &server,
146 &buf,
147 1,
148 (const struct sockaddr*) &addr,
149 sv_send_cb);
150 ASSERT(r == 0);
151
152 ASSERT(close_cb_called == 0);
153 ASSERT(cl_recv_cb_called == 0);
154 ASSERT(sv_send_cb_called == 0);
155
156 /* run the loop till all events are processed */
157 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
158
159 ASSERT(cl_recv_cb_called == 1);
160 ASSERT(sv_send_cb_called == 1);
161 ASSERT(close_cb_called == 2);
162
163 MAKE_VALGRIND_HAPPY();
164 return 0;
165}
166