2 * iwinfo - Wireless Information Library - Command line frontend
4 * Copyright (C) 2011 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
24 static char * format_bssid(unsigned char *mac)
28 snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
29 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
34 static char * format_ssid(char *ssid)
36 static char buf[IWINFO_ESSID_MAX_SIZE+3];
39 snprintf(buf, sizeof(buf), "\"%s\"", ssid);
41 snprintf(buf, sizeof(buf), "unknown");
46 static char * format_channel(int ch)
51 snprintf(buf, sizeof(buf), "unknown");
53 snprintf(buf, sizeof(buf), "%d", ch);
58 static char * format_frequency(int freq)
63 snprintf(buf, sizeof(buf), "unknown");
65 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)freq / 1000.0));
70 static char * format_txpower(int pwr)
75 snprintf(buf, sizeof(buf), "unknown");
77 snprintf(buf, sizeof(buf), "%d dBm", pwr);
82 static char * format_quality(int qual)
87 snprintf(buf, sizeof(buf), "unknown");
89 snprintf(buf, sizeof(buf), "%d", qual);
94 static char * format_quality_max(int qmax)
99 snprintf(buf, sizeof(buf), "unknown");
101 snprintf(buf, sizeof(buf), "%d", qmax);
106 static char * format_signal(int sig)
111 snprintf(buf, sizeof(buf), "unknown");
113 snprintf(buf, sizeof(buf), "%d dBm", sig);
118 static char * format_noise(int noise)
123 snprintf(buf, sizeof(buf), "unknown");
125 snprintf(buf, sizeof(buf), "%d dBm", noise);
130 static char * format_rate(int rate)
135 snprintf(buf, sizeof(buf), "unknown");
137 snprintf(buf, sizeof(buf), "%.1f MBit/s", ((float)rate / 1000.0));
142 static char * format_enc_ciphers(int ciphers)
144 static char str[128] = { 0 };
147 if (ciphers & IWINFO_CIPHER_WEP40)
148 pos += sprintf(pos, "WEP-40, ");
150 if (ciphers & IWINFO_CIPHER_WEP104)
151 pos += sprintf(pos, "WEP-104, ");
153 if (ciphers & IWINFO_CIPHER_TKIP)
154 pos += sprintf(pos, "TKIP, ");
156 if (ciphers & IWINFO_CIPHER_CCMP)
157 pos += sprintf(pos, "CCMP, ");
159 if (ciphers & IWINFO_CIPHER_WRAP)
160 pos += sprintf(pos, "WRAP, ");
162 if (ciphers & IWINFO_CIPHER_AESOCB)
163 pos += sprintf(pos, "AES-OCB, ");
165 if (ciphers & IWINFO_CIPHER_CKIP)
166 pos += sprintf(pos, "CKIP, ");
168 if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
169 pos += sprintf(pos, "NONE, ");
176 static char * format_enc_suites(int suites)
178 static char str[64] = { 0 };
181 if (suites & IWINFO_KMGMT_PSK)
182 pos += sprintf(pos, "PSK/");
184 if (suites & IWINFO_KMGMT_8021x)
185 pos += sprintf(pos, "802.1X/");
187 if (!suites || (suites & IWINFO_KMGMT_NONE))
188 pos += sprintf(pos, "NONE/");
195 static char * format_encryption(struct iwinfo_crypto_entry *c)
197 static char buf[512];
201 snprintf(buf, sizeof(buf), "unknown");
206 if (c->auth_algs && !c->wpa_version)
208 if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
209 (c->auth_algs & IWINFO_AUTH_SHARED))
211 snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
212 format_enc_ciphers(c->pair_ciphers));
214 else if (c->auth_algs & IWINFO_AUTH_OPEN)
216 snprintf(buf, sizeof(buf), "WEP Open System (%s)",
217 format_enc_ciphers(c->pair_ciphers));
219 else if (c->auth_algs & IWINFO_AUTH_SHARED)
221 snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
222 format_enc_ciphers(c->pair_ciphers));
227 else if (c->wpa_version)
229 switch (c->wpa_version) {
231 snprintf(buf, sizeof(buf), "mixed WPA/WPA2 %s (%s)",
232 format_enc_suites(c->auth_suites),
233 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
237 snprintf(buf, sizeof(buf), "WPA2 %s (%s)",
238 format_enc_suites(c->auth_suites),
239 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
243 snprintf(buf, sizeof(buf), "WPA %s (%s)",
244 format_enc_suites(c->auth_suites),
245 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
251 snprintf(buf, sizeof(buf), "none");
256 snprintf(buf, sizeof(buf), "none");
262 static char * format_hwmodes(int modes)
267 snprintf(buf, sizeof(buf), "unknown");
269 snprintf(buf, sizeof(buf), "802.11%s%s%s%s",
270 (modes & IWINFO_80211_A) ? "a" : "",
271 (modes & IWINFO_80211_B) ? "b" : "",
272 (modes & IWINFO_80211_G) ? "g" : "",
273 (modes & IWINFO_80211_N) ? "n" : "");
278 static char * format_assocrate(struct iwinfo_rate_entry *r)
286 snprintf(buf, sizeof(buf), "unknown");
290 p += snprintf(p, l, "%s", format_rate(r->rate));
291 l = sizeof(buf) - (p - buf);
295 p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, 20 + r->is_40mhz*20);
296 l = sizeof(buf) - (p - buf);
299 p += snprintf(p, l, ", short GI");
307 static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
309 const char *type = iwinfo_type(ifname);
310 return type ? type : "unknown";
313 static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
316 struct iwinfo_hardware_id ids;
318 if (!iw->hardware_id(ifname, (char *)&ids))
320 snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
321 ids.vendor_id, ids.device_id,
322 ids.subsystem_vendor_id, ids.subsystem_device_id);
326 snprintf(buf, sizeof(buf), "unknown");
332 static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
334 static char buf[128];
336 if (iw->hardware_name(ifname, buf))
337 snprintf(buf, sizeof(buf), "unknown");
342 static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
347 if (iw->txpower_offset(ifname, &off))
348 snprintf(buf, sizeof(buf), "unknown");
350 snprintf(buf, sizeof(buf), "%d dB", off);
352 snprintf(buf, sizeof(buf), "none");
357 static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
362 if (iw->frequency_offset(ifname, &off))
363 snprintf(buf, sizeof(buf), "unknown");
365 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
367 snprintf(buf, sizeof(buf), "none");
372 static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
374 char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
376 if (iw->ssid(ifname, buf))
377 memset(buf, 0, sizeof(buf));
379 return format_ssid(buf);
382 static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
384 static char buf[18] = { 0 };
386 if (iw->bssid(ifname, buf))
387 snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
392 static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
395 static char buf[128];
397 if (iw->mode(ifname, &mode))
398 mode = IWINFO_OPMODE_UNKNOWN;
400 snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
405 static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
408 if (iw->channel(ifname, &ch))
411 return format_channel(ch);
414 static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
417 if (iw->frequency(ifname, &freq))
420 return format_frequency(freq);
423 static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
426 if (iw->txpower_offset(ifname, &off))
429 if (iw->txpower(ifname, &pwr))
434 return format_txpower(pwr);
437 static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
440 if (iw->quality(ifname, &qual))
443 return format_quality(qual);
446 static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
449 if (iw->quality_max(ifname, &qmax))
452 return format_quality_max(qmax);
455 static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
458 if (iw->signal(ifname, &sig))
461 return format_signal(sig);
464 static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
467 if (iw->noise(ifname, &noise))
470 return format_noise(noise);
473 static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
476 if (iw->bitrate(ifname, &rate))
479 return format_rate(rate);
482 static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
484 struct iwinfo_crypto_entry c = { 0 };
485 if (iw->encryption(ifname, (char *)&c))
486 return format_encryption(NULL);
488 return format_encryption(&c);
491 static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
494 if (iw->hwmodelist(ifname, &modes))
497 return format_hwmodes(modes);
500 static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
505 if (iw->mbssid_support(ifname, &supp))
506 snprintf(buf, sizeof(buf), "no");
508 snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
514 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
516 printf("%-9s ESSID: %s\n",
518 print_ssid(iw, ifname));
519 printf(" Access Point: %s\n",
520 print_bssid(iw, ifname));
521 printf(" Mode: %s Channel: %s (%s)\n",
522 print_mode(iw, ifname),
523 print_channel(iw, ifname),
524 print_frequency(iw, ifname));
525 printf(" Tx-Power: %s Link Quality: %s/%s\n",
526 print_txpower(iw, ifname),
527 print_quality(iw, ifname),
528 print_quality_max(iw, ifname));
529 printf(" Signal: %s Noise: %s\n",
530 print_signal(iw, ifname),
531 print_noise(iw, ifname));
532 printf(" Bit Rate: %s\n",
533 print_rate(iw, ifname));
534 printf(" Encryption: %s\n",
535 print_encryption(iw, ifname));
536 printf(" Type: %s HW Mode(s): %s\n",
537 print_type(iw, ifname),
538 print_hwmodes(iw, ifname));
539 printf(" Hardware: %s [%s]\n",
540 print_hardware_id(iw, ifname),
541 print_hardware_name(iw, ifname));
542 printf(" TX power offset: %s\n",
543 print_txpower_offset(iw, ifname));
544 printf(" Frequency offset: %s\n",
545 print_frequency_offset(iw, ifname));
546 printf(" Supports VAPs: %s\n",
547 print_mbssid_supp(iw, ifname));
551 static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
554 char buf[IWINFO_BUFSIZE];
555 struct iwinfo_scanlist_entry *e;
557 if (iw->scanlist(ifname, buf, &len))
559 printf("Scanning not possible\n\n");
564 printf("No scan results\n\n");
568 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
570 e = (struct iwinfo_scanlist_entry *) &buf[i];
572 printf("Cell %02d - Address: %s\n",
574 format_bssid(e->mac));
575 printf(" ESSID: %s\n",
576 format_ssid(e->ssid));
577 printf(" Mode: %s Channel: %s\n",
578 IWINFO_OPMODE_NAMES[e->mode],
579 format_channel(e->channel));
580 printf(" Signal: %s Quality: %s/%s\n",
581 format_signal(e->signal - 0x100),
582 format_quality(e->quality),
583 format_quality_max(e->quality_max));
584 printf(" Encryption: %s\n\n",
585 format_encryption(&e->crypto));
590 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
592 int len, pwr, off, i;
593 char buf[IWINFO_BUFSIZE];
594 struct iwinfo_txpwrlist_entry *e;
596 if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
598 printf("No TX power information available\n");
602 if (iw->txpower(ifname, &pwr))
605 if (iw->txpower_offset(ifname, &off))
608 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
610 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
612 printf("%s%3d dBm (%4d mW)\n",
613 (pwr == e->dbm) ? "*" : " ",
615 iwinfo_dbm2mw(e->dbm + off));
620 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
623 char buf[IWINFO_BUFSIZE];
624 struct iwinfo_freqlist_entry *e;
626 if (iw->freqlist(ifname, buf, &len) || len <= 0)
628 printf("No frequency information available\n");
632 if (iw->channel(ifname, &ch))
635 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
637 e = (struct iwinfo_freqlist_entry *) &buf[i];
639 printf("%s %s (Channel %s)%s\n",
640 (ch == e->channel) ? "*" : " ",
641 format_frequency(e->mhz),
642 format_channel(e->channel),
643 e->restricted ? " [restricted]" : "");
648 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
651 char buf[IWINFO_BUFSIZE];
652 struct iwinfo_assoclist_entry *e;
654 if (iw->assoclist(ifname, buf, &len))
656 printf("No information available\n");
661 printf("No station connected\n");
665 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
667 e = (struct iwinfo_assoclist_entry *) &buf[i];
669 printf("%s %s / %s (SNR %d) %d ms ago\n",
670 format_bssid(e->mac),
671 format_signal(e->signal),
672 format_noise(e->noise),
673 (e->signal - e->noise),
676 printf(" RX: %-38s %8d Pkts.\n",
677 format_assocrate(&e->rx_rate),
681 printf(" TX: %-38s %8d Pkts.\n\n",
682 format_assocrate(&e->tx_rate),
689 static char * lookup_country(char *buf, int len, int iso3166)
692 struct iwinfo_country_entry *c;
694 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
696 c = (struct iwinfo_country_entry *) &buf[i];
698 if (c->iso3166 == iso3166)
705 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
708 char buf[IWINFO_BUFSIZE];
711 const struct iwinfo_iso3166_label *l;
713 if (iw->countrylist(ifname, buf, &len))
715 printf("No country code information available\n");
719 if (iw->country(ifname, curcode))
720 memset(curcode, 0, sizeof(curcode));
722 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
724 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
726 printf("%s %4s %c%c\n",
727 strncmp(ccode, curcode, 2) ? " " : "*",
728 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
734 int main(int argc, char **argv)
737 const struct iwinfo_ops *iw;
743 " iwinfo <device> info\n"
744 " iwinfo <device> scan\n"
745 " iwinfo <device> txpowerlist\n"
746 " iwinfo <device> freqlist\n"
747 " iwinfo <device> assoclist\n"
748 " iwinfo <device> countrylist\n"
754 iw = iwinfo_backend(argv[1]);
758 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
762 for (i = 2; i < argc; i++)
767 print_info(iw, argv[1]);
771 print_scanlist(iw, argv[1]);
775 print_txpwrlist(iw, argv[1]);
779 print_freqlist(iw, argv[1]);
783 print_assoclist(iw, argv[1]);
787 print_countrylist(iw, argv[1]);
791 fprintf(stderr, "Unknown command: %s\n", argv[i]);