xref: /freebsd/tools/regression/sockets/accf_data_attach/accf_data_attach.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright (c) 2004 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 
32 #include <netinet/in.h>
33 
34 #include <err.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #define	ACCF_NAME	"dataready"
42 
43 /*
44  * A number of small tests to confirm that attaching ACCF_DATA accept filters
45  * to inet4 ports works as expected.  We test:
46  *
47  * - That no accept filter is attached on a newly created socket.
48  * - That bind() has no affect on the accept filter state.
49  * - That we can't attach an accept filter to a socket that isn't in the
50  *   listen state.
51  * - That after we fail to attach the filter, querying the kernel shows no
52  *   filter attached.
53  * - That we can attach an accept filter to a socket that is in the listen
54  *   state.
55  * - That once an accept filter is attached, we can query to make sure it is
56  *   attached.
57  * - That once an accept filter is attached, we can remove it and query to
58  *   make sure it is removed.
59  */
60 int
61 main(void)
62 {
63 	struct accept_filter_arg afa;
64 	struct sockaddr_in sin;
65 	socklen_t len;
66 	int lso, ret;
67 
68 	printf("1..11\n");
69 
70 	/*
71 	 * Step 0. Open socket().
72 	 */
73 	lso = socket(PF_INET, SOCK_STREAM, 0);
74 	if (lso == -1)
75 		errx(-1, "not ok 1 - socket: %s", strerror(errno));
76 	printf("ok 1 - socket\n");
77 
78 	/*
79 	 * Step 1. After socket().  Should return EINVAL, since no accept
80 	 * filter should be attached.
81 	 */
82 	bzero(&afa, sizeof(afa));
83 	len = sizeof(afa);
84 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
85 	if (ret != -1)
86 		errx(-1, "not ok 2 - getsockopt() after socket() succeeded");
87 	if (errno != EINVAL)
88 		errx(-1, "not ok 2 - getsockopt() after socket() failed with "
89 		    "%d (%s)", errno, strerror(errno));
90 	printf("ok 2 - getsockopt\n");
91 
92 	/*
93 	 * Step 2. Bind().  Ideally this will succeed.
94 	 */
95 	bzero(&sin, sizeof(sin));
96 	sin.sin_len = sizeof(sin);
97 	sin.sin_family = AF_INET;
98 	sin.sin_port = htons(8080);
99 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
100 	if (bind(lso, (struct sockaddr *)&sin, sizeof(sin)) < 0)
101 		errx(-1, "not ok 3 - bind %s", strerror(errno));
102 	printf("ok 3 - bind\n");
103 
104 	/*
105 	 * Step 3: After bind().  getsockopt() should return EINVAL, since no
106 	 *  accept filter should be attached.
107 	 */
108 	len = sizeof(afa);
109 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
110 	if (ret != -1)
111 		errx(-1, "not ok 4 - getsockopt() after bind() succeeded");
112 	if (errno != EINVAL)
113 		errx(-1, "not ok 4 -  getsockopt() after bind() failed with %d (%s)",
114 		    errno, strerror(errno));
115 	printf("ok 4 - getsockopt\n");
116 
117 	/*
118 	 * Step 4: Setsockopt() before listen().  Should fail, since it's not
119 	 * yet a listen() socket.
120 	 */
121 	bzero(&afa, sizeof(afa));
122 	strcpy(afa.af_name, ACCF_NAME);
123 	ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
124 	if (ret == 0)
125 		errx(-1, "not ok 5 - setsockopt() before listen() succeeded");
126 	printf("ok 5 - setsockopt\n");
127 
128 	/*
129 	 * Step 5: Getsockopt() after pre-listen() setsockopt().  Should
130 	 * fail with EINVAL, since setsockopt() should have failed.
131 	 */
132 	len = sizeof(afa);
133 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
134 	if (ret == 0)
135 		errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "
136 		    "succeeded");
137 	if (errno != EINVAL)
138 		errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",
139 		    errno, strerror(errno));
140 	printf("ok 6 - getsockopt\n");
141 
142 	/*
143 	 * Step 6: listen().
144 	 */
145 	if (listen(lso, -1) < 0)
146 		errx(-1, "not ok 7 - listen: %s", strerror(errno));
147 	printf("ok 7 - listen\n");
148 
149 	/*
150 	 * Step 7: Getsockopt() after listen().  Should fail with EINVAL,
151 	 * since we have not installed accept filter yet.
152 	 */
153 	len = sizeof(afa);
154 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
155 	if (ret == 0)
156 		errx(-1, "not ok 8 - getsockopt() after listen() but before "
157 		    "setsockopt() succeeded");
158 	if (errno != EINVAL)
159 		errx(-1, "not ok 8 - getsockopt() after listen() but before "
160 		    "setsockopt() failed with %d (%s)", errno, strerror(errno));
161 	printf("ok 8 - getsockopt\n");
162 
163 	/*
164 	 * Step 8: After listen().  This call to setsockopt() should succeed.
165 	 */
166 	bzero(&afa, sizeof(afa));
167 	strcpy(afa.af_name, ACCF_NAME);
168 	ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
169 	if (ret != 0)
170 		errx(-1, "not ok 9 - setsockopt() after listen() failed with %d "
171 		    "(%s)", errno, strerror(errno));
172 	printf("ok 9 - setsockopt\n");
173 
174 	/*
175 	 * Step 9: After setsockopt().  Should succeed and identify
176 	 * ACCF_NAME.
177 	 */
178 	bzero(&afa, sizeof(afa));
179 	len = sizeof(afa);
180 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
181 	if (ret != 0)
182 		errx(-1, "not ok 10 - getsockopt() after listen() setsockopt() "
183 		    "failed with %d (%s)", errno, strerror(errno));
184 	if (len != sizeof(afa))
185 		errx(-1, "not ok 10 - getsockopt() after setsockopet()  after "
186 		    "listen() returned wrong size (got %d expected %zd)", len,
187 		    sizeof(afa));
188 	if (strcmp(afa.af_name, ACCF_NAME) != 0)
189 		errx(-1, "not ok 10 - getsockopt() after setsockopt() after "
190 		    "listen() mismatch (got %s expected %s)", afa.af_name,
191 		    ACCF_NAME);
192 	printf("ok 10 - getsockopt\n");
193 
194 	/*
195 	 * Step 10: Remove accept filter.  After removing the accept filter
196 	 * getsockopt() should fail with EINVAL.
197 	 */
198 	ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);
199 	if (ret != 0)
200 		errx(-1, "not ok 11 - setsockopt() after listen() "
201 		    "failed with %d (%s)", errno, strerror(errno));
202 	bzero(&afa, sizeof(afa));
203 	len = sizeof(afa);
204 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
205 	if (ret == 0)
206 		errx(-1, "not ok 11 - getsockopt() after removing "
207 		    "the accept filter returns valid accept filter %s",
208 		    afa.af_name);
209 	if (errno != EINVAL)
210 		errx(-1, "not ok 11 - getsockopt() after removing the accept"
211 		    "filter failed with %d (%s)", errno, strerror(errno));
212 	printf("ok 11 - setsockopt\n");
213 
214 	close(lso);
215 	return (0);
216 }
217