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 
33 static uv_udp_t server;
34 static uv_udp_t client;
35 
36 static int cl_recv_cb_called;
37 
38 static int sv_send_cb_called;
39 
40 static int close_cb_called;
41 
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)42 static 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 
close_cb(uv_handle_t * handle)53 static void close_cb(uv_handle_t* handle) {
54   CHECK_HANDLE(handle);
55   close_cb_called++;
56 }
57 
58 
sv_send_cb(uv_udp_send_t * req,int status)59 static 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 
cl_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * buf,const struct sockaddr * addr,unsigned flags)70 static 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 */
86     /* Don't count towards cl_recv_cb_called */
87     ASSERT(addr == NULL);
88     return;
89   }
90 
91   ASSERT(addr != NULL);
92   ASSERT(nread == 4);
93   ASSERT(!memcmp("PING", buf->base, nread));
94 
95   /* we are done with the client handle, we can close it */
96   uv_close((uv_handle_t*) &client, close_cb);
97 }
98 
99 
TEST_IMPL(udp_multicast_join6)100 TEST_IMPL(udp_multicast_join6) {
101   int r;
102   uv_udp_send_t req;
103   uv_buf_t buf;
104   struct sockaddr_in6 addr;
105 
106   if (!can_ipv6())
107     RETURN_SKIP("IPv6 not supported");
108 
109   ASSERT(0 == uv_ip6_addr("::1", TEST_PORT, &addr));
110 
111   r = uv_udp_init(uv_default_loop(), &server);
112   ASSERT(r == 0);
113 
114   r = uv_udp_init(uv_default_loop(), &client);
115   ASSERT(r == 0);
116 
117   /* bind to the desired port */
118   r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
119   ASSERT(r == 0);
120 
121   /* join the multicast channel */
122 #if defined(__APPLE__)          || \
123     defined(_AIX)               || \
124     defined(__MVS__)            || \
125     defined(__FreeBSD_kernel__) || \
126     defined(__NetBSD__)
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