1
/* Copyright (c) 2015, Santiago Gimeno <santiago.gimeno@gmail.com>
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:
10
* The above copyright notice and this permission notice shall be included in
11
* all copies or substantial portions of the Software.
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
28
static uv_tcp_t tcp_server;
29
static uv_tcp_t tcp_client;
30
static uv_tcp_t tcp_server_client;
31
static uv_connect_t connect_req;
32
static uv_write_t write_req;
34
static void alloc_cb(uv_handle_t* handle,
37
buf->base = malloc(size);
41
static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
43
ASSERT(nread == UV_EOF);
46
static void on_connect(uv_connect_t* req, int status) {
53
outbuf = uv_buf_init("ping", 4);
54
r = uv_write(&write_req, (uv_stream_t*) req->handle, &outbuf, 1, NULL);
57
r = uv_read_start((uv_stream_t*) req->handle, alloc_cb, read_cb);
61
static void on_connection(uv_stream_t* server, int status) {
66
r = uv_tcp_init(uv_default_loop(), &tcp_server_client);
69
r = uv_accept(server, (uv_stream_t*) &tcp_server_client);
72
uv_close((uv_handle_t*) &tcp_server_client, NULL);
73
uv_close((uv_handle_t*) &tcp_server, NULL);
76
static void start_server(void) {
77
struct sockaddr_in addr;
80
ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
82
r = uv_tcp_init(uv_default_loop(), &tcp_server);
85
r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
88
r = uv_listen((uv_stream_t*) &tcp_server, SOMAXCONN, on_connection);
92
static void start_client(void) {
93
struct sockaddr_in addr;
96
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
98
r = uv_tcp_init(uv_default_loop(), &tcp_client);
101
r = uv_tcp_connect(&connect_req,
103
(const struct sockaddr*) &addr,
109
TEST_IMPL(tcp_squelch_connreset) {
115
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
117
MAKE_VALGRIND_HAPPY();