xref: /freebsd/tools/regression/netinet/tcpfullwindowrst/tcpfullwindowrsttest.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /*
2 Copyright 2004 Michiel Boland.  All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 */
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <fcntl.h>
33 #include <poll.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 /*
41  * The following code sets up two connected TCP sockets that send data to each
42  * other until the window is closed. Then one of the sockets is closed, which
43  * will generate a RST once the TCP at the other socket does a window probe.
44  *
45  * All versions of FreeBSD prior to 11/26/2004 will ignore this RST into a 0
46  * window, causing the connection (and application) to hang indefinitely.
47  * On patched versions of FreeBSD (and other operating systems), the RST
48  * will be accepted and the program will exit in a few seconds.
49  */
50 
51 /*
52  * If the alarm fired then we've hung and the test failed.
53  */
54 void
55 do_alrm(int s)
56 {
57 	printf("not ok 1 - tcpfullwindowrst\n");
58 	exit(0);
59 }
60 
61 int
62 main(void)
63 {
64 	int o, s, t, u, do_t, do_u;
65 	struct pollfd pfd[2];
66 	struct sockaddr_in sa;
67 	char buf[4096];
68 
69 	printf("1..1\n");
70 	signal(SIGALRM, do_alrm);
71 	alarm(20);
72 
73 	s = socket(AF_INET, SOCK_STREAM, 0);
74 	if (s == -1)
75 		return 1;
76 	o = 1;
77 	setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &o, sizeof o);
78 	memset(&sa, 0, sizeof sa);
79 	sa.sin_family = AF_INET;
80 	sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
81 	sa.sin_port = htons(3737);
82 	if (bind(s, (struct sockaddr *) &sa, sizeof sa) == -1)
83 		return 1;
84 	if (listen(s, 1) == -1)
85 		return 1;
86 	t = socket(AF_INET, SOCK_STREAM, 0);
87 	if (t == -1)
88 		return 1;
89 	if (connect(t, (struct sockaddr *) &sa, sizeof sa) == -1)
90 		return 1;
91 	u = accept(s, 0, 0);
92 	if (u == -1)
93 		return 1;
94 	close(s);
95 	fcntl(t, F_SETFL, fcntl(t, F_GETFL) | O_NONBLOCK);
96 	fcntl(u, F_SETFL, fcntl(t, F_GETFL) | O_NONBLOCK);
97 	do_t = 1;
98 	do_u = 1;
99 	pfd[0].fd = t;
100 	pfd[0].events = POLLOUT;
101 	pfd[1].fd = u;
102 	pfd[1].events = POLLOUT;
103 	while (do_t || do_u) {
104 		if (poll(pfd, 2, 1000) == 0) {
105 			if (do_t) {
106 				close(t);
107 				pfd[0].fd = -1;
108 				do_t = 0;
109 			}
110 			continue;
111 		}
112 		if (pfd[0].revents & POLLOUT) {
113 			if (write(t, buf, sizeof buf) == -1) {
114 				close(t);
115 				pfd[0].fd = -1;
116 				do_t = 0;
117 			}
118 		}
119 		if (pfd[1].revents & POLLOUT) {
120 			if (write(u, buf, sizeof buf) == -1) {
121 				close(u);
122 				pfd[1].fd = -1;
123 				do_u = 0;
124 			}
125 		}
126 	}
127 
128 	printf("ok 1 - tcpfullwindowrst\n");
129 	return 0;
130 }
131