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