Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*-
 * Copyright (c) 2007 Bruce M. Simpson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Regression test utility for RFC 3678 Advanced Multicast API in FreeBSD.
 *
 * TODO: Test the SSM paths.
 * TODO: Support INET6. The code has been written to facilitate this later.
 * TODO: Merge multicast socket option tests from ipsockopt.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <pwd.h>
#include <setjmp.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>

#ifndef __SOCKUNION_DECLARED
union sockunion {
	struct sockaddr_storage	ss;
	struct sockaddr		sa;
	struct sockaddr_dl	sdl;
	struct sockaddr_in	sin;
#ifdef INET6
	struct sockaddr_in6	sin6;
#endif
};
typedef union sockunion sockunion_t;
#define __SOCKUNION_DECLARED
#endif /* __SOCKUNION_DECLARED */

#define ADDRBUF_LEN		16
#define DEFAULT_GROUP_STR	"238.1.1.0"
#define DEFAULT_IFNAME		"lo0"
#define DEFAULT_IFADDR_STR	"127.0.0.1"
#define DEFAULT_PORT		6698
#define DEFAULT_TIMEOUT		0		/* don't wait for traffic */
#define RXBUFSIZE		2048

static sockunion_t	 basegroup;
static const char	*basegroup_str = NULL;
static int		 dobindaddr = 0;
static int		 dodebug = 1;
static int		 doipv4 = 0;
static int		 domiscopts = 0;
static int		 dorandom = 0;
static int		 doreuseport = 0;
static int		 dossm = 0;
static int		 dossf = 0;
static int		 doverbose = 0;
static sockunion_t	 ifaddr;
static const char	*ifaddr_str = NULL;
static uint32_t		 ifindex = 0;
static const char	*ifname = NULL;
struct in_addr		*ipv4_sources = NULL;
static jmp_buf		 jmpbuf;
static size_t		 nmcastgroups = IP_MAX_MEMBERSHIPS;
static size_t		 nmcastsources = 0;
static uint16_t		 portno = DEFAULT_PORT;
static char		*progname = NULL;
struct sockaddr_storage	*ss_sources = NULL;
static uint32_t		 timeout = 0;

static int	do_asm_ipv4(void);
static int	do_asm_pim(void);
#ifdef notyet
static int	do_misc_opts(void);
#endif
static int	do_ssf_ipv4(void);
static int	do_ssf_pim(void);
static int	do_ssm_ipv4(void);
static int	do_ssm_pim(void);
static int	open_and_bind_socket(sockunion_t *);
static int	recv_loop_with_match(int, sockunion_t *, sockunion_t *);
static void	signal_handler(int);
static void	usage(void);

/*
 * Test the IPv4 set/getipv4sourcefilter() libc API functions.
 * Build a single socket.
 * Join a source group.
 * Repeatedly change the source filters via setipv4sourcefilter.
 * Read it back with getipv4sourcefilter up to IP_MAX_SOURCES
 * and check for inconsistency.
 */
static int
do_ssf_ipv4(void)
{

	fprintf(stderr, "not yet implemented\n");
	return (0);
}

/*
 * Test the protocol-independent set/getsourcefilter() functions.
 */
static int
do_ssf_pim(void)
{

	fprintf(stderr, "not yet implemented\n");
	return (0);
}

/*
 * Test the IPv4 ASM API.
 * Repeatedly join, block sources, unblock and leave groups.
 */
