nl80211: fix wpa supplicant ctrl socket permissions
[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[16];
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[11];
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[16];
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[16];
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[16];
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[18];
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_CCMP256)
162 pos += sprintf(pos, "CCMP-256, ");
163
164 if (ciphers & IWINFO_CIPHER_GCMP)
165 pos += sprintf(pos, "GCMP, ");
166
167 if (ciphers & IWINFO_CIPHER_GCMP256)
168 pos += sprintf(pos, "GCMP-256, ");
169
170 if (ciphers & IWINFO_CIPHER_WRAP)
171 pos += sprintf(pos, "WRAP, ");
172
173 if (ciphers & IWINFO_CIPHER_AESOCB)
174 pos += sprintf(pos, "AES-OCB, ");
175
176 if (ciphers & IWINFO_CIPHER_CKIP)
177 pos += sprintf(pos, "CKIP, ");
178
179 if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
180 pos += sprintf(pos, "NONE, ");
181
182 *(pos - 2) = 0;
183
184 return str;
185 }
186
187 static char * format_enc_suites(int suites)
188 {
189 static char str[64] = { 0 };
190 char *pos = str;
191
192 if (suites & IWINFO_KMGMT_PSK)
193 pos += sprintf(pos, "PSK/");
194
195 if (suites & IWINFO_KMGMT_8021x)
196 pos += sprintf(pos, "802.1X/");
197
198 if (suites & IWINFO_KMGMT_SAE)
199 pos += sprintf(pos, "SAE/");
200
201 if (suites & IWINFO_KMGMT_OWE)
202 pos += sprintf(pos, "OWE/");
203
204 if (!suites || (suites & IWINFO_KMGMT_NONE))
205 pos += sprintf(pos, "NONE/");
206
207 *(pos - 1) = 0;
208
209 return str;
210 }
211
212 static char * format_encryption(struct iwinfo_crypto_entry *c)
213 {
214 static char buf[512];
215 char *pos = buf;
216 int i, n;
217
218 if (!c)
219 {
220 snprintf(buf, sizeof(buf), "unknown");
221 }
222 else if (c->enabled)
223 {
224 /* WEP */
225 if (c->auth_algs && !c->wpa_version)
226 {
227 if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
228 (c->auth_algs & IWINFO_AUTH_SHARED))
229 {
230 snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
231 format_enc_ciphers(c->pair_ciphers));
232 }
233 else if (c->auth_algs & IWINFO_AUTH_OPEN)
234 {
235 snprintf(buf, sizeof(buf), "WEP Open System (%s)",
236 format_enc_ciphers(c->pair_ciphers));
237 }
238 else if (c->auth_algs & IWINFO_AUTH_SHARED)
239 {
240 snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
241 format_enc_ciphers(c->pair_ciphers));
242 }
243 }
244
245 /* WPA */
246 else if (c->wpa_version)
247 {
248 for (i = 0, n = 0; i < 3; i++)
249 if (c->wpa_version & (1 << i))
250 n++;
251
252 if (n > 1)
253 pos += sprintf(pos, "mixed ");
254
255 for (i = 0; i < 3; i++)
256 if (c->wpa_version & (1 << i))
257 {
258 if (i)
259 pos += sprintf(pos, "WPA%d/", i + 1);
260 else
261 pos += sprintf(pos, "WPA/");
262 }
263
264 pos--;
265
266 sprintf(pos, " %s (%s)",
267 format_enc_suites(c->auth_suites),
268 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
269 }
270 else
271 {
272 snprintf(buf, sizeof(buf), "none");
273 }
274 }
275 else
276 {
277 snprintf(buf, sizeof(buf), "none");
278 }
279
280 return buf;
281 }
282
283 static char * format_hwmodes(int modes)
284 {
285 static char buf[17];
286
287 if (modes <= 0)
288 snprintf(buf, sizeof(buf), "unknown");
289 else
290 snprintf(buf, sizeof(buf), "802.11%s%s%s%s%s%s%s",
291 (modes & IWINFO_80211_A) ? "a" : "",
292 (modes & IWINFO_80211_B) ? "b" : "",
293 (modes & IWINFO_80211_G) ? "g" : "",
294 (modes & IWINFO_80211_N) ? "n" : "",
295 (modes & IWINFO_80211_AC) ? "ac" : "",
296 (modes & IWINFO_80211_AD) ? "ad" : "",
297 (modes & IWINFO_80211_AX) ? "ax" : "");
298
299 return buf;
300 }
301
302 static char * format_assocrate(struct iwinfo_rate_entry *r)
303 {
304 static char buf[80];
305 char *p = buf;
306 int l = sizeof(buf);
307
308 if (r->rate <= 0)
309 {
310 snprintf(buf, sizeof(buf), "unknown");
311 }
312 else
313 {
314 p += snprintf(p, l, "%s", format_rate(r->rate));
315 l = sizeof(buf) - (p - buf);
316
317 if (r->is_ht)
318 {
319 p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, r->mhz);
320 l = sizeof(buf) - (p - buf);
321 }
322 else if (r->is_vht)
323 {
324 p += snprintf(p, l, ", VHT-MCS %d, %dMHz", r->mcs, r->mhz);
325 l = sizeof(buf) - (p - buf);
326
327 if (r->nss)
328 {
329 p += snprintf(p, l, ", VHT-NSS %d", r->nss);
330 l = sizeof(buf) - (p - buf);
331 }
332 }
333 else if (r->is_he)
334 {
335 p += snprintf(p, l, ", HE-MCS %d, %dMHz", r->mcs, r->mhz);
336 l = sizeof(buf) - (p - buf);
337
338 p += snprintf(p, l, ", HE-NSS %d", r->nss);
339 l = sizeof(buf) - (p - buf);
340
341 p += snprintf(p, l, ", HE-GI %d", r->he_gi);
342 l = sizeof(buf) - (p - buf);
343
344 p += snprintf(p, l, ", HE-DCM %d", r->he_dcm);
345 l = sizeof(buf) - (p - buf);
346 }
347 }
348
349 return buf;
350 }
351
352 static const char* format_chan_width(uint16_t width)
353 {
354 switch (width) {
355 case 20: return "20 MHz";
356 case 2040: return "40 MHz and upper or 20 MHz with intolerant bit";
357 case 40: return "40 MHz or lower";
358 case 80: return "80 MHz";
359 case 8080: return "80+80 MHz";
360 case 160: return "160 MHz";
361 }
362
363 return "unknown";
364 }
365
366
367 static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
368 {
369 const char *type = iwinfo_type(ifname);
370 return type ? type : "unknown";
371 }
372
373 static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
374 {
375 static char buf[20];
376 struct iwinfo_hardware_id ids;
377
378 if (!iw->hardware_id(ifname, (char *)&ids))
379 {
380 snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
381 ids.vendor_id, ids.device_id,
382 ids.subsystem_vendor_id, ids.subsystem_device_id);
383 }
384 else
385 {
386 snprintf(buf, sizeof(buf), "unknown");
387 }
388
389 return buf;
390 }
391
392 static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
393 {
394 static char buf[128];
395
396 if (iw->hardware_name(ifname, buf))
397 snprintf(buf, sizeof(buf), "unknown");
398
399 return buf;
400 }
401
402 static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
403 {
404 int off;
405 static char buf[12];
406
407 if (iw->txpower_offset(ifname, &off))
408 snprintf(buf, sizeof(buf), "unknown");
409 else if (off != 0)
410 snprintf(buf, sizeof(buf), "%d dB", off);
411 else
412 snprintf(buf, sizeof(buf), "none");
413
414 return buf;
415 }
416
417 static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
418 {
419 int off;
420 static char buf[12];
421
422 if (iw->frequency_offset(ifname, &off))
423 snprintf(buf, sizeof(buf), "unknown");
424 else if (off != 0)
425 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
426 else
427 snprintf(buf, sizeof(buf), "none");
428
429 return buf;
430 }
431
432 static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
433 {
434 char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
435
436 if (iw->ssid(ifname, buf))
437 memset(buf, 0, sizeof(buf));
438
439 return format_ssid(buf);
440 }
441
442 static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
443 {
444 static char buf[18] = { 0 };
445
446 if (iw->bssid(ifname, buf))
447 snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
448
449 return buf;
450 }
451
452 static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
453 {
454 int mode;
455 static char buf[128];
456
457 if (iw->mode(ifname, &mode))
458 mode = IWINFO_OPMODE_UNKNOWN;
459
460 snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
461
462 return buf;
463 }
464
465 static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
466 {
467 int ch;
468 if (iw->channel(ifname, &ch))
469 ch = -1;
470
471 return format_channel(ch);
472 }
473
474 static char * print_center_chan1(const struct iwinfo_ops *iw, const char *ifname)
475 {
476 int ch;
477 if (iw->center_chan1(ifname, &ch))
478 ch = -1;
479
480 return format_channel(ch);
481 }
482
483 static char * print_center_chan2(const struct iwinfo_ops *iw, const char *ifname)
484 {
485 int ch;
486 if (iw->center_chan2(ifname, &ch))
487 ch = -1;
488
489 return format_channel(ch);
490 }
491
492 static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
493 {
494 int freq;
495 if (iw->frequency(ifname, &freq))
496 freq = -1;
497
498 return format_frequency(freq);
499 }
500
501 static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
502 {
503 int pwr, off;
504 if (iw->txpower_offset(ifname, &off))
505 off = 0;
506
507 if (iw->txpower(ifname, &pwr))
508 pwr = -1;
509 else
510 pwr += off;
511
512 return format_txpower(pwr);
513 }
514
515 static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
516 {
517 int qual;
518 if (iw->quality(ifname, &qual))
519 qual = -1;
520
521 return format_quality(qual);
522 }
523
524 static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
525 {
526 int qmax;
527 if (iw->quality_max(ifname, &qmax))
528 qmax = -1;
529
530 return format_quality_max(qmax);
531 }
532
533 static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
534 {
535 int sig;
536 if (iw->signal(ifname, &sig))
537 sig = 0;
538
539 return format_signal(sig);
540 }
541
542 static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
543 {
544 int noise;
545 if (iw->noise(ifname, &noise))
546 noise = 0;
547
548 return format_noise(noise);
549 }
550
551 static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
552 {
553 int rate;
554 if (iw->bitrate(ifname, &rate))
555 rate = -1;
556
557 return format_rate(rate);
558 }
559
560 static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
561 {
562 struct iwinfo_crypto_entry c = { 0 };
563 if (iw->encryption(ifname, (char *)&c))
564 return format_encryption(NULL);
565
566 return format_encryption(&c);
567 }
568
569 static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
570 {
571 int modes;
572 if (iw->hwmodelist(ifname, &modes))
573 modes = -1;
574
575 return format_hwmodes(modes);
576 }
577
578 static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
579 {
580 int supp;
581 static char buf[4];
582
583 if (iw->mbssid_support(ifname, &supp))
584 snprintf(buf, sizeof(buf), "no");
585 else
586 snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
587
588 return buf;
589 }
590
591 static char * print_phyname(const struct iwinfo_ops *iw, const char *ifname)
592 {
593 static char buf[32];
594
595 if (!iw->phyname(ifname, buf))
596 return buf;
597
598 return "?";
599 }
600
601
602 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
603 {
604 printf("%-9s ESSID: %s\n",
605 ifname,
606 print_ssid(iw, ifname));
607 printf(" Access Point: %s\n",
608 print_bssid(iw, ifname));
609 printf(" Mode: %s Channel: %s (%s)\n",
610 print_mode(iw, ifname),
611 print_channel(iw, ifname),
612 print_frequency(iw, ifname));
613 if (iw->center_chan1 != NULL) {
614 printf(" Center Channel 1: %s",
615 print_center_chan1(iw, ifname));
616 printf(" 2: %s\n", print_center_chan2(iw, ifname));
617 }
618 printf(" Tx-Power: %s Link Quality: %s/%s\n",
619 print_txpower(iw, ifname),
620 print_quality(iw, ifname),
621 print_quality_max(iw, ifname));
622 printf(" Signal: %s Noise: %s\n",
623 print_signal(iw, ifname),
624 print_noise(iw, ifname));
625 printf(" Bit Rate: %s\n",
626 print_rate(iw, ifname));
627 printf(" Encryption: %s\n",
628 print_encryption(iw, ifname));
629 printf(" Type: %s HW Mode(s): %s\n",
630 print_type(iw, ifname),
631 print_hwmodes(iw, ifname));
632 printf(" Hardware: %s [%s]\n",
633 print_hardware_id(iw, ifname),
634 print_hardware_name(iw, ifname));
635 printf(" TX power offset: %s\n",
636 print_txpower_offset(iw, ifname));
637 printf(" Frequency offset: %s\n",
638 print_frequency_offset(iw, ifname));
639 printf(" Supports VAPs: %s PHY name: %s\n",
640 print_mbssid_supp(iw, ifname),
641 print_phyname(iw, ifname));
642 }
643
644
645 static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
646 {
647 int i, x, len;
648 char buf[IWINFO_BUFSIZE];
649 struct iwinfo_scanlist_entry *e;
650
651 if (iw->scanlist(ifname, buf, &len))
652 {
653 printf("Scanning not possible\n\n");
654 return;
655 }
656 else if (len <= 0)
657 {
658 printf("No scan results\n\n");
659 return;
660 }
661
662 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
663 {
664 e = (struct iwinfo_scanlist_entry *) &buf[i];
665
666 printf("Cell %02d - Address: %s\n",
667 x,
668 format_bssid(e->mac));
669 printf(" ESSID: %s\n",
670 format_ssid(e->ssid));
671 printf(" Mode: %s Channel: %s\n",
672 IWINFO_OPMODE_NAMES[e->mode],
673 format_channel(e->channel));
674 printf(" Signal: %s Quality: %s/%s\n",
675 format_signal(e->signal - 0x100),
676 format_quality(e->quality),
677 format_quality_max(e->quality_max));
678 printf(" Encryption: %s\n",
679 format_encryption(&e->crypto));
680 printf(" HT Operation:\n");
681 printf(" Primary Channel: %d\n",
682 e->ht_chan_info.primary_chan);
683 printf(" Secondary Channel Offset: %s\n",
684 ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
685 printf(" Channel Width: %s\n",
686 format_chan_width(e->ht_chan_info.chan_width));
687
688 if (e->vht_chan_info.center_chan_1) {
689 printf(" VHT Operation:\n");
690 printf(" Channel Width: %s\n",
691 format_chan_width(e->vht_chan_info.chan_width));
692 printf(" Center Frequency 1: %d\n",
693 e->vht_chan_info.center_chan_1);
694 printf(" Center Frequency 2: %d\n",
695 e->vht_chan_info.center_chan_2);
696 }
697
698 printf("\n");
699 }
700 }
701
702
703 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
704 {
705 int len, pwr, off, i;
706 char buf[IWINFO_BUFSIZE];
707 struct iwinfo_txpwrlist_entry *e;
708
709 if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
710 {
711 printf("No TX power information available\n");
712 return;
713 }
714
715 if (iw->txpower(ifname, &pwr))
716 pwr = -1;
717
718 if (iw->txpower_offset(ifname, &off))
719 off = 0;
720
721 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
722 {
723 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
724
725 printf("%s%3d dBm (%4d mW)\n",
726 (pwr == e->dbm) ? "*" : " ",
727 e->dbm + off,
728 iwinfo_dbm2mw(e->dbm + off));
729 }
730 }
731
732
733 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
734 {
735 int i, len, ch;
736 char buf[IWINFO_BUFSIZE];
737 struct iwinfo_freqlist_entry *e;
738
739 if (iw->freqlist(ifname, buf, &len) || len <= 0)
740 {
741 printf("No frequency information available\n");
742 return;
743 }
744
745 if (iw->channel(ifname, &ch))
746 ch = -1;
747
748 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
749 {
750 e = (struct iwinfo_freqlist_entry *) &buf[i];
751
752 printf("%s %s (Channel %s)%s\n",
753 (ch == e->channel) ? "*" : " ",
754 format_frequency(e->mhz),
755 format_channel(e->channel),
756 e->restricted ? " [restricted]" : "");
757 }
758 }
759
760
761 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
762 {
763 int i, len;
764 char buf[IWINFO_BUFSIZE];
765 struct iwinfo_assoclist_entry *e;
766
767 if (iw->assoclist(ifname, buf, &len))
768 {
769 printf("No information available\n");
770 return;
771 }
772 else if (len <= 0)
773 {
774 printf("No station connected\n");
775 return;
776 }
777
778 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
779 {
780 e = (struct iwinfo_assoclist_entry *) &buf[i];
781
782 printf("%s %s / %s (SNR %d) %d ms ago\n",
783 format_bssid(e->mac),
784 format_signal(e->signal),
785 format_noise(e->noise),
786 (e->signal - e->noise),
787 e->inactive);
788
789 printf(" RX: %-38s %8d Pkts.\n",
790 format_assocrate(&e->rx_rate),
791 e->rx_packets
792 );
793
794 printf(" TX: %-38s %8d Pkts.\n",
795 format_assocrate(&e->tx_rate),
796 e->tx_packets
797 );
798
799 printf(" expected throughput: %s\n\n",
800 format_rate(e->thr));
801 }
802 }
803
804
805 static char * lookup_country(char *buf, int len, int iso3166)
806 {
807 int i;
808 struct iwinfo_country_entry *c;
809
810 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
811 {
812 c = (struct iwinfo_country_entry *) &buf[i];
813
814 if (c->iso3166 == iso3166)
815 return c->ccode;
816 }
817
818 return NULL;
819 }
820
821 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
822 {
823 int len;
824 char buf[IWINFO_BUFSIZE];
825 char *ccode;
826 char curcode[3];
827 const struct iwinfo_iso3166_label *l;
828
829 if (iw->countrylist(ifname, buf, &len))
830 {
831 printf("No country code information available\n");
832 return;
833 }
834
835 if (iw->country(ifname, curcode))
836 memset(curcode, 0, sizeof(curcode));
837
838 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
839 {
840 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
841 {
842 printf("%s %4s %c%c\n",
843 strncmp(ccode, curcode, 2) ? " " : "*",
844 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
845 }
846 }
847 }
848
849 static void print_htmodelist(const struct iwinfo_ops *iw, const char *ifname)
850 {
851 int i, htmodes = 0;
852
853 if (iw->htmodelist(ifname, &htmodes))
854 {
855 printf("No HT mode information available\n");
856 return;
857 }
858
859 for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++)
860 if (htmodes & (1 << i))
861 printf("%s ", IWINFO_HTMODE_NAMES[i]);
862
863 printf("\n");
864 }
865
866 static void lookup_phy(const struct iwinfo_ops *iw, const char *section)
867 {
868 char buf[IWINFO_BUFSIZE];
869
870 if (!iw->lookup_phy)
871 {
872 fprintf(stderr, "Not supported\n");
873 return;
874 }
875
876 if (iw->lookup_phy(section, buf))
877 {
878 fprintf(stderr, "Phy not found\n");
879 return;
880 }
881
882 printf("%s\n", buf);
883 }
884
885
886 static void lookup_path(const struct iwinfo_ops *iw, const char *phy)
887 {
888 const char *path;
889
890 if (!iw->phy_path || iw->phy_path(phy, &path) || !path)
891 return;
892
893 printf("%s\n", path);
894 }
895
896 int main(int argc, char **argv)
897 {
898 int i, rv = 0;
899 char *p;
900 const struct iwinfo_ops *iw;
901 glob_t globbuf;
902
903 if (argc > 1 && argc < 3)
904 {
905 fprintf(stderr,
906 "Usage:\n"
907 " iwinfo <device> info\n"
908 " iwinfo <device> scan\n"
909 " iwinfo <device> txpowerlist\n"
910 " iwinfo <device> freqlist\n"
911 " iwinfo <device> assoclist\n"
912 " iwinfo <device> countrylist\n"
913 " iwinfo <device> htmodelist\n"
914 " iwinfo <backend> phyname <section>\n"
915 );
916
917 return 1;
918 }
919
920 if (argc == 1)
921 {
922 glob("/sys/class/net/*", 0, NULL, &globbuf);
923
924 for (i = 0; i < globbuf.gl_pathc; i++)
925 {
926 p = strrchr(globbuf.gl_pathv[i], '/');
927
928 if (!p)
929 continue;
930
931 iw = iwinfo_backend(++p);
932
933 if (!iw)
934 continue;
935
936 print_info(iw, p);
937 printf("\n");
938 }
939
940 globfree(&globbuf);
941 return 0;
942 }
943
944 if (argc > 3)
945 {
946 iw = iwinfo_backend_by_name(argv[1]);
947
948 if (!iw)
949 {
950 fprintf(stderr, "No such wireless backend: %s\n", argv[1]);
951 rv = 1;
952 }
953 else
954 {
955 if (!strcmp(argv[2], "path")) {
956 lookup_path(iw, argv[3]);
957 return 0;
958 }
959 switch (argv[2][0])
960 {
961 case 'p':
962 lookup_phy(iw, argv[3]);
963 break;
964
965 default:
966 fprintf(stderr, "Unknown command: %s\n", argv[2]);
967 rv = 1;
968 }
969 }
970 }
971 else
972 {
973 iw = iwinfo_backend(argv[1]);
974
975 if (!iw)
976 {
977 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
978 rv = 1;
979 }
980 else
981 {
982 for (i = 2; i < argc; i++)
983 {
984 switch(argv[i][0])
985 {
986 case 'i':
987 print_info(iw, argv[1]);
988 break;
989
990 case 's':
991 print_scanlist(iw, argv[1]);
992 break;
993
994 case 't':
995 print_txpwrlist(iw, argv[1]);
996 break;
997
998 case 'f':
999 print_freqlist(iw, argv[1]);
1000 break;
1001
1002 case 'a':
1003 print_assoclist(iw, argv[1]);
1004 break;
1005
1006 case 'c':
1007 print_countrylist(iw, argv[1]);
1008 break;
1009
1010 case 'h':
1011 print_htmodelist(iw, argv[1]);
1012 break;
1013
1014 default:
1015 fprintf(stderr, "Unknown command: %s\n", argv[i]);
1016 rv = 1;
1017 }
1018 }
1019 }
1020 }
1021
1022 iwinfo_finish();
1023
1024 return rv;
1025 }