test-tcp-oob.c
Go to the documentation of this file.
1 /* Copyright Fedor Indutny. 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 #if !defined(_WIN32)
23 
24 #include "uv.h"
25 #include "task.h"
26 
27 #include <errno.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30 
34 static uv_idle_t idle;
36 static int ticks;
37 static const int kMaxTicks = 10;
38 
40  size_t suggested_size,
41  uv_buf_t* buf) {
42  static char storage[1024];
43  *buf = uv_buf_init(storage, sizeof(storage));
44 }
45 
46 
47 static void idle_cb(uv_idle_t* idle) {
48  if (++ticks < kMaxTicks)
49  return;
50 
53  uv_close((uv_handle_t*) &peer_handle, NULL);
54  uv_close((uv_handle_t*) idle, NULL);
55 }
56 
57 
58 static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
59 #ifdef __MVS__
60  char lbuf[12];
61 #endif
62  uv_os_fd_t fd;
63 
64  ASSERT(nread >= 0);
65  ASSERT(0 == uv_fileno((uv_handle_t*)handle, &fd));
67 
68 #ifdef __MVS__
69  /* Need to flush out the OOB data. Otherwise, this callback will get
70  * triggered on every poll with nread = 0.
71  */
72  ASSERT(-1 != recv(fd, lbuf, sizeof(lbuf), MSG_OOB));
73 #endif
74 }
75 
76 
77 static void connect_cb(uv_connect_t* req, int status) {
79  ASSERT(0 == status);
80 }
81 
82 
83 static void connection_cb(uv_stream_t* handle, int status) {
84  int r;
85  uv_os_fd_t fd;
86 
87  ASSERT(0 == status);
90 
91  /* Send some OOB data */
92  ASSERT(0 == uv_fileno((uv_handle_t*) &client_handle, &fd));
93 
95 
96  /* The problem triggers only on a second message, it seem that xnu is not
97  * triggering `kevent()` for the first one
98  */
99  do {
100  r = send(fd, "hello", 5, MSG_OOB);
101  } while (r < 0 && errno == EINTR);
102  ASSERT(5 == r);
103 
104  do {
105  r = send(fd, "hello", 5, MSG_OOB);
106  } while (r < 0 && errno == EINTR);
107  ASSERT(5 == r);
108 
110 }
111 
112 
113 TEST_IMPL(tcp_oob) {
114  struct sockaddr_in addr;
115  uv_loop_t* loop;
116 
117  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
118  loop = uv_default_loop();
119 
123  ASSERT(0 == uv_idle_init(loop, &idle));
124  ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
126 
127  /* Ensure two separate packets */
129 
131  &client_handle,
132  (const struct sockaddr*) &addr,
133  connect_cb));
135 
136  ASSERT(ticks == kMaxTicks);
137 
139  return 0;
140 }
141 
142 #else
143 
144 typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
145 
146 #endif /* !_WIN32 */
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
task.h
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: test-tcp-oob.c:77
uv_connect_s
Definition: uv.h:580
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
read_cb
static void read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
Definition: test-tcp-oob.c:58
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
connect_req
static uv_connect_t connect_req
Definition: test-tcp-oob.c:35
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
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_stream_s
Definition: uv.h:491
uv_tcp_connect
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:315
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
ssize_t
intptr_t ssize_t
Definition: win.h:27
ticks
static int ticks
Definition: test-tcp-oob.c:36
req
static uv_connect_t req
Definition: test-connection-fail.c:30
idle
static uv_idle_t idle
Definition: test-tcp-oob.c:34
TEST_IMPL
TEST_IMPL(tcp_oob)
Definition: test-tcp-oob.c:113
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
kMaxTicks
static const int kMaxTicks
Definition: test-tcp-oob.c:37
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_accept
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: unix/stream.c:591
uv_tcp_s
Definition: uv.h:544
uv_stream_set_blocking
UV_EXTERN int uv_stream_set_blocking(uv_stream_t *handle, int blocking)
Definition: unix/stream.c:1688
idle_cb
static void idle_cb(uv_idle_t *idle)
Definition: test-tcp-oob.c:47
uv_idle_init
UV_EXTERN int uv_idle_init(uv_loop_t *, uv_idle_t *idle)
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_buf_t
Definition: unix.h:121
uv_fileno
UV_EXTERN int uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd)
Definition: unix/core.c:755
uv_idle_start
UV_EXTERN int uv_idle_start(uv_idle_t *idle, uv_idle_cb cb)
server_handle
static uv_tcp_t server_handle
Definition: test-tcp-oob.c:31
uv_os_fd_t
int uv_os_fd_t
Definition: unix.h:128
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: test-tcp-oob.c:39
file_has_no_tests
int file_has_no_tests
Definition: test-fs-fd-hash.c:131
uv_idle_s
Definition: uv.h:824
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
uv_tcp_nodelay
UV_EXTERN int uv_tcp_nodelay(uv_tcp_t *handle, int enable)
Definition: unix/tcp.c:407
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
peer_handle
static uv_tcp_t peer_handle
Definition: test-tcp-oob.c:33
uv_loop_s
Definition: uv.h:1767
connection_cb
static void connection_cb(uv_stream_t *handle, int status)
Definition: test-tcp-oob.c:83
absl::status_internal::storage
static ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES absl::base_internal::AtomicHook< StatusPayloadPrinter > storage
Definition: abseil-cpp/absl/status/status_payload_printer.cc:26
client_handle
static uv_tcp_t client_handle
Definition: test-tcp-oob.c:32
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
errno.h


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