Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tools/regression/sockets/accf_data_attach/accf_data_attach.c
48254 views
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
27
#include <sys/types.h>
28
#include <sys/module.h>
29
#include <sys/socket.h>
30
31
#include <netinet/in.h>
32
33
#include <err.h>
34
#include <errno.h>
35
#include <fcntl.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
struct linger linger;
66
socklen_t len;
67
int lso, so, i, ret;
68
69
/* XXX: PLAIN_TEST_REQUIRE_MODULE "backport" for stable/9 */
70
const char *_mod_name = "accf_data";
71
72
if (modfind(_mod_name) == -1) {
73
printf("1..0 # SKIP - module %s could not be resolved: %s\n",
74
_mod_name, strerror(errno));
75
_exit(0);
76
}
77
/* XXX: PLAIN_TEST_REQUIRE_MODULE for stable/9 */
78
79
printf("1..12\n");
80
81
/*
82
* Step 0. Open socket().
83
*/
84
lso = socket(PF_INET, SOCK_STREAM, 0);
85
if (lso == -1)
86
errx(-1, "not ok 1 - socket: %s", strerror(errno));
87
printf("ok 1 - socket\n");
88
89
/*
90
* Step 1. After socket(). Should return EINVAL, since no accept
91
* filter should be attached.
92
*/
93
bzero(&afa, sizeof(afa));
94
len = sizeof(afa);
95
ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
96
if (ret != -1)
97
errx(-1, "not ok 2 - getsockopt() after socket() succeeded");
98
if (errno != EINVAL)
99
errx(-1, "not ok 2 - getsockopt() after socket() failed with "
100
"%d (%s)", errno, strerror(errno));
101
printf("ok 2 - getsockopt\n");
102
103
/*
104
* Step 2. Bind(). Ideally this will succeed.
105
*/
106
setsockopt(lso, SOL_SOCKET, SO_REUSEADDR, &lso, sizeof(lso));
107
bzero(&sin, sizeof(sin));
108
sin.sin_len = sizeof(sin);
109
sin.sin_family = AF_INET;
110
sin.sin_port = htons(8080);
111
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
112
if (bind(lso, (struct sockaddr *)&sin, sizeof(sin)) < 0)
113
errx(-1, "not ok 3 - bind %s", strerror(errno));
114
printf("ok 3 - bind\n");
115
116
/*
117
* Step 3: After bind(). getsockopt() should return EINVAL, since no
118
* accept filter should be attached.
119
*/
120
len = sizeof(afa);
121
ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
122
if (ret != -1)
123
errx(-1, "not ok 4 - getsockopt() after bind() succeeded");
124
if (errno != EINVAL)
125
errx(-1, "not ok 4 - getsockopt() after bind() failed with %d (%s)",
126
errno, strerror(errno));
127
printf("ok 4 - getsockopt\n");
128
129
/*
130
* Step 4: Setsockopt() before listen(). Should fail, since it's not
131
* yet a listen() socket.
132
*/
133
bzero(&afa, sizeof(afa));
134
strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
135
ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
136
if (ret == 0)
137
errx(-1, "not ok 5 - setsockopt() before listen() succeeded");
138
printf("ok 5 - setsockopt\n");
139
140
/*
141
* Step 5: Getsockopt() after pre-listen() setsockopt(). Should
142
* fail with EINVAL, since setsockopt() should have failed.
143
*/
144
len = sizeof(afa);
145
ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
146
if (ret == 0)
147
errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "
148
"succeeded");
149
if (errno != EINVAL)
150
errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",
151
errno, strerror(errno));
152
printf("ok 6 - getsockopt\n");
153
154
/*
155
* Step 6: listen().
156
*/
157
if (listen(lso, -1) < 0)
158
errx(-1, "not ok 7 - listen: %s", strerror(errno));
159
printf("ok 7 - listen\n");
160
161
/*
162
* Step 7: Getsockopt() after listen(). Should fail with EINVAL,
163
* since we have not installed accept filter yet.
164
*/
165
len = sizeof(afa);
166
ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
167
if (ret == 0)
168
errx(-1, "not ok 8 - getsockopt() after listen() but before "
169
"setsockopt() succeeded");
170
if (errno != EINVAL)
171
errx(-1, "not ok 8 - getsockopt() after listen() but before "
172
"setsockopt() failed with %d (%s)", errno, strerror(errno));
173
printf("ok 8 - getsockopt\n");
174
175
/*
176
* Step 8: After listen(). This call to setsockopt() should succeed.
177
*/
178
bzero(&afa, sizeof(afa));
179
strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
180
ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
181
if (ret != 0)
182
errx(-1, "not ok 9 - setsockopt() after listen() failed with %d "
183
"(%s)", errno, strerror(errno));
184
printf("ok 9 - setsockopt\n");
185
186
/*
187
* Step 9: After setsockopt(). Should succeed and identify
188
* ACCF_NAME.
189
*/
190
bzero(&afa, sizeof(afa));
191
len = sizeof(afa);
192
ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
193
if (ret != 0)
194
errx(-1, "not ok 10 - getsockopt() after listen() setsockopt() "
195
"failed with %d (%s)", errno, strerror(errno));
196
if (len != sizeof(afa))
197
errx(-1, "not ok 10 - getsockopt() after setsockopet() after "
198
"listen() returned wrong size (got %d expected %zd)", len,
199
sizeof(afa));
200
if (strcmp(afa.af_name, ACCF_NAME) != 0)
201
errx(-1, "not ok 10 - getsockopt() after setsockopt() after "
202
"listen() mismatch (got %s expected %s)", afa.af_name,
203
ACCF_NAME);
204
printf("ok 10 - getsockopt\n");
205
206
/*
207
* Step 10: Set listening socket to non blocking mode. Open
208
* connection to our listening socket and try to accept. Should
209
* no succeed. Write a byte of data and try again. Should accept.
210
*/
211
i = fcntl(lso, F_GETFL);
212
if (i < 0)
213
errx(-1, "not ok 11 - ioctl(F_GETFL): %s", strerror(errno));
214
i |= O_NONBLOCK;
215
if (fcntl(lso, F_SETFL, i) != 0)
216
errx(-1, "not ok 11 - ioctl(F_SETFL): %s", strerror(errno));
217
so = socket(PF_INET, SOCK_STREAM, 0);
218
if (so == -1)
219
errx(-1, "not ok 11 - socket: %s", strerror(errno));
220
if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
221
errx(-1, "not ok 11 - connect %s", strerror(errno));
222
if (accept(lso, NULL, 0) != -1 && errno != EWOULDBLOCK)
223
errx(-1, "not ok 11 - accept #1 %s", strerror(errno));
224
if (write(so, "0", 1) != 1)
225
errx(-1, "not ok 11 - write %s", strerror(errno));
226
/*
227
* XXXGL: ugly, but we need to make sure that our write reaches
228
* remote side of the socket.
229
*/
230
usleep(10000);
231
if (accept(lso, NULL, 0) < 1)
232
errx(-1, "not ok 11 - accept #2 %s", strerror(errno));
233
if (close(so) != 0)
234
errx(-1, "not ok 11 - close(): %s", strerror(errno));
235
printf("ok 11 - accept\n");
236
237
/*
238
* Step 12: reset connection before accept filter allows it.
239
* In this case the connection must make it to the listen
240
* queue, but with ECONNABORTED code.
241
*/
242
so = socket(PF_INET, SOCK_STREAM, 0);
243
if (so == -1)
244
errx(-1, "not ok 12 - socket: %s", strerror(errno));
245
if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
246
errx(-1, "not ok 12 - connect %s", strerror(errno));
247
linger.l_onoff = 1;
248
linger.l_linger = 0;
249
ret = setsockopt(so, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
250
if (ret != 0)
251
errx(-1, "not ok 12 - setsockopt(SO_LINGER) failed with %d "
252
"(%s)", errno, strerror(errno));
253
if (close(so) != 0)
254
errx(-1, "not ok 12 - close(): %s", strerror(errno));
255
if (accept(lso, NULL, 0) != -1 && errno != ECONNABORTED)
256
errx(-1, "not ok 12 - accept #3 %s", strerror(errno));
257
printf("ok 12 - accept\n");
258
259
#if 1
260
/*
261
* XXXGL: this doesn't belong to the test itself, but is known
262
* to examine rarely examined paths in the kernel. Intentionally
263
* leave a socket on the incomplete queue, before the program
264
* exits.
265
*/
266
so = socket(PF_INET, SOCK_STREAM, 0);
267
if (so == -1)
268
errx(-1, "not ok 13 - socket: %s", strerror(errno));
269
if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
270
errx(-1, "not ok 13 - connect %s", strerror(errno));
271
#endif
272
273
/*
274
* Step 12: Remove accept filter. After removing the accept filter
275
* getsockopt() should fail with EINVAL.
276
*/
277
ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);
278
if (ret != 0)
279
errx(-1, "not ok 13 - setsockopt() after listen() "
280
"failed with %d (%s)", errno, strerror(errno));
281
bzero(&afa, sizeof(afa));
282
len = sizeof(afa);
283
ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
284
if (ret == 0)
285
errx(-1, "not ok 13 - getsockopt() after removing "
286
"the accept filter returns valid accept filter %s",
287
afa.af_name);
288
if (errno != EINVAL)
289
errx(-1, "not ok 13 - getsockopt() after removing the accept"
290
"filter failed with %d (%s)", errno, strerror(errno));
291
if (close(lso) != 0)
292
errx(-1, "not ok 13 - close() of listening socket: %s",
293
strerror(errno));
294
printf("ok 13 - setsockopt\n");
295
296
return (0);
297
}
298
299