xref: /netbsd-src/external/mit/libuv/dist/test/test-udp-multicast-join6.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
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 
300e552da7Schristos #define CHECK_HANDLE(handle) \
310e552da7Schristos   ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
320e552da7Schristos 
330e552da7Schristos #if defined(__APPLE__)          || \
340e552da7Schristos     defined(_AIX)               || \
350e552da7Schristos     defined(__MVS__)            || \
360e552da7Schristos     defined(__FreeBSD_kernel__) || \
370e552da7Schristos     defined(__NetBSD__)         || \
380e552da7Schristos     defined(__OpenBSD__)
390e552da7Schristos   #define MULTICAST_ADDR "ff02::1%lo0"
400e552da7Schristos   #define INTERFACE_ADDR "::1%lo0"
410e552da7Schristos #else
420e552da7Schristos   #define MULTICAST_ADDR "ff02::1"
430e552da7Schristos   #define INTERFACE_ADDR NULL
440e552da7Schristos #endif
450e552da7Schristos 
460e552da7Schristos static uv_udp_t server;
470e552da7Schristos static uv_udp_t client;
480e552da7Schristos static uv_udp_send_t req;
490e552da7Schristos static uv_udp_send_t req_ss;
500e552da7Schristos 
510e552da7Schristos static int cl_recv_cb_called;
520e552da7Schristos 
530e552da7Schristos static int sv_send_cb_called;
540e552da7Schristos 
550e552da7Schristos static int close_cb_called;
560e552da7Schristos 
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)570e552da7Schristos static void alloc_cb(uv_handle_t* handle,
580e552da7Schristos                      size_t suggested_size,
590e552da7Schristos                      uv_buf_t* buf) {
600e552da7Schristos   static char slab[65536];
610e552da7Schristos   CHECK_HANDLE(handle);
620e552da7Schristos   ASSERT(suggested_size <= sizeof(slab));
630e552da7Schristos   buf->base = slab;
640e552da7Schristos   buf->len = sizeof(slab);
650e552da7Schristos }
660e552da7Schristos 
670e552da7Schristos 
close_cb(uv_handle_t * handle)680e552da7Schristos static void close_cb(uv_handle_t* handle) {
690e552da7Schristos   CHECK_HANDLE(handle);
700e552da7Schristos   close_cb_called++;
710e552da7Schristos }
720e552da7Schristos 
730e552da7Schristos 
sv_send_cb(uv_udp_send_t * req,int status)740e552da7Schristos static void sv_send_cb(uv_udp_send_t* req, int status) {
75*5f2f4271Schristos   ASSERT_NOT_NULL(req);
760e552da7Schristos   ASSERT(status == 0);
770e552da7Schristos   CHECK_HANDLE(req->handle);
780e552da7Schristos 
790e552da7Schristos   sv_send_cb_called++;
800e552da7Schristos 
810e552da7Schristos   if (sv_send_cb_called == 2)
820e552da7Schristos     uv_close((uv_handle_t*) req->handle, close_cb);
830e552da7Schristos }
840e552da7Schristos 
850e552da7Schristos 
do_send(uv_udp_send_t * send_req)860e552da7Schristos static int do_send(uv_udp_send_t* send_req) {
870e552da7Schristos   uv_buf_t buf;
880e552da7Schristos   struct sockaddr_in6 addr;
890e552da7Schristos 
900e552da7Schristos   buf = uv_buf_init("PING", 4);
910e552da7Schristos 
920e552da7Schristos   ASSERT(0 == uv_ip6_addr(MULTICAST_ADDR, TEST_PORT, &addr));
930e552da7Schristos 
940e552da7Schristos   /* client sends "PING" */
950e552da7Schristos   return uv_udp_send(send_req,
960e552da7Schristos                      &client,
970e552da7Schristos                      &buf,
980e552da7Schristos                      1,
990e552da7Schristos                      (const struct sockaddr*) &addr,
1000e552da7Schristos                      sv_send_cb);
1010e552da7Schristos }
1020e552da7Schristos 
1030e552da7Schristos 
cl_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * buf,const struct sockaddr * addr,unsigned flags)1040e552da7Schristos static void cl_recv_cb(uv_udp_t* handle,
1050e552da7Schristos                        ssize_t nread,
1060e552da7Schristos                        const uv_buf_t* buf,
1070e552da7Schristos                        const struct sockaddr* addr,
1080e552da7Schristos                        unsigned flags) {
1090e552da7Schristos   CHECK_HANDLE(handle);
1100e552da7Schristos   ASSERT(flags == 0);
1110e552da7Schristos 
1120e552da7Schristos   if (nread < 0) {
1130e552da7Schristos     ASSERT(0 && "unexpected error");
1140e552da7Schristos   }
1150e552da7Schristos 
1160e552da7Schristos   if (nread == 0) {
1170e552da7Schristos     /* Returning unused buffer. Don't count towards cl_recv_cb_called */
118*5f2f4271Schristos     ASSERT_NULL(addr);
1190e552da7Schristos     return;
1200e552da7Schristos   }
1210e552da7Schristos 
122*5f2f4271Schristos   ASSERT_NOT_NULL(addr);
1230e552da7Schristos   ASSERT(nread == 4);
1240e552da7Schristos   ASSERT(!memcmp("PING", buf->base, nread));
1250e552da7Schristos 
1260e552da7Schristos   cl_recv_cb_called++;
1270e552da7Schristos 
1280e552da7Schristos   if (cl_recv_cb_called == 2) {
1290e552da7Schristos     /* we are done with the server handle, we can close it */
1300e552da7Schristos     uv_close((uv_handle_t*) &server, close_cb);
1310e552da7Schristos   } else {
1320e552da7Schristos     int r;
1330e552da7Schristos     char source_addr[64];
1340e552da7Schristos 
1350e552da7Schristos     r = uv_ip6_name((const struct sockaddr_in6*)addr, source_addr, sizeof(source_addr));
1360e552da7Schristos     ASSERT(r == 0);
1370e552da7Schristos 
1380e552da7Schristos     r = uv_udp_set_membership(&server, MULTICAST_ADDR, INTERFACE_ADDR, UV_LEAVE_GROUP);
1390e552da7Schristos     ASSERT(r == 0);
1400e552da7Schristos 
1410e552da7Schristos     r = uv_udp_set_source_membership(&server, MULTICAST_ADDR, INTERFACE_ADDR, source_addr, UV_JOIN_GROUP);
1420e552da7Schristos     ASSERT(r == 0);
1430e552da7Schristos 
1440e552da7Schristos     r = do_send(&req_ss);
1450e552da7Schristos     ASSERT(r == 0);
1460e552da7Schristos   }
1470e552da7Schristos }
1480e552da7Schristos 
1490e552da7Schristos 
can_ipv6_external(void)1500e552da7Schristos static int can_ipv6_external(void) {
1510e552da7Schristos   uv_interface_address_t* addr;
1520e552da7Schristos   int supported;
1530e552da7Schristos   int count;
1540e552da7Schristos   int i;
1550e552da7Schristos 
1560e552da7Schristos   if (uv_interface_addresses(&addr, &count))
1570e552da7Schristos     return 0;  /* Assume no IPv6 support on failure. */
1580e552da7Schristos 
1590e552da7Schristos   supported = 0;
1600e552da7Schristos   for (i = 0; supported == 0 && i < count; i += 1)
1610e552da7Schristos     supported = (AF_INET6 == addr[i].address.address6.sin6_family &&
1620e552da7Schristos                  !addr[i].is_internal);
1630e552da7Schristos 
1640e552da7Schristos   uv_free_interface_addresses(addr, count);
1650e552da7Schristos   return supported;
1660e552da7Schristos }
1670e552da7Schristos 
1680e552da7Schristos 
TEST_IMPL(udp_multicast_join6)1690e552da7Schristos TEST_IMPL(udp_multicast_join6) {
1700e552da7Schristos   int r;
1710e552da7Schristos   struct sockaddr_in6 addr;
1720e552da7Schristos 
1730e552da7Schristos   if (!can_ipv6_external())
1740e552da7Schristos     RETURN_SKIP("No external IPv6 interface available");
1750e552da7Schristos 
1760e552da7Schristos   ASSERT(0 == uv_ip6_addr("::", TEST_PORT, &addr));
1770e552da7Schristos 
1780e552da7Schristos   r = uv_udp_init(uv_default_loop(), &server);
1790e552da7Schristos   ASSERT(r == 0);
1800e552da7Schristos 
1810e552da7Schristos   r = uv_udp_init(uv_default_loop(), &client);
1820e552da7Schristos   ASSERT(r == 0);
1830e552da7Schristos 
1840e552da7Schristos   /* bind to the desired port */
1850e552da7Schristos   r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
1860e552da7Schristos   ASSERT(r == 0);
1870e552da7Schristos 
1880e552da7Schristos   r = uv_udp_set_membership(&server, MULTICAST_ADDR, INTERFACE_ADDR, UV_JOIN_GROUP);
1890e552da7Schristos   if (r == UV_ENODEV) {
1900e552da7Schristos     MAKE_VALGRIND_HAPPY();
1910e552da7Schristos     RETURN_SKIP("No ipv6 multicast route");
1920e552da7Schristos   }
1930e552da7Schristos 
1940e552da7Schristos   ASSERT(r == 0);
1950e552da7Schristos 
1960e552da7Schristos /* TODO(gengjiawen): Fix test on QEMU. */
1970e552da7Schristos #if defined(__QEMU__)
1980e552da7Schristos   RETURN_SKIP("Test does not currently work in QEMU");
1990e552da7Schristos #endif
2000e552da7Schristos   r = uv_udp_recv_start(&server, alloc_cb, cl_recv_cb);
2010e552da7Schristos   ASSERT(r == 0);
2020e552da7Schristos 
2030e552da7Schristos   r = do_send(&req);
2040e552da7Schristos   ASSERT(r == 0);
2050e552da7Schristos 
2060e552da7Schristos   ASSERT(close_cb_called == 0);
2070e552da7Schristos   ASSERT(cl_recv_cb_called == 0);
2080e552da7Schristos   ASSERT(sv_send_cb_called == 0);
2090e552da7Schristos 
2100e552da7Schristos   /* run the loop till all events are processed */
2110e552da7Schristos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
2120e552da7Schristos 
2130e552da7Schristos   ASSERT(cl_recv_cb_called == 2);
2140e552da7Schristos   ASSERT(sv_send_cb_called == 2);
2150e552da7Schristos   ASSERT(close_cb_called == 2);
2160e552da7Schristos 
2170e552da7Schristos   MAKE_VALGRIND_HAPPY();
2180e552da7Schristos   return 0;
2190e552da7Schristos }
220