xref: /netbsd-src/usr.sbin/iwictl/iwictl.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: iwictl.c,v 1.8 2007/12/15 19:44:55 perry Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2005
5  *	Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: iwictl.c,v 1.8 2007/12/15 19:44:55 perry Exp $");
32 
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 
39 #include <net/if.h>
40 
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <unistd.h>
49 
50 #define SIOCGRADIO	_IOWR('i', 139, struct ifreq)
51 #define SIOCGTABLE0	_IOWR('i', 140, struct ifreq)
52 
53 static void usage(void) __dead;
54 static int do_req(const char *, unsigned long, void *);
55 static void get_radio_state(const char *);
56 static void get_statistics(const char *);
57 
58 int
main(int argc,char ** argv)59 main(int argc, char **argv)
60 {
61 	int ch;
62 	char *iface = NULL;
63 	int noflag = 1, rflag = 0;
64 
65 	setprogname(argv[0]);
66 	if (argc > 1 && argv[1][0] != '-') {
67 		iface = argv[1];
68 		optind++;
69 	}
70 
71 	while ((ch = getopt(argc, argv, "i:r")) != -1) {
72 		if (ch != 'i')
73 			noflag = 0;
74 
75 		switch (ch) {
76 		case 'i':
77 			iface = optarg;
78 			break;
79 
80 		case 'r':
81 			rflag = 1;
82 			break;
83 
84 		default:
85 			usage();
86 		}
87 	}
88 
89 	if (iface == NULL)
90 		usage();
91 
92 	if (rflag)
93 		get_radio_state(iface);
94 
95 	if (noflag)
96 		get_statistics(iface);
97 
98 	return EX_OK;
99 }
100 
101 static void
usage(void)102 usage(void)
103 {
104 	(void)fprintf(stderr, "Usage:  %s [-i] iface\n"
105 	    "\t%s [-i] iface -r\n", getprogname(), getprogname());
106 
107 	exit(EX_USAGE);
108 }
109 
110 static int
do_req(const char * iface,unsigned long req,void * data)111 do_req(const char *iface, unsigned long req, void *data)
112 {
113 	int s;
114 	struct ifreq ifr;
115 	int error, serrno;
116 
117 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
118 		err(EX_OSERR, "Can't create socket");
119 
120 	(void)memset(&ifr, 0, sizeof(ifr));
121 	(void)strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
122 	ifr.ifr_data = data;
123 	error = ioctl(s, req, &ifr);
124 	serrno = errno;
125 	(void)close(s);
126 	errno = serrno;
127 
128 	return error;
129 }
130 
131 static void
get_radio_state(const char * iface)132 get_radio_state(const char *iface)
133 {
134 	int radio;
135 
136 	if (do_req(iface, SIOCGRADIO, &radio) == -1)
137 		err(EX_OSERR, "Can't read radio");
138 
139 	(void)printf("Radio is %s\n", radio ? "ON" : "OFF");
140 }
141 
142 struct statistic {
143 	int 		index;
144 	const char	*desc;
145 };
146 
147 static const struct statistic tbl[] = {
148 	{  1, "Current transmission rate" },
149 	{  2, "Fragmentation threshold" },
150 	{  3, "RTS threshold" },
151 	{  4, "Number of frames submitted for transfer" },
152 	{  5, "Number of frames transmitted" },
153 	{  6, "Number of unicast frames transmitted" },
154 	{  7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
155 	{  8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
156 	{  9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
157 	{ 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
158 
159 	{ 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
160 	{ 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
161 	{ 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
162 	{ 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
163 	{ 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
164 	{ 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
165 	{ 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
166 	{ 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
167 	{ 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
168 	{ 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
169 	{ 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
170 	{ 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
171 	{ 31, "Number of multicast frames transmitted" },
172 	{ 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
173 	{ 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
174 	{ 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
175 	{ 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
176 
177 	{ 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
178 	{ 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
179 	{ 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
180 	{ 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
181 	{ 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
182 	{ 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
183 	{ 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
184 	{ 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
185 	{ 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
186 	{ 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
187 	{ 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
188 	{ 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
189 	{ 56, "Number of transmission retries" },
190 	{ 57, "Number of transmission failures" },
191 	{ 58, "Number of frames with a bad CRC received" },
192 
193 	{ 61, "Number of full scans" },
194 	{ 62, "Number of partial scans" },
195 
196 	{ 64, "Number of bytes transmitted" },
197 	{ 65, "Current RSSI" },
198 	{ 66, "Number of beacons received" },
199 	{ 67, "Number of beacons missed" },
200 
201 	{ 0, NULL }
202 };
203 
204 static void
get_statistics(const char * iface)205 get_statistics(const char *iface)
206 {
207 	static u_int32_t stats[256];
208 	const struct statistic *st;
209 
210 	if (do_req(iface, SIOCGTABLE0, stats) == -1)
211 		err(EX_OSERR, "Can't read statistics");
212 
213 	for (st = tbl; st->index != 0; st++)
214 		(void)printf("%-60s[%u]\n", st->desc, stats[st->index]);
215 }
216