test-udp-multicast-join6.c 5.5 KB

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