static int
do_asm_ipv4(void)
{
	int			 error;
	char			 gaddrbuf[ADDRBUF_LEN];
	int			 i;
	sockunion_t		 laddr;
	struct ip_mreq		 mreq;
	struct ip_mreq_source	 mreqs;
	in_addr_t		 ngroupbase;
	char			 saddrbuf[ADDRBUF_LEN];
	int			 sock;
	sockunion_t		 tmpgroup;
	sockunion_t		 tmpsource;

	memset(&mreq, 0, sizeof(struct ip_mreq));
	memset(&mreqs, 0, sizeof(struct ip_mreq_source));
	memset(&laddr, 0, sizeof(sockunion_t));

	if (dobindaddr) {
		laddr = ifaddr;
	} else {
		laddr.sin.sin_family = AF_INET;
		laddr.sin.sin_len = sizeof(struct sockaddr_in);
		laddr.sin.sin_addr.s_addr = INADDR_ANY;
	}
	laddr.sin.sin_port = htons(portno);

	tmpgroup = basegroup;
	ngroupbase = ntohl(basegroup.sin.sin_addr.s_addr) + 1;	/* XXX */
	tmpgroup.sin.sin_addr.s_addr = htonl(ngroupbase);

	sock = open_and_bind_socket(&laddr);
	if (sock == -1)
		return (EX_OSERR);

	for (i = 0; i < (signed)nmcastgroups; i++) {
		mreq.imr_multiaddr.s_addr = htonl((ngroupbase + i));
		mreq.imr_interface = ifaddr.sin.sin_addr;
		if (doverbose) {
			inet_ntop(AF_INET, &mreq.imr_multiaddr, gaddrbuf,
			    sizeof(gaddrbuf));
			fprintf(stderr, "IP_ADD_MEMBERSHIP %s %s\n",
			    gaddrbuf, inet_ntoa(mreq.imr_interface));
		}
		error = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
		    &mreq, sizeof(struct ip_mreq));
		if (error < 0) {
			warn("setsockopt IP_ADD_MEMBERSHIP");
			close(sock);
			return (EX_OSERR);
		}
	}

	/*
	 * If no test sources auto-generated or specified on command line,
	 * skip source filter portion of ASM test.
	*/
	if (nmcastsources == 0)
		goto skipsources;

	/*
	 * Begin blocking sources on the first group chosen.
	 */
	for (i = 0; i < (signed)nmcastsources; i++) {
		mreqs.imr_multiaddr = tmpgroup.sin.sin_addr;
		mreqs.imr_interface = ifaddr.sin.sin_addr;
		mreqs.imr_sourceaddr = ipv4_sources[i];
		if (doverbose) {
			inet_ntop(AF_INET, &mreqs.imr_multiaddr, gaddrbuf,
			    sizeof(gaddrbuf));
			inet_ntop(AF_INET, &mreqs.imr_sourceaddr, saddrbuf,
			    sizeof(saddrbuf));
			fprintf(stderr, "IP_BLOCK_SOURCE %s %s %s\n",
			    gaddrbuf, inet_ntoa(mreqs.imr_interface),
			    saddrbuf);
		}
		error = setsockopt(sock, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs,
		    sizeof(struct ip_mreq_source));
		if (error < 0) {
			warn("setsockopt IP_BLOCK_SOURCE");
			close(sock);
			return (EX_OSERR);
		}
	}

	/*
	 * Choose the first group and source for a match.
	 * Enter the I/O loop.
	 */
	memset(&tmpsource, 0, sizeof(sockunion_t));
	tmpsource.sin.sin_family = AF_INET;
	tmpsource.sin.sin_len = sizeof(struct sockaddr_in);
	tmpsource.sin.sin_addr = ipv4_sources[0];

	error = recv_loop_with_match(sock, &tmpgroup, &tmpsource);

	/*
	 * Unblock sources.
	 */
	for (i = nmcastsources-1; i >= 0; i--) {
		mreqs.imr_multiaddr = tmpgroup.sin.sin_addr;
		mreqs.imr_interface = ifaddr.sin.sin_addr;
		mreqs.imr_sourceaddr = ipv4_sources[i];
		if (doverbose) {
			inet_ntop(AF_INET, &mreqs.imr_multiaddr, gaddrbuf,
			    sizeof(gaddrbuf));
			inet_ntop(AF_INET, &mreqs.imr_sourceaddr, saddrbuf,
			    sizeof(saddrbuf));
			fprintf(stderr, "IP_UNBLOCK_SOURCE %s %s %s\n",
			    gaddrbuf, inet_ntoa(mreqs.imr_interface),
			    saddrbuf);
		}
		error = setsockopt(sock, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs,
		    sizeof(struct ip_mreq_source));
		if (error < 0) {
			warn("setsockopt IP_UNBLOCK_SOURCE");
			close(sock);
			return (EX_OSERR);
		}
	}

