1137130Srwatson/*-
2146250Srwatson * Copyright (c) 2004-2005 Robert N. M. Watson
3137130Srwatson * All rights reserved.
4137130Srwatson *
5137130Srwatson * Redistribution and use in source and binary forms, with or without
6137130Srwatson * modification, are permitted provided that the following conditions
7137130Srwatson * are met:
8137130Srwatson * 1. Redistributions of source code must retain the above copyright
9137130Srwatson *    notice, this list of conditions and the following disclaimer.
10137130Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11137130Srwatson *    notice, this list of conditions and the following disclaimer in the
12137130Srwatson *    documentation and/or other materials provided with the distribution.
13137130Srwatson *
14137130Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15137130Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16137130Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17137130Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18137130Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19137130Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20137130Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21137130Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22137130Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23137130Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24137130Srwatson * SUCH DAMAGE.
25137130Srwatson *
26137130Srwatson * $FreeBSD$
27137130Srwatson */
28137130Srwatson
29137130Srwatson#include <sys/types.h>
30137130Srwatson#include <sys/socket.h>
31137130Srwatson
32137130Srwatson#include <netinet/in.h>
33137130Srwatson
34137130Srwatson#include <arpa/inet.h>
35137130Srwatson
36137130Srwatson#include <err.h>
37137130Srwatson#include <errno.h>
38137130Srwatson#include <fcntl.h>
39137130Srwatson#include <stdio.h>
40137130Srwatson#include <string.h>
41137130Srwatson#include <unistd.h>
42137130Srwatson
43137130Srwatson/*
44137130Srwatson * The listenclose regression test is designed to catch kernel bugs that may
45137130Srwatson * trigger as a result of performing a close on a listen() socket with as-yet
46137130Srwatson * unaccepted connections in its queues.  This results in the connections
47137130Srwatson * being aborted, which is a not-often-followed code path.  To do this, we
48137130Srwatson * create a local TCP socket, build a non-blocking connection to it, and then
49137130Srwatson * close the accept socket.  The connection must be non-blocking or the
50137130Srwatson * program will block and as such connect() will not return as accept() is
51137130Srwatson * never called.
52137130Srwatson */
53137130Srwatson
54137130Srwatsonint
55137130Srwatsonmain(int argc, char *argv[])
56137130Srwatson{
57137130Srwatson	int listen_sock, connect_sock;
58137130Srwatson	struct sockaddr_in sin;
59137130Srwatson	socklen_t len;
60137130Srwatson	u_short port;
61137130Srwatson	int arg;
62137130Srwatson
63137130Srwatson	listen_sock = socket(PF_INET, SOCK_STREAM, 0);
64137130Srwatson	if (listen_sock == -1)
65137130Srwatson		errx(-1,
66137130Srwatson		    "socket(PF_INET, SOCK_STREAM, 0) for listen socket: %s",
67137130Srwatson		    strerror(errno));
68137130Srwatson
69137130Srwatson
70137130Srwatson	bzero(&sin, sizeof(sin));
71137130Srwatson	sin.sin_len = sizeof(sin);
72137130Srwatson	sin.sin_family = AF_INET;
73137130Srwatson	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
74137130Srwatson	sin.sin_port = 0;
75137130Srwatson
76137130Srwatson	if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
77137130Srwatson		errx(-1, "bind(%s, %d) for listen socket: %s",
78137130Srwatson		    inet_ntoa(sin.sin_addr), 0, strerror(errno));
79137130Srwatson
80137130Srwatson	len = sizeof(sin);
81137130Srwatson	if (getsockname(listen_sock, (struct sockaddr *)&sin, &len) < 0)
82137130Srwatson		errx(-1, "getsockname() for listen socket: %s",
83137130Srwatson		    strerror(errno));
84137130Srwatson	port = sin.sin_port;
85137130Srwatson
86137130Srwatson	if (listen(listen_sock, -1) < 0)
87137130Srwatson		errx(-1, "listen() for listen socket: %s", strerror(errno));
88137130Srwatson
89137130Srwatson	connect_sock = socket(PF_INET, SOCK_STREAM, 0);
90137130Srwatson	if (connect_sock == -1)
91137130Srwatson		errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect "
92137130Srwatson		    "socket: %s", strerror(errno));
93137130Srwatson
94137130Srwatson	arg = O_NONBLOCK;
95137130Srwatson	if (fcntl(connect_sock, F_SETFL, &arg) < 0)
96137130Srwatson		errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect socket"
97137130Srwatson		    ": %s", strerror(errno));
98137130Srwatson
99137130Srwatson	bzero(&sin, sizeof(sin));
100137130Srwatson	sin.sin_len = sizeof(sin);
101137130Srwatson	sin.sin_family = AF_INET;
102137130Srwatson	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
103137130Srwatson	sin.sin_port = port;
104137130Srwatson
105137130Srwatson	if (connect(connect_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
106137130Srwatson		errx(-1, "connect() for connect socket: %s", strerror(errno));
107146250Srwatson	close(connect_sock);
108137130Srwatson	close(listen_sock);
109137130Srwatson
110137130Srwatson	return (0);
111137130Srwatson}
112