830fa39f8e06a1b5696ba719097380f479dba3c2
[dragonfly.git] / usr.sbin / raycontrol / raycontrol.c
1 /*
2  * Copyright (c) 1999, 2000
3  * Dr. Duncan McLennan Barclay, dmlb@ragnet.demon.co.uk.  All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY DUNCAN BARCLAY AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL DUNCAN BARCLAY OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/usr.sbin/raycontrol/raycontrol.c,v 1.1.2.3 2002/12/16 08:39:41 roam Exp $
33  * $DragonFly: src/usr.sbin/raycontrol/Attic/raycontrol.c,v 1.2 2003/06/17 04:30:02 dillon Exp $
34  */
35
36 #include <sys/types.h>
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42
43 #include <net/if.h>
44 #include <net/ethernet.h>
45 #include <net/if_ieee80211.h>
46
47 #include <dev/ray/if_rayreg.h>
48 #include <dev/ray/if_raymib.h>
49
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <errno.h>
55 #include <err.h>
56
57 static char *   ray_printhex    (u_int8_t *d, char *s, int len);
58 static void     ray_getval      (char *iface, struct ray_param_req *rreq);
59 static void     ray_getstats    (char *iface, struct ray_stats_req *sreq);
60 static int      ray_version     (char *iface);
61 static void     ray_dumpstats   (char *iface);
62 static void     ray_dumpinfo    (char *iface);
63 static void     ray_setstr      (char *iface, u_int8_t mib, char *s);
64 static void     ray_setword     (char *iface, u_int8_t mib, u_int16_t v);
65 static void     ray_setval      (char *iface, struct ray_param_req *rreq);
66 static void     usage           (char *p);
67
68 static char     *mib_strings[] = RAY_MIB_STRINGS;
69 static char     *mib_help_strings[] = RAY_MIB_HELP_STRINGS;
70 static int      mib_info[RAY_MIB_MAX+1][3] = RAY_MIB_INFO;
71
72 static char *
73 ray_printhex(u_int8_t *d, char *s, int len)
74 {
75         static char buf[3*256];
76         char *p;
77         int i;
78
79         if (2 * len + strlen(s) * (len - 1) > sizeof(buf) - 1)
80                 err(1, "Byte string too long");
81
82         sprintf(buf, "%02x", *d);
83         for (p = buf + 2, i = 1; i < len; i++)
84                 p += sprintf(p, "%s%02x", s, *(d+i));
85
86         return(buf);
87 }
88
89 static void
90 ray_getval(char *iface, struct ray_param_req *rreq)
91 {
92         struct ifreq ifr;
93         int s;
94
95         bzero((char *)&ifr, sizeof(ifr));
96
97         strcpy(ifr.ifr_name, iface);
98         ifr.ifr_data = (caddr_t)rreq;
99
100         s = socket(AF_INET, SOCK_DGRAM, 0);
101
102         if (s == -1)
103                 err(1, "socket");
104
105         if (ioctl(s, SIOCGRAYPARAM, &ifr) == -1)
106                 warn("SIOCGRAYPARAM failed with failcode 0x%02x",
107                     rreq->r_failcause);
108
109         close(s);
110 }
111
112 static void
113 ray_getsiglev(char *iface, struct ray_siglev *siglev)
114 {
115         struct ifreq ifr;
116         int s;
117
118         bzero((char *)&ifr, sizeof(ifr));
119
120         strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
121         ifr.ifr_data = (caddr_t)siglev;
122
123         s = socket(AF_INET, SOCK_DGRAM, 0);
124
125         if (s == -1)
126                 err(1, "socket");
127
128         if (ioctl(s, SIOCGRAYSIGLEV, &ifr) == -1)
129                 err(1, "SIOCGRAYSIGLEV failed");
130
131         close(s);
132 }
133
134 static void
135 ray_getstats(char *iface, struct ray_stats_req *sreq)
136 {
137         struct ifreq ifr;
138         int s;
139
140         bzero((char *)&ifr, sizeof(ifr));
141
142         strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
143         ifr.ifr_data = (caddr_t)sreq;
144
145         s = socket(AF_INET, SOCK_DGRAM, 0);
146
147         if (s == -1)
148                 err(1, "socket");
149
150         if (ioctl(s, SIOCGRAYSTATS, &ifr) == -1)
151                 err(1, "SIOCGRAYSTATS failed");
152
153         close(s);
154 }
155
156 static int
157 ray_version(char *iface)
158 {
159         struct ray_param_req rreq;
160
161         if (iface == NULL)
162                 errx(1, "must specify interface name");
163
164         bzero((char *)&rreq, sizeof(rreq));
165         rreq.r_paramid = RAY_MIB_VERSION;
166         ray_getval(iface, &rreq);
167         return(*rreq.r_data);
168 }
169
170 static void
171 ray_dumpinfo(char *iface)
172 {
173         struct ray_param_req rreq;
174         u_int8_t mib, version;
175
176         if (iface == NULL)
177                 errx(1, "must specify interface name");
178
179         bzero((char *)&rreq, sizeof(rreq));
180
181         version = ray_version(iface);
182         printf("%-26s\t",  mib_strings[RAY_MIB_VERSION]);
183         printf("%d\n", 3+version);
184
185         for (mib = RAY_MIB_NET_TYPE; mib <= RAY_MIB_MAX; mib++) {
186
187                 if ((mib_info[mib][0] & version) == 0)
188                         continue;
189                 if (mib == RAY_MIB_VERSION)
190                         continue;
191
192                 rreq.r_paramid = mib;
193                 ray_getval(iface, &rreq);
194                 printf("%-26s\t",  mib_strings[mib]);
195                 switch (rreq.r_len) {
196
197                 case 2:
198                         printf("0x%02x%02x", *rreq.r_data, *(rreq.r_data+1));
199                         break;
200
201                 case ETHER_ADDR_LEN:
202                         printf("%s",
203                             ray_printhex(rreq.r_data, ":", rreq.r_len));
204                         break;
205
206                 case IEEE80211_NWID_LEN:
207                         printf("%-32s", (char *)rreq.r_data);
208                         break;
209
210
211                 case 1:
212                 default:
213                         printf("0x%02x", *rreq.r_data);
214                         break;
215                 }
216                 printf("\t%s\n",  mib_help_strings[mib]);
217         }
218 }
219
220 static void
221 ray_dumpsiglev(char *iface)
222 {
223         struct ray_siglev siglevs[RAY_NSIGLEVRECS];
224         int i;
225
226         if (iface == NULL)
227                 errx(1, "must specify interface name");
228
229         bzero((char *)siglevs, sizeof(siglevs));
230
231         ray_getsiglev(iface, siglevs);
232
233         for (i = 0; i < RAY_NSIGLEVRECS; i++) {
234                 printf("Slot %d: %s", i,
235                     ray_printhex(siglevs[i].rsl_host, ":", ETHER_ADDR_LEN));
236                 printf("  %s",
237                     ray_printhex(siglevs[i].rsl_siglevs, ",", RAY_NSIGLEV));
238                 printf("  %s\n",
239                     ray_printhex(siglevs[i].rsl_antennas, "", RAY_NANTENNA));
240         }
241
242 }
243
244 static void
245 ray_dumpstats(char *iface)
246 {
247         struct ray_stats_req sreq;
248
249         if (iface == NULL)
250                 errx(1, "must specify interface name");
251
252         bzero((char *)&sreq, sizeof(sreq));
253
254         ray_getstats(iface, &sreq);
255
256         printf("Receiver overflows        %lu\n",
257             (unsigned long int)sreq.rxoverflow);
258         printf("Receiver checksum errors  %lu\n",
259             (unsigned long int)sreq.rxcksum);
260         printf("Header checksum errors    %lu\n",
261             (unsigned long int)sreq.rxhcksum);
262         printf("Clear channel noise level %u\n", sreq.rxnoise);
263 }
264
265 static void
266 ray_setval(char *iface, struct ray_param_req *rreq)
267 {
268         struct ifreq ifr;
269         int s;
270
271         bzero((char *)&ifr, sizeof(ifr));
272
273         strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
274         ifr.ifr_data = (caddr_t)rreq;
275
276         s = socket(AF_INET, SOCK_DGRAM, 0);
277
278         if (s == -1)
279                 err(1, "socket");
280
281         if (ioctl(s, SIOCSRAYPARAM, &ifr) == -1) {
282                 err(1, "SIOCSRAYPARAM failed with failcode 0x%02x",
283                     rreq->r_failcause);
284         }
285
286         close(s);
287 }
288
289 static void
290 ray_setword(char *iface, u_int8_t mib, u_int16_t v)
291 {
292         struct ray_param_req rreq;
293
294         if (iface == NULL)
295                 errx(1, "must specify interface name");
296
297         bzero((char *)&rreq, sizeof(rreq));
298
299         rreq.r_paramid = mib;
300         rreq.r_len = RAY_MIB_SIZE(mib_info, mib, ray_version(iface));
301         switch (rreq.r_len) {
302
303         case 1:
304                 *rreq.r_data = (u_int8_t)(v & 0xff);
305                 break;
306
307         case 2:
308                 *rreq.r_data = (u_int8_t)((v & 0xff00) >> 8);
309                 *(rreq.r_data+1) = (u_int8_t)(v & 0xff);
310                 break;
311
312         default:
313                 break;
314         }
315
316         ray_setval(iface, &rreq);
317 }
318
319 static void
320 ray_setstr(char *iface, u_int8_t mib, char *s)
321 {
322         struct ray_param_req rreq;
323
324         if (iface == NULL)
325                 errx(1, "must specify interface name");
326         if (s == NULL)
327                 errx(1, "must specify string");
328         if (strlen(s) > RAY_MIB_SIZE(mib_info, mib, ray_version(iface)))
329                 errx(1, "string too long");
330
331         bzero((char *)&rreq, sizeof(rreq));
332
333         rreq.r_paramid = mib;
334         rreq.r_len = RAY_MIB_SIZE(mib_info, mib, ray_version(iface));
335         bcopy(s, (char *)rreq.r_data, strlen(s));
336
337         ray_setval(iface, &rreq);
338 }
339
340 static void
341 usage(char *p)
342 {
343         fprintf(stderr, "usage:  %s -i iface\n", p);
344         fprintf(stderr, "\t%s -i iface -o\n", p);
345         fprintf(stderr, "\t%s -i iface -t tx rate\n", p);
346         fprintf(stderr, "\t%s -i iface -n network name\n", p);
347         fprintf(stderr, "\t%s -i iface -p port type\n", p);
348         fprintf(stderr, "\t%s -i iface -m mac address\n", p);
349         fprintf(stderr, "\t%s -i iface -d max data length\n", p);
350         fprintf(stderr, "\t%s -i iface -r RTS threshold\n", p);
351         fprintf(stderr, "\t%s -i iface -f hopset\n", p);
352         fprintf(stderr, "\t%s -i iface -P 0|1\n", p);
353         fprintf(stderr, "\t%s -i iface -S max sleep duration\n", p);
354         fprintf(stderr, "\t%s -i iface -C print signal cache\n", p);
355
356         exit(1);
357 }
358
359 int
360 main(int argc, char *argv[])
361 {
362
363         char *iface, *p;
364         int ch, val;
365
366         iface = NULL;
367         p = argv[0];
368
369         /* Get the interface name */
370         opterr = 0;
371         ch = getopt(argc, argv, "i:");
372         if (ch == 'i') {
373                 iface = optarg;
374         } else {
375                 if (argc > 1 && *argv[1] != '-') {
376                         iface = argv[1];
377                         optind = 2; 
378                 } else {
379                         iface = "ray0";
380                         optind = 1;
381                 }
382                 optreset = 1;
383         }
384         opterr = 1;
385
386         while ((ch = getopt(argc, argv, "hoCi:d:f:n:p:r:t:W:")) != -1) {
387                 switch (ch) {
388
389                 case 'i':
390                         iface = optarg;
391                         break;
392
393                 case 'd':
394                         val = atoi(optarg);
395                         if (((val < 350) &&
396                             (val != -1)) || (val > RAY_MIB_FRAG_THRESH_MAXIMUM))
397                                 usage(p);
398                         if (val == -1)
399                                 val = 0x7fff;
400                         ray_setword(iface, RAY_MIB_FRAG_THRESH, val);
401                         exit(0);
402                         break;
403
404                 case 'f':
405                         val = atoi(optarg);
406                         if ((val < RAY_MIB_COUNTRY_CODE_MIMIMUM) ||
407                             (val > RAY_MIB_COUNTRY_CODE_MAXIMUM))
408                                 usage(p);
409                         ray_setword(iface, RAY_MIB_COUNTRY_CODE, val);
410                         exit(0);
411                         break;
412
413                 case 'n':
414                         ray_setstr(iface, RAY_MIB_SSID, optarg);
415                         exit(0);
416                         break;
417
418                 case 'o':
419                         ray_dumpstats(iface);
420                         exit(0);
421                         break;
422
423                 case 'p':
424                         val = atoi(optarg);
425                         if ((val < 0) || (val > 1))
426                                 usage(p);
427                         ray_setword(iface, RAY_MIB_NET_TYPE, val);
428                         exit(0);
429                         break;
430
431
432                 case 'r':
433                         val = atoi(optarg);
434                         if ((val < -1) || (val > RAY_MIB_RTS_THRESH_MAXIMUM))
435                                 usage(p);
436                         if (val == -1)
437                                 val = 0x7fff;
438                         ray_setword(iface, RAY_MIB_RTS_THRESH, val);
439                         exit(0);
440                         break;
441
442                 case 't':
443                         val = atoi(optarg);
444                         if ((val < RAY_MIB_BASIC_RATE_SET_MINIMUM) ||
445                             (val > RAY_MIB_BASIC_RATE_SET_MAXIMUM))
446                                 usage(p);
447                         ray_setword(iface, RAY_MIB_BASIC_RATE_SET, val);
448                         exit(0);
449                         break;
450
451                 case 'C':
452                         ray_dumpsiglev(iface);
453                         exit(0);
454                         break;
455
456                 case 'W':
457                         {
458                                 char *stringp, **ap, *av[5];
459                                 u_int8_t mib;
460
461                                 stringp = optarg;
462                                 ap = av;
463                                 *ap = strsep(&stringp, ":");
464                                 ap++;
465                                 *ap = strsep(&stringp, ":");
466                                 mib = atoi(av[0]);
467                                 sscanf(av[1], "%x", &val);
468                                 printf("mib %d, val 0x%02x\n", mib, val);
469                                 ray_setword(iface, mib, val);
470                         }
471                         exit(0);
472                         break;
473
474                 case 'h':
475                 default:
476                         usage(p);
477
478                 }
479         }
480
481         if (iface == NULL)
482                 usage(p);
483
484         ray_dumpinfo(iface);
485
486         exit(0);
487 }