1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/* $OpenBSD: runtest.c,v 1.6 2020/12/30 18:56:56 benno Exp $ */
/*
* Copyright (c) 2015 Vincent Gross <vincent.gross@kilob.yt>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <ifaddrs.h>
int
runtest(int *sockp, struct addrinfo *ai, int reuseaddr, int reuseport,
void *mreq, int expected)
{
int error, optval;
struct ip_mreq imr;
*sockp = socket(ai->ai_family, ai->ai_socktype, 0);
if (*sockp == -1) {
warn("%s : socket()", ai->ai_canonname);
return (3);
}
if (reuseaddr) {
optval = 1;
error = setsockopt(*sockp, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(int));
if (error) {
warn("%s : setsockopt(SO_REUSEADDR)", ai->ai_canonname);
return (2);
}
}
if (reuseport) {
optval = 1;
error = setsockopt(*sockp, SOL_SOCKET, SO_REUSEPORT,
&optval, sizeof(int));
if (error) {
warn("%s : setsockopt(SO_REUSEPORT)", ai->ai_canonname);
return (2);
}
}
if (mreq) {
switch (ai->ai_family) {
case AF_INET6:
error = setsockopt(*sockp, IPPROTO_IPV6, IPV6_JOIN_GROUP,
mreq, sizeof(struct ipv6_mreq));
if (error) {
warn("%s : setsockopt(IPV6_JOIN_GROUP)",
ai->ai_canonname);
return (2);
}
break;
case AF_INET:
error = setsockopt(*sockp, IPPROTO_IP, IP_ADD_MEMBERSHIP,
mreq, sizeof(struct ip_mreq));
if (error) {
warn("%s : setsockopt(IP_ADD_MEMBERSHIP)",
ai->ai_canonname);
return (2);
}
break;
default:
warnx("%s : trying to join multicast group in unknown AF",
ai->ai_canonname);
return (2);
}
}
error = bind(*sockp, ai->ai_addr, ai->ai_addrlen);
if (error && (expected == 0 || expected != errno)) {
warn("bind(%s,%s,%s)", ai->ai_canonname,
reuseaddr ? "REUSEADDR" : "", reuseport ? "REUSEPORT" : "");
return (1);
}
if (error == 0 && expected != 0) {
warnx("bind(%s,%s,%s) succeeded, expected : %s", ai->ai_canonname,
reuseaddr ? "REUSEADDR" : "", reuseport ? "REUSEPORT" : "",
strerror(errno));
return (1);
}
return (0);
}
void
cleanup(int *fds, int num_fds)
{
while (num_fds-- > 0)
if (close(*fds++) && errno != EBADF)
err(2, "unable to clean up sockets, aborting");
}
int
unicast_testsuite(struct addrinfo *local, struct addrinfo *any)
{
int test_rc, rc, *s;
int sockets[4];
test_rc = 0;
rc = 0; s = sockets;
rc |= runtest(s++, local, 0, 0, NULL, 0);
rc |= runtest(s++, any, 0, 0, NULL, EADDRINUSE);
rc |= runtest(s++, any, 1, 0, NULL, 0);
cleanup(sockets, 3);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 1);
rc = 0; s = sockets;
rc |= runtest(s++, any, 0, 0, NULL, 0);
rc |= runtest(s++, local, 0, 0, NULL, EADDRINUSE);
rc |= runtest(s++, local, 1, 0, NULL, 0);
cleanup(sockets, 3);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 2);
rc = 0; s = sockets;
rc |= runtest(s++, local, 0, 1, NULL, 0);
rc |= runtest(s++, local, 0, 1, NULL, 0);
rc |= runtest(s++, local, 1, 0, NULL, EADDRINUSE);
rc |= runtest(s++, local, 0, 0, NULL, EADDRINUSE);
cleanup(sockets, 4);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 3);
rc = 0; s = sockets;
rc |= runtest(s++, any, 0, 1, NULL, 0);
rc |= runtest(s++, any, 0, 1, NULL, 0);
rc |= runtest(s++, any, 1, 0, NULL, EADDRINUSE);
rc |= runtest(s++, any, 0, 0, NULL, EADDRINUSE);
cleanup(sockets, 4);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 4);
rc = 0; s = sockets;
rc |= runtest(s++, local, 1, 0, NULL, 0);
rc |= runtest(s++, local, 1, 0, NULL, EADDRINUSE);
rc |= runtest(s++, local, 0, 1, NULL, EADDRINUSE);
cleanup(sockets, 3);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 5);
rc = 0; s = sockets;
rc |= runtest(s++, any, 1, 0, NULL, 0);
rc |= runtest(s++, any, 1, 0, NULL, EADDRINUSE);
rc |= runtest(s++, any, 0, 1, NULL, EADDRINUSE);
cleanup(sockets, 3);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 6);
return (test_rc);
}
int
mcast_reuse_testsuite(struct addrinfo *local, void *mr)
{
int test_rc, rc, *s;
int sockets[6];
int testnum = 1;
test_rc = 0;
rc = 0; s = sockets;
rc |= runtest(s++, local, 0, 0, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, EADDRINUSE);
rc |= runtest(s++, local, 0, 1, mr, EADDRINUSE);
rc |= runtest(s++, local, 1, 1, mr, EADDRINUSE);
cleanup(sockets, 4);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 1);
rc = 0; s = sockets;
rc |= runtest(s++, local, 0, 1, mr, 0);
rc |= runtest(s++, local, 0, 0, mr, EADDRINUSE);
rc |= runtest(s++, local, 0, 1, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 1, 1, mr, 0);
cleanup(sockets, 5);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 2);
rc = 0; s = sockets;
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 0, 0, mr, EADDRINUSE);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 0, 1, mr, 0);
rc |= runtest(s++, local, 1, 1, mr, 0);
cleanup(sockets, 5);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 3);
rc = 0; s = sockets;
rc |= runtest(s++, local, 1, 1, mr, 0);
rc |= runtest(s++, local, 0, 0, mr, EADDRINUSE);
rc |= runtest(s++, local, 0, 1, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 1, 1, mr, 0);
cleanup(sockets, 5);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 4);
#if 0
rc = 0; s = sockets;
rc |= runtest(s++, local, 1, 1, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 0, 1, mr, 0);
cleanup(sockets, 3);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 5);
rc = 0; s = sockets;
rc |= runtest(s++, local, 1, 1, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 1, 1, mr, 0);
rc |= runtest(s++, local, 0, 1, mr, 0);
cleanup(sockets, 5);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 6);
rc = 0; s = sockets;
rc |= runtest(s++, local, 1, 1, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 1, 1, mr, 0);
rc |= runtest(s++, local, 1, 0, mr, 0);
rc |= runtest(s++, local, 0, 1, mr, 0);
cleanup(sockets, 5);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 7);
#endif
return (test_rc);
}
int
mcast6_testsuite(struct addrinfo *local, struct ipv6_mreq *local_mreq,
struct addrinfo *any, struct ipv6_mreq *any_mreq)
{
int test_rc, rc, *s;
int sockets[4];
int testnum = 1;
test_rc = 0;
rc = 0; s = sockets;
rc |= runtest(s++, local, 0, 0, local_mreq, 0);
rc |= runtest(s++, any, 0, 0, any_mreq, EADDRINUSE);
rc |= runtest(s++, any, 1, 0, any_mreq, 0);
cleanup(sockets, 3);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 1);
rc = 0; s = sockets;
rc |= runtest(s++, any, 0, 0, any_mreq, 0);
rc |= runtest(s++, local, 0, 0, local_mreq, EADDRINUSE);
rc |= runtest(s++, local, 1, 0, local_mreq, 0);
cleanup(sockets, 3);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 2);
rc = 0; s = sockets;
rc |= runtest(s++, local, 0, 1, local_mreq, 0);
rc |= runtest(s++, local, 0, 1, local_mreq, 0);
rc |= runtest(s++, local, 1, 0, local_mreq, 0);
rc |= runtest(s++, local, 0, 0, local_mreq, EADDRINUSE);
cleanup(sockets, 4);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 3);
/*
* :: is not a multicast address, SO_REUSEADDR and SO_REUSEPORT
* keep their unicast semantics although we are binding on multicast
*/
rc = 0; s = sockets;
rc |= runtest(s++, any, 0, 1, any_mreq, 0);
rc |= runtest(s++, any, 0, 1, any_mreq, 0);
rc |= runtest(s++, any, 1, 0, any_mreq, EADDRINUSE);
rc |= runtest(s++, any, 0, 0, any_mreq, EADDRINUSE);
cleanup(sockets, 4);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 4);
rc = 0; s = sockets;
rc |= runtest(s++, local, 1, 0, local_mreq, 0);
rc |= runtest(s++, local, 1, 0, local_mreq, 0);
rc |= runtest(s++, local, 0, 1, local_mreq, 0);
rc |= runtest(s++, local, 0, 0, local_mreq, EADDRINUSE);
cleanup(sockets, 4);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 5);
/* See above */
rc = 0; s = sockets;
rc |= runtest(s++, any, 1, 0, any_mreq, 0);
rc |= runtest(s++, any, 1, 0, any_mreq, EADDRINUSE);
rc |= runtest(s++, any, 0, 1, any_mreq, EADDRINUSE);
rc |= runtest(s++, any, 0, 0, any_mreq, EADDRINUSE);
cleanup(sockets, 4);
test_rc |= rc;
if (rc)
warnx("%s : test #%d failed", __func__, 6);
return (test_rc);
}
int
main(int argc, char *argv[])
{
int error, rc;
char *baddr_s, *bport_s, *bmifa_s;
struct addrinfo hints, *baddr, *any, *mifa;
struct ifaddrs *ifap, *curifa;
struct ip_mreq local_imr, any_imr;
struct ipv6_mreq local_i6mr, any_i6mr;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
int *s;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICHOST | AI_NUMERICSERV | \
AI_PASSIVE;
hints.ai_socktype = SOCK_DGRAM;
baddr_s = argv[1];
bport_s = argv[2];
if ((error = getaddrinfo(baddr_s, bport_s, &hints, &baddr)))
errx(2, "getaddrinfo(%s,%s): %s", baddr_s, bport_s,
gai_strerror(error));
baddr->ai_canonname = baddr_s;
hints.ai_family = baddr->ai_family;
if ((error = getaddrinfo(NULL, bport_s, &hints, &any)))
errx(2, "getaddrinfo(NULL,%s): %s", bport_s,
gai_strerror(error));
any->ai_canonname = "*";
switch (baddr->ai_family) {
case AF_INET:
sin = (struct sockaddr_in *)baddr->ai_addr;
if (!IN_MULTICAST( ntohl(sin->sin_addr.s_addr) )) {
puts("executing unicast testsuite");
return unicast_testsuite(baddr, any);
}
bmifa_s = argv[3];
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICHOST;
if ((error = getaddrinfo(bmifa_s, NULL, &hints, &mifa)))
errx(2, "getaddrinfo(%s,NULL): %s", bmifa_s,
gai_strerror(error));
local_imr.imr_interface =
((struct sockaddr_in *)mifa->ai_addr)->sin_addr;
local_imr.imr_multiaddr =
((struct sockaddr_in *)baddr->ai_addr)->sin_addr;
puts("executing ipv4 multicast testsuite");
/* no 'any' mcast group in ipv4 */
return mcast_reuse_testsuite(baddr, &local_imr);
case AF_INET6:
sin6 = (struct sockaddr_in6 *)baddr->ai_addr;
if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
puts("executing unicast testsuite");
return unicast_testsuite(baddr, any);
}
bmifa_s = argv[3];
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICHOST;
if ((error = getaddrinfo(bmifa_s, NULL, &hints, &mifa)))
errx(2, "getaddrinfo(%s,NULL): %s", bmifa_s,
gai_strerror(error));
if (getifaddrs(&ifap))
err(2, "getifaddrs()");
curifa = ifap;
while (curifa) {
if (curifa->ifa_addr != NULL &&
memcmp(curifa->ifa_addr,
mifa->ai_addr,
mifa->ai_addrlen) == 0)
break;
curifa = curifa->ifa_next;
}
if (curifa == NULL)
errx(2, "no interface configured with %s", argv[4]);
local_i6mr.ipv6mr_interface =
if_nametoindex(curifa->ifa_name);
if (local_i6mr.ipv6mr_interface == 0)
errx(2, "unable to get \"%s\" index",
curifa->ifa_name);
freeifaddrs(ifap);
local_i6mr.ipv6mr_multiaddr =
((struct sockaddr_in6 *)baddr->ai_addr)->sin6_addr;
any_i6mr.ipv6mr_interface = local_i6mr.ipv6mr_interface;
any_i6mr.ipv6mr_multiaddr =
((struct sockaddr_in6 *)any->ai_addr)->sin6_addr;
puts("executing ipv6 multicast testsuite");
rc = 0;
rc |= mcast_reuse_testsuite(baddr, &local_i6mr);
if (geteuid() == 0)
rc |= mcast6_testsuite(baddr, &local_i6mr, any, &any_i6mr);
else
warnx("skipping mcast6_testsuite() due to insufficient privs, please run again as root");
return (rc);
default:
errx(2,"unknown AF");
}
return (2);
}
|