1
0

test-udp-multicast-join.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. static uv_udp_t server;
  29. static uv_udp_t client;
  30. static int cl_recv_cb_called;
  31. static int sv_send_cb_called;
  32. static int close_cb_called;
  33. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
  34. static char slab[65536];
  35. CHECK_HANDLE(handle);
  36. ASSERT(suggested_size <= sizeof slab);
  37. return uv_buf_init(slab, sizeof slab);
  38. }
  39. static void close_cb(uv_handle_t* handle) {
  40. CHECK_HANDLE(handle);
  41. close_cb_called++;
  42. }
  43. static void sv_send_cb(uv_udp_send_t* req, int status) {
  44. ASSERT(req != NULL);
  45. ASSERT(status == 0);
  46. CHECK_HANDLE(req->handle);
  47. sv_send_cb_called++;
  48. uv_close((uv_handle_t*) req->handle, close_cb);
  49. }
  50. static void cl_recv_cb(uv_udp_t* handle,
  51. ssize_t nread,
  52. uv_buf_t buf,
  53. struct sockaddr* addr,
  54. unsigned flags) {
  55. CHECK_HANDLE(handle);
  56. ASSERT(flags == 0);
  57. cl_recv_cb_called++;
  58. if (nread < 0) {
  59. ASSERT(0 && "unexpected error");
  60. }
  61. if (nread == 0) {
  62. /* Returning unused buffer */
  63. /* Don't count towards cl_recv_cb_called */
  64. ASSERT(addr == NULL);
  65. return;
  66. }
  67. ASSERT(addr != NULL);
  68. ASSERT(nread == 4);
  69. ASSERT(!memcmp("PING", buf.base, nread));
  70. /* we are done with the client handle, we can close it */
  71. uv_close((uv_handle_t*) &client, close_cb);
  72. }
  73. TEST_IMPL(udp_multicast_join) {
  74. int r;
  75. uv_udp_send_t req;
  76. uv_buf_t buf;
  77. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  78. r = uv_udp_init(uv_default_loop(), &server);
  79. ASSERT(r == 0);
  80. r = uv_udp_init(uv_default_loop(), &client);
  81. ASSERT(r == 0);
  82. /* bind to the desired port */
  83. r = uv_udp_bind(&client, addr, 0);
  84. ASSERT(r == 0);
  85. /* join the multicast channel */
  86. r = uv_udp_set_membership(&client, "239.255.0.1", NULL, UV_JOIN_GROUP);
  87. ASSERT(r == 0);
  88. r = uv_udp_recv_start(&client, alloc_cb, cl_recv_cb);
  89. ASSERT(r == 0);
  90. buf = uv_buf_init("PING", 4);
  91. /* server sends "PING" */
  92. r = uv_udp_send(&req, &server, &buf, 1, addr, sv_send_cb);
  93. ASSERT(r == 0);
  94. ASSERT(close_cb_called == 0);
  95. ASSERT(cl_recv_cb_called == 0);
  96. ASSERT(sv_send_cb_called == 0);
  97. /* run the loop till all events are processed */
  98. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  99. ASSERT(cl_recv_cb_called == 1);
  100. ASSERT(sv_send_cb_called == 1);
  101. ASSERT(close_cb_called == 2);
  102. MAKE_VALGRIND_HAPPY();
  103. return 0;
  104. }