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