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