2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
41 #define ACCF_NAME "dataready"
44 * A number of small tests to confirm that attaching ACCF_DATA accept filters
45 * to inet4 ports works as expected. We test:
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
51 * - That after we fail to attach the filter, querying the kernel shows no
53 * - That we can attach an accept filter to a socket that is in the listen
55 * - That once an accept filter is attached, we can query to make sure it is
57 * - That once an accept filter is attached, we can remove it and query to
58 * make sure it is removed.
61 main(int argc, char *argv[])
63 struct accept_filter_arg afa;
64 struct sockaddr_in sin;
71 * Step 0. Open socket().
73 lso = socket(PF_INET, SOCK_STREAM, 0);
75 errx(-1, "not ok 1 - socket: %s", strerror(errno));
76 printf("ok 1 - socket\n");
79 * Step 1. After socket(). Should return EINVAL, since no accept
80 * filter should be attached.
82 bzero(&afa, sizeof(afa));
84 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
86 errx(-1, "not ok 2 - getsockopt() after socket() succeeded");
88 errx(-1, "not ok 2 - getsockopt() after socket() failed with "
89 "%d (%s)", errno, strerror(errno));
90 printf("ok 2 - getsockopt\n");
93 * Step 2. Bind(). Ideally this will succeed.
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");
105 * Step 3: After bind(). getsockopt() should return EINVAL, since no
106 * accept filter should be attached.
109 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
111 errx(-1, "not ok 4 - getsockopt() after bind() succeeded");
113 errx(-1, "not ok 4 - getsockopt() after bind() failed with %d (%s)",
114 errno, strerror(errno));
115 printf("ok 4 - getsockopt\n");
118 * Step 4: Setsockopt() before listen(). Should fail, since it's not
119 * yet a listen() socket.
121 bzero(&afa, sizeof(afa));
122 strcpy(afa.af_name, ACCF_NAME);
123 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
125 errx(-1, "not ok 5 - setsockopt() before listen() succeeded");
126 printf("ok 5 - setsockopt\n");
129 * Step 5: Getsockopt() after pre-listen() setsockopt(). Should
130 * fail with EINVAL, since setsockopt() should have failed.
133 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
135 errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "
138 errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",
139 errno, strerror(errno));
140 printf("ok 6 - getsockopt\n");
145 if (listen(lso, -1) < 0)
146 errx(-1, "not ok 7 - listen: %s", strerror(errno));
147 printf("ok 7 - listen\n");
150 * Step 7: Getsockopt() after listen(). Should fail with EINVAL,
151 * since we have not installed accept filter yet.
154 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
156 errx(-1, "not ok 8 - getsockopt() after listen() but before "
157 "setsockopt() succeeded");
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");
164 * Step 8: After listen(). This call to setsockopt() should succeed.
166 bzero(&afa, sizeof(afa));
167 strcpy(afa.af_name, ACCF_NAME);
168 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
170 errx(-1, "not ok 9 - setsockopt() after listen() failed with %d "
171 "(%s)", errno, strerror(errno));
172 printf("ok 9 - setsockopt\n");
175 * Step 9: After setsockopt(). Should succeed and identify
178 bzero(&afa, sizeof(afa));
180 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
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,
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,
192 printf("ok 10 - getsockopt\n");
195 * Step 10: Remove accept filter. After removing the accept filter
196 * getsockopt() should fail with EINVAL.
198 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);
200 errx(-1, "not ok 11 - setsockopt() after listen() "
201 "failed with %d (%s)", errno, strerror(errno));
202 bzero(&afa, sizeof(afa));
204 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
206 errx(-1, "not ok 11 - getsockopt() after removing "
207 "the accept filter returns valid accept filter %s",
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");