1/*-
2 * Copyright (c) 2004-2005 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <sys/types.h>
30#include <sys/socket.h>
31
32#include <netinet/in.h>
33
34#include <arpa/inet.h>
35
36#include <err.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <stdio.h>
40#include <string.h>
41#include <unistd.h>
42
43/*
44 * The listenclose regression test is designed to catch kernel bugs that may
45 * trigger as a result of performing a close on a listen() socket with as-yet
46 * unaccepted connections in its queues.  This results in the connections
47 * being aborted, which is a not-often-followed code path.  To do this, we
48 * create a local TCP socket, build a non-blocking connection to it, and then
49 * close the accept socket.  The connection must be non-blocking or the
50 * program will block and as such connect() will not return as accept() is
51 * never called.
52 */
53
54int
55main(void)
56{
57	int listen_sock, connect_sock;
58	struct sockaddr_in sin;
59	socklen_t len;
60	u_short port;
61	int arg;
62
63	listen_sock = socket(PF_INET, SOCK_STREAM, 0);
64	if (listen_sock == -1)
65		errx(-1,
66		    "socket(PF_INET, SOCK_STREAM, 0) for listen socket: %s",
67		    strerror(errno));
68
69
70	bzero(&sin, sizeof(sin));
71	sin.sin_len = sizeof(sin);
72	sin.sin_family = AF_INET;
73	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
74	sin.sin_port = 0;
75
76	if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
77		errx(-1, "bind(%s, %d) for listen socket: %s",
78		    inet_ntoa(sin.sin_addr), 0, strerror(errno));
79
80	len = sizeof(sin);
81	if (getsockname(listen_sock, (struct sockaddr *)&sin, &len) < 0)
82		errx(-1, "getsockname() for listen socket: %s",
83		    strerror(errno));
84	port = sin.sin_port;
85
86	if (listen(listen_sock, -1) < 0)
87		errx(-1, "listen() for listen socket: %s", strerror(errno));
88
89	connect_sock = socket(PF_INET, SOCK_STREAM, 0);
90	if (connect_sock == -1)
91		errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect "
92		    "socket: %s", strerror(errno));
93
94	arg = O_NONBLOCK;
95	if (fcntl(connect_sock, F_SETFL, &arg) < 0)
96		errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect socket"
97		    ": %s", strerror(errno));
98
99	bzero(&sin, sizeof(sin));
100	sin.sin_len = sizeof(sin);
101	sin.sin_family = AF_INET;
102	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
103	sin.sin_port = port;
104
105	if (connect(connect_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
106		errx(-1, "connect() for connect socket: %s", strerror(errno));
107	close(connect_sock);
108	close(listen_sock);
109
110	return (0);
111}
112