2 * Copyright (C) 2005 The FreeBSD Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
43 struct sockaddr_in sock;
45 int listen_sock, connect_sock;
48 /* Shutdown(2) on an invalid file descriptor has to return EBADF. */
49 if ((shutdown(listen_sock, SHUT_RDWR) != -1) && (errno != EBADF))
50 errx(-1, "shutdown() for invalid file descriptor does not "
53 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
54 if (listen_sock == -1)
56 "socket(PF_INET, SOCK_STREAM, 0) for listen socket: %s",
59 bzero(&sock, sizeof(sock));
60 sock.sin_len = sizeof(sock);
61 sock.sin_family = AF_INET;
62 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
65 if (bind(listen_sock, (struct sockaddr *)&sock, sizeof(sock)) < 0)
66 errx(-1, "bind(%s, %d) for listen socket: %s",
67 inet_ntoa(sock.sin_addr), sock.sin_port, strerror(errno));
70 if (getsockname(listen_sock, (struct sockaddr *)&sock, &len) < 0)
71 errx(-1, "getsockname() for listen socket: %s",
75 if (listen(listen_sock, -1) < 0)
76 errx(-1, "listen() for listen socket: %s", strerror(errno));
78 connect_sock = socket(PF_INET, SOCK_STREAM, 0);
79 if (connect_sock == -1)
80 errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect "
81 "socket: %s", strerror(errno));
83 bzero(&sock, sizeof(sock));
84 sock.sin_len = sizeof(sock);
85 sock.sin_family = AF_INET;
86 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
89 if (connect(connect_sock, (struct sockaddr *)&sock, sizeof(sock)) < 0)
90 errx(-1, "connect() for connect socket: %s", strerror(errno));
91 /* Try to pass an invalid flags. */
92 if ((shutdown(connect_sock, SHUT_RD - 1) != -1) && (errno != EINVAL))
93 errx(-1, "shutdown(SHUT_RD - 1) does not return EINVAL");
94 if ((shutdown(connect_sock, SHUT_RDWR + 1) != -1) && (errno != EINVAL))
95 errx(-1, "shutdown(SHUT_RDWR + 1) does not return EINVAL");
97 if (shutdown(connect_sock, SHUT_RD) < 0)
98 errx(-1, "shutdown(SHUT_RD) for connect socket: %s",
100 if (shutdown(connect_sock, SHUT_WR) < 0)
101 errx(-1, "shutdown(SHUT_WR) for connect socket: %s",