skipsources:
	/*
	 * Leave groups.
	 */
	for (i = nmcastgroups-1; i >= 0; i--) {
		mreq.imr_multiaddr.s_addr = htonl((ngroupbase + i));
		mreq.imr_interface = ifaddr.sin.sin_addr;
		if (doverbose) {
			inet_ntop(AF_INET, &mreq.imr_multiaddr, gaddrbuf,
			    sizeof(gaddrbuf));
			fprintf(stderr, "IP_DROP_MEMBERSHIP %s %s\n",
			    gaddrbuf, inet_ntoa(mreq.imr_interface));
		}
		error = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
		    &mreq, sizeof(struct ip_mreq));
		if (error < 0) {
			warn("setsockopt IP_DROP_MEMBERSHIP");
			close(sock);
			return (EX_OSERR);
		}
	}

	return (0);
}

static int
do_asm_pim(void)
{

	fprintf(stderr, "not yet implemented\n");
	return (0);
}

#ifdef notyet
/*
 * Test misceallaneous IPv4 options.
 */
static int
do_misc_opts(void)
{
	int sock;

	sock = open_and_bind_socket(NULL);
	if (sock == -1)
		return (EX_OSERR);
	test_ip_uchar(sock, socktypename, IP_MULTICAST_TTL,
	    "IP_MULTICAST_TTL", 1);
	close(sock);

	sock = open_and_bind_socket(NULL);
	if (sock == -1)
		return (EX_OSERR);
	test_ip_boolean(sock, socktypename, IP_MULTICAST_LOOP,
	    "IP_MULTICAST_LOOP", 1, BOOLEAN_ANYONE);
	close(sock);

	return (0);
}
#endif

/*
 * Test the IPv4 SSM API.
 */
static int
do_ssm_ipv4(void)
{

	fprintf(stderr, "not yet implemented\n");
	return (0);
}

/*
 * Test the protocol-independent SSM API with IPv4 addresses.
 */
static int
do_ssm_pim(void)
{

	fprintf(stderr, "not yet implemented\n");
	return (0);
}

