PageRenderTime 43ms CodeModel.GetById 16ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test-udp-multicast-join.c

http://github.com/joyent/libuv
C | 140 lines | 77 code | 36 blank | 27 comment | 25 complexity | 084842f332da057a444354d170306fad MD5 | raw file
  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
 22#include "uv.h"
 23#include "task.h"
 24
 25#include <stdio.h>
 26#include <stdlib.h>
 27#include <string.h>
 28
 29#define CHECK_HANDLE(handle) \
 30  ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
 31
 32static uv_udp_t server;
 33static uv_udp_t client;
 34
 35static int cl_recv_cb_called;
 36
 37static int sv_send_cb_called;
 38
 39static int close_cb_called;
 40
 41static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
 42  static char slab[65536];
 43
 44  CHECK_HANDLE(handle);
 45  ASSERT(suggested_size <= sizeof slab);
 46
 47  return uv_buf_init(slab, sizeof slab);
 48}
 49
 50
 51static void close_cb(uv_handle_t* handle) {
 52  CHECK_HANDLE(handle);
 53  close_cb_called++;
 54}
 55
 56
 57static void sv_send_cb(uv_udp_send_t* req, int status) {
 58  ASSERT(req != NULL);
 59  ASSERT(status == 0);
 60  CHECK_HANDLE(req->handle);
 61
 62  sv_send_cb_called++;
 63
 64  uv_close((uv_handle_t*) req->handle, close_cb);
 65}
 66
 67
 68static void cl_recv_cb(uv_udp_t* handle,
 69                       ssize_t nread,
 70                       uv_buf_t buf,
 71                       struct sockaddr* addr,
 72                       unsigned flags) {
 73  CHECK_HANDLE(handle);
 74  ASSERT(flags == 0);
 75
 76  cl_recv_cb_called++;
 77
 78  if (nread < 0) {
 79    ASSERT(0 && "unexpected error");
 80  }
 81
 82  if (nread == 0) {
 83    /* Returning unused buffer */
 84    /* Don't count towards cl_recv_cb_called */
 85    ASSERT(addr == NULL);
 86    return;
 87  }
 88
 89  ASSERT(addr != NULL);
 90  ASSERT(nread == 4);
 91  ASSERT(!memcmp("PING", buf.base, nread));
 92
 93  /* we are done with the client handle, we can close it */
 94  uv_close((uv_handle_t*) &client, close_cb);
 95}
 96
 97
 98TEST_IMPL(udp_multicast_join) {
 99  int r;
100  uv_udp_send_t req;
101  uv_buf_t buf;
102  struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
103
104  r = uv_udp_init(uv_default_loop(), &server);
105  ASSERT(r == 0);
106
107  r = uv_udp_init(uv_default_loop(), &client);
108  ASSERT(r == 0);
109
110  /* bind to the desired port */
111  r = uv_udp_bind(&client, addr, 0);
112  ASSERT(r == 0);
113
114  /* join the multicast channel */
115  r = uv_udp_set_membership(&client, "239.255.0.1", NULL, UV_JOIN_GROUP);
116  ASSERT(r == 0);
117
118  r = uv_udp_recv_start(&client, alloc_cb, cl_recv_cb);
119  ASSERT(r == 0);
120
121  buf = uv_buf_init("PING", 4);
122
123  /* server sends "PING" */
124  r = uv_udp_send(&req, &server, &buf, 1, addr, sv_send_cb);
125  ASSERT(r == 0);
126
127  ASSERT(close_cb_called == 0);
128  ASSERT(cl_recv_cb_called == 0);
129  ASSERT(sv_send_cb_called == 0);
130
131  /* run the loop till all events are processed */
132  uv_run(uv_default_loop());
133
134  ASSERT(cl_recv_cb_called == 1);
135  ASSERT(sv_send_cb_called == 1);
136  ASSERT(close_cb_called == 2);
137
138  MAKE_VALGRIND_HAPPY();
139  return 0;
140}