|
160
|
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
|
|
|
28 typedef struct {
|
|
|
29 uv_tcp_t handle;
|
|
|
30 uv_shutdown_t shutdown_req;
|
|
|
31 } conn_rec;
|
|
|
32
|
|
|
33 static uv_tcp_t tcp_server;
|
|
|
34
|
|
|
35 static void connection_cb(uv_stream_t* stream, int status);
|
|
|
36 static void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
|
|
|
37 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
|
|
|
38 static void shutdown_cb(uv_shutdown_t* req, int status);
|
|
|
39 static void close_cb(uv_handle_t* handle);
|
|
|
40
|
|
|
41
|
|
|
42 static void connection_cb(uv_stream_t* stream, int status) {
|
|
|
43 conn_rec* conn;
|
|
|
44 int r;
|
|
|
45
|
|
|
46 ASSERT_OK(status);
|
|
|
47 ASSERT_PTR_EQ(stream, (uv_stream_t*)&tcp_server);
|
|
|
48
|
|
|
49 conn = malloc(sizeof *conn);
|
|
|
50 ASSERT_NOT_NULL(conn);
|
|
|
51
|
|
|
52 r = uv_tcp_init(stream->loop, &conn->handle);
|
|
|
53 ASSERT_OK(r);
|
|
|
54
|
|
|
55 r = uv_accept(stream, (uv_stream_t*)&conn->handle);
|
|
|
56 ASSERT_OK(r);
|
|
|
57
|
|
|
58 r = uv_read_start((uv_stream_t*)&conn->handle, alloc_cb, read_cb);
|
|
|
59 ASSERT_OK(r);
|
|
|
60 }
|
|
|
61
|
|
|
62
|
|
|
63 static void alloc_cb(uv_handle_t* handle,
|
|
|
64 size_t suggested_size,
|
|
|
65 uv_buf_t* buf) {
|
|
|
66 static char slab[65536];
|
|
|
67 buf->base = slab;
|
|
|
68 buf->len = sizeof(slab);
|
|
|
69 }
|
|
|
70
|
|
|
71
|
|
|
72 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
|
|
|
73 conn_rec* conn;
|
|
|
74 int r;
|
|
|
75
|
|
|
76 if (nread >= 0)
|
|
|
77 return;
|
|
|
78
|
|
|
79 ASSERT_EQ(nread, UV_EOF);
|
|
|
80
|
|
|
81 conn = container_of(stream, conn_rec, handle);
|
|
|
82
|
|
|
83 r = uv_shutdown(&conn->shutdown_req, stream, shutdown_cb);
|
|
|
84 ASSERT_OK(r);
|
|
|
85 }
|
|
|
86
|
|
|
87
|
|
|
88 static void shutdown_cb(uv_shutdown_t* req, int status) {
|
|
|
89 conn_rec* conn = container_of(req, conn_rec, shutdown_req);
|
|
|
90 uv_close((uv_handle_t*)&conn->handle, close_cb);
|
|
|
91 }
|
|
|
92
|
|
|
93
|
|
|
94 static void close_cb(uv_handle_t* handle) {
|
|
|
95 conn_rec* conn = container_of(handle, conn_rec, handle);
|
|
|
96 free(conn);
|
|
|
97 }
|
|
|
98
|
|
|
99
|
|
|
100 HELPER_IMPL(tcp4_blackhole_server) {
|
|
|
101 struct sockaddr_in addr;
|
|
|
102 uv_loop_t* loop;
|
|
|
103 int r;
|
|
|
104
|
|
|
105 loop = uv_default_loop();
|
|
|
106 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
|
|
107
|
|
|
108 r = uv_tcp_init(loop, &tcp_server);
|
|
|
109 ASSERT_OK(r);
|
|
|
110
|
|
|
111 r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
|
|
|
112 ASSERT_OK(r);
|
|
|
113
|
|
|
114 r = uv_listen((uv_stream_t*)&tcp_server, 128, connection_cb);
|
|
|
115 ASSERT_OK(r);
|
|
|
116
|
|
|
117 notify_parent_process();
|
|
|
118 r = uv_run(loop, UV_RUN_DEFAULT);
|
|
|
119 ASSERT(0 && "Blackhole server dropped out of event loop.");
|
|
|
120
|
|
|
121 return 0;
|
|
|
122 }
|