xref: /aosp_15_r20/external/libpcap/testprogs/can_set_rfmon_test.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 <stdio.h>
31*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
32*8b26181fSAndroid Build Coastguard Worker #include <string.h>
33*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h>
34*8b26181fSAndroid Build Coastguard Worker 
35*8b26181fSAndroid Build Coastguard Worker #include <pcap.h>
36*8b26181fSAndroid Build Coastguard Worker 
37*8b26181fSAndroid Build Coastguard Worker #include "pcap/funcattrs.h"
38*8b26181fSAndroid Build Coastguard Worker 
39*8b26181fSAndroid Build Coastguard Worker static const char *program_name;
40*8b26181fSAndroid Build Coastguard Worker 
41*8b26181fSAndroid Build Coastguard Worker /* Forwards */
42*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN error(PCAP_FORMAT_STRING(const char *), ...) PCAP_PRINTFLIKE(1,2);
43*8b26181fSAndroid Build Coastguard Worker 
44*8b26181fSAndroid Build Coastguard Worker int
main(int argc,char ** argv)45*8b26181fSAndroid Build Coastguard Worker main(int argc, char **argv)
46*8b26181fSAndroid Build Coastguard Worker {
47*8b26181fSAndroid Build Coastguard Worker 	const char *cp;
48*8b26181fSAndroid Build Coastguard Worker 	pcap_t *pd;
49*8b26181fSAndroid Build Coastguard Worker 	char ebuf[PCAP_ERRBUF_SIZE];
50*8b26181fSAndroid Build Coastguard Worker 	int status;
51*8b26181fSAndroid Build Coastguard Worker 
52*8b26181fSAndroid Build Coastguard Worker 	if ((cp = strrchr(argv[0], '/')) != NULL)
53*8b26181fSAndroid Build Coastguard Worker 		program_name = cp + 1;
54*8b26181fSAndroid Build Coastguard Worker 	else
55*8b26181fSAndroid Build Coastguard Worker 		program_name = argv[0];
56*8b26181fSAndroid Build Coastguard Worker 
57*8b26181fSAndroid Build Coastguard Worker 	if (argc != 2) {
58*8b26181fSAndroid Build Coastguard Worker 		fprintf(stderr, "Usage: %s <device>\n", program_name);
59*8b26181fSAndroid Build Coastguard Worker 		return 2;
60*8b26181fSAndroid Build Coastguard Worker 	}
61*8b26181fSAndroid Build Coastguard Worker 
62*8b26181fSAndroid Build Coastguard Worker 	pd = pcap_create(argv[1], ebuf);
63*8b26181fSAndroid Build Coastguard Worker 	if (pd == NULL)
64*8b26181fSAndroid Build Coastguard Worker 		error("%s", ebuf);
65*8b26181fSAndroid Build Coastguard Worker 	status = pcap_can_set_rfmon(pd);
66*8b26181fSAndroid Build Coastguard Worker 	if (status < 0) {
67*8b26181fSAndroid Build Coastguard Worker 		if (status == PCAP_ERROR)
68*8b26181fSAndroid Build Coastguard Worker 			error("%s: pcap_can_set_rfmon failed: %s", argv[1],
69*8b26181fSAndroid Build Coastguard Worker 			    pcap_geterr(pd));
70*8b26181fSAndroid Build Coastguard Worker 		else
71*8b26181fSAndroid Build Coastguard Worker 			error("%s: pcap_can_set_rfmon failed: %s", argv[1],
72*8b26181fSAndroid Build Coastguard Worker 			    pcap_statustostr(status));
73*8b26181fSAndroid Build Coastguard Worker 	}
74*8b26181fSAndroid Build Coastguard Worker 	printf("%s: Monitor mode %s be set\n", argv[1], status ? "can" : "cannot");
75*8b26181fSAndroid Build Coastguard Worker 	return 0;
76*8b26181fSAndroid Build Coastguard Worker }
77*8b26181fSAndroid Build Coastguard Worker 
78*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
79*8b26181fSAndroid Build Coastguard Worker static void
error(const char * fmt,...)80*8b26181fSAndroid Build Coastguard Worker error(const char *fmt, ...)
81*8b26181fSAndroid Build Coastguard Worker {
82*8b26181fSAndroid Build Coastguard Worker 	va_list ap;
83*8b26181fSAndroid Build Coastguard Worker 
84*8b26181fSAndroid Build Coastguard Worker 	(void)fprintf(stderr, "%s: ", program_name);
85*8b26181fSAndroid Build Coastguard Worker 	va_start(ap, fmt);
86*8b26181fSAndroid Build Coastguard Worker 	(void)vfprintf(stderr, fmt, ap);
87*8b26181fSAndroid Build Coastguard Worker 	va_end(ap);
88*8b26181fSAndroid Build Coastguard Worker 	if (*fmt) {
89*8b26181fSAndroid Build Coastguard Worker 		fmt += strlen(fmt);
90*8b26181fSAndroid Build Coastguard Worker 		if (fmt[-1] != '\n')
91*8b26181fSAndroid Build Coastguard Worker 			(void)fputc('\n', stderr);
92*8b26181fSAndroid Build Coastguard Worker 	}
93*8b26181fSAndroid Build Coastguard Worker 	exit(1);
94*8b26181fSAndroid Build Coastguard Worker 	/* NOTREACHED */
95*8b26181fSAndroid Build Coastguard Worker }
96