Initial commit
[project/iwinfo.git] / iwinfo_cli.c
1 /*
2 * iwinfo - Wireless Information Library - Command line frontend
3 *
4 * Copyright (C) 2011 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17 */
18
19 #include <stdio.h>
20 #include <glob.h>
21
22 #include "iwinfo.h"
23
24
25 static char * format_bssid(unsigned char *mac)
26 {
27 static char buf[18];
28
29 snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
30 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
31
32 return buf;
33 }
34
35 static char * format_ssid(char *ssid)
36 {
37 static char buf[IWINFO_ESSID_MAX_SIZE+3];
38
39 if (ssid && ssid[0])
40 snprintf(buf, sizeof(buf), "\"%s\"", ssid);
41 else
42 snprintf(buf, sizeof(buf), "unknown");
43
44 return buf;
45 }
46
47 static char * format_channel(int ch)
48 {
49 static char buf[8];
50
51 if (ch <= 0)
52 snprintf(buf, sizeof(buf), "unknown");
53 else
54 snprintf(buf, sizeof(buf), "%d", ch);
55
56 return buf;
57 }
58
59 static char * format_frequency(int freq)
60 {
61 static char buf[10];
62
63 if (freq <= 0)
64 snprintf(buf, sizeof(buf), "unknown");
65 else
66 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)freq / 1000.0));
67
68 return buf;
69 }
70
71 static char * format_txpower(int pwr)
72 {
73 static char buf[10];
74
75 if (pwr < 0)
76 snprintf(buf, sizeof(buf), "unknown");
77 else
78 snprintf(buf, sizeof(buf), "%d dBm", pwr);
79
80 return buf;
81 }
82
83 static char * format_quality(int qual)
84 {
85 static char buf[8];
86
87 if (qual < 0)
88 snprintf(buf, sizeof(buf), "unknown");
89 else
90 snprintf(buf, sizeof(buf), "%d", qual);
91
92 return buf;
93 }
94
95 static char * format_quality_max(int qmax)
96 {
97 static char buf[8];
98
99 if (qmax < 0)
100 snprintf(buf, sizeof(buf), "unknown");
101 else
102 snprintf(buf, sizeof(buf), "%d", qmax);
103
104 return buf;
105 }
106
107 static char * format_signal(int sig)
108 {
109 static char buf[10];
110
111 if (!sig)
112 snprintf(buf, sizeof(buf), "unknown");
113 else
114 snprintf(buf, sizeof(buf), "%d dBm", sig);
115
116 return buf;
117 }
118
119 static char * format_noise(int noise)
120 {
121 static char buf[10];
122
123 if (!noise)
124 snprintf(buf, sizeof(buf), "unknown");
125 else
126 snprintf(buf, sizeof(buf), "%d dBm", noise);
127
128 return buf;
129 }
130
131 static char * format_rate(int rate)
132 {
133 static char buf[14];
134
135 if (rate <= 0)
136 snprintf(buf, sizeof(buf), "unknown");
137 else
138 snprintf(buf, sizeof(buf), "%d.%d MBit/s",
139 rate / 1000, (rate % 1000) / 100);
140
141 return buf;
142 }
143
144 static char * format_enc_ciphers(int ciphers)
145 {
146 static char str[128] = { 0 };
147 char *pos = str;
148
149 if (ciphers & IWINFO_CIPHER_WEP40)
150 pos += sprintf(pos, "WEP-40, ");
151
152 if (ciphers & IWINFO_CIPHER_WEP104)
153 pos += sprintf(pos, "WEP-104, ");
154
155 if (ciphers & IWINFO_CIPHER_TKIP)
156 pos += sprintf(pos, "TKIP, ");
157
158 if (ciphers & IWINFO_CIPHER_CCMP)
159 pos += sprintf(pos, "CCMP, ");
160
161 if (ciphers & IWINFO_CIPHER_WRAP)
162 pos += sprintf(pos, "WRAP, ");
163
164 if (ciphers & IWINFO_CIPHER_AESOCB)
165 pos += sprintf(pos, "AES-OCB, ");
166
167 if (ciphers & IWINFO_CIPHER_CKIP)
168 pos += sprintf(pos, "CKIP, ");
169
170 if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
171 pos += sprintf(pos, "NONE, ");
172
173 *(pos - 2) = 0;
174
175 return str;
176 }
177
178 static char * format_enc_suites(int suites)
179 {
180 static char str[64] = { 0 };
181 char *pos = str;
182
183 if (suites & IWINFO_KMGMT_PSK)
184 pos += sprintf(pos, "PSK/");
185
186 if (suites & IWINFO_KMGMT_8021x)
187 pos += sprintf(pos, "802.1X/");
188
189 if (!suites || (suites & IWINFO_KMGMT_NONE))
190 pos += sprintf(pos, "NONE/");
191
192 *(pos - 1) = 0;
193
194 return str;
195 }
196
197 static char * format_encryption(struct iwinfo_crypto_entry *c)
198 {
199 static char buf[512];
200
201 if (!c)
202 {
203 snprintf(buf, sizeof(buf), "unknown");
204 }
205 else if (c->enabled)
206 {
207 /* WEP */
208 if (c->auth_algs && !c->wpa_version)
209 {
210 if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
211 (c->auth_algs & IWINFO_AUTH_SHARED))
212 {
213 snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
214 format_enc_ciphers(c->pair_ciphers));
215 }
216 else if (c->auth_algs & IWINFO_AUTH_OPEN)
217 {
218 snprintf(buf, sizeof(buf), "WEP Open System (%s)",
219 format_enc_ciphers(c->pair_ciphers));
220 }
221 else if (c->auth_algs & IWINFO_AUTH_SHARED)
222 {
223 snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
224 format_enc_ciphers(c->pair_ciphers));
225 }
226 }
227
228 /* WPA */
229 else if (c->wpa_version)
230 {
231 switch (c->wpa_version) {
232 case 3:
233 snprintf(buf, sizeof(buf), "mixed WPA/WPA2 %s (%s)",
234 format_enc_suites(c->auth_suites),
235 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
236 break;
237
238 case 2:
239 snprintf(buf, sizeof(buf), "WPA2 %s (%s)",
240 format_enc_suites(c->auth_suites),
241 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
242 break;
243
244 case 1:
245 snprintf(buf, sizeof(buf), "WPA %s (%s)",
246 format_enc_suites(c->auth_suites),
247 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
248 break;
249 }
250 }
251 else
252 {
253 snprintf(buf, sizeof(buf), "none");
254 }
255 }
256 else
257 {
258 snprintf(buf, sizeof(buf), "none");
259 }
260
261 return buf;
262 }
263
264 static char * format_hwmodes(int modes)
265 {
266 static char buf[12];
267
268 if (modes <= 0)
269 snprintf(buf, sizeof(buf), "unknown");
270 else
271 snprintf(buf, sizeof(buf), "802.11%s%s%s%s%s",
272 (modes & IWINFO_80211_A) ? "a" : "",
273 (modes & IWINFO_80211_B) ? "b" : "",
274 (modes & IWINFO_80211_G) ? "g" : "",
275 (modes & IWINFO_80211_N) ? "n" : "",
276 (modes & IWINFO_80211_AC) ? "ac" : "");
277
278 return buf;
279 }
280
281 static char * format_assocrate(struct iwinfo_rate_entry *r)
282 {
283 static char buf[40];
284 char *p = buf;
285 int l = sizeof(buf);
286
287 if (r->rate <= 0)
288 {
289 snprintf(buf, sizeof(buf), "unknown");
290 }
291 else
292 {
293 p += snprintf(p, l, "%s", format_rate(r->rate));
294 l = sizeof(buf) - (p - buf);
295
296 if (r->mcs >= 0)
297 {
298 p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, 20 + r->is_40mhz*20);
299 l = sizeof(buf) - (p - buf);
300
301 if (r->is_short_gi)
302 p += snprintf(p, l, ", short GI");
303 }
304 }
305
306 return buf;
307 }
308
309
310 static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
311 {
312 const char *type = iwinfo_type(ifname);
313 return type ? type : "unknown";
314 }
315
316 static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
317 {
318 static char buf[20];
319 struct iwinfo_hardware_id ids;
320
321 if (!iw->hardware_id(ifname, (char *)&ids))
322 {
323 snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
324 ids.vendor_id, ids.device_id,
325 ids.subsystem_vendor_id, ids.subsystem_device_id);
326 }
327 else
328 {
329 snprintf(buf, sizeof(buf), "unknown");
330 }
331
332 return buf;
333 }
334
335 static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
336 {
337 static char buf[128];
338
339 if (iw->hardware_name(ifname, buf))
340 snprintf(buf, sizeof(buf), "unknown");
341
342 return buf;
343 }
344
345 static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
346 {
347 int off;
348 static char buf[12];
349
350 if (iw->txpower_offset(ifname, &off))
351 snprintf(buf, sizeof(buf), "unknown");
352 else if (off != 0)
353 snprintf(buf, sizeof(buf), "%d dB", off);
354 else
355 snprintf(buf, sizeof(buf), "none");
356
357 return buf;
358 }
359
360 static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
361 {
362 int off;
363 static char buf[12];
364
365 if (iw->frequency_offset(ifname, &off))
366 snprintf(buf, sizeof(buf), "unknown");
367 else if (off != 0)
368 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
369 else
370 snprintf(buf, sizeof(buf), "none");
371
372 return buf;
373 }
374
375 static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
376 {
377 char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
378
379 if (iw->ssid(ifname, buf))
380 memset(buf, 0, sizeof(buf));
381
382 return format_ssid(buf);
383 }
384
385 static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
386 {
387 static char buf[18] = { 0 };
388
389 if (iw->bssid(ifname, buf))
390 snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
391
392 return buf;
393 }
394
395 static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
396 {
397 int mode;
398 static char buf[128];
399
400 if (iw->mode(ifname, &mode))
401 mode = IWINFO_OPMODE_UNKNOWN;
402
403 snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
404
405 return buf;
406 }
407
408 static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
409 {
410 int ch;
411 if (iw->channel(ifname, &ch))
412 ch = -1;
413
414 return format_channel(ch);
415 }
416
417 static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
418 {
419 int freq;
420 if (iw->frequency(ifname, &freq))
421 freq = -1;
422
423 return format_frequency(freq);
424 }
425
426 static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
427 {
428 int pwr, off;
429 if (iw->txpower_offset(ifname, &off))
430 off = 0;
431
432 if (iw->txpower(ifname, &pwr))
433 pwr = -1;
434 else
435 pwr += off;
436
437 return format_txpower(pwr);
438 }
439
440 static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
441 {
442 int qual;
443 if (iw->quality(ifname, &qual))
444 qual = -1;
445
446 return format_quality(qual);
447 }
448
449 static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
450 {
451 int qmax;
452 if (iw->quality_max(ifname, &qmax))
453 qmax = -1;
454
455 return format_quality_max(qmax);
456 }
457
458 static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
459 {
460 int sig;
461 if (iw->signal(ifname, &sig))
462 sig = 0;
463
464 return format_signal(sig);
465 }
466
467 static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
468 {
469 int noise;
470 if (iw->noise(ifname, &noise))
471 noise = 0;
472
473 return format_noise(noise);
474 }
475
476 static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
477 {
478 int rate;
479 if (iw->bitrate(ifname, &rate))
480 rate = -1;
481
482 return format_rate(rate);
483 }
484
485 static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
486 {
487 struct iwinfo_crypto_entry c = { 0 };
488 if (iw->encryption(ifname, (char *)&c))
489 return format_encryption(NULL);
490
491 return format_encryption(&c);
492 }
493
494 static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
495 {
496 int modes;
497 if (iw->hwmodelist(ifname, &modes))
498 modes = -1;
499
500 return format_hwmodes(modes);
501 }
502
503 static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
504 {
505 int supp;
506 static char buf[4];
507
508 if (iw->mbssid_support(ifname, &supp))
509 snprintf(buf, sizeof(buf), "no");
510 else
511 snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
512
513 return buf;
514 }
515
516 static char * print_phyname(const struct iwinfo_ops *iw, const char *ifname)
517 {
518 static char buf[32];
519
520 if (!iw->phyname(ifname, buf))
521 return buf;
522
523 return "?";
524 }
525
526
527 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
528 {
529 printf("%-9s ESSID: %s\n",
530 ifname,
531 print_ssid(iw, ifname));
532 printf(" Access Point: %s\n",
533 print_bssid(iw, ifname));
534 printf(" Mode: %s Channel: %s (%s)\n",
535 print_mode(iw, ifname),
536 print_channel(iw, ifname),
537 print_frequency(iw, ifname));
538 printf(" Tx-Power: %s Link Quality: %s/%s\n",
539 print_txpower(iw, ifname),
540 print_quality(iw, ifname),
541 print_quality_max(iw, ifname));
542 printf(" Signal: %s Noise: %s\n",
543 print_signal(iw, ifname),
544 print_noise(iw, ifname));
545 printf(" Bit Rate: %s\n",
546 print_rate(iw, ifname));
547 printf(" Encryption: %s\n",
548 print_encryption(iw, ifname));
549 printf(" Type: %s HW Mode(s): %s\n",
550 print_type(iw, ifname),
551 print_hwmodes(iw, ifname));
552 printf(" Hardware: %s [%s]\n",
553 print_hardware_id(iw, ifname),
554 print_hardware_name(iw, ifname));
555 printf(" TX power offset: %s\n",
556 print_txpower_offset(iw, ifname));
557 printf(" Frequency offset: %s\n",
558 print_frequency_offset(iw, ifname));
559 printf(" Supports VAPs: %s PHY name: %s\n",
560 print_mbssid_supp(iw, ifname),
561 print_phyname(iw, ifname));
562 }
563
564
565 static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
566 {
567 int i, x, len;
568 char buf[IWINFO_BUFSIZE];
569 struct iwinfo_scanlist_entry *e;
570
571 if (iw->scanlist(ifname, buf, &len))
572 {
573 printf("Scanning not possible\n\n");
574 return;
575 }
576 else if (len <= 0)
577 {
578 printf("No scan results\n\n");
579 return;
580 }
581
582 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
583 {
584 e = (struct iwinfo_scanlist_entry *) &buf[i];
585
586 printf("Cell %02d - Address: %s\n",
587 x,
588 format_bssid(e->mac));
589 printf(" ESSID: %s\n",
590 format_ssid(e->ssid));
591 printf(" Mode: %s Channel: %s\n",
592 IWINFO_OPMODE_NAMES[e->mode],
593 format_channel(e->channel));
594 printf(" Signal: %s Quality: %s/%s\n",
595 format_signal(e->signal - 0x100),
596 format_quality(e->quality),
597 format_quality_max(e->quality_max));
598 printf(" Encryption: %s\n\n",
599 format_encryption(&e->crypto));
600 }
601 }
602
603
604 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
605 {
606 int len, pwr, off, i;
607 char buf[IWINFO_BUFSIZE];
608 struct iwinfo_txpwrlist_entry *e;
609
610 if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
611 {
612 printf("No TX power information available\n");
613 return;
614 }
615
616 if (iw->txpower(ifname, &pwr))
617 pwr = -1;
618
619 if (iw->txpower_offset(ifname, &off))
620 off = 0;
621
622 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
623 {
624 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
625
626 printf("%s%3d dBm (%4d mW)\n",
627 (pwr == e->dbm) ? "*" : " ",
628 e->dbm + off,
629 iwinfo_dbm2mw(e->dbm + off));
630 }
631 }
632
633
634 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
635 {
636 int i, len, ch;
637 char buf[IWINFO_BUFSIZE];
638 struct iwinfo_freqlist_entry *e;
639
640 if (iw->freqlist(ifname, buf, &len) || len <= 0)
641 {
642 printf("No frequency information available\n");
643 return;
644 }
645
646 if (iw->channel(ifname, &ch))
647 ch = -1;
648
649 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
650 {
651 e = (struct iwinfo_freqlist_entry *) &buf[i];
652
653 printf("%s %s (Channel %s)%s\n",
654 (ch == e->channel) ? "*" : " ",
655 format_frequency(e->mhz),
656 format_channel(e->channel),
657 e->restricted ? " [restricted]" : "");
658 }
659 }
660
661
662 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
663 {
664 int i, len;
665 char buf[IWINFO_BUFSIZE];
666 struct iwinfo_assoclist_entry *e;
667
668 if (iw->assoclist(ifname, buf, &len))
669 {
670 printf("No information available\n");
671 return;
672 }
673 else if (len <= 0)
674 {
675 printf("No station connected\n");
676 return;
677 }
678
679 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
680 {
681 e = (struct iwinfo_assoclist_entry *) &buf[i];
682
683 printf("%s %s / %s (SNR %d) %d ms ago\n",
684 format_bssid(e->mac),
685 format_signal(e->signal),
686 format_noise(e->noise),
687 (e->signal - e->noise),
688 e->inactive);
689
690 printf(" RX: %-38s %8d Pkts.\n",
691 format_assocrate(&e->rx_rate),
692 e->rx_packets
693 );
694
695 printf(" TX: %-38s %8d Pkts.\n\n",
696 format_assocrate(&e->tx_rate),
697 e->tx_packets
698 );
699 }
700 }
701
702
703 static char * lookup_country(char *buf, int len, int iso3166)
704 {
705 int i;
706 struct iwinfo_country_entry *c;
707
708 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
709 {
710 c = (struct iwinfo_country_entry *) &buf[i];
711
712 if (c->iso3166 == iso3166)
713 return c->ccode;
714 }
715
716 return NULL;
717 }
718
719 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
720 {
721 int len;
722 char buf[IWINFO_BUFSIZE];
723 char *ccode;
724 char curcode[3];
725 const struct iwinfo_iso3166_label *l;
726
727 if (iw->countrylist(ifname, buf, &len))
728 {
729 printf("No country code information available\n");
730 return;
731 }
732
733 if (iw->country(ifname, curcode))
734 memset(curcode, 0, sizeof(curcode));
735
736 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
737 {
738 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
739 {
740 printf("%s %4s %c%c\n",
741 strncmp(ccode, curcode, 2) ? " " : "*",
742 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
743 }
744 }
745 }
746
747
748 int main(int argc, char **argv)
749 {
750 int i;
751 char *p;
752 const struct iwinfo_ops *iw;
753 glob_t globbuf;
754
755 if (argc > 1 && argc < 3)
756 {
757 fprintf(stderr,
758 "Usage:\n"
759 " iwinfo <device> info\n"
760 " iwinfo <device> scan\n"
761 " iwinfo <device> txpowerlist\n"
762 " iwinfo <device> freqlist\n"
763 " iwinfo <device> assoclist\n"
764 " iwinfo <device> countrylist\n"
765 );
766
767 return 1;
768 }
769
770 if (argc == 1)
771 {
772 glob("/sys/class/net/*", 0, NULL, &globbuf);
773
774 for (i = 0; i < globbuf.gl_pathc; i++)
775 {
776 p = strrchr(globbuf.gl_pathv[i], '/');
777
778 if (!p)
779 continue;
780
781 iw = iwinfo_backend(++p);
782
783 if (!iw)
784 continue;
785
786 print_info(iw, p);
787 printf("\n");
788 }
789
790 globfree(&globbuf);
791 return 0;
792 }
793
794 iw = iwinfo_backend(argv[1]);
795
796 if (!iw)
797 {
798 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
799 return 1;
800 }
801
802 for (i = 2; i < argc; i++)
803 {
804 switch(argv[i][0])
805 {
806 case 'i':
807 print_info(iw, argv[1]);
808 break;
809
810 case 's':
811 print_scanlist(iw, argv[1]);
812 break;
813
814 case 't':
815 print_txpwrlist(iw, argv[1]);
816 break;
817
818 case 'f':
819 print_freqlist(iw, argv[1]);
820 break;
821
822 case 'a':
823 print_assoclist(iw, argv[1]);
824 break;
825
826 case 'c':
827 print_countrylist(iw, argv[1]);
828 break;
829
830 default:
831 fprintf(stderr, "Unknown command: %s\n", argv[i]);
832 return 1;
833 }
834 }
835
836 iwinfo_finish();
837
838 return 0;
839 }