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