10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos
250e552da7Schristos #include <stdio.h>
260e552da7Schristos #include <stdlib.h>
270e552da7Schristos #include <string.h>
280e552da7Schristos
290e552da7Schristos #define CHECK_HANDLE(handle) \
300e552da7Schristos ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
310e552da7Schristos
320e552da7Schristos #define MULTICAST_ADDR "239.255.0.1"
330e552da7Schristos
340e552da7Schristos static uv_udp_t server;
350e552da7Schristos static uv_udp_t client;
360e552da7Schristos static uv_udp_send_t req;
370e552da7Schristos static uv_udp_send_t req_ss;
380e552da7Schristos
390e552da7Schristos static int cl_recv_cb_called;
400e552da7Schristos
410e552da7Schristos static int sv_send_cb_called;
420e552da7Schristos
430e552da7Schristos static int close_cb_called;
440e552da7Schristos
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)450e552da7Schristos static void alloc_cb(uv_handle_t* handle,
460e552da7Schristos size_t suggested_size,
470e552da7Schristos uv_buf_t* buf) {
480e552da7Schristos static char slab[65536];
490e552da7Schristos CHECK_HANDLE(handle);
500e552da7Schristos ASSERT(suggested_size <= sizeof(slab));
510e552da7Schristos buf->base = slab;
520e552da7Schristos buf->len = sizeof(slab);
530e552da7Schristos }
540e552da7Schristos
550e552da7Schristos
close_cb(uv_handle_t * handle)560e552da7Schristos static void close_cb(uv_handle_t* handle) {
570e552da7Schristos CHECK_HANDLE(handle);
580e552da7Schristos close_cb_called++;
590e552da7Schristos }
600e552da7Schristos
610e552da7Schristos
sv_send_cb(uv_udp_send_t * req,int status)620e552da7Schristos static void sv_send_cb(uv_udp_send_t* req, int status) {
63*5f2f4271Schristos ASSERT_NOT_NULL(req);
640e552da7Schristos ASSERT(status == 0);
650e552da7Schristos CHECK_HANDLE(req->handle);
660e552da7Schristos
670e552da7Schristos sv_send_cb_called++;
680e552da7Schristos
690e552da7Schristos if (sv_send_cb_called == 2)
700e552da7Schristos uv_close((uv_handle_t*) req->handle, close_cb);
710e552da7Schristos }
720e552da7Schristos
730e552da7Schristos
do_send(uv_udp_send_t * send_req)740e552da7Schristos static int do_send(uv_udp_send_t* send_req) {
750e552da7Schristos uv_buf_t buf;
760e552da7Schristos struct sockaddr_in addr;
770e552da7Schristos
780e552da7Schristos buf = uv_buf_init("PING", 4);
790e552da7Schristos
800e552da7Schristos ASSERT(0 == uv_ip4_addr(MULTICAST_ADDR, TEST_PORT, &addr));
810e552da7Schristos
820e552da7Schristos /* client sends "PING" */
830e552da7Schristos return uv_udp_send(send_req,
840e552da7Schristos &client,
850e552da7Schristos &buf,
860e552da7Schristos 1,
870e552da7Schristos (const struct sockaddr*) &addr,
880e552da7Schristos sv_send_cb);
890e552da7Schristos }
900e552da7Schristos
910e552da7Schristos
cl_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * buf,const struct sockaddr * addr,unsigned flags)920e552da7Schristos static void cl_recv_cb(uv_udp_t* handle,
930e552da7Schristos ssize_t nread,
940e552da7Schristos const uv_buf_t* buf,
950e552da7Schristos const struct sockaddr* addr,
960e552da7Schristos unsigned flags) {
970e552da7Schristos CHECK_HANDLE(handle);
980e552da7Schristos ASSERT(flags == 0);
990e552da7Schristos
1000e552da7Schristos if (nread < 0) {
1010e552da7Schristos ASSERT(0 && "unexpected error");
1020e552da7Schristos }
1030e552da7Schristos
1040e552da7Schristos if (nread == 0) {
1050e552da7Schristos /* Returning unused buffer. Don't count towards cl_recv_cb_called */
106*5f2f4271Schristos ASSERT_NULL(addr);
1070e552da7Schristos return;
1080e552da7Schristos }
1090e552da7Schristos
110*5f2f4271Schristos ASSERT_NOT_NULL(addr);
1110e552da7Schristos ASSERT(nread == 4);
1120e552da7Schristos ASSERT(!memcmp("PING", buf->base, nread));
1130e552da7Schristos
1140e552da7Schristos cl_recv_cb_called++;
1150e552da7Schristos
1160e552da7Schristos if (cl_recv_cb_called == 2) {
1170e552da7Schristos /* we are done with the server handle, we can close it */
1180e552da7Schristos uv_close((uv_handle_t*) &server, close_cb);
1190e552da7Schristos } else {
1200e552da7Schristos int r;
1210e552da7Schristos char source_addr[64];
1220e552da7Schristos
1230e552da7Schristos r = uv_ip4_name((const struct sockaddr_in*)addr, source_addr, sizeof(source_addr));
1240e552da7Schristos ASSERT(r == 0);
1250e552da7Schristos
1260e552da7Schristos r = uv_udp_set_membership(&server, MULTICAST_ADDR, NULL, UV_LEAVE_GROUP);
1270e552da7Schristos ASSERT(r == 0);
1280e552da7Schristos
1290e552da7Schristos #if !defined(__OpenBSD__) && !defined(__NetBSD__)
1300e552da7Schristos r = uv_udp_set_source_membership(&server, MULTICAST_ADDR, NULL, source_addr, UV_JOIN_GROUP);
1310e552da7Schristos ASSERT(r == 0);
1320e552da7Schristos #endif
1330e552da7Schristos
1340e552da7Schristos r = do_send(&req_ss);
1350e552da7Schristos ASSERT(r == 0);
1360e552da7Schristos }
1370e552da7Schristos }
1380e552da7Schristos
1390e552da7Schristos
TEST_IMPL(udp_multicast_join)1400e552da7Schristos TEST_IMPL(udp_multicast_join) {
1410e552da7Schristos int r;
1420e552da7Schristos struct sockaddr_in addr;
1430e552da7Schristos
1440e552da7Schristos ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
1450e552da7Schristos
1460e552da7Schristos r = uv_udp_init(uv_default_loop(), &server);
1470e552da7Schristos ASSERT(r == 0);
1480e552da7Schristos
1490e552da7Schristos r = uv_udp_init(uv_default_loop(), &client);
1500e552da7Schristos ASSERT(r == 0);
1510e552da7Schristos
1520e552da7Schristos /* bind to the desired port */
1530e552da7Schristos r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
1540e552da7Schristos ASSERT(r == 0);
1550e552da7Schristos
1560e552da7Schristos /* join the multicast channel */
1570e552da7Schristos r = uv_udp_set_membership(&server, MULTICAST_ADDR, NULL, UV_JOIN_GROUP);
1580e552da7Schristos if (r == UV_ENODEV)
1590e552da7Schristos RETURN_SKIP("No multicast support.");
1600e552da7Schristos ASSERT(r == 0);
1610e552da7Schristos
1620e552da7Schristos r = uv_udp_recv_start(&server, alloc_cb, cl_recv_cb);
1630e552da7Schristos ASSERT(r == 0);
1640e552da7Schristos
1650e552da7Schristos r = do_send(&req);
1660e552da7Schristos ASSERT(r == 0);
1670e552da7Schristos
1680e552da7Schristos ASSERT(close_cb_called == 0);
1690e552da7Schristos ASSERT(cl_recv_cb_called == 0);
1700e552da7Schristos ASSERT(sv_send_cb_called == 0);
1710e552da7Schristos
1720e552da7Schristos /* run the loop till all events are processed */
1730e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1740e552da7Schristos
1750e552da7Schristos ASSERT(cl_recv_cb_called == 2);
1760e552da7Schristos ASSERT(sv_send_cb_called == 2);
1770e552da7Schristos ASSERT(close_cb_called == 2);
1780e552da7Schristos
1790e552da7Schristos MAKE_VALGRIND_HAPPY();
1800e552da7Schristos return 0;
1810e552da7Schristos }
182