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 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.
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,
69 tcp_server(pid_t partner)
71 int error, listen_fd, accept_fd;
72 struct sockaddr_in sin;
76 listen_fd = socket(PF_INET, SOCK_STREAM, 0);
79 (void)kill(partner, SIGTERM);
81 err(-1, "tcp_server: socket");
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);
90 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
92 (void)kill(partner, SIGTERM);
94 err(-1, "tcp_server: bind");
97 if (listen(listen_fd, -1) < 0) {
99 (void)kill(partner, SIGTERM);
101 err(-1, "tcp_server: listen");
104 accept_fd = accept(listen_fd, NULL, NULL);
107 (void)kill(partner, SIGTERM);
109 err(-1, "tcp_server: accept");
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.
118 len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);
121 (void)kill(partner, SIGTERM);
123 err(-1, "tcp_server: send (1)");
125 if (len != sizeof(ch)) {
126 (void)kill(partner, SIGTERM);
127 errx(-1, "tcp_server: send (1) len");
133 len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);
135 (void)kill(partner, SIGTERM);
136 errx(-1, "tcp_server: send (2): success");
137 } else if (errno != EPIPE) {
139 (void)kill(partner, SIGTERM);
141 err(-1, "tcp_server: send (2)");
149 tcp_client(pid_t partner)
151 struct sockaddr_in sin, sin_local;
159 sock = socket(PF_INET, SOCK_STREAM, 0);
162 (void)kill(partner, SIGTERM);
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);
173 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
175 (void)kill(partner, SIGTERM);
180 slen = sizeof(sin_local);
181 if (getsockname(sock, (struct sockaddr *)&sin_local, &slen) < 0) {
183 (void)kill(partner, SIGTERM);
185 err(-1, "getsockname");
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.
194 len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL);
197 (void)kill(partner, SIGTERM);
199 err(-1, "tcp_client: send (1)");
201 if (len != sizeof(ch)) {
202 (void)kill(partner, SIGTERM);
203 errx(-1, "tcp_client: send (1) len");
207 if (tcp_drop(&sin_local, &sin) < 0) {
209 (void)kill(partner, SIGTERM);
211 err(-1, "tcp_client: tcp_drop");
216 len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL);
218 (void)kill(partner, SIGTERM);
219 errx(-1, "tcp_client: send (2): success");
220 } else if (errno != EPIPE) {
222 (void)kill(partner, SIGTERM);
224 err(-1, "tcp_client: send (2)");
230 main(int argc, char *argv[])
232 pid_t child_pid, parent_pid;
234 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
237 parent_pid = getpid();
241 if (child_pid == 0) {
242 child_pid = getpid();
243 tcp_server(parent_pid);
245 tcp_client(child_pid);