• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdio.h>
25 #include <stdlib.h>
26 
27 typedef struct {
28   uv_write_t req;
29   uv_buf_t buf;
30 } write_req_t;
31 
32 static uv_loop_t* loop;
33 
34 static int server_closed;
35 static stream_type serverType;
36 static uv_tcp_t tcpServer;
37 static uv_udp_t udpServer;
38 static uv_pipe_t pipeServer;
39 static uv_handle_t* server;
40 static uv_udp_send_t* send_freelist;
41 
42 static void after_write(uv_write_t* req, int status);
43 static void after_read(uv_stream_t*, ssize_t nread, const uv_buf_t* buf);
44 static void on_close(uv_handle_t* peer);
45 static void on_server_close(uv_handle_t* handle);
46 static void on_connection(uv_stream_t*, int status);
47 
48 
after_write(uv_write_t * req,int status)49 static void after_write(uv_write_t* req, int status) {
50   write_req_t* wr;
51 
52   /* Free the read/write buffer and the request */
53   wr = (write_req_t*) req;
54   free(wr->buf.base);
55   free(wr);
56 
57   if (status == 0)
58     return;
59 
60   fprintf(stderr,
61           "uv_write error: %s - %s\n",
62           uv_err_name(status),
63           uv_strerror(status));
64 }
65 
66 
after_shutdown(uv_shutdown_t * req,int status)67 static void after_shutdown(uv_shutdown_t* req, int status) {
68   uv_close((uv_handle_t*) req->handle, on_close);
69   free(req);
70 }
71 
72 
on_shutdown(uv_shutdown_t * req,int status)73 static void on_shutdown(uv_shutdown_t* req, int status) {
74   free(req);
75 }
76 
77 
after_read(uv_stream_t * handle,ssize_t nread,const uv_buf_t * buf)78 static void after_read(uv_stream_t* handle,
79                        ssize_t nread,
80                        const uv_buf_t* buf) {
81   int i;
82   write_req_t *wr;
83   uv_shutdown_t* sreq;
84   int shutdown = 0;
85 
86   if (nread < 0) {
87     /* Error or EOF */
88     ASSERT_EQ(nread, UV_EOF);
89 
90     free(buf->base);
91     sreq = malloc(sizeof* sreq);
92     if (uv_is_writable(handle)) {
93       ASSERT_EQ(0, uv_shutdown(sreq, handle, after_shutdown));
94     }
95     return;
96   }
97 
98   if (nread == 0) {
99     /* Everything OK, but nothing read. */
100     free(buf->base);
101     return;
102   }
103 
104   /*
105    * Scan for the letter Q which signals that we should quit the server.
106    * If we get QS it means close the stream.
107    * If we get QSS it means shutdown the stream.
108    * If we get QSH it means disable linger before close the socket.
109    */
110   for (i = 0; i < nread; i++) {
111     if (buf->base[i] == 'Q') {
112       if (i + 1 < nread && buf->base[i + 1] == 'S') {
113         int reset = 0;
114         if (i + 2 < nread && buf->base[i + 2] == 'S')
115           shutdown = 1;
116         if (i + 2 < nread && buf->base[i + 2] == 'H')
117           reset = 1;
118         if (reset && handle->type == UV_TCP)
119           ASSERT_EQ(0, uv_tcp_close_reset((uv_tcp_t*) handle, on_close));
120         else if (shutdown)
121           break;
122         else
123           uv_close((uv_handle_t*) handle, on_close);
124         free(buf->base);
125         return;
126       } else if (!server_closed) {
127         uv_close(server, on_server_close);
128         server_closed = 1;
129       }
130     }
131   }
132 
133   wr = (write_req_t*) malloc(sizeof *wr);
134   ASSERT_NOT_NULL(wr);
135   wr->buf = uv_buf_init(buf->base, nread);
136 
137   if (uv_write(&wr->req, handle, &wr->buf, 1, after_write)) {
138     FATAL("uv_write failed");
139   }
140 
141   if (shutdown)
142     ASSERT_EQ(0, uv_shutdown(malloc(sizeof* sreq), handle, on_shutdown));
143 }
144 
145 
on_close(uv_handle_t * peer)146 static void on_close(uv_handle_t* peer) {
147   free(peer);
148 }
149 
150 
echo_alloc(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)151 static void echo_alloc(uv_handle_t* handle,
152                        size_t suggested_size,
153                        uv_buf_t* buf) {
154   buf->base = malloc(suggested_size);
155   buf->len = suggested_size;
156 }
157 
slab_alloc(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)158 static void slab_alloc(uv_handle_t* handle,
159                        size_t suggested_size,
160                        uv_buf_t* buf) {
161   /* up to 16 datagrams at once */
162   static char slab[16 * 64 * 1024];
163   buf->base = slab;
164   buf->len = sizeof(slab);
165 }
166 
on_connection(uv_stream_t * server,int status)167 static void on_connection(uv_stream_t* server, int status) {
168   uv_stream_t* stream;
169   int r;
170 
171   if (status != 0) {
172     fprintf(stderr, "Connect error %s\n", uv_err_name(status));
173   }
174   ASSERT(status == 0);
175 
176   switch (serverType) {
177   case TCP:
178     stream = malloc(sizeof(uv_tcp_t));
179     ASSERT_NOT_NULL(stream);
180     r = uv_tcp_init(loop, (uv_tcp_t*)stream);
181     ASSERT(r == 0);
182     break;
183 
184   case PIPE:
185     stream = malloc(sizeof(uv_pipe_t));
186     ASSERT_NOT_NULL(stream);
187     r = uv_pipe_init(loop, (uv_pipe_t*)stream, 0);
188     ASSERT(r == 0);
189     break;
190 
191   default:
192     ASSERT(0 && "Bad serverType");
193     abort();
194   }
195 
196   /* associate server with stream */
197   stream->data = server;
198 
199   r = uv_accept(server, stream);
200   ASSERT(r == 0);
201 
202   r = uv_read_start(stream, echo_alloc, after_read);
203   ASSERT(r == 0);
204 }
205 
206 
on_server_close(uv_handle_t * handle)207 static void on_server_close(uv_handle_t* handle) {
208   ASSERT(handle == server);
209 }
210 
send_alloc(void)211 static uv_udp_send_t* send_alloc(void) {
212   uv_udp_send_t* req = send_freelist;
213   if (req != NULL)
214     send_freelist = req->data;
215   else
216     req = malloc(sizeof(*req));
217   return req;
218 }
219 
on_send(uv_udp_send_t * req,int status)220 static void on_send(uv_udp_send_t* req, int status) {
221   ASSERT_NOT_NULL(req);
222   ASSERT(status == 0);
223   req->data = send_freelist;
224   send_freelist = req;
225 }
226 
on_recv(uv_udp_t * handle,ssize_t nread,const uv_buf_t * rcvbuf,const struct sockaddr * addr,unsigned flags)227 static void on_recv(uv_udp_t* handle,
228                     ssize_t nread,
229                     const uv_buf_t* rcvbuf,
230                     const struct sockaddr* addr,
231                     unsigned flags) {
232   uv_buf_t sndbuf;
233   uv_udp_send_t* req;
234 
235   if (nread == 0) {
236     /* Everything OK, but nothing read. */
237     return;
238   }
239 
240   ASSERT(nread > 0);
241   ASSERT(addr->sa_family == AF_INET);
242 
243   req = send_alloc();
244   ASSERT_NOT_NULL(req);
245   sndbuf = uv_buf_init(rcvbuf->base, nread);
246   ASSERT(0 <= uv_udp_send(req, handle, &sndbuf, 1, addr, on_send));
247 }
248 
tcp4_echo_start(int port)249 static int tcp4_echo_start(int port) {
250   struct sockaddr_in addr;
251   int r;
252 
253   ASSERT(0 == uv_ip4_addr("127.0.0.1", port, &addr));
254 
255   server = (uv_handle_t*)&tcpServer;
256   serverType = TCP;
257 
258   r = uv_tcp_init(loop, &tcpServer);
259   if (r) {
260     /* TODO: Error codes */
261     fprintf(stderr, "Socket creation error\n");
262     return 1;
263   }
264 
265   r = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &addr, 0);
266   if (r) {
267     /* TODO: Error codes */
268     fprintf(stderr, "Bind error\n");
269     return 1;
270   }
271 
272   r = uv_listen((uv_stream_t*)&tcpServer, SOMAXCONN, on_connection);
273   if (r) {
274     /* TODO: Error codes */
275     fprintf(stderr, "Listen error %s\n", uv_err_name(r));
276     return 1;
277   }
278 
279   return 0;
280 }
281 
282 
tcp6_echo_start(int port)283 static int tcp6_echo_start(int port) {
284   struct sockaddr_in6 addr6;
285   int r;
286 
287   ASSERT(0 == uv_ip6_addr("::1", port, &addr6));
288 
289   server = (uv_handle_t*)&tcpServer;
290   serverType = TCP;
291 
292   r = uv_tcp_init(loop, &tcpServer);
293   if (r) {
294     /* TODO: Error codes */
295     fprintf(stderr, "Socket creation error\n");
296     return 1;
297   }
298 
299   /* IPv6 is optional as not all platforms support it */
300   r = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &addr6, 0);
301   if (r) {
302     /* show message but return OK */
303     fprintf(stderr, "IPv6 not supported\n");
304     return 0;
305   }
306 
307   r = uv_listen((uv_stream_t*)&tcpServer, SOMAXCONN, on_connection);
308   if (r) {
309     /* TODO: Error codes */
310     fprintf(stderr, "Listen error\n");
311     return 1;
312   }
313 
314   return 0;
315 }
316 
317 
udp4_echo_start(int port)318 static int udp4_echo_start(int port) {
319   struct sockaddr_in addr;
320   int r;
321 
322   ASSERT(0 == uv_ip4_addr("127.0.0.1", port, &addr));
323   server = (uv_handle_t*)&udpServer;
324   serverType = UDP;
325 
326   r = uv_udp_init(loop, &udpServer);
327   if (r) {
328     fprintf(stderr, "uv_udp_init: %s\n", uv_strerror(r));
329     return 1;
330   }
331 
332   r = uv_udp_bind(&udpServer, (const struct sockaddr*) &addr, 0);
333   if (r) {
334     fprintf(stderr, "uv_udp_bind: %s\n", uv_strerror(r));
335     return 1;
336   }
337 
338   r = uv_udp_recv_start(&udpServer, slab_alloc, on_recv);
339   if (r) {
340     fprintf(stderr, "uv_udp_recv_start: %s\n", uv_strerror(r));
341     return 1;
342   }
343 
344   return 0;
345 }
346 
347 
pipe_echo_start(char * pipeName)348 static int pipe_echo_start(char* pipeName) {
349   int r;
350 
351 #ifndef _WIN32
352   {
353     uv_fs_t req;
354     uv_fs_unlink(NULL, &req, pipeName, NULL);
355     uv_fs_req_cleanup(&req);
356   }
357 #endif
358 
359   server = (uv_handle_t*)&pipeServer;
360   serverType = PIPE;
361 
362   r = uv_pipe_init(loop, &pipeServer, 0);
363   if (r) {
364     fprintf(stderr, "uv_pipe_init: %s\n", uv_strerror(r));
365     return 1;
366   }
367 
368   r = uv_pipe_bind(&pipeServer, pipeName);
369   if (r) {
370     fprintf(stderr, "uv_pipe_bind: %s\n", uv_strerror(r));
371     return 1;
372   }
373 
374   r = uv_listen((uv_stream_t*)&pipeServer, SOMAXCONN, on_connection);
375   if (r) {
376     fprintf(stderr, "uv_pipe_listen: %s\n", uv_strerror(r));
377     return 1;
378   }
379 
380   return 0;
381 }
382 
383 
HELPER_IMPL(tcp4_echo_server)384 HELPER_IMPL(tcp4_echo_server) {
385   loop = uv_default_loop();
386 
387   if (tcp4_echo_start(TEST_PORT))
388     return 1;
389 
390   notify_parent_process();
391   uv_run(loop, UV_RUN_DEFAULT);
392   return 0;
393 }
394 
395 
HELPER_IMPL(tcp6_echo_server)396 HELPER_IMPL(tcp6_echo_server) {
397   loop = uv_default_loop();
398 
399   if (tcp6_echo_start(TEST_PORT))
400     return 1;
401 
402   notify_parent_process();
403   uv_run(loop, UV_RUN_DEFAULT);
404   return 0;
405 }
406 
407 
HELPER_IMPL(pipe_echo_server)408 HELPER_IMPL(pipe_echo_server) {
409   loop = uv_default_loop();
410 
411   if (pipe_echo_start(TEST_PIPENAME))
412     return 1;
413 
414   notify_parent_process();
415   uv_run(loop, UV_RUN_DEFAULT);
416   return 0;
417 }
418 
419 
HELPER_IMPL(udp4_echo_server)420 HELPER_IMPL(udp4_echo_server) {
421   loop = uv_default_loop();
422 
423   if (udp4_echo_start(TEST_PORT))
424     return 1;
425 
426   notify_parent_process();
427   uv_run(loop, UV_RUN_DEFAULT);
428   return 0;
429 }
430