|
160
|
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
|
|
|
31 static uv_tcp_t server_handle;
|
|
|
32 static uv_tcp_t client_handle;
|
|
|
33 static uv_tcp_t peer_handle;
|
|
|
34 static uv_idle_t idle;
|
|
|
35 static uv_connect_t connect_req;
|
|
|
36 static int ticks;
|
|
|
37 static const int kMaxTicks = 10;
|
|
|
38
|
|
|
39 static void alloc_cb(uv_handle_t* handle,
|
|
|
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
|
|
|
51 uv_close((uv_handle_t*) &server_handle, NULL);
|
|
|
52 uv_close((uv_handle_t*) &client_handle, NULL);
|
|
|
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_GE(nread, 0);
|
|
|
65 ASSERT_OK(uv_fileno((uv_handle_t*)handle, &fd));
|
|
|
66 ASSERT_OK(uv_idle_start(&idle, idle_cb));
|
|
|
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_NE(-1, recv(fd, lbuf, sizeof(lbuf), MSG_OOB));
|
|
|
73 #endif
|
|
|
74 }
|
|
|
75
|
|
|
76
|
|
|
77 static void connect_cb(uv_connect_t* req, int status) {
|
|
|
78 ASSERT_PTR_EQ(req->handle, (uv_stream_t*) &client_handle);
|
|
|
79 ASSERT_OK(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_OK(status);
|
|
|
88 ASSERT_OK(uv_accept(handle, (uv_stream_t*) &peer_handle));
|
|
|
89 ASSERT_OK(uv_read_start((uv_stream_t*) &peer_handle, alloc_cb, read_cb));
|
|
|
90
|
|
|
91 /* Send some OOB data */
|
|
|
92 ASSERT_OK(uv_fileno((uv_handle_t*) &client_handle, &fd));
|
|
|
93
|
|
|
94 ASSERT_OK(uv_stream_set_blocking((uv_stream_t*) &client_handle, 1));
|
|
|
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_EQ(5, r);
|
|
|
103
|
|
|
104 do {
|
|
|
105 r = send(fd, "hello", 5, MSG_OOB);
|
|
|
106 } while (r < 0 && errno == EINTR);
|
|
|
107 ASSERT_EQ(5, r);
|
|
|
108
|
|
|
109 ASSERT_OK(uv_stream_set_blocking((uv_stream_t*) &client_handle, 0));
|
|
|
110 }
|
|
|
111
|
|
|
112
|
|
|
113 TEST_IMPL(tcp_oob) {
|
|
|
114 struct sockaddr_in addr;
|
|
|
115 uv_loop_t* loop;
|
|
|
116
|
|
|
117 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
|
|
118 loop = uv_default_loop();
|
|
|
119
|
|
|
120 ASSERT_OK(uv_tcp_init(loop, &server_handle));
|
|
|
121 ASSERT_OK(uv_tcp_init(loop, &client_handle));
|
|
|
122 ASSERT_OK(uv_tcp_init(loop, &peer_handle));
|
|
|
123 ASSERT_OK(uv_idle_init(loop, &idle));
|
|
|
124 ASSERT_OK(uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
|
|
|
125 ASSERT_OK(uv_listen((uv_stream_t*) &server_handle, 1, connection_cb));
|
|
|
126
|
|
|
127 /* Ensure two separate packets */
|
|
|
128 ASSERT_OK(uv_tcp_nodelay(&client_handle, 1));
|
|
|
129
|
|
|
130 ASSERT_OK(uv_tcp_connect(&connect_req,
|
|
|
131 &client_handle,
|
|
|
132 (const struct sockaddr*) &addr,
|
|
|
133 connect_cb));
|
|
|
134 ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
|
|
|
135
|
|
|
136 ASSERT_EQ(ticks, kMaxTicks);
|
|
|
137
|
|
|
138 MAKE_VALGRIND_HAPPY(loop);
|
|
|
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 */
|