xref: /freebsd/contrib/libpcap/testprogs/nonblocktest.c (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
1*6f9cba8fSJoseph Mingrone /*
2*6f9cba8fSJoseph Mingrone  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3*6f9cba8fSJoseph Mingrone  *	The Regents of the University of California.  All rights reserved.
4*6f9cba8fSJoseph Mingrone  *
5*6f9cba8fSJoseph Mingrone  * Redistribution and use in source and binary forms, with or without
6*6f9cba8fSJoseph Mingrone  * modification, are permitted provided that: (1) source code distributions
7*6f9cba8fSJoseph Mingrone  * retain the above copyright notice and this paragraph in its entirety, (2)
8*6f9cba8fSJoseph Mingrone  * distributions including binary code include the above copyright notice and
9*6f9cba8fSJoseph Mingrone  * this paragraph in its entirety in the documentation or other materials
10*6f9cba8fSJoseph Mingrone  * provided with the distribution, and (3) all advertising materials mentioning
11*6f9cba8fSJoseph Mingrone  * features or use of this software display the following acknowledgement:
12*6f9cba8fSJoseph Mingrone  * ``This product includes software developed by the University of California,
13*6f9cba8fSJoseph Mingrone  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*6f9cba8fSJoseph Mingrone  * the University nor the names of its contributors may be used to endorse
15*6f9cba8fSJoseph Mingrone  * or promote products derived from this software without specific prior
16*6f9cba8fSJoseph Mingrone  * written permission.
17*6f9cba8fSJoseph Mingrone  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*6f9cba8fSJoseph Mingrone  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*6f9cba8fSJoseph Mingrone  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*6f9cba8fSJoseph Mingrone  */
21*6f9cba8fSJoseph Mingrone 
22*6f9cba8fSJoseph Mingrone #include "varattrs.h"
23*6f9cba8fSJoseph Mingrone 
24*6f9cba8fSJoseph Mingrone /*
25*6f9cba8fSJoseph Mingrone  * Tests for pcap_set_nonblock / pcap_get_nonblock:
26*6f9cba8fSJoseph Mingrone  * - idempotency
27*6f9cba8fSJoseph Mingrone  * - set/get are symmetric
28*6f9cba8fSJoseph Mingrone  * - get returns the same before/after activate
29*6f9cba8fSJoseph Mingrone  * - pcap_breakloop works after setting nonblock on and then off
30*6f9cba8fSJoseph Mingrone  *
31*6f9cba8fSJoseph Mingrone  * Really this is meant to
32*6f9cba8fSJoseph Mingrone  * be run manually under strace, to check for extra
33*6f9cba8fSJoseph Mingrone  * calls to eventfd or close.
34*6f9cba8fSJoseph Mingrone  */
35*6f9cba8fSJoseph Mingrone #include <pcap.h>
36*6f9cba8fSJoseph Mingrone #include <stdio.h>
37*6f9cba8fSJoseph Mingrone #include <stdlib.h>
38*6f9cba8fSJoseph Mingrone #include <string.h>
39*6f9cba8fSJoseph Mingrone #include <stdarg.h>
40*6f9cba8fSJoseph Mingrone #include <unistd.h>
41*6f9cba8fSJoseph Mingrone #include <sys/types.h>
42*6f9cba8fSJoseph Mingrone #include <sys/stat.h>
43*6f9cba8fSJoseph Mingrone #include <fcntl.h>
44*6f9cba8fSJoseph Mingrone 
45*6f9cba8fSJoseph Mingrone static pcap_t *pd;
46*6f9cba8fSJoseph Mingrone static char *program_name = "nonblocktest";
47*6f9cba8fSJoseph Mingrone /* Forwards */
48*6f9cba8fSJoseph Mingrone static void PCAP_NORETURN usage(void);
49*6f9cba8fSJoseph Mingrone static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
50*6f9cba8fSJoseph Mingrone static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
51*6f9cba8fSJoseph Mingrone 
52*6f9cba8fSJoseph Mingrone /* VARARGS */
53*6f9cba8fSJoseph Mingrone static void
error(const char * fmt,...)54*6f9cba8fSJoseph Mingrone error(const char *fmt, ...)
55*6f9cba8fSJoseph Mingrone {
56*6f9cba8fSJoseph Mingrone 	va_list ap;
57*6f9cba8fSJoseph Mingrone 
58*6f9cba8fSJoseph Mingrone 	(void)fprintf(stderr, "%s: ", program_name);
59*6f9cba8fSJoseph Mingrone 	va_start(ap, fmt);
60*6f9cba8fSJoseph Mingrone 	(void)vfprintf(stderr, fmt, ap);
61*6f9cba8fSJoseph Mingrone 	va_end(ap);
62*6f9cba8fSJoseph Mingrone 	if (*fmt) {
63*6f9cba8fSJoseph Mingrone 		fmt += strlen(fmt);
64*6f9cba8fSJoseph Mingrone 		if (fmt[-1] != '\n')
65*6f9cba8fSJoseph Mingrone 			(void)fputc('\n', stderr);
66*6f9cba8fSJoseph Mingrone 	}
67*6f9cba8fSJoseph Mingrone 	exit(1);
68*6f9cba8fSJoseph Mingrone 	/* NOTREACHED */
69*6f9cba8fSJoseph Mingrone }
70*6f9cba8fSJoseph Mingrone 
71*6f9cba8fSJoseph Mingrone /* VARARGS */
72*6f9cba8fSJoseph Mingrone static void
warning(const char * fmt,...)73*6f9cba8fSJoseph Mingrone warning(const char *fmt, ...)
74*6f9cba8fSJoseph Mingrone {
75*6f9cba8fSJoseph Mingrone 	va_list ap;
76*6f9cba8fSJoseph Mingrone 
77*6f9cba8fSJoseph Mingrone 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
78*6f9cba8fSJoseph Mingrone 	va_start(ap, fmt);
79*6f9cba8fSJoseph Mingrone 	(void)vfprintf(stderr, fmt, ap);
80*6f9cba8fSJoseph Mingrone 	va_end(ap);
81*6f9cba8fSJoseph Mingrone 	if (*fmt) {
82*6f9cba8fSJoseph Mingrone 		fmt += strlen(fmt);
83*6f9cba8fSJoseph Mingrone 		if (fmt[-1] != '\n')
84*6f9cba8fSJoseph Mingrone 			(void)fputc('\n', stderr);
85*6f9cba8fSJoseph Mingrone 	}
86*6f9cba8fSJoseph Mingrone }
87*6f9cba8fSJoseph Mingrone 
88*6f9cba8fSJoseph Mingrone static void
usage(void)89*6f9cba8fSJoseph Mingrone usage(void)
90*6f9cba8fSJoseph Mingrone {
91*6f9cba8fSJoseph Mingrone 	(void)fprintf(stderr, "Usage: %s [ -i interface ]\n",
92*6f9cba8fSJoseph Mingrone 	    program_name);
93*6f9cba8fSJoseph Mingrone 	exit(1);
94*6f9cba8fSJoseph Mingrone }
95*6f9cba8fSJoseph Mingrone 
96*6f9cba8fSJoseph Mingrone static void
breakme(u_char * user _U_,const struct pcap_pkthdr * h _U_,const u_char * sp _U_)97*6f9cba8fSJoseph Mingrone breakme(u_char *user _U_, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
98*6f9cba8fSJoseph Mingrone {
99*6f9cba8fSJoseph Mingrone 	warning("using pcap_breakloop()");
100*6f9cba8fSJoseph Mingrone 	pcap_breakloop(pd);
101*6f9cba8fSJoseph Mingrone }
102*6f9cba8fSJoseph Mingrone 
103*6f9cba8fSJoseph Mingrone int
main(int argc,char ** argv)104*6f9cba8fSJoseph Mingrone main(int argc, char **argv)
105*6f9cba8fSJoseph Mingrone {
106*6f9cba8fSJoseph Mingrone 	int status, op, i, ret;
107*6f9cba8fSJoseph Mingrone 	char *device;
108*6f9cba8fSJoseph Mingrone 	pcap_if_t *devlist;
109*6f9cba8fSJoseph Mingrone 	char ebuf[PCAP_ERRBUF_SIZE];
110*6f9cba8fSJoseph Mingrone 
111*6f9cba8fSJoseph Mingrone 	device = NULL;
112*6f9cba8fSJoseph Mingrone 	while ((op = getopt(argc, argv, "i:sptnq")) != -1) {
113*6f9cba8fSJoseph Mingrone 		switch (op) {
114*6f9cba8fSJoseph Mingrone 
115*6f9cba8fSJoseph Mingrone 		case 'i':
116*6f9cba8fSJoseph Mingrone 			device = optarg;
117*6f9cba8fSJoseph Mingrone 			break;
118*6f9cba8fSJoseph Mingrone 
119*6f9cba8fSJoseph Mingrone 		default:
120*6f9cba8fSJoseph Mingrone 			usage();
121*6f9cba8fSJoseph Mingrone 			/* NOTREACHED */
122*6f9cba8fSJoseph Mingrone 		}
123*6f9cba8fSJoseph Mingrone 	}
124*6f9cba8fSJoseph Mingrone 	if (device == NULL) {
125*6f9cba8fSJoseph Mingrone 		if (pcap_findalldevs(&devlist, ebuf) == -1)
126*6f9cba8fSJoseph Mingrone 			error("%s", ebuf);
127*6f9cba8fSJoseph Mingrone 		if (devlist == NULL)
128*6f9cba8fSJoseph Mingrone 			error("no interfaces available for capture");
129*6f9cba8fSJoseph Mingrone 		device = strdup(devlist->name);
130*6f9cba8fSJoseph Mingrone 		warning("listening on %s", device);
131*6f9cba8fSJoseph Mingrone 		pcap_freealldevs(devlist);
132*6f9cba8fSJoseph Mingrone 	}
133*6f9cba8fSJoseph Mingrone 	*ebuf = '\0';
134*6f9cba8fSJoseph Mingrone 	pd = pcap_create(device, ebuf);
135*6f9cba8fSJoseph Mingrone 	if (pd == NULL)
136*6f9cba8fSJoseph Mingrone 		error("%s", ebuf);
137*6f9cba8fSJoseph Mingrone 	else if (*ebuf)
138*6f9cba8fSJoseph Mingrone 		warning("%s", ebuf);
139*6f9cba8fSJoseph Mingrone 	/* set nonblock before activate */
140*6f9cba8fSJoseph Mingrone 	if (pcap_setnonblock(pd, 1, ebuf) < 0)
141*6f9cba8fSJoseph Mingrone 		error("pcap_setnonblock failed: %s", ebuf);
142*6f9cba8fSJoseph Mingrone 	/* getnonblock just returns "not activated yet" */
143*6f9cba8fSJoseph Mingrone 	ret = pcap_getnonblock(pd, ebuf);
144*6f9cba8fSJoseph Mingrone 	if (ret != PCAP_ERROR_NOT_ACTIVATED)
145*6f9cba8fSJoseph Mingrone 		error("pcap_getnonblock unexpectedly succeeded");
146*6f9cba8fSJoseph Mingrone 	if ((status = pcap_activate(pd)) < 0)
147*6f9cba8fSJoseph Mingrone 		error("pcap_activate failed");
148*6f9cba8fSJoseph Mingrone 	ret = pcap_getnonblock(pd, ebuf);
149*6f9cba8fSJoseph Mingrone 	if (ret != 1)
150*6f9cba8fSJoseph Mingrone 		error( "pcap_getnonblock did not return nonblocking" );
151*6f9cba8fSJoseph Mingrone 
152*6f9cba8fSJoseph Mingrone 	/* Set nonblock multiple times, ensure with strace that it's a noop */
153*6f9cba8fSJoseph Mingrone 	for (i=0; i<10; i++) {
154*6f9cba8fSJoseph Mingrone 		if (pcap_setnonblock(pd, 1, ebuf) < 0)
155*6f9cba8fSJoseph Mingrone 			error("pcap_setnonblock failed: %s", ebuf);
156*6f9cba8fSJoseph Mingrone 		ret = pcap_getnonblock(pd, ebuf);
157*6f9cba8fSJoseph Mingrone 		if (ret != 1)
158*6f9cba8fSJoseph Mingrone 			error( "pcap_getnonblock did not return nonblocking" );
159*6f9cba8fSJoseph Mingrone 	}
160*6f9cba8fSJoseph Mingrone 	/* Set block multiple times, ensure with strace that it's a noop */
161*6f9cba8fSJoseph Mingrone 	for (i=0; i<10; i++) {
162*6f9cba8fSJoseph Mingrone 		if (pcap_setnonblock(pd, 0, ebuf) < 0)
163*6f9cba8fSJoseph Mingrone 			error("pcap_setnonblock failed: %s", ebuf);
164*6f9cba8fSJoseph Mingrone 		ret = pcap_getnonblock(pd, ebuf);
165*6f9cba8fSJoseph Mingrone 		if (ret != 0)
166*6f9cba8fSJoseph Mingrone 			error( "pcap_getnonblock did not return blocking" );
167*6f9cba8fSJoseph Mingrone 	}
168*6f9cba8fSJoseph Mingrone 
169*6f9cba8fSJoseph Mingrone 	/* Now pcap_loop forever, with a callback that
170*6f9cba8fSJoseph Mingrone 	 * uses pcap_breakloop to get out of forever */
171*6f9cba8fSJoseph Mingrone 	pcap_loop(pd, -1, breakme, NULL);
172*6f9cba8fSJoseph Mingrone 
173*6f9cba8fSJoseph Mingrone         /* Now test that pcap_setnonblock fails if we can't open the
174*6f9cba8fSJoseph Mingrone          * eventfd. */
175*6f9cba8fSJoseph Mingrone         if (pcap_setnonblock(pd, 1, ebuf) < 0)
176*6f9cba8fSJoseph Mingrone                 error("pcap_setnonblock failed: %s", ebuf);
177*6f9cba8fSJoseph Mingrone         while (1) {
178*6f9cba8fSJoseph Mingrone                 ret = open("/dev/null", O_RDONLY);
179*6f9cba8fSJoseph Mingrone                 if (ret < 0)
180*6f9cba8fSJoseph Mingrone                         break;
181*6f9cba8fSJoseph Mingrone         }
182*6f9cba8fSJoseph Mingrone         ret = pcap_setnonblock(pd, 0, ebuf);
183*6f9cba8fSJoseph Mingrone         if (ret == 0)
184*6f9cba8fSJoseph Mingrone                 error("pcap_setnonblock succeeded even though file table is full");
185*6f9cba8fSJoseph Mingrone         else
186*6f9cba8fSJoseph Mingrone                 warning("pcap_setnonblock failed as expected: %s", ebuf);
187*6f9cba8fSJoseph Mingrone }
188