int
main(int argc, char *argv[])
{
	struct addrinfo		 aih;
	struct addrinfo		*aip;
	int			 ch;
	int			 error;
	int			 exitval;
	size_t			 i;
	struct in_addr		*pina;
	struct sockaddr_storage	*pbss;

	ifname = DEFAULT_IFNAME;
	ifaddr_str = DEFAULT_IFADDR_STR;
	basegroup_str = DEFAULT_GROUP_STR;
	ifname = DEFAULT_IFNAME;
	portno = DEFAULT_PORT;
	basegroup.ss.ss_family = AF_UNSPEC;
	ifaddr.ss.ss_family = AF_UNSPEC;

	progname = basename(argv[0]);
	while ((ch = getopt(argc, argv, "4bg:i:I:mM:p:rsS:tT:v")) != -1) {
		switch (ch) {
		case '4':
			doipv4 = 1;
			break;
		case 'b':
			dobindaddr = 1;
			break;
		case 'g':
			basegroup_str = optarg;
			break;
		case 'i':
			ifname = optarg;
			break;
		case 'I':
			ifaddr_str = optarg;
			break;
		case 'm':
			usage();	/* notyet */
			/*NOTREACHED*/
			domiscopts = 1;
			break;
		case 'M':
			nmcastgroups = atoi(optarg);
			break;
		case 'p':
			portno = atoi(optarg);
			break;
		case 'r':
			doreuseport = 1;
			break;
		case 'S':
			nmcastsources = atoi(optarg);
			break;
		case 's':
			dossm = 1;
			break;
		case 't':
			dossf = 1;
			break;
		case 'T':
			timeout = atoi(optarg);
			break;
		case 'v':
			doverbose = 1;
			break;
		default:
			usage();
			break;
			/*NOTREACHED*/
		}
	}
	argc -= optind;
	argv += optind;

	memset(&aih, 0, sizeof(struct addrinfo));
	aih.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
	aih.ai_family = PF_INET;
	aih.ai_socktype = SOCK_DGRAM;
	aih.ai_protocol = IPPROTO_UDP;

	/*
	 * Fill out base group.
	 */
	aip = NULL;
	error = getaddrinfo(basegroup_str, NULL, &aih, &aip);
	if (error != 0) {
		fprintf(stderr, "%s: getaddrinfo: %s\n", progname,
		    gai_strerror(error));
		exit(EX_USAGE);
	}
	memcpy(&basegroup, aip->ai_addr, aip->ai_addrlen);
	if (dodebug) {
		fprintf(stderr, "debug: gai thinks %s is %s\n",
		    basegroup_str, inet_ntoa(basegroup.sin.sin_addr));
	}
	freeaddrinfo(aip);

	assert(basegroup.ss.ss_family == AF_INET);

	/*
	 * If user specified interface as an address, and protocol
	 * specific APIs were selected, parse it.
	 * Otherwise, parse interface index from name if protocol
	 * independent APIs were selected (the default).
	 */
	if (doipv4) {
		if (ifaddr_str == NULL) {
			warnx("required argument missing: ifaddr");
			usage();
			/* NOTREACHED */
		}
		aip = NULL;
		error = getaddrinfo(ifaddr_str, NULL, &aih, &aip);
		if (error != 0) {
			fprintf(stderr, "%s: getaddrinfo: %s\n", progname,
			    gai_strerror(error));
			exit(EX_USAGE);
		}
		memcpy(&ifaddr, aip->ai_addr, aip->ai_addrlen);
		if (dodebug) {
			fprintf(stderr, "debug: gai thinks %s is %s\n",
			    ifaddr_str, inet_ntoa(ifaddr.sin.sin_addr));
		}
		freeaddrinfo(aip);
	}

	if (!doipv4) {
		if (ifname == NULL) {
			warnx("required argument missing: ifname");
			usage();
			/* NOTREACHED */
		}
		ifindex = if_nametoindex(ifname);
		if (ifindex == 0)
			err(EX_USAGE, "if_nametoindex");
	}

	/*
	 * Introduce randomness into group base if specified.
	 */
	if (dorandom) {
		in_addr_t ngroupbase;

		srandomdev();
		ngroupbase = ntohl(basegroup.sin.sin_addr.s_addr);
		ngroupbase |= ((random() % ((1 << 11) - 1)) << 16);
		basegroup.sin.sin_addr.s_addr = htonl(ngroupbase);
	}

	if (argc > 0) {
		nmcastsources = argc;
		if (doipv4) {
			ipv4_sources = calloc(nmcastsources,
			    sizeof(struct in_addr));
			if (ipv4_sources == NULL) {
				exitval = EX_OSERR;
				goto out;
			}
		} else {
			ss_sources = calloc(nmcastsources,
			    sizeof(struct sockaddr_storage));
			if (ss_sources == NULL) {
				exitval = EX_OSERR;
				goto out;
			}
		}
	}

	/*
	 * Parse source list, if any were specified on the command line.
	 */
	assert(aih.ai_family == PF_INET);
	pbss = ss_sources;
	pina = ipv4_sources;
	for (i = 0; i < (size_t)argc; i++) {
		aip = NULL;
		error = getaddrinfo(argv[i], NULL, &aih, &aip);
		if (error != 0) {
			fprintf(stderr, "getaddrinfo: %s\n",
			    gai_strerror(error));
			exitval = EX_USAGE;
			goto out;
		}
		if (doipv4) {
			struct sockaddr_in *sin =
			    (struct sockaddr_in *)aip->ai_addr;
			*pina++ = sin->sin_addr;
		} else {
			memcpy(pbss++, aip->ai_addr, aip->ai_addrlen);
		}
		freeaddrinfo(aip);
	}

	/*
	 * Perform the regression tests which the user requested.
	 */
#ifdef notyet
	if (domiscopts) {
		exitval = do_misc_opts();
		if (exitval)
			goto out;
	}
#endif
	if (doipv4) {
		/* IPv4 protocol specific API tests */
		if (dossm) {
			/* Source-specific multicast */
			exitval = do_ssm_ipv4();
			if (exitval)
				goto out;
			if (dossf) {
				/* Do setipvsourcefilter() too */
				exitval = do_ssf_ipv4();
			}
		} else {
			/* Any-source multicast */
			exitval = do_asm_ipv4();
		}
	} else {
		/* Protocol independent API tests */
		if (dossm) {
			/* Source-specific multicast */
			exitval = do_ssm_pim();
			if (exitval)
				goto out;
			if (dossf) {
				/* Do setsourcefilter() too */
				exitval = do_ssf_pim();
			}
		} else {
			/* Any-source multicast */
			exitval = do_asm_pim();
		}
	}

out:
	if (ipv4_sources != NULL)
		free(ipv4_sources);

	if (ss_sources != NULL)
		free(ss_sources);

	exit(exitval);
}

