test-udp-options.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 static int udp_options_test(const struct sockaddr* addr) {
31  static int invalid_ttls[] = { -1, 0, 256 };
32  uv_loop_t* loop;
33  uv_udp_t h;
34  int i, r;
35 
37 
38  r = uv_udp_init(loop, &h);
39  ASSERT(r == 0);
40 
41  uv_unref((uv_handle_t*)&h); /* don't keep the loop alive */
42 
43  r = uv_udp_bind(&h, addr, 0);
44  ASSERT(r == 0);
45 
46  r = uv_udp_set_broadcast(&h, 1);
47  r |= uv_udp_set_broadcast(&h, 1);
48  r |= uv_udp_set_broadcast(&h, 0);
49  r |= uv_udp_set_broadcast(&h, 0);
50  ASSERT(r == 0);
51 
52  /* values 1-255 should work */
53  for (i = 1; i <= 255; i++) {
54  r = uv_udp_set_ttl(&h, i);
55 #if defined(__MVS__)
56  if (addr->sa_family == AF_INET6)
57  ASSERT(r == 0);
58  else
59  ASSERT(r == UV_ENOTSUP);
60 #else
61  ASSERT(r == 0);
62 #endif
63  }
64 
65  for (i = 0; i < (int) ARRAY_SIZE(invalid_ttls); i++) {
66  r = uv_udp_set_ttl(&h, invalid_ttls[i]);
67  ASSERT(r == UV_EINVAL);
68  }
69 
71  r |= uv_udp_set_multicast_loop(&h, 1);
72  r |= uv_udp_set_multicast_loop(&h, 0);
73  r |= uv_udp_set_multicast_loop(&h, 0);
74  ASSERT(r == 0);
75 
76  /* values 0-255 should work */
77  for (i = 0; i <= 255; i++) {
79  ASSERT(r == 0);
80  }
81 
82  /* anything >255 should fail */
83  r = uv_udp_set_multicast_ttl(&h, 256);
84  ASSERT(r == UV_EINVAL);
85  /* don't test ttl=-1, it's a valid value on some platforms */
86 
88  ASSERT(r == 0);
89 
91  return 0;
92 }
93 
94 
95 TEST_IMPL(udp_options) {
96  struct sockaddr_in addr;
97 
98  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
99  return udp_options_test((const struct sockaddr*) &addr);
100 }
101 
102 
103 TEST_IMPL(udp_options6) {
104  struct sockaddr_in6 addr;
105 
106  if (!can_ipv6())
107  RETURN_SKIP("IPv6 not supported");
108 
109  ASSERT(0 == uv_ip6_addr("::", TEST_PORT, &addr));
110  return udp_options_test((const struct sockaddr*) &addr);
111 }
112 
113 
114 TEST_IMPL(udp_no_autobind) {
115  uv_loop_t* loop;
116  uv_udp_t h;
117  uv_udp_t h2;
118 
119  loop = uv_default_loop();
120 
121  /* Test a lazy initialized socket. */
122  ASSERT(0 == uv_udp_init(loop, &h));
123  ASSERT(UV_EBADF == uv_udp_set_multicast_ttl(&h, 32));
124  ASSERT(UV_EBADF == uv_udp_set_broadcast(&h, 1));
125 #if defined(__MVS__)
126  ASSERT(UV_ENOTSUP == uv_udp_set_ttl(&h, 1));
127 #else
128  ASSERT(UV_EBADF == uv_udp_set_ttl(&h, 1));
129 #endif
130  ASSERT(UV_EBADF == uv_udp_set_multicast_loop(&h, 1));
131  ASSERT(UV_EBADF == uv_udp_set_multicast_interface(&h, "0.0.0.0"));
132 
133  uv_close((uv_handle_t*) &h, NULL);
134 
135  /* Test a non-lazily initialized socket. */
136  ASSERT(0 == uv_udp_init_ex(loop, &h2, AF_INET));
137  ASSERT(0 == uv_udp_set_multicast_ttl(&h2, 32));
138  ASSERT(0 == uv_udp_set_broadcast(&h2, 1));
139 
140 #if defined(__MVS__)
141  /* zOS only supports setting ttl for IPv6 sockets. */
142  ASSERT(UV_ENOTSUP == uv_udp_set_ttl(&h2, 1));
143 #else
144  ASSERT(0 == uv_udp_set_ttl(&h2, 1));
145 #endif
146 
147  ASSERT(0 == uv_udp_set_multicast_loop(&h2, 1));
148  ASSERT(0 == uv_udp_set_multicast_interface(&h2, "0.0.0.0"));
149 
150  uv_close((uv_handle_t*) &h2, NULL);
151 
153 
155  return 0;
156 }
uv_udp_set_multicast_ttl
UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t *handle, int ttl)
Definition: unix/udp.c:1185
uv_udp_set_broadcast
UV_EXTERN int uv_udp_set_broadcast(uv_udp_t *handle, int on)
Definition: unix/udp.c:1135
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
uv_ip6_addr
UV_EXTERN int uv_ip6_addr(const char *ip, int port, struct sockaddr_in6 *addr)
Definition: uv-common.c:232
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
TEST_IMPL
TEST_IMPL(udp_options)
Definition: test-udp-options.c:95
task.h
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
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
uv_unref
UV_EXTERN void uv_unref(uv_handle_t *)
Definition: uv-common.c:522
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
xds_interop_client.int
int
Definition: xds_interop_client.py:113
sockaddr_in6
Definition: ares_ipv6.h:25
uv_udp_set_multicast_loop
UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t *handle, int on)
Definition: unix/udp.c:1210
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_set_multicast_interface
UV_EXTERN int uv_udp_set_multicast_interface(uv_udp_t *handle, const char *interface_addr)
Definition: unix/udp.c:1234
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
fix_build_deps.r
r
Definition: fix_build_deps.py:491
udp_options_test
static int udp_options_test(const struct sockaddr *addr)
Definition: test-udp-options.c:30
can_ipv6
static UNUSED int can_ipv6(void)
Definition: task.h:315
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
uv_udp_set_ttl
UV_EXTERN int uv_udp_set_ttl(uv_udp_t *handle, int ttl)
Definition: unix/udp.c:1148
uv_udp_init_ex
UV_EXTERN int uv_udp_init_ex(uv_loop_t *, uv_udp_t *handle, unsigned int flags)
Definition: unix/udp.c:947
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:27