xref: /freebsd/tools/regression/sockets/accf_data_attach/accf_data_attach.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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  */
58 int
59 main(int argc, char *argv[])
60 {
61 	struct accept_filter_arg afa;
62 	struct sockaddr_in sin;
63 	socklen_t len;
64 	int lso, ret;
65 
66 	printf("1..9\n");
67 
68 	/*
69 	 * Step 0. Open socket().
70 	 */
71 	lso = socket(PF_INET, SOCK_STREAM, 0);
72 	if (lso == -1)
73 		errx(-1, "not ok 1 - socket: %s", strerror(errno));
74 	printf("ok 1 - socket\n");
75 
76 	/*
77 	 * Step 1. After socket().  Should return EINVAL, since no accept
78 	 * filter should be attached.
79 	 */
80 	bzero(&afa, sizeof(afa));
81 	len = sizeof(afa);
82 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
83 	if (ret != -1)
84 		errx(-1, "not ok 2 - getsockopt() after socket() succeeded");
85 	if (errno != EINVAL)
86 		errx(-1, "not ok 2 - getsockopt() after socket() failed with "
87 		    "%d (%s)", errno, strerror(errno));
88 	printf("ok 2 - getsockopt\n");
89 
90 	/*
91 	 * Step 2. Bind().  Ideally this will succeed.
92 	 */
93 	bzero(&sin, sizeof(sin));
94 	sin.sin_len = sizeof(sin);
95 	sin.sin_family = AF_INET;
96 	sin.sin_port = htons(8080);
97 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
98 	if (bind(lso, (struct sockaddr *)&sin, sizeof(sin)) < 0)
99 		errx(-1, "not ok 3 - bind %s", strerror(errno));
100 	printf("ok 3 - bind\n");
101 
102 	/*
103 	 * Step 3: After bind().  getsockopt() should return EINVAL, since no
104 	 *  accept filter should be attached.
105 	 */
106 	len = sizeof(afa);
107 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
108 	if (ret != -1)
109 		errx(-1, "not ok 4 - getsockopt() after bind() succeeded");
110 	if (errno != EINVAL)
111 		errx(-1, "not ok 4 -  getsockopt() after bind() failed with %d (%s)",
112 		    errno, strerror(errno));
113 	printf("ok 4 - getsockopt\n");
114 
115 	/*
116 	 * Step 4: Setsockopt() before listen().  Should fail, since it's not
117 	 * yet a listen() socket.
118 	 */
119 	bzero(&afa, sizeof(afa));
120 	strcpy(afa.af_name, ACCF_NAME);
121 	ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
122 	if (ret == 0)
123 		errx(-1, "not ok 5 - setsockopt() before listen() succeeded");
124 	printf("ok 5 - setsockopt\n");
125 
126 	/*
127 	 * Step 5: Getsockopt() after pre-listen() setsockopt().  Should
128 	 * fail with EINVAL, since setsockopt() should have failed.
129 	 */
130 	len = sizeof(afa);
131 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
132 	if (ret == 0)
133 		errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "
134 		    "succeeded");
135 	if (errno != EINVAL)
136 		errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",
137 		    errno, strerror(errno));
138 	printf("ok 6 - getsockopt\n");
139 
140 	/*
141 	 * Step 6: listen().
142 	 */
143 	if (listen(lso, -1) < 0)
144 		errx(-1, "not ok 7 - listen: %s", strerror(errno));
145 	printf("ok 7 - listen\n");
146 
147 	/*
148 	 * Step 7: After listen().  This call to setsockopt() should succeed.
149 	 */
150 	bzero(&afa, sizeof(afa));
151 	strcpy(afa.af_name, ACCF_NAME);
152 	ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
153 	if (ret != 0)
154 		errx(-1, "not ok 8 - setsockopt() after listen() failed with %d "
155 		    "(%s)", errno, strerror(errno));
156 	if (len != sizeof(afa))
157 		errx(-1, "not ok 8 - setsockopt() after listen() returned wrong "
158 		    "size (%d vs expected %d)", len, sizeof(afa));
159 	printf("ok 8 - setsockopt\n");
160 
161 	/*
162 	 * Step 8: After setsockopt().  Should succeed and identify
163 	 * ACCF_NAME.
164 	 */
165 	bzero(&afa, sizeof(afa));
166 	len = sizeof(afa);
167 	ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
168 	if (ret != 0)
169 		errx(-1, "not ok 9 - getsockopt() after listen() setsockopt() "
170 		    "failed with %d (%s)", errno, strerror(errno));
171 	if (len != sizeof(afa))
172 		errx(-1, "not ok 9 - getsockopt() after setsockopet()  after "
173 		    "listen() returned wrong size (got %d expected %d)", len,
174 		    sizeof(afa));
175 	if (strcmp(afa.af_name, ACCF_NAME) != 0)
176 		errx(-1, "not ok 9 - getsockopt() after setsockopt() after "
177 		    "listen() mismatch (got %s expected %s)", afa.af_name,
178 		    ACCF_NAME);
179 	printf("ok 9 - getsockopt\n");
180 
181 	close(lso);
182 	return (0);
183 }
184