iwinfo_cli.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. #include <stdio.h>
  19. #include <glob.h>
  20. #include "iwinfo.h"
  21. static char * format_bssid(unsigned char *mac)
  22. {
  23. static char buf[18];
  24. snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
  25. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  26. return buf;
  27. }
  28. static char * format_ssid(char *ssid)
  29. {
  30. static char buf[IWINFO_ESSID_MAX_SIZE+3];
  31. if (ssid && ssid[0])
  32. snprintf(buf, sizeof(buf), "\"%s\"", ssid);
  33. else
  34. snprintf(buf, sizeof(buf), "unknown");
  35. return buf;
  36. }
  37. static char * format_channel(int ch)
  38. {
  39. static char buf[8];
  40. if (ch <= 0)
  41. snprintf(buf, sizeof(buf), "unknown");
  42. else
  43. snprintf(buf, sizeof(buf), "%d", ch);
  44. return buf;
  45. }
  46. static char * format_frequency(int freq)
  47. {
  48. static char buf[11];
  49. if (freq <= 0)
  50. snprintf(buf, sizeof(buf), "unknown");
  51. else
  52. snprintf(buf, sizeof(buf), "%.3f GHz", ((float)freq / 1000.0));
  53. return buf;
  54. }
  55. static char * format_txpower(int pwr)
  56. {
  57. static char buf[10];
  58. if (pwr < 0)
  59. snprintf(buf, sizeof(buf), "unknown");
  60. else
  61. snprintf(buf, sizeof(buf), "%d dBm", pwr);
  62. return buf;
  63. }
  64. static char * format_quality(int qual)
  65. {
  66. static char buf[8];
  67. if (qual < 0)
  68. snprintf(buf, sizeof(buf), "unknown");
  69. else
  70. snprintf(buf, sizeof(buf), "%d", qual);
  71. return buf;
  72. }
  73. static char * format_quality_max(int qmax)
  74. {
  75. static char buf[8];
  76. if (qmax < 0)
  77. snprintf(buf, sizeof(buf), "unknown");
  78. else
  79. snprintf(buf, sizeof(buf), "%d", qmax);
  80. return buf;
  81. }
  82. static char * format_signal(int sig)
  83. {
  84. static char buf[10];
  85. if (!sig)
  86. snprintf(buf, sizeof(buf), "unknown");
  87. else
  88. snprintf(buf, sizeof(buf), "%d dBm", sig);
  89. return buf;
  90. }
  91. static char * format_noise(int noise)
  92. {
  93. static char buf[10];
  94. if (!noise)
  95. snprintf(buf, sizeof(buf), "unknown");
  96. else
  97. snprintf(buf, sizeof(buf), "%d dBm", noise);
  98. return buf;
  99. }
  100. static char * format_rate(int rate)
  101. {
  102. static char buf[18];
  103. if (rate <= 0)
  104. snprintf(buf, sizeof(buf), "unknown");
  105. else
  106. snprintf(buf, sizeof(buf), "%d.%d MBit/s",
  107. rate / 1000, (rate % 1000) / 100);
  108. return buf;
  109. }
  110. static char * format_enc_ciphers(int ciphers)
  111. {
  112. static char str[128] = { 0 };
  113. char *pos = str;
  114. if (ciphers & IWINFO_CIPHER_WEP40)
  115. pos += sprintf(pos, "WEP-40, ");
  116. if (ciphers & IWINFO_CIPHER_WEP104)
  117. pos += sprintf(pos, "WEP-104, ");
  118. if (ciphers & IWINFO_CIPHER_TKIP)
  119. pos += sprintf(pos, "TKIP, ");
  120. if (ciphers & IWINFO_CIPHER_CCMP)
  121. pos += sprintf(pos, "CCMP, ");
  122. if (ciphers & IWINFO_CIPHER_GCMP)
  123. pos += sprintf(pos, "GCMP, ");
  124. if (ciphers & IWINFO_CIPHER_WRAP)
  125. pos += sprintf(pos, "WRAP, ");
  126. if (ciphers & IWINFO_CIPHER_AESOCB)
  127. pos += sprintf(pos, "AES-OCB, ");
  128. if (ciphers & IWINFO_CIPHER_CKIP)
  129. pos += sprintf(pos, "CKIP, ");
  130. if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
  131. pos += sprintf(pos, "NONE, ");
  132. *(pos - 2) = 0;
  133. return str;
  134. }
  135. static char * format_enc_suites(int suites)
  136. {
  137. static char str[64] = { 0 };
  138. char *pos = str;
  139. if (suites & IWINFO_KMGMT_PSK)
  140. pos += sprintf(pos, "PSK/");
  141. if (suites & IWINFO_KMGMT_8021x)
  142. pos += sprintf(pos, "802.1X/");
  143. if (suites & IWINFO_KMGMT_SAE)
  144. pos += sprintf(pos, "SAE/");
  145. if (suites & IWINFO_KMGMT_OWE)
  146. pos += sprintf(pos, "OWE/");
  147. if (!suites || (suites & IWINFO_KMGMT_NONE))
  148. pos += sprintf(pos, "NONE/");
  149. *(pos - 1) = 0;
  150. return str;
  151. }
  152. static char * format_encryption(struct iwinfo_crypto_entry *c)
  153. {
  154. static char buf[512];
  155. char *pos = buf;
  156. int i, n;
  157. if (!c)
  158. {
  159. snprintf(buf, sizeof(buf), "unknown");
  160. }
  161. else if (c->enabled)
  162. {
  163. /* WEP */
  164. if (c->auth_algs && !c->wpa_version)
  165. {
  166. if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
  167. (c->auth_algs & IWINFO_AUTH_SHARED))
  168. {
  169. snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
  170. format_enc_ciphers(c->pair_ciphers));
  171. }
  172. else if (c->auth_algs & IWINFO_AUTH_OPEN)
  173. {
  174. snprintf(buf, sizeof(buf), "WEP Open System (%s)",
  175. format_enc_ciphers(c->pair_ciphers));
  176. }
  177. else if (c->auth_algs & IWINFO_AUTH_SHARED)
  178. {
  179. snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
  180. format_enc_ciphers(c->pair_ciphers));
  181. }
  182. }
  183. /* WPA */
  184. else if (c->wpa_version)
  185. {
  186. for (i = 0, n = 0; i < 3; i++)
  187. if (c->wpa_version & (1 << i))
  188. n++;
  189. if (n > 1)
  190. pos += sprintf(pos, "mixed ");
  191. for (i = 0; i < 3; i++)
  192. if (c->wpa_version & (1 << i))
  193. if (i)
  194. pos += sprintf(pos, "WPA%d/", i + 1);
  195. else
  196. pos += sprintf(pos, "WPA/");
  197. pos--;
  198. sprintf(pos, " %s (%s)",
  199. format_enc_suites(c->auth_suites),
  200. format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
  201. }
  202. else
  203. {
  204. snprintf(buf, sizeof(buf), "none");
  205. }
  206. }
  207. else
  208. {
  209. snprintf(buf, sizeof(buf), "none");
  210. }
  211. return buf;
  212. }
  213. static char * format_hwmodes(int modes)
  214. {
  215. static char buf[15];
  216. if (modes <= 0)
  217. snprintf(buf, sizeof(buf), "unknown");
  218. else
  219. snprintf(buf, sizeof(buf), "802.11%s%s%s%s%s%s",
  220. (modes & IWINFO_80211_A) ? "a" : "",
  221. (modes & IWINFO_80211_B) ? "b" : "",
  222. (modes & IWINFO_80211_G) ? "g" : "",
  223. (modes & IWINFO_80211_N) ? "n" : "",
  224. (modes & IWINFO_80211_AC) ? "ac" : "",
  225. (modes & IWINFO_80211_AD) ? "ad" : "");
  226. return buf;
  227. }
  228. static char * format_assocrate(struct iwinfo_rate_entry *r)
  229. {
  230. static char buf[80];
  231. char *p = buf;
  232. int l = sizeof(buf);
  233. if (r->rate <= 0)
  234. {
  235. snprintf(buf, sizeof(buf), "unknown");
  236. }
  237. else
  238. {
  239. p += snprintf(p, l, "%s", format_rate(r->rate));
  240. l = sizeof(buf) - (p - buf);
  241. if (r->is_ht)
  242. {
  243. p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, r->mhz);
  244. l = sizeof(buf) - (p - buf);
  245. }
  246. else if (r->is_vht)
  247. {
  248. p += snprintf(p, l, ", VHT-MCS %d, %dMHz", r->mcs, r->mhz);
  249. l = sizeof(buf) - (p - buf);
  250. if (r->nss)
  251. {
  252. p += snprintf(p, l, ", VHT-NSS %d", r->nss);
  253. l = sizeof(buf) - (p - buf);
  254. }
  255. }
  256. }
  257. return buf;
  258. }
  259. static const char* format_chan_width(uint16_t width)
  260. {
  261. switch (width) {
  262. case 20: return "20 MHz";
  263. case 2040: return "40 MHz and upper or 20 MHz with intolerant bit";
  264. case 40: return "40 MHz or lower";
  265. case 80: return "80 MHz";
  266. case 8080: return "80+80 MHz";
  267. case 160: return "160 MHz";
  268. }
  269. return "unknown";
  270. }
  271. static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
  272. {
  273. const char *type = iwinfo_type(ifname);
  274. return type ? type : "unknown";
  275. }
  276. static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
  277. {
  278. static char buf[20];
  279. struct iwinfo_hardware_id ids;
  280. if (!iw->hardware_id(ifname, (char *)&ids))
  281. {
  282. snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
  283. ids.vendor_id, ids.device_id,
  284. ids.subsystem_vendor_id, ids.subsystem_device_id);
  285. }
  286. else
  287. {
  288. snprintf(buf, sizeof(buf), "unknown");
  289. }
  290. return buf;
  291. }
  292. static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
  293. {
  294. static char buf[128];
  295. if (iw->hardware_name(ifname, buf))
  296. snprintf(buf, sizeof(buf), "unknown");
  297. return buf;
  298. }
  299. static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
  300. {
  301. int off;
  302. static char buf[12];
  303. if (iw->txpower_offset(ifname, &off))
  304. snprintf(buf, sizeof(buf), "unknown");
  305. else if (off != 0)
  306. snprintf(buf, sizeof(buf), "%d dB", off);
  307. else
  308. snprintf(buf, sizeof(buf), "none");
  309. return buf;
  310. }
  311. static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
  312. {
  313. int off;
  314. static char buf[12];
  315. if (iw->frequency_offset(ifname, &off))
  316. snprintf(buf, sizeof(buf), "unknown");
  317. else if (off != 0)
  318. snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
  319. else
  320. snprintf(buf, sizeof(buf), "none");
  321. return buf;
  322. }
  323. static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
  324. {
  325. char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
  326. if (iw->ssid(ifname, buf))
  327. memset(buf, 0, sizeof(buf));
  328. return format_ssid(buf);
  329. }
  330. static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
  331. {
  332. static char buf[18] = { 0 };
  333. if (iw->bssid(ifname, buf))
  334. snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
  335. return buf;
  336. }
  337. static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
  338. {
  339. int mode;
  340. static char buf[128];
  341. if (iw->mode(ifname, &mode))
  342. mode = IWINFO_OPMODE_UNKNOWN;
  343. snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
  344. return buf;
  345. }
  346. static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
  347. {
  348. int ch;
  349. if (iw->channel(ifname, &ch))
  350. ch = -1;
  351. return format_channel(ch);
  352. }
  353. static char * print_center_chan1(const struct iwinfo_ops *iw, const char *ifname)
  354. {
  355. int ch;
  356. if (iw->center_chan1(ifname, &ch))
  357. ch = -1;
  358. return format_channel(ch);
  359. }
  360. static char * print_center_chan2(const struct iwinfo_ops *iw, const char *ifname)
  361. {
  362. int ch;
  363. if (iw->center_chan2(ifname, &ch))
  364. ch = -1;
  365. return format_channel(ch);
  366. }
  367. static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
  368. {
  369. int freq;
  370. if (iw->frequency(ifname, &freq))
  371. freq = -1;
  372. return format_frequency(freq);
  373. }
  374. static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
  375. {
  376. int pwr, off;
  377. if (iw->txpower_offset(ifname, &off))
  378. off = 0;
  379. if (iw->txpower(ifname, &pwr))
  380. pwr = -1;
  381. else
  382. pwr += off;
  383. return format_txpower(pwr);
  384. }
  385. static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
  386. {
  387. int qual;
  388. if (iw->quality(ifname, &qual))
  389. qual = -1;
  390. return format_quality(qual);
  391. }
  392. static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
  393. {
  394. int qmax;
  395. if (iw->quality_max(ifname, &qmax))
  396. qmax = -1;
  397. return format_quality_max(qmax);
  398. }
  399. static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
  400. {
  401. int sig;
  402. if (iw->signal(ifname, &sig))
  403. sig = 0;
  404. return format_signal(sig);
  405. }
  406. static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
  407. {
  408. int noise;
  409. if (iw->noise(ifname, &noise))
  410. noise = 0;
  411. return format_noise(noise);
  412. }
  413. static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
  414. {
  415. int rate;
  416. if (iw->bitrate(ifname, &rate))
  417. rate = -1;
  418. return format_rate(rate);
  419. }
  420. static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
  421. {
  422. struct iwinfo_crypto_entry c = { 0 };
  423. if (iw->encryption(ifname, (char *)&c))
  424. return format_encryption(NULL);
  425. return format_encryption(&c);
  426. }
  427. static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
  428. {
  429. int modes;
  430. if (iw->hwmodelist(ifname, &modes))
  431. modes = -1;
  432. return format_hwmodes(modes);
  433. }
  434. static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
  435. {
  436. int supp;
  437. static char buf[4];
  438. if (iw->mbssid_support(ifname, &supp))
  439. snprintf(buf, sizeof(buf), "no");
  440. else
  441. snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
  442. return buf;
  443. }
  444. static char * print_phyname(const struct iwinfo_ops *iw, const char *ifname)
  445. {
  446. static char buf[32];
  447. if (!iw->phyname(ifname, buf))
  448. return buf;
  449. return "?";
  450. }
  451. static void print_info(const struct iwinfo_ops *iw, const char *ifname)
  452. {
  453. printf("%-9s ESSID: %s\n",
  454. ifname,
  455. print_ssid(iw, ifname));
  456. printf(" Access Point: %s\n",
  457. print_bssid(iw, ifname));
  458. printf(" Mode: %s Channel: %s (%s)\n",
  459. print_mode(iw, ifname),
  460. print_channel(iw, ifname),
  461. print_frequency(iw, ifname));
  462. if (iw->center_chan1 != NULL) {
  463. printf(" Center Channel 1: %s",
  464. print_center_chan1(iw, ifname));
  465. printf(" 2: %s\n", print_center_chan2(iw, ifname));
  466. }
  467. printf(" Tx-Power: %s Link Quality: %s/%s\n",
  468. print_txpower(iw, ifname),
  469. print_quality(iw, ifname),
  470. print_quality_max(iw, ifname));
  471. printf(" Signal: %s Noise: %s\n",
  472. print_signal(iw, ifname),
  473. print_noise(iw, ifname));
  474. printf(" Bit Rate: %s\n",
  475. print_rate(iw, ifname));
  476. printf(" Encryption: %s\n",
  477. print_encryption(iw, ifname));
  478. printf(" Type: %s HW Mode(s): %s\n",
  479. print_type(iw, ifname),
  480. print_hwmodes(iw, ifname));
  481. printf(" Hardware: %s [%s]\n",
  482. print_hardware_id(iw, ifname),
  483. print_hardware_name(iw, ifname));
  484. printf(" TX power offset: %s\n",
  485. print_txpower_offset(iw, ifname));
  486. printf(" Frequency offset: %s\n",
  487. print_frequency_offset(iw, ifname));
  488. printf(" Supports VAPs: %s PHY name: %s\n",
  489. print_mbssid_supp(iw, ifname),
  490. print_phyname(iw, ifname));
  491. }
  492. static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
  493. {
  494. int i, x, len;
  495. char buf[IWINFO_BUFSIZE];
  496. struct iwinfo_scanlist_entry *e;
  497. if (iw->scanlist(ifname, buf, &len))
  498. {
  499. printf("Scanning not possible\n\n");
  500. return;
  501. }
  502. else if (len <= 0)
  503. {
  504. printf("No scan results\n\n");
  505. return;
  506. }
  507. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
  508. {
  509. e = (struct iwinfo_scanlist_entry *) &buf[i];
  510. printf("Cell %02d - Address: %s\n",
  511. x,
  512. format_bssid(e->mac));
  513. printf(" ESSID: %s\n",
  514. format_ssid(e->ssid));
  515. printf(" Mode: %s Channel: %s\n",
  516. IWINFO_OPMODE_NAMES[e->mode],
  517. format_channel(e->channel));
  518. printf(" Signal: %s Quality: %s/%s\n",
  519. format_signal(e->signal - 0x100),
  520. format_quality(e->quality),
  521. format_quality_max(e->quality_max));
  522. printf(" Encryption: %s\n",
  523. format_encryption(&e->crypto));
  524. printf(" HT Operation:\n");
  525. printf(" Primary Channel: %d\n",
  526. e->ht_chan_info.primary_chan);
  527. printf(" Secondary Channel Offset: %s\n",
  528. ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
  529. printf(" Channel Width: %s\n",
  530. format_chan_width(e->ht_chan_info.chan_width));
  531. if (e->vht_chan_info.center_chan_1) {
  532. printf(" VHT Operation:\n");
  533. printf(" Channel Width: %s\n",
  534. format_chan_width(e->vht_chan_info.chan_width));
  535. printf(" Center Frequency 1: %d\n",
  536. e->vht_chan_info.center_chan_1);
  537. printf(" Center Frequency 2: %d\n",
  538. e->vht_chan_info.center_chan_2);
  539. }
  540. printf("\n");
  541. }
  542. }
  543. static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
  544. {
  545. int len, pwr, off, i;
  546. char buf[IWINFO_BUFSIZE];
  547. struct iwinfo_txpwrlist_entry *e;
  548. if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
  549. {
  550. printf("No TX power information available\n");
  551. return;
  552. }
  553. if (iw->txpower(ifname, &pwr))
  554. pwr = -1;
  555. if (iw->txpower_offset(ifname, &off))
  556. off = 0;
  557. for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
  558. {
  559. e = (struct iwinfo_txpwrlist_entry *) &buf[i];
  560. printf("%s%3d dBm (%4d mW)\n",
  561. (pwr == e->dbm) ? "*" : " ",
  562. e->dbm + off,
  563. iwinfo_dbm2mw(e->dbm + off));
  564. }
  565. }
  566. static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
  567. {
  568. int i, len, ch;
  569. char buf[IWINFO_BUFSIZE];
  570. struct iwinfo_freqlist_entry *e;
  571. if (iw->freqlist(ifname, buf, &len) || len <= 0)
  572. {
  573. printf("No frequency information available\n");
  574. return;
  575. }
  576. if (iw->channel(ifname, &ch))
  577. ch = -1;
  578. for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
  579. {
  580. e = (struct iwinfo_freqlist_entry *) &buf[i];
  581. printf("%s %s (Channel %s)%s\n",
  582. (ch == e->channel) ? "*" : " ",
  583. format_frequency(e->mhz),
  584. format_channel(e->channel),
  585. e->restricted ? " [restricted]" : "");
  586. }
  587. }
  588. static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
  589. {
  590. int i, len;
  591. char buf[IWINFO_BUFSIZE];
  592. struct iwinfo_assoclist_entry *e;
  593. if (iw->assoclist(ifname, buf, &len))
  594. {
  595. printf("No information available\n");
  596. return;
  597. }
  598. else if (len <= 0)
  599. {
  600. printf("No station connected\n");
  601. return;
  602. }
  603. for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
  604. {
  605. e = (struct iwinfo_assoclist_entry *) &buf[i];
  606. printf("%s %s / %s (SNR %d) %d ms ago\n",
  607. format_bssid(e->mac),
  608. format_signal(e->signal),
  609. format_noise(e->noise),
  610. (e->signal - e->noise),
  611. e->inactive);
  612. printf(" RX: %-38s %8d Pkts.\n",
  613. format_assocrate(&e->rx_rate),
  614. e->rx_packets
  615. );
  616. printf(" TX: %-38s %8d Pkts.\n",
  617. format_assocrate(&e->tx_rate),
  618. e->tx_packets
  619. );
  620. printf(" expected throughput: %s\n\n",
  621. format_rate(e->thr));
  622. }
  623. }
  624. static char * lookup_country(char *buf, int len, int iso3166)
  625. {
  626. int i;
  627. struct iwinfo_country_entry *c;
  628. for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
  629. {
  630. c = (struct iwinfo_country_entry *) &buf[i];
  631. if (c->iso3166 == iso3166)
  632. return c->ccode;
  633. }
  634. return NULL;
  635. }
  636. static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
  637. {
  638. int len;
  639. char buf[IWINFO_BUFSIZE];
  640. char *ccode;
  641. char curcode[3];
  642. const struct iwinfo_iso3166_label *l;
  643. if (iw->countrylist(ifname, buf, &len))
  644. {
  645. printf("No country code information available\n");
  646. return;
  647. }
  648. if (iw->country(ifname, curcode))
  649. memset(curcode, 0, sizeof(curcode));
  650. for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
  651. {
  652. if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
  653. {
  654. printf("%s %4s %c%c\n",
  655. strncmp(ccode, curcode, 2) ? " " : "*",
  656. ccode, (l->iso3166 / 256), (l->iso3166 % 256));
  657. }
  658. }
  659. }
  660. static void print_htmodelist(const struct iwinfo_ops *iw, const char *ifname)
  661. {
  662. int i, htmodes = 0;
  663. if (iw->htmodelist(ifname, &htmodes))
  664. {
  665. printf("No HT mode information available\n");
  666. return;
  667. }
  668. for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++)
  669. if (htmodes & (1 << i))
  670. printf("%s ", IWINFO_HTMODE_NAMES[i]);
  671. printf("\n");
  672. }
  673. static void lookup_phy(const struct iwinfo_ops *iw, const char *section)
  674. {
  675. char buf[IWINFO_BUFSIZE];
  676. if (!iw->lookup_phy)
  677. {
  678. fprintf(stderr, "Not supported\n");
  679. return;
  680. }
  681. if (iw->lookup_phy(section, buf))
  682. {
  683. fprintf(stderr, "Phy not found\n");
  684. return;
  685. }
  686. printf("%s\n", buf);
  687. }
  688. int main(int argc, char **argv)
  689. {
  690. int i, rv = 0;
  691. char *p;
  692. const struct iwinfo_ops *iw;
  693. glob_t globbuf;
  694. if (argc > 1 && argc < 3)
  695. {
  696. fprintf(stderr,
  697. "Usage:\n"
  698. " iwinfo <device> info\n"
  699. " iwinfo <device> scan\n"
  700. " iwinfo <device> txpowerlist\n"
  701. " iwinfo <device> freqlist\n"
  702. " iwinfo <device> assoclist\n"
  703. " iwinfo <device> countrylist\n"
  704. " iwinfo <device> htmodelist\n"
  705. " iwinfo <backend> phyname <section>\n"
  706. );
  707. return 1;
  708. }
  709. if (argc == 1)
  710. {
  711. glob("/sys/class/net/*", 0, NULL, &globbuf);
  712. for (i = 0; i < globbuf.gl_pathc; i++)
  713. {
  714. p = strrchr(globbuf.gl_pathv[i], '/');
  715. if (!p)
  716. continue;
  717. iw = iwinfo_backend(++p);
  718. if (!iw)
  719. continue;
  720. print_info(iw, p);
  721. printf("\n");
  722. }
  723. globfree(&globbuf);
  724. return 0;
  725. }
  726. if (argc > 3)
  727. {
  728. iw = iwinfo_backend_by_name(argv[1]);
  729. if (!iw)
  730. {
  731. fprintf(stderr, "No such wireless backend: %s\n", argv[1]);
  732. rv = 1;
  733. }
  734. else
  735. {
  736. switch (argv[2][0])
  737. {
  738. case 'p':
  739. lookup_phy(iw, argv[3]);
  740. break;
  741. default:
  742. fprintf(stderr, "Unknown command: %s\n", argv[2]);
  743. rv = 1;
  744. }
  745. }
  746. }
  747. else
  748. {
  749. iw = iwinfo_backend(argv[1]);
  750. if (!iw)
  751. {
  752. fprintf(stderr, "No such wireless device: %s\n", argv[1]);
  753. rv = 1;
  754. }
  755. else
  756. {
  757. for (i = 2; i < argc; i++)
  758. {
  759. switch(argv[i][0])
  760. {
  761. case 'i':
  762. print_info(iw, argv[1]);
  763. break;
  764. case 's':
  765. print_scanlist(iw, argv[1]);
  766. break;
  767. case 't':
  768. print_txpwrlist(iw, argv[1]);
  769. break;
  770. case 'f':
  771. print_freqlist(iw, argv[1]);
  772. break;
  773. case 'a':
  774. print_assoclist(iw, argv[1]);
  775. break;
  776. case 'c':
  777. print_countrylist(iw, argv[1]);
  778. break;
  779. case 'h':
  780. print_htmodelist(iw, argv[1]);
  781. break;
  782. default:
  783. fprintf(stderr, "Unknown command: %s\n", argv[i]);
  784. rv = 1;
  785. }
  786. }
  787. }
  788. }
  789. iwinfo_finish();
  790. return rv;
  791. }