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/.
25 static char * format_bssid(unsigned char *mac
)
29 snprintf(buf
, sizeof(buf
), "%02X:%02X:%02X:%02X:%02X:%02X",
30 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
35 static char * format_ssid(char *ssid
)
37 static char buf
[IWINFO_ESSID_MAX_SIZE
+3];
40 snprintf(buf
, sizeof(buf
), "\"%s\"", ssid
);
42 snprintf(buf
, sizeof(buf
), "unknown");
47 static char * format_channel(int ch
)
52 snprintf(buf
, sizeof(buf
), "unknown");
54 snprintf(buf
, sizeof(buf
), "%d", ch
);
59 static char * format_frequency(int freq
)
64 snprintf(buf
, sizeof(buf
), "unknown");
66 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)freq
/ 1000.0));
71 static char * format_txpower(int pwr
)
76 snprintf(buf
, sizeof(buf
), "unknown");
78 snprintf(buf
, sizeof(buf
), "%d dBm", pwr
);
83 static char * format_quality(int qual
)
88 snprintf(buf
, sizeof(buf
), "unknown");
90 snprintf(buf
, sizeof(buf
), "%d", qual
);
95 static char * format_quality_max(int qmax
)
100 snprintf(buf
, sizeof(buf
), "unknown");
102 snprintf(buf
, sizeof(buf
), "%d", qmax
);
107 static char * format_signal(int sig
)
112 snprintf(buf
, sizeof(buf
), "unknown");
114 snprintf(buf
, sizeof(buf
), "%d dBm", sig
);
119 static char * format_noise(int noise
)
124 snprintf(buf
, sizeof(buf
), "unknown");
126 snprintf(buf
, sizeof(buf
), "%d dBm", noise
);
131 static char * format_rate(int rate
)
136 snprintf(buf
, sizeof(buf
), "unknown");
138 snprintf(buf
, sizeof(buf
), "%d.%d MBit/s",
139 rate
/ 1000, (rate
% 1000) / 100);
144 static char * format_enc_ciphers(int ciphers
)
146 static char str
[128] = { 0 };
149 if (ciphers
& IWINFO_CIPHER_WEP40
)
150 pos
+= sprintf(pos
, "WEP-40, ");
152 if (ciphers
& IWINFO_CIPHER_WEP104
)
153 pos
+= sprintf(pos
, "WEP-104, ");
155 if (ciphers
& IWINFO_CIPHER_TKIP
)
156 pos
+= sprintf(pos
, "TKIP, ");
158 if (ciphers
& IWINFO_CIPHER_CCMP
)
159 pos
+= sprintf(pos
, "CCMP, ");
161 if (ciphers
& IWINFO_CIPHER_WRAP
)
162 pos
+= sprintf(pos
, "WRAP, ");
164 if (ciphers
& IWINFO_CIPHER_AESOCB
)
165 pos
+= sprintf(pos
, "AES-OCB, ");
167 if (ciphers
& IWINFO_CIPHER_CKIP
)
168 pos
+= sprintf(pos
, "CKIP, ");
170 if (!ciphers
|| (ciphers
& IWINFO_CIPHER_NONE
))
171 pos
+= sprintf(pos
, "NONE, ");
178 static char * format_enc_suites(int suites
)
180 static char str
[64] = { 0 };
183 if (suites
& IWINFO_KMGMT_PSK
)
184 pos
+= sprintf(pos
, "PSK/");
186 if (suites
& IWINFO_KMGMT_8021x
)
187 pos
+= sprintf(pos
, "802.1X/");
189 if (!suites
|| (suites
& IWINFO_KMGMT_NONE
))
190 pos
+= sprintf(pos
, "NONE/");
197 static char * format_encryption(struct iwinfo_crypto_entry
*c
)
199 static char buf
[512];
203 snprintf(buf
, sizeof(buf
), "unknown");
208 if (c
->auth_algs
&& !c
->wpa_version
)
210 if ((c
->auth_algs
& IWINFO_AUTH_OPEN
) &&
211 (c
->auth_algs
& IWINFO_AUTH_SHARED
))
213 snprintf(buf
, sizeof(buf
), "WEP Open/Shared (%s)",
214 format_enc_ciphers(c
->pair_ciphers
));
216 else if (c
->auth_algs
& IWINFO_AUTH_OPEN
)
218 snprintf(buf
, sizeof(buf
), "WEP Open System (%s)",
219 format_enc_ciphers(c
->pair_ciphers
));
221 else if (c
->auth_algs
& IWINFO_AUTH_SHARED
)
223 snprintf(buf
, sizeof(buf
), "WEP Shared Auth (%s)",
224 format_enc_ciphers(c
->pair_ciphers
));
229 else if (c
->wpa_version
)
231 switch (c
->wpa_version
) {
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
));
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
));
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
));
253 snprintf(buf
, sizeof(buf
), "none");
258 snprintf(buf
, sizeof(buf
), "none");
264 static char * format_hwmodes(int modes
)
269 snprintf(buf
, sizeof(buf
), "unknown");
271 snprintf(buf
, sizeof(buf
), "802.11%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" : "");
280 static char * format_assocrate(struct iwinfo_rate_entry
*r
)
288 snprintf(buf
, sizeof(buf
), "unknown");
292 p
+= snprintf(p
, l
, "%s", format_rate(r
->rate
));
293 l
= sizeof(buf
) - (p
- buf
);
297 p
+= snprintf(p
, l
, ", MCS %d, %dMHz", r
->mcs
, 20 + r
->is_40mhz
*20);
298 l
= sizeof(buf
) - (p
- buf
);
301 p
+= snprintf(p
, l
, ", short GI");
309 static const char * print_type(const struct iwinfo_ops
*iw
, const char *ifname
)
311 const char *type
= iwinfo_type(ifname
);
312 return type
? type
: "unknown";
315 static char * print_hardware_id(const struct iwinfo_ops
*iw
, const char *ifname
)
318 struct iwinfo_hardware_id ids
;
320 if (!iw
->hardware_id(ifname
, (char *)&ids
))
322 snprintf(buf
, sizeof(buf
), "%04X:%04X %04X:%04X",
323 ids
.vendor_id
, ids
.device_id
,
324 ids
.subsystem_vendor_id
, ids
.subsystem_device_id
);
328 snprintf(buf
, sizeof(buf
), "unknown");
334 static char * print_hardware_name(const struct iwinfo_ops
*iw
, const char *ifname
)
336 static char buf
[128];
338 if (iw
->hardware_name(ifname
, buf
))
339 snprintf(buf
, sizeof(buf
), "unknown");
344 static char * print_txpower_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
349 if (iw
->txpower_offset(ifname
, &off
))
350 snprintf(buf
, sizeof(buf
), "unknown");
352 snprintf(buf
, sizeof(buf
), "%d dB", off
);
354 snprintf(buf
, sizeof(buf
), "none");
359 static char * print_frequency_offset(const struct iwinfo_ops
*iw
, const char *ifname
)
364 if (iw
->frequency_offset(ifname
, &off
))
365 snprintf(buf
, sizeof(buf
), "unknown");
367 snprintf(buf
, sizeof(buf
), "%.3f GHz", ((float)off
/ 1000.0));
369 snprintf(buf
, sizeof(buf
), "none");
374 static char * print_ssid(const struct iwinfo_ops
*iw
, const char *ifname
)
376 char buf
[IWINFO_ESSID_MAX_SIZE
+1] = { 0 };
378 if (iw
->ssid(ifname
, buf
))
379 memset(buf
, 0, sizeof(buf
));
381 return format_ssid(buf
);
384 static char * print_bssid(const struct iwinfo_ops
*iw
, const char *ifname
)
386 static char buf
[18] = { 0 };
388 if (iw
->bssid(ifname
, buf
))
389 snprintf(buf
, sizeof(buf
), "00:00:00:00:00:00");
394 static char * print_mode(const struct iwinfo_ops
*iw
, const char *ifname
)
397 static char buf
[128];
399 if (iw
->mode(ifname
, &mode
))
400 mode
= IWINFO_OPMODE_UNKNOWN
;
402 snprintf(buf
, sizeof(buf
), "%s", IWINFO_OPMODE_NAMES
[mode
]);
407 static char * print_channel(const struct iwinfo_ops
*iw
, const char *ifname
)
410 if (iw
->channel(ifname
, &ch
))
413 return format_channel(ch
);
416 static char * print_frequency(const struct iwinfo_ops
*iw
, const char *ifname
)
419 if (iw
->frequency(ifname
, &freq
))
422 return format_frequency(freq
);
425 static char * print_txpower(const struct iwinfo_ops
*iw
, const char *ifname
)
428 if (iw
->txpower_offset(ifname
, &off
))
431 if (iw
->txpower(ifname
, &pwr
))
436 return format_txpower(pwr
);
439 static char * print_quality(const struct iwinfo_ops
*iw
, const char *ifname
)
442 if (iw
->quality(ifname
, &qual
))
445 return format_quality(qual
);
448 static char * print_quality_max(const struct iwinfo_ops
*iw
, const char *ifname
)
451 if (iw
->quality_max(ifname
, &qmax
))
454 return format_quality_max(qmax
);
457 static char * print_signal(const struct iwinfo_ops
*iw
, const char *ifname
)
460 if (iw
->signal(ifname
, &sig
))
463 return format_signal(sig
);
466 static char * print_noise(const struct iwinfo_ops
*iw
, const char *ifname
)
469 if (iw
->noise(ifname
, &noise
))
472 return format_noise(noise
);
475 static char * print_rate(const struct iwinfo_ops
*iw
, const char *ifname
)
478 if (iw
->bitrate(ifname
, &rate
))
481 return format_rate(rate
);
484 static char * print_encryption(const struct iwinfo_ops
*iw
, const char *ifname
)
486 struct iwinfo_crypto_entry c
= { 0 };
487 if (iw
->encryption(ifname
, (char *)&c
))
488 return format_encryption(NULL
);
490 return format_encryption(&c
);
493 static char * print_hwmodes(const struct iwinfo_ops
*iw
, const char *ifname
)
496 if (iw
->hwmodelist(ifname
, &modes
))
499 return format_hwmodes(modes
);
502 static char * print_mbssid_supp(const struct iwinfo_ops
*iw
, const char *ifname
)
507 if (iw
->mbssid_support(ifname
, &supp
))
508 snprintf(buf
, sizeof(buf
), "no");
510 snprintf(buf
, sizeof(buf
), "%s", supp
? "yes" : "no");
515 static char * print_phyname(const struct iwinfo_ops
*iw
, const char *ifname
)
519 if (!iw
->phyname(ifname
, buf
))
526 static void print_info(const struct iwinfo_ops
*iw
, const char *ifname
)
528 printf("%-9s ESSID: %s\n",
530 print_ssid(iw
, ifname
));
531 printf(" Access Point: %s\n",
532 print_bssid(iw
, ifname
));
533 printf(" Mode: %s Channel: %s (%s)\n",
534 print_mode(iw
, ifname
),
535 print_channel(iw
, ifname
),
536 print_frequency(iw
, ifname
));
537 printf(" Tx-Power: %s Link Quality: %s/%s\n",
538 print_txpower(iw
, ifname
),
539 print_quality(iw
, ifname
),
540 print_quality_max(iw
, ifname
));
541 printf(" Signal: %s Noise: %s\n",
542 print_signal(iw
, ifname
),
543 print_noise(iw
, ifname
));
544 printf(" Bit Rate: %s\n",
545 print_rate(iw
, ifname
));
546 printf(" Encryption: %s\n",
547 print_encryption(iw
, ifname
));
548 printf(" Type: %s HW Mode(s): %s\n",
549 print_type(iw
, ifname
),
550 print_hwmodes(iw
, ifname
));
551 printf(" Hardware: %s [%s]\n",
552 print_hardware_id(iw
, ifname
),
553 print_hardware_name(iw
, ifname
));
554 printf(" TX power offset: %s\n",
555 print_txpower_offset(iw
, ifname
));
556 printf(" Frequency offset: %s\n",
557 print_frequency_offset(iw
, ifname
));
558 printf(" Supports VAPs: %s PHY name: %s\n",
559 print_mbssid_supp(iw
, ifname
),
560 print_phyname(iw
, ifname
));
564 static void print_scanlist(const struct iwinfo_ops
*iw
, const char *ifname
)
567 char buf
[IWINFO_BUFSIZE
];
568 struct iwinfo_scanlist_entry
*e
;
570 if (iw
->scanlist(ifname
, buf
, &len
))
572 printf("Scanning not possible\n\n");
577 printf("No scan results\n\n");
581 for (i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
), x
++)
583 e
= (struct iwinfo_scanlist_entry
*) &buf
[i
];
585 printf("Cell %02d - Address: %s\n",
587 format_bssid(e
->mac
));
588 printf(" ESSID: %s\n",
589 format_ssid(e
->ssid
));
590 printf(" Mode: %s Channel: %s\n",
591 IWINFO_OPMODE_NAMES
[e
->mode
],
592 format_channel(e
->channel
));
593 printf(" Signal: %s Quality: %s/%s\n",
594 format_signal(e
->signal
- 0x100),
595 format_quality(e
->quality
),
596 format_quality_max(e
->quality_max
));
597 printf(" Encryption: %s\n\n",
598 format_encryption(&e
->crypto
));
603 static void print_txpwrlist(const struct iwinfo_ops
*iw
, const char *ifname
)
605 int len
, pwr
, off
, i
;
606 char buf
[IWINFO_BUFSIZE
];
607 struct iwinfo_txpwrlist_entry
*e
;
609 if (iw
->txpwrlist(ifname
, buf
, &len
) || len
<= 0)
611 printf("No TX power information available\n");
615 if (iw
->txpower(ifname
, &pwr
))
618 if (iw
->txpower_offset(ifname
, &off
))
621 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
))
623 e
= (struct iwinfo_txpwrlist_entry
*) &buf
[i
];
625 printf("%s%3d dBm (%4d mW)\n",
626 (pwr
== e
->dbm
) ? "*" : " ",
628 iwinfo_dbm2mw(e
->dbm
+ off
));
633 static void print_freqlist(const struct iwinfo_ops
*iw
, const char *ifname
)
636 char buf
[IWINFO_BUFSIZE
];
637 struct iwinfo_freqlist_entry
*e
;
639 if (iw
->freqlist(ifname
, buf
, &len
) || len
<= 0)
641 printf("No frequency information available\n");
645 if (iw
->channel(ifname
, &ch
))
648 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
))
650 e
= (struct iwinfo_freqlist_entry
*) &buf
[i
];
652 printf("%s %s (Channel %s)%s\n",
653 (ch
== e
->channel
) ? "*" : " ",
654 format_frequency(e
->mhz
),
655 format_channel(e
->channel
),
656 e
->restricted
? " [restricted]" : "");
661 static void print_assoclist(const struct iwinfo_ops
*iw
, const char *ifname
)
664 char buf
[IWINFO_BUFSIZE
];
665 struct iwinfo_assoclist_entry
*e
;
667 if (iw
->assoclist(ifname
, buf
, &len
))
669 printf("No information available\n");
674 printf("No station connected\n");
678 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
))
680 e
= (struct iwinfo_assoclist_entry
*) &buf
[i
];
682 printf("%s %s / %s (SNR %d) %d ms ago\n",
683 format_bssid(e
->mac
),
684 format_signal(e
->signal
),
685 format_noise(e
->noise
),
686 (e
->signal
- e
->noise
),
689 printf(" RX: %-38s %8d Pkts.\n",
690 format_assocrate(&e
->rx_rate
),
694 printf(" TX: %-38s %8d Pkts.\n\n",
695 format_assocrate(&e
->tx_rate
),
702 static char * lookup_country(char *buf
, int len
, int iso3166
)
705 struct iwinfo_country_entry
*c
;
707 for (i
= 0; i
< len
; i
+= sizeof(struct iwinfo_country_entry
))
709 c
= (struct iwinfo_country_entry
*) &buf
[i
];
711 if (c
->iso3166
== iso3166
)
718 static void print_countrylist(const struct iwinfo_ops
*iw
, const char *ifname
)
721 char buf
[IWINFO_BUFSIZE
];
724 const struct iwinfo_iso3166_label
*l
;
726 if (iw
->countrylist(ifname
, buf
, &len
))
728 printf("No country code information available\n");
732 if (iw
->country(ifname
, curcode
))
733 memset(curcode
, 0, sizeof(curcode
));
735 for (l
= IWINFO_ISO3166_NAMES
; l
->iso3166
; l
++)
737 if ((ccode
= lookup_country(buf
, len
, l
->iso3166
)) != NULL
)
739 printf("%s %4s %c%c\n",
740 strncmp(ccode
, curcode
, 2) ? " " : "*",
741 ccode
, (l
->iso3166
/ 256), (l
->iso3166
% 256));
747 int main(int argc
, char **argv
)
751 const struct iwinfo_ops
*iw
;
754 if (argc
> 1 && argc
< 3)
758 " iwinfo <device> info\n"
759 " iwinfo <device> scan\n"
760 " iwinfo <device> txpowerlist\n"
761 " iwinfo <device> freqlist\n"
762 " iwinfo <device> assoclist\n"
763 " iwinfo <device> countrylist\n"
771 glob("/sys/class/net/*", 0, NULL
, &globbuf
);
773 for (i
= 0; i
< globbuf
.gl_pathc
; i
++)
775 p
= strrchr(globbuf
.gl_pathv
[i
], '/');
780 iw
= iwinfo_backend(++p
);
793 iw
= iwinfo_backend(argv
[1]);
797 fprintf(stderr
, "No such wireless device: %s\n", argv
[1]);
801 for (i
= 2; i
< argc
; i
++)
806 print_info(iw
, argv
[1]);
810 print_scanlist(iw
, argv
[1]);
814 print_txpwrlist(iw
, argv
[1]);
818 print_freqlist(iw
, argv
[1]);
822 print_assoclist(iw
, argv
[1]);
826 print_countrylist(iw
, argv
[1]);
830 fprintf(stderr
, "Unknown command: %s\n", argv
[i
]);