1 /*- 2 * Copyright (c) 2006 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 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. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/socket.h> 37 #include <sys/sysctl.h> 38 39 #include <netinet/in.h> 40 41 #include <err.h> 42 #include <errno.h> 43 #include <signal.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #define TCP_PORT 9005 50 51 static int 52 tcp_drop(struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote) 53 { 54 struct sockaddr_storage addrs[2]; 55 56 /* 57 * Sysctl accepts an array of two sockaddr's, the first being the 58 * 'foreign' sockaddr, the second being the 'local' sockaddr. 59 */ 60 61 bcopy(sin_remote, &addrs[0], sizeof(*sin_remote)); 62 bcopy(sin_local, &addrs[1], sizeof(*sin_local)); 63 64 return (sysctlbyname("net.inet.tcp.drop", NULL, 0, addrs, 65 sizeof(addrs))); 66 } 67 68 69 static void 70 tcp_server(pid_t partner) 71 { 72 int error, listen_fd, accept_fd; 73 struct sockaddr_in sin; 74 75 listen_fd = socket(PF_INET, SOCK_STREAM, 0); 76 if (listen_fd < 0) { 77 error = errno; 78 (void)kill(partner, SIGKILL); 79 errno = error; 80 err(-1, "tcp_server: socket"); 81 } 82 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); 88 89 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 90 error = errno; 91 (void)kill(partner, SIGKILL); 92 errno = error; 93 err(-1, "tcp_server: bind"); 94 } 95 96 if (listen(listen_fd, -1) < 0) { 97 error = errno; 98 (void)kill(partner, SIGKILL); 99 errno = error; 100 err(-1, "tcp_server: listen"); 101 } 102 103 sleep(10); 104 105 accept_fd = accept(listen_fd, NULL, NULL); 106 if (accept_fd < 0) { 107 error = errno; 108 (void)kill(partner, SIGKILL); 109 errno = error; 110 err(-1, "tcp_server: accept"); 111 } 112 close(accept_fd); 113 close(listen_fd); 114 } 115 116 static void 117 tcp_client(pid_t partner, int dropflag) 118 { 119 struct sockaddr_in sin, sin_local; 120 int error, sock; 121 socklen_t slen; 122 123 sleep(1); 124 125 sock = socket(PF_INET, SOCK_STREAM, 0); 126 if (sock < 0) { 127 error = errno; 128 (void)kill(partner, SIGKILL); 129 errno = error; 130 err(-1, "socket"); 131 } 132 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); 138 139 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 140 error = errno; 141 (void)kill(partner, SIGKILL); 142 errno = error; 143 err(-1, "connect"); 144 } 145 146 slen = sizeof(sin_local); 147 if (getsockname(sock, (struct sockaddr *)&sin_local, &slen) < 0) { 148 error = errno; 149 (void)kill(partner, SIGKILL); 150 errno = error; 151 err(-1, "getsockname"); 152 } 153 154 if (dropflag) { 155 if (tcp_drop(&sin_local, &sin) < 0) { 156 error = errno; 157 (void)kill(partner, SIGKILL); 158 errno = error; 159 err(-1, "tcp_drop"); 160 } 161 sleep(2); 162 } 163 close(sock); 164 } 165 166 int 167 main(int argc, char *argv[]) 168 { 169 pid_t child_pid, parent_pid; 170 171 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) 172 err(-1, "signal"); 173 174 parent_pid = getpid(); 175 child_pid = fork(); 176 if (child_pid < 0) 177 err(-1, "fork"); 178 if (child_pid == 0) { 179 child_pid = getpid(); 180 tcp_server(parent_pid); 181 return (0); 182 } else 183 tcp_client(child_pid, 0); 184 (void)kill(child_pid, SIGTERM); 185 186 sleep(5); 187 188 parent_pid = getpid(); 189 child_pid = fork(); 190 if (child_pid < 0) 191 err(-1, "fork"); 192 if (child_pid == 0) { 193 child_pid = getpid(); 194 tcp_server(parent_pid); 195 return (0); 196 } else 197 tcp_client(child_pid, 1); 198 (void)kill(child_pid, SIGTERM); 199 200 return (0); 201 } 202