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