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 which opens a loopback TCP session, and closes it
31 * before the remote endpoint (server) can accept it. Run the test twice,
32 * once using an explicit close() from the client, a second using a tcp drop.
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
39 #include <netinet/in.h>
52 tcp_drop(struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote)
54 struct sockaddr_storage addrs[2];
57 * Sysctl accepts an array of two sockaddr's, the first being the
58 * 'foreign' sockaddr, the second being the 'local' sockaddr.
61 bcopy(sin_remote, &addrs[0], sizeof(*sin_remote));
62 bcopy(sin_local, &addrs[1], sizeof(*sin_local));
64 return (sysctlbyname("net.inet.tcp.drop", NULL, 0, addrs,
70 tcp_server(pid_t partner)
72 int error, listen_fd, accept_fd;
73 struct sockaddr_in sin;
75 listen_fd = socket(PF_INET, SOCK_STREAM, 0);
78 (void)kill(partner, SIGKILL);
80 err(-1, "tcp_server: socket");
83 bzero(&sin, sizeof(sin));
84 sin.sin_family = AF_INET;
85 sin.sin_len = sizeof(sin);
86 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
87 sin.sin_port = htons(TCP_PORT);
89 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
91 (void)kill(partner, SIGKILL);
93 err(-1, "tcp_server: bind");
96 if (listen(listen_fd, -1) < 0) {
98 (void)kill(partner, SIGKILL);
100 err(-1, "tcp_server: listen");
105 accept_fd = accept(listen_fd, NULL, NULL);
108 (void)kill(partner, SIGKILL);
110 err(-1, "tcp_server: accept");
117 tcp_client(pid_t partner, int dropflag)
119 struct sockaddr_in sin, sin_local;
125 sock = socket(PF_INET, SOCK_STREAM, 0);
128 (void)kill(partner, SIGKILL);
133 bzero(&sin, sizeof(sin));
134 sin.sin_family = AF_INET;
135 sin.sin_len = sizeof(sin);
136 sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
137 sin.sin_port = htons(TCP_PORT);
139 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
141 (void)kill(partner, SIGKILL);
146 slen = sizeof(sin_local);
147 if (getsockname(sock, (struct sockaddr *)&sin_local, &slen) < 0) {
149 (void)kill(partner, SIGKILL);
151 err(-1, "getsockname");
155 if (tcp_drop(&sin_local, &sin) < 0) {
157 (void)kill(partner, SIGKILL);
167 main(int argc, char *argv[])
169 pid_t child_pid, parent_pid;
171 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
174 parent_pid = getpid();
178 if (child_pid == 0) {
179 child_pid = getpid();
180 tcp_server(parent_pid);
183 tcp_client(child_pid, 0);
184 (void)kill(child_pid, SIGTERM);
188 parent_pid = getpid();
192 if (child_pid == 0) {
193 child_pid = getpid();
194 tcp_server(parent_pid);
197 tcp_client(child_pid, 1);
198 (void)kill(child_pid, SIGTERM);