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
32 static uv_udp_t server;
33 static uv_udp_t client;
34
35 static int cl_send_cb_called;
36 static int sv_recv_cb_called;
37 static int close_cb_called;
38
39
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)40 static void alloc_cb(uv_handle_t* handle,
41 size_t suggested_size,
42 uv_buf_t* buf) {
43 static char slab[65536];
44 CHECK_HANDLE(handle);
45 ASSERT(suggested_size <= sizeof(slab));
46 buf->base = slab;
47 buf->len = sizeof(slab);
48 }
49
50
close_cb(uv_handle_t * handle)51 static void close_cb(uv_handle_t* handle) {
52 CHECK_HANDLE(handle);
53 ASSERT(1 == uv_is_closing(handle));
54 close_cb_called++;
55 }
56
57
cl_send_cb(uv_udp_send_t * req,int status)58 static void cl_send_cb(uv_udp_send_t* req, int status) {
59 ASSERT_NOT_NULL(req);
60 ASSERT(status == 0);
61 CHECK_HANDLE(req->handle);
62
63 cl_send_cb_called++;
64 }
65
66
sv_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * rcvbuf,const struct sockaddr * addr,unsigned flags)67 static void sv_recv_cb(uv_udp_t* handle,
68 ssize_t nread,
69 const uv_buf_t* rcvbuf,
70 const struct sockaddr* addr,
71 unsigned flags) {
72 if (nread < 0) {
73 ASSERT(0 && "unexpected error");
74 }
75
76 if (nread == 0) {
77 /* Returning unused buffer. Don't count towards sv_recv_cb_called */
78 ASSERT_NULL(addr);
79 return;
80 }
81
82 CHECK_HANDLE(handle);
83 ASSERT(flags == 0);
84
85 ASSERT_NOT_NULL(addr);
86 ASSERT(nread == 4);
87 ASSERT(memcmp("PING", rcvbuf->base, nread) == 0 ||
88 memcmp("PANG", rcvbuf->base, nread) == 0);
89
90 if (++sv_recv_cb_called == 2) {
91 uv_close((uv_handle_t*) &server, close_cb);
92 uv_close((uv_handle_t*) &client, close_cb);
93 }
94 }
95
96
TEST_IMPL(udp_send_immediate)97 TEST_IMPL(udp_send_immediate) {
98 struct sockaddr_in addr;
99 uv_udp_send_t req1, req2;
100 uv_buf_t buf;
101 int r;
102
103 ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
104
105 r = uv_udp_init(uv_default_loop(), &server);
106 ASSERT(r == 0);
107
108 r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
109 ASSERT(r == 0);
110
111 r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
112 ASSERT(r == 0);
113
114 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
115
116 r = uv_udp_init(uv_default_loop(), &client);
117 ASSERT(r == 0);
118
119 /* client sends "PING", then "PANG" */
120 buf = uv_buf_init("PING", 4);
121
122 r = uv_udp_send(&req1,
123 &client,
124 &buf,
125 1,
126 (const struct sockaddr*) &addr,
127 cl_send_cb);
128 ASSERT(r == 0);
129
130 buf = uv_buf_init("PANG", 4);
131
132 r = uv_udp_send(&req2,
133 &client,
134 &buf,
135 1,
136 (const struct sockaddr*) &addr,
137 cl_send_cb);
138 ASSERT(r == 0);
139
140 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
141
142 ASSERT(cl_send_cb_called == 2);
143 ASSERT(sv_recv_cb_called == 2);
144 ASSERT(close_cb_called == 2);
145
146 MAKE_VALGRIND_HAPPY();
147 return 0;
148 }
149