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 for the tcpdrop sysctl; build a loopback TCP 31 * connection, drop it, and make sure both endpoints return that the 32 * connection has been reset. 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 9001 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 static void 69 tcp_server(pid_t partner) 70 { 71 int error, listen_fd, accept_fd; 72 struct sockaddr_in sin; 73 ssize_t len; 74 char ch; 75 76 listen_fd = socket(PF_INET, SOCK_STREAM, 0); 77 if (listen_fd < 0) { 78 error = errno; 79 (void)kill(partner, SIGTERM); 80 errno = error; 81 err(-1, "tcp_server: socket"); 82 } 83 84 bzero(&sin, sizeof(sin)); 85 sin.sin_family = AF_INET; 86 sin.sin_len = sizeof(sin); 87 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 88 sin.sin_port = htons(TCP_PORT); 89 90 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 91 error = errno; 92 (void)kill(partner, SIGTERM); 93 errno = error; 94 err(-1, "tcp_server: bind"); 95 } 96 97 if (listen(listen_fd, -1) < 0) { 98 error = errno; 99 (void)kill(partner, SIGTERM); 100 errno = error; 101 err(-1, "tcp_server: listen"); 102 } 103 104 accept_fd = accept(listen_fd, NULL, NULL); 105 if (accept_fd < 0) { 106 error = errno; 107 (void)kill(partner, SIGTERM); 108 errno = error; 109 err(-1, "tcp_server: accept"); 110 } 111 112 /* 113 * Send one byte, make sure that worked, wait for the drop, and try 114 * sending another. By sending small amounts, we avoid blocking 115 * waiting on the remote buffer to be drained. 116 */ 117 ch = 'A'; 118 len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL); 119 if (len < 0) { 120 error = errno; 121 (void)kill(partner, SIGTERM); 122 errno = error; 123 err(-1, "tcp_server: send (1)"); 124 } 125 if (len != sizeof(ch)) { 126 (void)kill(partner, SIGTERM); 127 errx(-1, "tcp_server: send (1) len"); 128 } 129 130 sleep (10); 131 132 ch = 'A'; 133 len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL); 134 if (len >= 0) { 135 (void)kill(partner, SIGTERM); 136 errx(-1, "tcp_server: send (2): success"); 137 } else if (errno != EPIPE) { 138 error = errno; 139 (void)kill(partner, SIGTERM); 140 errno = error; 141 err(-1, "tcp_server: send (2)"); 142 } 143 144 close(accept_fd); 145 close(listen_fd); 146 } 147 148 static void 149 tcp_client(pid_t partner) 150 { 151 struct sockaddr_in sin, sin_local; 152 int error, sock; 153 socklen_t slen; 154 ssize_t len; 155 char ch; 156 157 sleep(1); 158 159 sock = socket(PF_INET, SOCK_STREAM, 0); 160 if (sock < 0) { 161 error = errno; 162 (void)kill(partner, SIGTERM); 163 errno = error; 164 err(-1, "socket"); 165 } 166 167 bzero(&sin, sizeof(sin)); 168 sin.sin_family = AF_INET; 169 sin.sin_len = sizeof(sin); 170 sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK); 171 sin.sin_port = htons(TCP_PORT); 172 173 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 174 error = errno; 175 (void)kill(partner, SIGTERM); 176 errno = error; 177 err(-1, "connect"); 178 } 179 180 slen = sizeof(sin_local); 181 if (getsockname(sock, (struct sockaddr *)&sin_local, &slen) < 0) { 182 error = errno; 183 (void)kill(partner, SIGTERM); 184 errno = error; 185 err(-1, "getsockname"); 186 } 187 188 /* 189 * Send one byte, make sure that worked, wait for the drop, and try 190 * sending another. By sending small amounts, we avoid blocking 191 * waiting on the remote buffer to be drained. 192 */ 193 ch = 'A'; 194 len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL); 195 if (len < 0) { 196 error = errno; 197 (void)kill(partner, SIGTERM); 198 errno = error; 199 err(-1, "tcp_client: send (1)"); 200 } 201 if (len != sizeof(ch)) { 202 (void)kill(partner, SIGTERM); 203 errx(-1, "tcp_client: send (1) len"); 204 } 205 206 sleep(5); 207 if (tcp_drop(&sin_local, &sin) < 0) { 208 error = errno; 209 (void)kill(partner, SIGTERM); 210 errno = error; 211 err(-1, "tcp_client: tcp_drop"); 212 } 213 sleep(5); 214 215 ch = 'A'; 216 len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL); 217 if (len >= 0) { 218 (void)kill(partner, SIGTERM); 219 errx(-1, "tcp_client: send (2): success"); 220 } else if (errno != EPIPE) { 221 error = errno; 222 (void)kill(partner, SIGTERM); 223 errno = error; 224 err(-1, "tcp_client: send (2)"); 225 } 226 close(sock); 227 } 228 229 int 230 main(int argc, char *argv[]) 231 { 232 pid_t child_pid, parent_pid; 233 234 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) 235 err(-1, "signal"); 236 237 parent_pid = getpid(); 238 child_pid = fork(); 239 if (child_pid < 0) 240 err(-1, "fork"); 241 if (child_pid == 0) { 242 child_pid = getpid(); 243 tcp_server(parent_pid); 244 } else 245 tcp_client(child_pid); 246 247 return (0); 248 } 249