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