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