Path: blob/main/tools/regression/netinet/tcpfullwindowrst/tcpfullwindowrsttest.c
39491 views
/*1Copyright 2004 Michiel Boland. All rights reserved.23Redistribution and use in source and binary forms, with or without4modification, are permitted provided that the following conditions5are met:671. Redistributions of source code must retain the above copyright8notice, this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in the12documentation and/or other materials provided with the distribution.1314THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS15OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE18LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,19OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT20OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR21BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,22WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE23OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,24EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2526*/2728#include <sys/types.h>29#include <sys/socket.h>30#include <netinet/in.h>31#include <fcntl.h>32#include <poll.h>33#include <unistd.h>34#include <signal.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>3839/*40* The following code sets up two connected TCP sockets that send data to each41* other until the window is closed. Then one of the sockets is closed, which42* will generate a RST once the TCP at the other socket does a window probe.43*44* All versions of FreeBSD prior to 11/26/2004 will ignore this RST into a 045* window, causing the connection (and application) to hang indefinitely.46* On patched versions of FreeBSD (and other operating systems), the RST47* will be accepted and the program will exit in a few seconds.48*/4950/*51* If the alarm fired then we've hung and the test failed.52*/53void54do_alrm(int s)55{56printf("not ok 1 - tcpfullwindowrst\n");57exit(0);58}5960int61main(void)62{63int o, s, t, u, do_t, do_u;64struct pollfd pfd[2];65struct sockaddr_in sa;66char buf[4096];6768printf("1..1\n");69signal(SIGALRM, do_alrm);70alarm(20);7172s = socket(AF_INET, SOCK_STREAM, 0);73if (s == -1)74return 1;75o = 1;76setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &o, sizeof o);77memset(&sa, 0, sizeof sa);78sa.sin_family = AF_INET;79sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);80sa.sin_port = htons(3737);81if (bind(s, (struct sockaddr *) &sa, sizeof sa) == -1)82return 1;83if (listen(s, 1) == -1)84return 1;85t = socket(AF_INET, SOCK_STREAM, 0);86if (t == -1)87return 1;88if (connect(t, (struct sockaddr *) &sa, sizeof sa) == -1)89return 1;90u = accept(s, 0, 0);91if (u == -1)92return 1;93close(s);94fcntl(t, F_SETFL, fcntl(t, F_GETFL) | O_NONBLOCK);95fcntl(u, F_SETFL, fcntl(t, F_GETFL) | O_NONBLOCK);96do_t = 1;97do_u = 1;98pfd[0].fd = t;99pfd[0].events = POLLOUT;100pfd[1].fd = u;101pfd[1].events = POLLOUT;102while (do_t || do_u) {103if (poll(pfd, 2, 1000) == 0) {104if (do_t) {105close(t);106pfd[0].fd = -1;107do_t = 0;108}109continue;110}111if (pfd[0].revents & POLLOUT) {112if (write(t, buf, sizeof buf) == -1) {113close(t);114pfd[0].fd = -1;115do_t = 0;116}117}118if (pfd[1].revents & POLLOUT) {119if (write(u, buf, sizeof buf) == -1) {120close(u);121pfd[1].fd = -1;122do_u = 0;123}124}125}126127printf("ok 1 - tcpfullwindowrst\n");128return 0;129}130131132