xref: /aosp_15_r20/external/libpcap/testprogs/opentest.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 #ifndef lint
25*8b26181fSAndroid Build Coastguard Worker static const char copyright[] _U_ =
26*8b26181fSAndroid Build Coastguard Worker     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27*8b26181fSAndroid Build Coastguard Worker The Regents of the University of California.  All rights reserved.\n";
28*8b26181fSAndroid Build Coastguard Worker #endif
29*8b26181fSAndroid Build Coastguard Worker 
30*8b26181fSAndroid Build Coastguard Worker #include <pcap.h>
31*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
32*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
33*8b26181fSAndroid Build Coastguard Worker #include <string.h>
34*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h>
35*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
36*8b26181fSAndroid Build Coastguard Worker   #include "getopt.h"
37*8b26181fSAndroid Build Coastguard Worker #else
38*8b26181fSAndroid Build Coastguard Worker   #include <unistd.h>
39*8b26181fSAndroid Build Coastguard Worker #endif
40*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
41*8b26181fSAndroid Build Coastguard Worker 
42*8b26181fSAndroid Build Coastguard Worker #include "pcap/funcattrs.h"
43*8b26181fSAndroid Build Coastguard Worker 
44*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
45*8b26181fSAndroid Build Coastguard Worker   #include "portability.h"
46*8b26181fSAndroid Build Coastguard Worker #endif
47*8b26181fSAndroid Build Coastguard Worker 
48*8b26181fSAndroid Build Coastguard Worker #define MAXIMUM_SNAPLEN		262144
49*8b26181fSAndroid Build Coastguard Worker 
50*8b26181fSAndroid Build Coastguard Worker static char *program_name;
51*8b26181fSAndroid Build Coastguard Worker 
52*8b26181fSAndroid Build Coastguard Worker /* Forwards */
53*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN usage(void);
54*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
55*8b26181fSAndroid Build Coastguard Worker static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
56*8b26181fSAndroid Build Coastguard Worker 
57*8b26181fSAndroid Build Coastguard Worker int
main(int argc,char ** argv)58*8b26181fSAndroid Build Coastguard Worker main(int argc, char **argv)
59*8b26181fSAndroid Build Coastguard Worker {
60*8b26181fSAndroid Build Coastguard Worker 	register int op;
61*8b26181fSAndroid Build Coastguard Worker 	register char *cp, *device;
62*8b26181fSAndroid Build Coastguard Worker 	int dorfmon, dopromisc, snaplen, useactivate, bufsize;
63*8b26181fSAndroid Build Coastguard Worker 	char ebuf[PCAP_ERRBUF_SIZE];
64*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *devlist;
65*8b26181fSAndroid Build Coastguard Worker 	pcap_t *pd;
66*8b26181fSAndroid Build Coastguard Worker 	int status = 0;
67*8b26181fSAndroid Build Coastguard Worker 
68*8b26181fSAndroid Build Coastguard Worker 	device = NULL;
69*8b26181fSAndroid Build Coastguard Worker 	dorfmon = 0;
70*8b26181fSAndroid Build Coastguard Worker 	dopromisc = 0;
71*8b26181fSAndroid Build Coastguard Worker 	snaplen = MAXIMUM_SNAPLEN;
72*8b26181fSAndroid Build Coastguard Worker 	bufsize = 0;
73*8b26181fSAndroid Build Coastguard Worker 	useactivate = 0;
74*8b26181fSAndroid Build Coastguard Worker 	if ((cp = strrchr(argv[0], '/')) != NULL)
75*8b26181fSAndroid Build Coastguard Worker 		program_name = cp + 1;
76*8b26181fSAndroid Build Coastguard Worker 	else
77*8b26181fSAndroid Build Coastguard Worker 		program_name = argv[0];
78*8b26181fSAndroid Build Coastguard Worker 
79*8b26181fSAndroid Build Coastguard Worker 	opterr = 0;
80*8b26181fSAndroid Build Coastguard Worker 	while ((op = getopt(argc, argv, "i:Ips:aB:")) != -1) {
81*8b26181fSAndroid Build Coastguard Worker 		switch (op) {
82*8b26181fSAndroid Build Coastguard Worker 
83*8b26181fSAndroid Build Coastguard Worker 		case 'i':
84*8b26181fSAndroid Build Coastguard Worker 			device = strdup(optarg);
85*8b26181fSAndroid Build Coastguard Worker 			break;
86*8b26181fSAndroid Build Coastguard Worker 
87*8b26181fSAndroid Build Coastguard Worker 		case 'I':
88*8b26181fSAndroid Build Coastguard Worker 			dorfmon = 1;
89*8b26181fSAndroid Build Coastguard Worker 			useactivate = 1;	/* required for rfmon */
90*8b26181fSAndroid Build Coastguard Worker 			break;
91*8b26181fSAndroid Build Coastguard Worker 
92*8b26181fSAndroid Build Coastguard Worker 		case 'p':
93*8b26181fSAndroid Build Coastguard Worker 			dopromisc = 1;
94*8b26181fSAndroid Build Coastguard Worker 			break;
95*8b26181fSAndroid Build Coastguard Worker 
96*8b26181fSAndroid Build Coastguard Worker 		case 's': {
97*8b26181fSAndroid Build Coastguard Worker 			char *end;
98*8b26181fSAndroid Build Coastguard Worker 			long long_snaplen;
99*8b26181fSAndroid Build Coastguard Worker 
100*8b26181fSAndroid Build Coastguard Worker 			long_snaplen = strtol(optarg, &end, 0);
101*8b26181fSAndroid Build Coastguard Worker 			if (optarg == end || *end != '\0'
102*8b26181fSAndroid Build Coastguard Worker 			    || long_snaplen < 0
103*8b26181fSAndroid Build Coastguard Worker 			    || long_snaplen > MAXIMUM_SNAPLEN)
104*8b26181fSAndroid Build Coastguard Worker 				error("invalid snaplen %s", optarg);
105*8b26181fSAndroid Build Coastguard Worker 			else {
106*8b26181fSAndroid Build Coastguard Worker 				if (snaplen == 0)
107*8b26181fSAndroid Build Coastguard Worker 					snaplen = MAXIMUM_SNAPLEN;
108*8b26181fSAndroid Build Coastguard Worker 				else
109*8b26181fSAndroid Build Coastguard Worker 					snaplen = (int)long_snaplen;
110*8b26181fSAndroid Build Coastguard Worker 			}
111*8b26181fSAndroid Build Coastguard Worker 			break;
112*8b26181fSAndroid Build Coastguard Worker 		}
113*8b26181fSAndroid Build Coastguard Worker 
114*8b26181fSAndroid Build Coastguard Worker 		case 'B':
115*8b26181fSAndroid Build Coastguard Worker 			bufsize = atoi(optarg)*1024;
116*8b26181fSAndroid Build Coastguard Worker 			if (bufsize <= 0)
117*8b26181fSAndroid Build Coastguard Worker 				error("invalid packet buffer size %s", optarg);
118*8b26181fSAndroid Build Coastguard Worker 			useactivate = 1;	/* required for bufsize */
119*8b26181fSAndroid Build Coastguard Worker 			break;
120*8b26181fSAndroid Build Coastguard Worker 
121*8b26181fSAndroid Build Coastguard Worker 		case 'a':
122*8b26181fSAndroid Build Coastguard Worker 			useactivate = 1;
123*8b26181fSAndroid Build Coastguard Worker 			break;
124*8b26181fSAndroid Build Coastguard Worker 
125*8b26181fSAndroid Build Coastguard Worker 		default:
126*8b26181fSAndroid Build Coastguard Worker 			usage();
127*8b26181fSAndroid Build Coastguard Worker 			/* NOTREACHED */
128*8b26181fSAndroid Build Coastguard Worker 		}
129*8b26181fSAndroid Build Coastguard Worker 	}
130*8b26181fSAndroid Build Coastguard Worker 
131*8b26181fSAndroid Build Coastguard Worker 	if (device == NULL) {
132*8b26181fSAndroid Build Coastguard Worker 		if (pcap_findalldevs(&devlist, ebuf) == -1)
133*8b26181fSAndroid Build Coastguard Worker 			error("%s", ebuf);
134*8b26181fSAndroid Build Coastguard Worker 		if (devlist == NULL)
135*8b26181fSAndroid Build Coastguard Worker 			error("no interfaces available for capture");
136*8b26181fSAndroid Build Coastguard Worker 		device = strdup(devlist->name);
137*8b26181fSAndroid Build Coastguard Worker 		pcap_freealldevs(devlist);
138*8b26181fSAndroid Build Coastguard Worker 	}
139*8b26181fSAndroid Build Coastguard Worker 	if (useactivate) {
140*8b26181fSAndroid Build Coastguard Worker 		pd = pcap_create(device, ebuf);
141*8b26181fSAndroid Build Coastguard Worker 		if (pd == NULL)
142*8b26181fSAndroid Build Coastguard Worker 			error("%s: pcap_create failed: %s", device, ebuf);
143*8b26181fSAndroid Build Coastguard Worker 		status = pcap_set_snaplen(pd, snaplen);
144*8b26181fSAndroid Build Coastguard Worker 		if (status != 0)
145*8b26181fSAndroid Build Coastguard Worker 			error("%s: pcap_set_snaplen failed: %s",
146*8b26181fSAndroid Build Coastguard Worker 			    device, pcap_statustostr(status));
147*8b26181fSAndroid Build Coastguard Worker 		if (dopromisc) {
148*8b26181fSAndroid Build Coastguard Worker 			status = pcap_set_promisc(pd, 1);
149*8b26181fSAndroid Build Coastguard Worker 			if (status != 0)
150*8b26181fSAndroid Build Coastguard Worker 				error("%s: pcap_set_promisc failed: %s",
151*8b26181fSAndroid Build Coastguard Worker 				    device, pcap_statustostr(status));
152*8b26181fSAndroid Build Coastguard Worker 		}
153*8b26181fSAndroid Build Coastguard Worker 		if (dorfmon) {
154*8b26181fSAndroid Build Coastguard Worker 			status = pcap_set_rfmon(pd, 1);
155*8b26181fSAndroid Build Coastguard Worker 			if (status != 0)
156*8b26181fSAndroid Build Coastguard Worker 				error("%s: pcap_set_rfmon failed: %s",
157*8b26181fSAndroid Build Coastguard Worker 				    device, pcap_statustostr(status));
158*8b26181fSAndroid Build Coastguard Worker 		}
159*8b26181fSAndroid Build Coastguard Worker 		status = pcap_set_timeout(pd, 1000);
160*8b26181fSAndroid Build Coastguard Worker 		if (status != 0)
161*8b26181fSAndroid Build Coastguard Worker 			error("%s: pcap_set_timeout failed: %s",
162*8b26181fSAndroid Build Coastguard Worker 			    device, pcap_statustostr(status));
163*8b26181fSAndroid Build Coastguard Worker 		if (bufsize != 0) {
164*8b26181fSAndroid Build Coastguard Worker 			status = pcap_set_buffer_size(pd, bufsize);
165*8b26181fSAndroid Build Coastguard Worker 			if (status != 0)
166*8b26181fSAndroid Build Coastguard Worker 				error("%s: pcap_set_buffer_size failed: %s",
167*8b26181fSAndroid Build Coastguard Worker 				    device, pcap_statustostr(status));
168*8b26181fSAndroid Build Coastguard Worker 		}
169*8b26181fSAndroid Build Coastguard Worker 		status = pcap_activate(pd);
170*8b26181fSAndroid Build Coastguard Worker 		if (status < 0) {
171*8b26181fSAndroid Build Coastguard Worker 			/*
172*8b26181fSAndroid Build Coastguard Worker 			 * pcap_activate() failed.
173*8b26181fSAndroid Build Coastguard Worker 			 */
174*8b26181fSAndroid Build Coastguard Worker 			error("%s: %s\n(%s)", device,
175*8b26181fSAndroid Build Coastguard Worker 			    pcap_statustostr(status), pcap_geterr(pd));
176*8b26181fSAndroid Build Coastguard Worker 		} else if (status > 0) {
177*8b26181fSAndroid Build Coastguard Worker 			/*
178*8b26181fSAndroid Build Coastguard Worker 			 * pcap_activate() succeeded, but it's warning us
179*8b26181fSAndroid Build Coastguard Worker 			 * of a problem it had.
180*8b26181fSAndroid Build Coastguard Worker 			 */
181*8b26181fSAndroid Build Coastguard Worker 			warning("%s: %s\n(%s)", device,
182*8b26181fSAndroid Build Coastguard Worker 			    pcap_statustostr(status), pcap_geterr(pd));
183*8b26181fSAndroid Build Coastguard Worker 		} else
184*8b26181fSAndroid Build Coastguard Worker 			printf("%s opened successfully\n", device);
185*8b26181fSAndroid Build Coastguard Worker 	} else {
186*8b26181fSAndroid Build Coastguard Worker 		*ebuf = '\0';
187*8b26181fSAndroid Build Coastguard Worker 		pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
188*8b26181fSAndroid Build Coastguard Worker 		if (pd == NULL)
189*8b26181fSAndroid Build Coastguard Worker 			error("%s", ebuf);
190*8b26181fSAndroid Build Coastguard Worker 		else if (*ebuf)
191*8b26181fSAndroid Build Coastguard Worker 			warning("%s", ebuf);
192*8b26181fSAndroid Build Coastguard Worker 		else
193*8b26181fSAndroid Build Coastguard Worker 			printf("%s opened successfully\n", device);
194*8b26181fSAndroid Build Coastguard Worker 	}
195*8b26181fSAndroid Build Coastguard Worker 	free(device);
196*8b26181fSAndroid Build Coastguard Worker 	pcap_close(pd);
197*8b26181fSAndroid Build Coastguard Worker 	exit(status < 0 ? 1 : 0);
198*8b26181fSAndroid Build Coastguard Worker }
199*8b26181fSAndroid Build Coastguard Worker 
200*8b26181fSAndroid Build Coastguard Worker static void
usage(void)201*8b26181fSAndroid Build Coastguard Worker usage(void)
202*8b26181fSAndroid Build Coastguard Worker {
203*8b26181fSAndroid Build Coastguard Worker 	(void)fprintf(stderr,
204*8b26181fSAndroid Build Coastguard Worker 	    "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
205*8b26181fSAndroid Build Coastguard Worker 	    program_name);
206*8b26181fSAndroid Build Coastguard Worker 	exit(1);
207*8b26181fSAndroid Build Coastguard Worker }
208*8b26181fSAndroid Build Coastguard Worker 
209*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
210*8b26181fSAndroid Build Coastguard Worker static void
error(const char * fmt,...)211*8b26181fSAndroid Build Coastguard Worker error(const char *fmt, ...)
212*8b26181fSAndroid Build Coastguard Worker {
213*8b26181fSAndroid Build Coastguard Worker 	va_list ap;
214*8b26181fSAndroid Build Coastguard Worker 
215*8b26181fSAndroid Build Coastguard Worker 	(void)fprintf(stderr, "%s: ", program_name);
216*8b26181fSAndroid Build Coastguard Worker 	va_start(ap, fmt);
217*8b26181fSAndroid Build Coastguard Worker 	(void)vfprintf(stderr, fmt, ap);
218*8b26181fSAndroid Build Coastguard Worker 	va_end(ap);
219*8b26181fSAndroid Build Coastguard Worker 	if (*fmt) {
220*8b26181fSAndroid Build Coastguard Worker 		fmt += strlen(fmt);
221*8b26181fSAndroid Build Coastguard Worker 		if (fmt[-1] != '\n')
222*8b26181fSAndroid Build Coastguard Worker 			(void)fputc('\n', stderr);
223*8b26181fSAndroid Build Coastguard Worker 	}
224*8b26181fSAndroid Build Coastguard Worker 	exit(1);
225*8b26181fSAndroid Build Coastguard Worker 	/* NOTREACHED */
226*8b26181fSAndroid Build Coastguard Worker }
227*8b26181fSAndroid Build Coastguard Worker 
228*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
229*8b26181fSAndroid Build Coastguard Worker static void
warning(const char * fmt,...)230*8b26181fSAndroid Build Coastguard Worker warning(const char *fmt, ...)
231*8b26181fSAndroid Build Coastguard Worker {
232*8b26181fSAndroid Build Coastguard Worker 	va_list ap;
233*8b26181fSAndroid Build Coastguard Worker 
234*8b26181fSAndroid Build Coastguard Worker 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
235*8b26181fSAndroid Build Coastguard Worker 	va_start(ap, fmt);
236*8b26181fSAndroid Build Coastguard Worker 	(void)vfprintf(stderr, fmt, ap);
237*8b26181fSAndroid Build Coastguard Worker 	va_end(ap);
238*8b26181fSAndroid Build Coastguard Worker 	if (*fmt) {
239*8b26181fSAndroid Build Coastguard Worker 		fmt += strlen(fmt);
240*8b26181fSAndroid Build Coastguard Worker 		if (fmt[-1] != '\n')
241*8b26181fSAndroid Build Coastguard Worker 			(void)fputc('\n', stderr);
242*8b26181fSAndroid Build Coastguard Worker 	}
243*8b26181fSAndroid Build Coastguard Worker }
244