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 $FreeBSD$
28 
29 */
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <fcntl.h>
35 #include <poll.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 /*
43  * The following code sets up two connected TCP sockets that send data to each
44  * other until the window is closed. Then one of the sockets is closed, which
45  * will generate a RST once the TCP at the other socket does a window probe.
46  *
47  * All versions of FreeBSD prior to 11/26/2004 will ignore this RST into a 0
48  * window, causing the connection (and application) to hang indefinitely.
49  * On patched versions of FreeBSD (and other operating systems), the RST
50  * will be accepted and the program will exit in a few seconds.
51  */
52 
53 /*
54  * If the alarm fired then we've hung and the test failed.
55  */
56 void
57 do_alrm(int s)
58 {
59 	printf("not ok 1 - tcpfullwindowrst\n");
60 	exit(0);
61 }
62 
63 int
64 main(void)
65 {
66 	int o, s, t, u, do_t, do_u;
67 	struct pollfd pfd[2];
68 	struct sockaddr_in sa;
69 	char buf[4096];
70 
71 	printf("1..1\n");
72 	signal(SIGALRM, do_alrm);
73 	alarm(20);
74 
75 	s = socket(AF_INET, SOCK_STREAM, 0);
76 	if (s == -1)
77 		return 1;
78 	o = 1;
79 	setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &o, sizeof o);
80 	memset(&sa, 0, sizeof sa);
81 	sa.sin_family = AF_INET;
82 	sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
83 	sa.sin_port = htons(3737);
84 	if (bind(s, (struct sockaddr *) &sa, sizeof sa) == -1)
85 		return 1;
86 	if (listen(s, 1) == -1)
87 		return 1;
88 	t = socket(AF_INET, SOCK_STREAM, 0);
89 	if (t == -1)
90 		return 1;
91 	if (connect(t, (struct sockaddr *) &sa, sizeof sa) == -1)
92 		return 1;
93 	u = accept(s, 0, 0);
94 	if (u == -1)
95 		return 1;
96 	close(s);
97 	fcntl(t, F_SETFL, fcntl(t, F_GETFL) | O_NONBLOCK);
98 	fcntl(u, F_SETFL, fcntl(t, F_GETFL) | O_NONBLOCK);
99 	do_t = 1;
100 	do_u = 1;
101 	pfd[0].fd = t;
102 	pfd[0].events = POLLOUT;
103 	pfd[1].fd = u;
104 	pfd[1].events = POLLOUT;
105 	while (do_t || do_u) {
106 		if (poll(pfd, 2, 1000) == 0) {
107 			if (do_t) {
108 				close(t);
109 				pfd[0].fd = -1;
110 				do_t = 0;
111 			}
112 			continue;
113 		}
114 		if (pfd[0].revents & POLLOUT) {
115 			if (write(t, buf, sizeof buf) == -1) {
116 				close(t);
117 				pfd[0].fd = -1;
118 				do_t = 0;
119 			}
120 		}
121 		if (pfd[1].revents & POLLOUT) {
122 			if (write(u, buf, sizeof buf) == -1) {
123 				close(u);
124 				pfd[1].fd = -1;
125 				do_u = 0;
126 			}
127 		}
128 	}
129 
130 	printf("ok 1 - tcpfullwindowrst\n");
131 	return 0;
132 }
133