test-udp-bind.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 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 
30 TEST_IMPL(udp_bind) {
31  struct sockaddr_in addr;
32  uv_loop_t* loop;
33  uv_udp_t h1, h2;
34  int r;
35 
36  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
37 
39 
40  r = uv_udp_init(loop, &h1);
41  ASSERT(r == 0);
42 
43  r = uv_udp_init(loop, &h2);
44  ASSERT(r == 0);
45 
46  r = uv_udp_bind(&h1, (const struct sockaddr*) &addr, 0);
47  ASSERT(r == 0);
48 
49  r = uv_udp_bind(&h2, (const struct sockaddr*) &addr, 0);
50  ASSERT(r == UV_EADDRINUSE);
51 
52  uv_close((uv_handle_t*) &h1, NULL);
53  uv_close((uv_handle_t*) &h2, NULL);
54 
56  ASSERT(r == 0);
57 
59  return 0;
60 }
61 
62 
63 TEST_IMPL(udp_bind_reuseaddr) {
64  struct sockaddr_in addr;
65  uv_loop_t* loop;
66  uv_udp_t h1, h2;
67  int r;
68 
69  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
70 
72 
73  r = uv_udp_init(loop, &h1);
74  ASSERT(r == 0);
75 
76  r = uv_udp_init(loop, &h2);
77  ASSERT(r == 0);
78 
79  r = uv_udp_bind(&h1, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR);
80  ASSERT(r == 0);
81 
82  r = uv_udp_bind(&h2, (const struct sockaddr*) &addr, UV_UDP_REUSEADDR);
83  ASSERT(r == 0);
84 
85  uv_close((uv_handle_t*) &h1, NULL);
86  uv_close((uv_handle_t*) &h2, NULL);
87 
89  ASSERT(r == 0);
90 
92  return 0;
93 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
task.h
string.h
ASSERT
#define ASSERT(expr)
Definition: task.h:102
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_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
uv_udp_init
UV_EXTERN int uv_udp_init(uv_loop_t *, uv_udp_t *handle)
Definition: unix/udp.c:988
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
TEST_IMPL
TEST_IMPL(udp_bind)
Definition: test-udp-bind.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_udp_s
Definition: uv.h:629
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
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
UV_UDP_REUSEADDR
@ UV_UDP_REUSEADDR
Definition: uv.h:608
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
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:30