static int
open_and_bind_socket(sockunion_t *bsu)
{
	int	 error, optval, sock;

	sock = -1;

	sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (sock == -1) {
		warn("socket");
		return (-1);
	}

	if (doreuseport) {
		optval = 1;
		if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval,
		    sizeof(optval)) < 0) {
			warn("setsockopt SO_REUSEPORT");
			close(sock);
			return (-1);
		}
	}

	if (bsu != NULL) {
		error = bind(sock, &bsu->sa, bsu->sa.sa_len);
		if (error == -1) {
			warn("bind");
			close(sock);
			return (-1);
		}
	}

	return (sock);
}

/*
 * Protocol-agnostic multicast I/O loop.
 *
 * Wait for 'timeout' seconds looking for traffic on group, so that manual
 * or automated regression tests (possibly running on another host) have an
 * opportunity to transmit within the group to test source filters.
 *
 * If the filter failed, this loop will report if we received traffic
 * from the source we elected to monitor.
 */
static int
recv_loop_with_match(int sock, sockunion_t *group, sockunion_t *source)
{
	int		 error;
	sockunion_t	 from;
	char		 groupname[NI_MAXHOST];
	ssize_t		 len;
	size_t		 npackets;
	int		 jmpretval;
	char		 rxbuf[RXBUFSIZE];
	char		 sourcename[NI_MAXHOST];

	assert(source->sa.sa_family == AF_INET);

	/*
	 * Return immediately if we don't need to wait for traffic.
	 */
	if (timeout == 0)
		return (0);

	error = getnameinfo(&group->sa, group->sa.sa_len, groupname,
	    NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
	if (error) {
		fprintf(stderr, "getnameinfo: %s\n", gai_strerror(error));
		return (error);
	}

	error = getnameinfo(&source->sa, source->sa.sa_len, sourcename,
	    NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
	if (error) {
		fprintf(stderr, "getnameinfo: %s\n", gai_strerror(error));
		return (error);
	}

	fprintf(stdout,
	    "Waiting %d seconds for inbound traffic on group %s\n"
	    "Expecting no traffic from blocked source: %s\n",
	    (int)timeout, groupname, sourcename);

	signal(SIGINT, signal_handler);
	signal(SIGALRM, signal_handler);

	error = 0;
	npackets = 0;
	alarm(timeout);
	while (0 == (jmpretval = setjmp(jmpbuf))) {
		len = recvfrom(sock, rxbuf, RXBUFSIZE, 0, &from.sa,
		    (socklen_t *)&from.sa.sa_len);
		if (dodebug) {
			fprintf(stderr, "debug: packet received from %s\n",
			    inet_ntoa(from.sin.sin_addr));
		}
		if (source &&
		    source->sin.sin_addr.s_addr == from.sin.sin_addr.s_addr)
			break;
		npackets++;
	}

	if (doverbose) {
		fprintf(stderr, "Number of datagrams received from "
		    "non-blocked sources: %d\n", (int)npackets);
	}

	switch (jmpretval) {
	case SIGALRM:	/* ok */
		break;
	case SIGINT:	/* go bye bye */
		fprintf(stderr, "interrupted\n");
		error = 20;
		break;
	case 0:		/* Broke out of loop; saw a bad source. */
		fprintf(stderr, "FAIL: got packet from blocked source\n");
		error = EX_IOERR;
		break;
	default:
		warnx("recvfrom");
		error = EX_OSERR;
		break;
	}

	signal(SIGINT, SIG_DFL);
	signal(SIGALRM, SIG_DFL);

	return (error);
}

static void
signal_handler(int signo)
{

	longjmp(jmpbuf, signo);
}

static void
usage(void)
{

	fprintf(stderr, "\nIP multicast regression test utility\n");
	fprintf(stderr,
"usage: %s [-4] [-b] [-g groupaddr] [-i ifname] [-I ifaddr] [-m]\n"
"       [-M ngroups] [-p portno] [-r] [-R] [-s] [-S nsources] [-t] [-T timeout]\n"
"       [-v] [blockaddr ...]\n\n", progname);
	fprintf(stderr, "-4: Use IPv4 API "
	                "(default: Use protocol-independent API)\n");
	fprintf(stderr, "-b: bind listening socket to ifaddr "
	    "(default: INADDR_ANY)\n");
	fprintf(stderr, "-g: Base IPv4 multicast group to join (default: %s)\n",
	    DEFAULT_GROUP_STR);
	fprintf(stderr, "-i: interface for multicast joins (default: %s)\n",
	    DEFAULT_IFNAME);
	fprintf(stderr, "-I: IPv4 address to join groups on, if using IPv4 "
	    "API\n    (default: %s)\n", DEFAULT_IFADDR_STR);
#ifdef notyet
	fprintf(stderr, "-m: Test misc IPv4 multicast socket options "
	    "(default: off)\n");
#endif
	fprintf(stderr, "-M: Number of multicast groups to join "
	    "(default: %d)\n", (int)nmcastgroups);
	fprintf(stderr, "-p: Set local and remote port (default: %d)\n",
	    DEFAULT_PORT);
	fprintf(stderr, "-r: Set SO_REUSEPORT on (default: off)\n");
	fprintf(stderr, "-R: Randomize groups/sources (default: off)\n");
	fprintf(stderr, "-s: Test source-specific API "
	    "(default: test any-source API)\n");
	fprintf(stderr, "-S: Number of multicast sources to generate if\n"
	    "    none specified on command line (default: %d)\n",
	    (int)nmcastsources);
	fprintf(stderr, "-t: Test get/setNsourcefilter() (default: off)\n");
	fprintf(stderr, "-T: Timeout to wait for blocked traffic on first "
	    "group (default: %d)\n", DEFAULT_TIMEOUT);
	fprintf(stderr, "-v: Be verbose (default: off)\n");
	fprintf(stderr, "\nRemaining arguments are treated as a list of IPv4 "
	    "sources to filter.\n\n");

	exit(EX_USAGE);
}