xref: /netbsd-src/tests/net/tcp/tcp_shutdown.c (revision 3df6c9b812215d4068516867ebe41f42f4fb2777)
1 /*	$NetBSD: tcp_shutdown.c,v 1.1 2022/11/04 08:01:42 ozaki-r Exp $	*/
2 
3 /*-
4  * Copyright (c) 2022 Internet Initiative Japan Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 #ifdef __RCSID
33 __RCSID("$NetBSD: tcp_shutdown.c,v 1.1 2022/11/04 08:01:42 ozaki-r Exp $");
34 #else
35 extern const char *__progname;
36 #define getprogname() __progname
37 #endif
38 
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <stdbool.h>
51 
52 static inline bool
match(const char * a,const char * b)53 match(const char *a, const char *b)
54 {
55 
56 	return strncmp(a, b, strlen(b)) == 0;
57 }
58 
59 int
main(int argc,char * argv[])60 main(int argc, char *argv[])
61 {
62 	int s, e;
63 	char *target;
64 
65 	if (argc != 2)
66 		errx(EXIT_FAILURE, "invalid argument");
67 	target = argv[1];
68 
69 	s = socket(AF_INET, SOCK_STREAM, 0);
70 	if (s == -1)
71 		err(EXIT_FAILURE, "socket");
72 	e = shutdown(s, SHUT_RDWR);
73 	if (e == -1)
74 		err(EXIT_FAILURE, "shutdown");
75 
76 	if (match(target, "connect")) {
77 		struct sockaddr_in sin;
78 
79 		memset(&sin, 0, sizeof(sin));
80 		sin.sin_port = htons(31522);
81 		sin.sin_addr.s_addr = inet_addr("127.0.0.1");
82 		sin.sin_family = AF_INET;
83 
84 		e = connect(s, (struct sockaddr *)&sin, sizeof(sin));
85 		if (e == 0)
86 			err(EXIT_FAILURE, "connect didn't fail on a shudown socket");
87 		if (e == -1 && errno != EINVAL)
88 			err(EXIT_FAILURE, "connect failed with unexpected error");
89 	} else if (match(target, "setsockopt")) {
90 		int opt = 1;
91 		e = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
92 		if (e == 0)
93 			err(EXIT_FAILURE, "setsockopt didn't fail on a shutdown socket");
94 		if (e == -1 && errno != ECONNRESET)
95 			err(EXIT_FAILURE, "setsockopt failed with unexpected error");
96 	} else if (match(target, "getsockname")) {
97 		struct sockaddr_storage ss;
98 		socklen_t len;
99 		e = getsockname(s, (struct sockaddr *)&ss, &len);
100 		if (e == 0)
101 			err(EXIT_FAILURE, "getsockname didn't fail on a shutdown socket");
102 		if (e == -1 && errno != EINVAL)
103 			err(EXIT_FAILURE, "getsockname failed with unexpected error");
104 	} else if (match(target, "listen")) {
105 		e = listen(s, 5);
106 		if (e == 0)
107 			err(EXIT_FAILURE, "listen didn't fail on a shutdown socket");
108 		if (e == -1 && errno != EINVAL)
109 			err(EXIT_FAILURE, "listen failed with unexpected error");
110 	} else if (match(target, "bind")) {
111 		struct sockaddr_in sin;
112 
113 		memset(&sin, 0, sizeof(sin));
114 		sin.sin_port = htons(31522);
115 		sin.sin_addr.s_addr = inet_addr("127.0.0.1");
116 		sin.sin_family = AF_INET;
117 
118 		e = bind(s, (struct sockaddr *)&sin, sizeof(sin));
119 		if (e == 0)
120 			err(EXIT_FAILURE, "bind didn't fail on a shutdown socket");
121 		if (e == -1 && errno != EINVAL)
122 			err(EXIT_FAILURE, "bind failed with unexpected error");
123 	} else if (match(target, "shutdown")) {
124 		e = shutdown(s, SHUT_RDWR);
125 		if (e == 0)
126 			err(EXIT_FAILURE, "shutdown didn't fail on a shutdown socket");
127 		if (e == -1 && errno != EINVAL)
128 			err(EXIT_FAILURE, "shutdown failed with unexpected error");
129 	} else {
130 		errx(EXIT_FAILURE, "unknown target: %s", target);
131 	}
132 
133 	e = close(s);
134 	if (e == -1)
135 		err(EXIT_FAILURE, "close");
136 	return 0;
137 }
138