xref: /freebsd/contrib/libpcap/testprogs/can_set_rfmon_test.c (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
1*57e22627SCy Schubert /*
2*57e22627SCy Schubert  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3*57e22627SCy Schubert  *	The Regents of the University of California.  All rights reserved.
4*57e22627SCy Schubert  *
5*57e22627SCy Schubert  * Redistribution and use in source and binary forms, with or without
6*57e22627SCy Schubert  * modification, are permitted provided that: (1) source code distributions
7*57e22627SCy Schubert  * retain the above copyright notice and this paragraph in its entirety, (2)
8*57e22627SCy Schubert  * distributions including binary code include the above copyright notice and
9*57e22627SCy Schubert  * this paragraph in its entirety in the documentation or other materials
10*57e22627SCy Schubert  * provided with the distribution, and (3) all advertising materials mentioning
11*57e22627SCy Schubert  * features or use of this software display the following acknowledgement:
12*57e22627SCy Schubert  * ``This product includes software developed by the University of California,
13*57e22627SCy Schubert  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*57e22627SCy Schubert  * the University nor the names of its contributors may be used to endorse
15*57e22627SCy Schubert  * or promote products derived from this software without specific prior
16*57e22627SCy Schubert  * written permission.
17*57e22627SCy Schubert  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*57e22627SCy Schubert  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*57e22627SCy Schubert  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*57e22627SCy Schubert  */
21*57e22627SCy Schubert 
22*57e22627SCy Schubert #include "varattrs.h"
23*57e22627SCy Schubert 
24*57e22627SCy Schubert #ifndef lint
25*57e22627SCy Schubert static const char copyright[] _U_ =
26*57e22627SCy Schubert     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27*57e22627SCy Schubert The Regents of the University of California.  All rights reserved.\n";
28*57e22627SCy Schubert #endif
29*57e22627SCy Schubert 
30*57e22627SCy Schubert #include <stdio.h>
31*57e22627SCy Schubert #include <stdlib.h>
32*57e22627SCy Schubert #include <string.h>
33*57e22627SCy Schubert #include <stdarg.h>
34*57e22627SCy Schubert 
35*57e22627SCy Schubert #include <pcap.h>
36*57e22627SCy Schubert 
37*57e22627SCy Schubert #include "pcap/funcattrs.h"
38*57e22627SCy Schubert 
39*57e22627SCy Schubert static const char *program_name;
40*57e22627SCy Schubert 
41*57e22627SCy Schubert /* Forwards */
42*57e22627SCy Schubert static void PCAP_NORETURN error(PCAP_FORMAT_STRING(const char *), ...) PCAP_PRINTFLIKE(1,2);
43*57e22627SCy Schubert 
44*57e22627SCy Schubert int
main(int argc,char ** argv)45*57e22627SCy Schubert main(int argc, char **argv)
46*57e22627SCy Schubert {
47*57e22627SCy Schubert 	const char *cp;
48*57e22627SCy Schubert 	pcap_t *pd;
49*57e22627SCy Schubert 	char ebuf[PCAP_ERRBUF_SIZE];
50*57e22627SCy Schubert 	int status;
51*57e22627SCy Schubert 
52*57e22627SCy Schubert 	if ((cp = strrchr(argv[0], '/')) != NULL)
53*57e22627SCy Schubert 		program_name = cp + 1;
54*57e22627SCy Schubert 	else
55*57e22627SCy Schubert 		program_name = argv[0];
56*57e22627SCy Schubert 
57*57e22627SCy Schubert 	if (argc != 2) {
58*57e22627SCy Schubert 		fprintf(stderr, "Usage: %s <device>\n", program_name);
59*57e22627SCy Schubert 		return 2;
60*57e22627SCy Schubert 	}
61*57e22627SCy Schubert 
62*57e22627SCy Schubert 	pd = pcap_create(argv[1], ebuf);
63*57e22627SCy Schubert 	if (pd == NULL)
64*57e22627SCy Schubert 		error("%s", ebuf);
65*57e22627SCy Schubert 	status = pcap_can_set_rfmon(pd);
66*57e22627SCy Schubert 	if (status < 0) {
67*57e22627SCy Schubert 		if (status == PCAP_ERROR)
68*57e22627SCy Schubert 			error("%s: pcap_can_set_rfmon failed: %s", argv[1],
69*57e22627SCy Schubert 			    pcap_geterr(pd));
70*57e22627SCy Schubert 		else
71*57e22627SCy Schubert 			error("%s: pcap_can_set_rfmon failed: %s", argv[1],
72*57e22627SCy Schubert 			    pcap_statustostr(status));
73*57e22627SCy Schubert 	}
74*57e22627SCy Schubert 	printf("%s: Monitor mode %s be set\n", argv[1], status ? "can" : "cannot");
75*57e22627SCy Schubert 	return 0;
76*57e22627SCy Schubert }
77*57e22627SCy Schubert 
78*57e22627SCy Schubert /* VARARGS */
79*57e22627SCy Schubert static void
error(const char * fmt,...)80*57e22627SCy Schubert error(const char *fmt, ...)
81*57e22627SCy Schubert {
82*57e22627SCy Schubert 	va_list ap;
83*57e22627SCy Schubert 
84*57e22627SCy Schubert 	(void)fprintf(stderr, "%s: ", program_name);
85*57e22627SCy Schubert 	va_start(ap, fmt);
86*57e22627SCy Schubert 	(void)vfprintf(stderr, fmt, ap);
87*57e22627SCy Schubert 	va_end(ap);
88*57e22627SCy Schubert 	if (*fmt) {
89*57e22627SCy Schubert 		fmt += strlen(fmt);
90*57e22627SCy Schubert 		if (fmt[-1] != '\n')
91*57e22627SCy Schubert 			(void)fputc('\n', stderr);
92*57e22627SCy Schubert 	}
93*57e22627SCy Schubert 	exit(1);
94*57e22627SCy Schubert 	/* NOTREACHED */
95*57e22627SCy Schubert }
96