2 * Copyright (c) 2006 Robert N. M. Watson
3 * Copyright (c) 2011 Juniper Networks, Inc.
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to Juniper Networks, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * TCP regression test for the tcpdrop sysctl; build a loopback TCP
35 * connection, drop it, and make sure both endpoints return that the
36 * connection has been reset.
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
43 #include <netinet/in.h>
54 tcp_drop(struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote)
56 struct sockaddr_storage addrs[2];
59 * Sysctl accepts an array of two sockaddr's, the first being the
60 * 'foreign' sockaddr, the second being the 'local' sockaddr.
63 bcopy(sin_remote, &addrs[0], sizeof(*sin_remote));
64 bcopy(sin_local, &addrs[1], sizeof(*sin_local));
66 return (sysctlbyname("net.inet.tcp.drop", NULL, 0, addrs,
71 tcp_server(pid_t partner, int listen_fd)
77 accept_fd = accept(listen_fd, NULL, NULL);
80 (void)kill(partner, SIGTERM);
82 err(-1, "tcp_server: accept");
86 * Send one byte, make sure that worked, wait for the drop, and try
87 * sending another. By sending small amounts, we avoid blocking
88 * waiting on the remote buffer to be drained.
91 len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);
94 (void)kill(partner, SIGTERM);
96 err(-1, "tcp_server: send (1)");
98 if (len != sizeof(ch)) {
99 (void)kill(partner, SIGTERM);
100 errx(-1, "tcp_server: send (1) len");
106 len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);
108 (void)kill(partner, SIGTERM);
109 errx(-1, "tcp_server: send (2): success");
110 } else if (errno != EPIPE) {
112 (void)kill(partner, SIGTERM);
114 err(-1, "tcp_server: send (2)");
122 tcp_client(pid_t partner, u_short port)
124 struct sockaddr_in sin, sin_local;
132 sock = socket(PF_INET, SOCK_STREAM, 0);
135 (void)kill(partner, SIGTERM);
140 bzero(&sin, sizeof(sin));
141 sin.sin_family = AF_INET;
142 sin.sin_len = sizeof(sin);
143 sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
146 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
148 (void)kill(partner, SIGTERM);
153 slen = sizeof(sin_local);
154 if (getsockname(sock, (struct sockaddr *)&sin_local, &slen) < 0) {
156 (void)kill(partner, SIGTERM);
158 err(-1, "getsockname");
162 * Send one byte, make sure that worked, wait for the drop, and try
163 * sending another. By sending small amounts, we avoid blocking
164 * waiting on the remote buffer to be drained.
167 len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL);
170 (void)kill(partner, SIGTERM);
172 err(-1, "tcp_client: send (1)");
174 if (len != sizeof(ch)) {
175 (void)kill(partner, SIGTERM);
176 errx(-1, "tcp_client: send (1) len");
180 if (tcp_drop(&sin_local, &sin) < 0) {
182 (void)kill(partner, SIGTERM);
184 err(-1, "tcp_client: tcp_drop");
189 len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL);
191 (void)kill(partner, SIGTERM);
192 errx(-1, "tcp_client: send (2): success");
193 } else if (errno != EPIPE) {
195 (void)kill(partner, SIGTERM);
197 err(-1, "tcp_client: send (2)");
203 main(int argc, char *argv[])
205 pid_t child_pid, parent_pid;
206 struct sockaddr_in sin;
211 listen_fd = socket(PF_INET, SOCK_STREAM, 0);
216 * We use the loopback, but let the kernel select a port for the
219 bzero(&sin, sizeof(sin));
220 sin.sin_family = AF_INET;
221 sin.sin_len = sizeof(sin);
222 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
224 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
227 if (listen(listen_fd, -1) < 0)
231 * Query the port so that the client can use it.
233 bzero(&sin, sizeof(sin));
234 sin.sin_family = AF_INET;
235 sin.sin_len = sizeof(sin);
237 if (getsockname(listen_fd, (struct sockaddr *)&sin, &len) < 0)
238 err(-1, "getsockname");
240 printf("Using port %d\n", ntohs(port));
242 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
245 parent_pid = getpid();
249 if (child_pid == 0) {
250 child_pid = getpid();
251 tcp_server(parent_pid, listen_fd);
253 tcp_client(child_pid, port);