2 * Copyright (c) 2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * TCP regression test that opens a loopback TCP session, then closes one end
31 * while shutting down the other. This triggers an unusual TCP stack case in
32 * which an open file descriptor / socket is associated with a closed TCP
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
51 tcp_server(pid_t partner)
53 int error, listen_fd, accept_fd;
54 struct sockaddr_in sin;
56 listen_fd = socket(PF_INET, SOCK_STREAM, 0);
59 (void)kill(partner, SIGTERM);
61 err(-1, "tcp_server: socket");
64 bzero(&sin, sizeof(sin));
65 sin.sin_family = AF_INET;
66 sin.sin_len = sizeof(sin);
67 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
68 sin.sin_port = htons(TCP_PORT);
70 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
72 (void)kill(partner, SIGTERM);
74 err(-1, "tcp_server: bind");
77 if (listen(listen_fd, -1) < 0) {
79 (void)kill(partner, SIGTERM);
81 err(-1, "tcp_server: listen");
84 accept_fd = accept(listen_fd, NULL, NULL);
87 (void)kill(partner, SIGTERM);
89 err(-1, "tcp_server: accept");
96 tcp_client(pid_t partner, int secs)
98 struct sockaddr_in sin;
103 sock = socket(PF_INET, SOCK_STREAM, 0);
106 (void)kill(partner, SIGTERM);
111 bzero(&sin, sizeof(sin));
112 sin.sin_family = AF_INET;
113 sin.sin_len = sizeof(sin);
114 sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
115 sin.sin_port = htons(TCP_PORT);
117 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
119 (void)kill(partner, SIGTERM);
124 if (shutdown(sock, SHUT_RDWR) < 0) {
126 (void)kill(partner, SIGTERM);
136 main(int argc, char *argv[])
138 pid_t child_pid, parent_pid;
140 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
144 * Run the whole thing twice: once, with a short sleep in the client,
145 * so that we close before time wait runs out, and once with a long
146 * sleep so that the time wait terminates while the socket is open.
148 parent_pid = getpid();
152 if (child_pid == 0) {
153 child_pid = getpid();
154 tcp_server(child_pid);
157 tcp_client(parent_pid, 1);
158 (void)kill(child_pid, SIGTERM);
161 parent_pid = getpid();
165 if (child_pid == 0) {
166 child_pid = getpid();
167 tcp_server(parent_pid);
169 tcp_client(child_pid, 800);