test-udp-multicast-join.c 4.7 KB

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