test-udp-open.c
Go to the documentation of this 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 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #ifndef _WIN32
29 # include <unistd.h>
30 # include <sys/socket.h>
31 # include <sys/un.h>
32 #endif
33 
34 static int send_cb_called = 0;
35 static int close_cb_called = 0;
36 
38 
39 
40 static void startup(void) {
41 #ifdef _WIN32
42  struct WSAData wsa_data;
43  int r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
44  ASSERT(r == 0);
45 #endif
46 }
47 
48 
50  uv_os_sock_t sock;
51 
52  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
53 #ifdef _WIN32
54  ASSERT(sock != INVALID_SOCKET);
55 #else
56  ASSERT(sock >= 0);
57 #endif
58 
59 #ifndef _WIN32
60  {
61  /* Allow reuse of the port. */
62  int yes = 1;
63  int r = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
64  ASSERT(r == 0);
65  }
66 #endif
67 
68  return sock;
69 }
70 
71 
72 static void close_socket(uv_os_sock_t sock) {
73  int r;
74 #ifdef _WIN32
75  r = closesocket(sock);
76 #else
77  r = close(sock);
78 #endif
79  ASSERT(r == 0);
80 }
81 
82 
84  size_t suggested_size,
85  uv_buf_t* buf) {
86  static char slab[65536];
87  ASSERT(suggested_size <= sizeof(slab));
88  buf->base = slab;
89  buf->len = sizeof(slab);
90 }
91 
92 
93 static void close_cb(uv_handle_t* handle) {
94  ASSERT(handle != NULL);
96 }
97 
98 
99 static void recv_cb(uv_udp_t* handle,
100  ssize_t nread,
101  const uv_buf_t* buf,
102  const struct sockaddr* addr,
103  unsigned flags) {
104  int r;
105 
106  if (nread < 0) {
107  ASSERT(0 && "unexpected error");
108  }
109 
110  if (nread == 0) {
111  /* Returning unused buffer. Don't count towards sv_recv_cb_called */
112  ASSERT(addr == NULL);
113  return;
114  }
115 
116  ASSERT(flags == 0);
117 
118  ASSERT(addr != NULL);
119  ASSERT(nread == 4);
120  ASSERT(memcmp("PING", buf->base, nread) == 0);
121 
123  ASSERT(r == 0);
124 
126 }
127 
128 
129 static void send_cb(uv_udp_send_t* req, int status) {
130  ASSERT(req != NULL);
131  ASSERT(status == 0);
132 
133  send_cb_called++;
135 }
136 
137 
138 TEST_IMPL(udp_open) {
139  struct sockaddr_in addr;
140  uv_buf_t buf = uv_buf_init("PING", 4);
142  uv_os_sock_t sock;
143  int r;
144 
145  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
146 
147  startup();
148  sock = create_udp_socket();
149 
151  ASSERT(r == 0);
152 
153  r = uv_udp_open(&client, sock);
154  ASSERT(r == 0);
155 
156  r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
157  ASSERT(r == 0);
158 
160  ASSERT(r == 0);
161 
163  &client,
164  &buf,
165  1,
166  (const struct sockaddr*) &addr,
167  send_cb);
168  ASSERT(r == 0);
169 
170 #ifndef _WIN32
171  {
172  uv_udp_t client2;
173 
174  r = uv_udp_init(uv_default_loop(), &client2);
175  ASSERT(r == 0);
176 
177  r = uv_udp_open(&client2, sock);
178  ASSERT(r == UV_EEXIST);
179 
180  uv_close((uv_handle_t*) &client2, NULL);
181  }
182 #endif /* !_WIN32 */
183 
185 
186  ASSERT(send_cb_called == 1);
187  ASSERT(close_cb_called == 1);
188 
189  ASSERT(client.send_queue_size == 0);
190 
192  return 0;
193 }
194 
195 
196 TEST_IMPL(udp_open_twice) {
198  uv_os_sock_t sock1, sock2;
199  int r;
200 
201  startup();
202  sock1 = create_udp_socket();
203  sock2 = create_udp_socket();
204 
206  ASSERT(r == 0);
207 
208  r = uv_udp_open(&client, sock1);
209  ASSERT(r == 0);
210 
211  r = uv_udp_open(&client, sock2);
212  ASSERT(r == UV_EBUSY);
213  close_socket(sock2);
214 
215  uv_close((uv_handle_t*) &client, NULL);
217 
219  return 0;
220 }
221 
222 TEST_IMPL(udp_open_bound) {
223  struct sockaddr_in addr;
225  uv_os_sock_t sock;
226  int r;
227 
228  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
229 
230  startup();
231  sock = create_udp_socket();
232 
233  r = bind(sock, (struct sockaddr*) &addr, sizeof(addr));
234  ASSERT(r == 0);
235 
237  ASSERT(r == 0);
238 
239  r = uv_udp_open(&client, sock);
240  ASSERT(r == 0);
241 
243  ASSERT(r == 0);
244 
245  uv_close((uv_handle_t*) &client, NULL);
247 
249  return 0;
250 }
251 
252 TEST_IMPL(udp_open_connect) {
253  struct sockaddr_in addr;
254  uv_buf_t buf = uv_buf_init("PING", 4);
257  uv_os_sock_t sock;
258  int r;
259 
260  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
261 
262  startup();
263  sock = create_udp_socket();
264 
266  ASSERT(r == 0);
267 
268  r = connect(sock, (const struct sockaddr*) &addr, sizeof(addr));
269  ASSERT(r == 0);
270 
271  r = uv_udp_open(&client, sock);
272  ASSERT(r == 0);
273 
275  ASSERT(r == 0);
276 
277  r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
278  ASSERT(r == 0);
279 
281  ASSERT(r == 0);
282 
284  &client,
285  &buf,
286  1,
287  NULL,
288  send_cb);
289  ASSERT(r == 0);
290 
292 
293  ASSERT(send_cb_called == 1);
294  ASSERT(close_cb_called == 2);
295 
296  ASSERT(client.send_queue_size == 0);
297 
299  return 0;
300 }
301 
302 #ifndef _WIN32
303 TEST_IMPL(udp_send_unix) {
304  /* Test that "uv_udp_send()" supports sending over
305  a "sockaddr_un" address. */
306  struct sockaddr_un addr;
309  uv_loop_t* loop;
310  uv_buf_t buf = uv_buf_init("PING", 4);
311  int fd;
312  int r;
313 
314  loop = uv_default_loop();
315 
316  memset(&addr, 0, sizeof addr);
317  addr.sun_family = AF_UNIX;
318  ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
319  memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
320 
321  fd = socket(AF_UNIX, SOCK_STREAM, 0);
322  ASSERT(fd >= 0);
323 
325  ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
326  ASSERT(0 == listen(fd, 1));
327 
328  r = uv_udp_init(loop, &handle);
329  ASSERT(r == 0);
330  r = uv_udp_open(&handle, fd);
331  ASSERT(r == 0);
333 
334  r = uv_udp_send(&req,
335  &handle,
336  &buf,
337  1,
338  (const struct sockaddr*) &addr,
339  NULL);
340  ASSERT(r == 0);
341 
342  uv_close((uv_handle_t*)&handle, NULL);
344  close(fd);
346 
348  return 0;
349 }
350 #endif
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
slab
static char slab[1]
Definition: test-watcher-cross-stop.c:37
recv_cb
static void recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags)
Definition: test-udp-open.c:99
task.h
memset
return memset(p, 0, total)
client
Definition: examples/python/async_streaming/client.py:1
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
uv_udp_send
UV_EXTERN int uv_udp_send(uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, uv_udp_send_cb send_cb)
Definition: uv-common.c:408
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
send_cb
static void send_cb(uv_udp_send_t *req, int status)
Definition: test-udp-open.c:129
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_os_sock_t
int uv_os_sock_t
Definition: unix.h:127
client
static uv_tcp_t client
Definition: test-callback-stack.c:33
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
send_cb_called
static int send_cb_called
Definition: test-udp-open.c:34
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_udp_init
UV_EXTERN int uv_udp_init(uv_loop_t *, uv_udp_t *handle)
Definition: unix/udp.c:988
ssize_t
intptr_t ssize_t
Definition: win.h:27
send_req
static uv_udp_send_t send_req
Definition: test-udp-open.c:37
TEST_IMPL
TEST_IMPL(udp_open)
Definition: test-udp-open.c:138
close_socket
static void close_socket(uv_os_sock_t sock)
Definition: test-udp-open.c:72
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_udp_s
Definition: uv.h:629
create_udp_socket
static uv_os_sock_t create_udp_socket(void)
Definition: test-udp-open.c:49
startup
static void startup(void)
Definition: test-udp-open.c:40
close
#define close
Definition: test-fs.c:48
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-udp-open.c:93
close_cb_called
static int close_cb_called
Definition: test-udp-open.c:35
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: test-udp-open.c:83
uv_udp_send_s
Definition: uv.h:645
uv.h
uv_udp_recv_start
UV_EXTERN int uv_udp_recv_start(uv_udp_t *handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb)
Definition: uv-common.c:438
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_buf_t
Definition: unix.h:121
uv_udp_bind
UV_EXTERN int uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:296
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
server
Definition: examples/python/async_streaming/server.py:1
http2_test_server.listen
def listen(endpoint, test_case)
Definition: http2_test_server.py:87
uv_udp_open
UV_EXTERN int uv_udp_open(uv_udp_t *handle, uv_os_sock_t sock)
Definition: unix/udp.c:993
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_buf_init
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
unlink
#define unlink
Definition: test-fs-copyfile.c:33
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
TEST_PIPENAME
#define TEST_PIPENAME
Definition: task.h:61
test_server.socket
socket
Definition: test_server.py:65
closesocket
static int closesocket(int sock)
Definition: bio_test.cc:46
uv_loop_s
Definition: uv.h:1767
uv_udp_recv_stop
UV_EXTERN int uv_udp_recv_stop(uv_udp_t *handle)
Definition: uv-common.c:448
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:31