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;
50 /* Shutdown(2) on an invalid file descriptor has to return EBADF. */
51 if ((shutdown(listen_sock, SHUT_RDWR) != -1) && (errno != EBADF))
52 errx(-1, "shutdown() for invalid file descriptor does not "
55 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
56 if (listen_sock == -1)
58 "socket(PF_INET, SOCK_STREAM, 0) for listen socket: %s",
61 bzero(&sock, sizeof(sock));
62 sock.sin_len = sizeof(sock);
63 sock.sin_family = AF_INET;
64 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
67 if (bind(listen_sock, (struct sockaddr *)&sock, sizeof(sock)) < 0)
68 errx(-1, "bind(%s, %d) for listen socket: %s",
69 inet_ntoa(sock.sin_addr), sock.sin_port, strerror(errno));
72 if (getsockname(listen_sock, (struct sockaddr *)&sock, &len) < 0)
73 errx(-1, "getsockname() for listen socket: %s",
77 if (listen(listen_sock, -1) < 0)
78 errx(-1, "listen() for listen socket: %s", strerror(errno));
80 connect_sock = socket(PF_INET, SOCK_STREAM, 0);
81 if (connect_sock == -1)
82 errx(-1, "socket(PF_INET, SOCK_STREAM, 0) for connect "
83 "socket: %s", strerror(errno));
85 bzero(&sock, sizeof(sock));
86 sock.sin_len = sizeof(sock);
87 sock.sin_family = AF_INET;
88 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
91 if (connect(connect_sock, (struct sockaddr *)&sock, sizeof(sock)) < 0)
92 errx(-1, "connect() for connect socket: %s", strerror(errno));
93 /* Try to pass an invalid flags. */
94 if ((shutdown(connect_sock, SHUT_RD - 1) != -1) && (errno != EINVAL))
95 errx(-1, "shutdown(SHUT_RD - 1) does not return EINVAL");
96 if ((shutdown(connect_sock, SHUT_RDWR + 1) != -1) && (errno != EINVAL))
97 errx(-1, "shutdown(SHUT_RDWR + 1) does not return EINVAL");
99 if (shutdown(connect_sock, SHUT_RD) < 0)
100 errx(-1, "shutdown(SHUT_RD) for connect socket: %s",
102 if (shutdown(connect_sock, SHUT_WR) < 0)
103 errx(-1, "shutdown(SHUT_WR) for connect socket: %s",