test-udp-multicast-ttl.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 sv_send_cb_called;
  31. static int close_cb_called;
  32. static void close_cb(uv_handle_t* handle) {
  33. CHECK_HANDLE(handle);
  34. close_cb_called++;
  35. }
  36. static void sv_send_cb(uv_udp_send_t* req, int status) {
  37. ASSERT(req != NULL);
  38. ASSERT(status == 0);
  39. CHECK_HANDLE(req->handle);
  40. sv_send_cb_called++;
  41. uv_close((uv_handle_t*) req->handle, close_cb);
  42. }
  43. TEST_IMPL(udp_multicast_ttl) {
  44. int r;
  45. uv_udp_send_t req;
  46. uv_buf_t buf;
  47. struct sockaddr_in addr;
  48. r = uv_udp_init(uv_default_loop(), &server);
  49. ASSERT(r == 0);
  50. ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &addr));
  51. r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
  52. ASSERT(r == 0);
  53. r = uv_udp_set_multicast_ttl(&server, 32);
  54. ASSERT(r == 0);
  55. /* server sends "PING" */
  56. buf = uv_buf_init("PING", 4);
  57. ASSERT(0 == uv_ip4_addr("239.255.0.1", TEST_PORT, &addr));
  58. r = uv_udp_send(&req,
  59. &server,
  60. &buf,
  61. 1,
  62. (const struct sockaddr*) &addr,
  63. sv_send_cb);
  64. ASSERT(r == 0);
  65. ASSERT(close_cb_called == 0);
  66. ASSERT(sv_send_cb_called == 0);
  67. /* run the loop till all events are processed */
  68. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  69. ASSERT(sv_send_cb_called == 1);
  70. ASSERT(close_cb_called == 1);
  71. MAKE_VALGRIND_HAPPY();
  72. return 0;
  73. }