xref: /freebsd/tools/regression/sockets/shutdown/shutdown.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*-
2  * Copyright (C) 2005 The FreeBSD Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 
34 #include <err.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 int
41 main(void)
42 {
43 	struct sockaddr_in sock;
44 	socklen_t len;
45 	int listen_sock, connect_sock;
46 	u_short port;
47 
48 	listen_sock = -1;
49 
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 "
53 		    "return EBADF");
54 
55 	listen_sock = socket(PF_INET, SOCK_STREAM, 0);
56 	if (listen_sock == -1)
57 		errx(-1,
58 		    "socket(PF_INET, SOCK_STREAM, 0) for listen socket: %s",
59 		    strerror(errno));
60 
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);
65 	sock.sin_port = 0;
66 
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));
70 
71 	len = sizeof(sock);
72 	if (getsockname(listen_sock, (struct sockaddr *)&sock, &len) < 0)
73 		errx(-1, "getsockname() for listen socket: %s",
74 		    strerror(errno));
75 	port = sock.sin_port;
76 
77 	if (listen(listen_sock, -1) < 0)
78 		errx(-1, "listen() for listen socket: %s", strerror(errno));
79 
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));
84 
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);
89 	sock.sin_port = port;
90 
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");
98 
99 	if (shutdown(connect_sock, SHUT_RD) < 0)
100 		errx(-1, "shutdown(SHUT_RD) for connect socket: %s",
101 		    strerror(errno));
102 	if (shutdown(connect_sock, SHUT_WR) < 0)
103 		errx(-1, "shutdown(SHUT_WR) for connect socket: %s",
104 		    strerror(errno));
105 
106 	close(connect_sock);
107 	close(listen_sock);
108 
109 	return (0);
110 }
111