iwinfo_cli.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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[17];
  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%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. (modes & IWINFO_80211_AX) ? "ax" : "");
  227. return buf;
  228. }
  229. static char * format_assocrate(struct iwinfo_rate_entry *r)
  230. {
  231. static char buf[80];
  232. char *p = buf;
  233. int l = sizeof(buf);
  234. if (r->rate <= 0)
  235. {
  236. snprintf(buf, sizeof(buf), "unknown");
  237. }
  238. else
  239. {
  240. p += snprintf(p, l, "%s", format_rate(r->rate));
  241. l = sizeof(buf) - (p - buf);
  242. if (r->is_ht)
  243. {
  244. p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, r->mhz);
  245. l = sizeof(buf) - (p - buf);
  246. }
  247. else if (r->is_vht)
  248. {
  249. p += snprintf(p, l, ", VHT-MCS %d, %dMHz", r->mcs, r->mhz);
  250. l = sizeof(buf) - (p - buf);
  251. if (r->nss)
  252. {
  253. p += snprintf(p, l, ", VHT-NSS %d", r->nss);
  254. l = sizeof(buf) - (p - buf);
  255. }
  256. }
  257. else if (r->is_he)
  258. {
  259. p += snprintf(p, l, ", HE-MCS %d, %dMHz", r->mcs, r->mhz);
  260. l = sizeof(buf) - (p - buf);
  261. p += snprintf(p, l, ", HE-NSS %d", r->nss);
  262. l = sizeof(buf) - (p - buf);
  263. p += snprintf(p, l, ", HE-GI %d", r->he_gi);
  264. l = sizeof(buf) - (p - buf);
  265. p += snprintf(p, l, ", HE-DCM %d", r->he_dcm);
  266. l = sizeof(buf) - (p - buf);
  267. }
  268. }
  269. return buf;
  270. }
  271. static const char* format_chan_width(uint16_t width)
  272. {
  273. switch (width) {
  274. case 20: return "20 MHz";
  275. case 2040: return "40 MHz and upper or 20 MHz with intolerant bit";
  276. case 40: return "40 MHz or lower";
  277. case 80: return "80 MHz";
  278. case 8080: return "80+80 MHz";
  279. case 160: return "160 MHz";
  280. }
  281. return "unknown";
  282. }
  283. static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
  284. {
  285. const char *type = iwinfo_type(ifname);
  286. return type ? type : "unknown";
  287. }
  288. static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
  289. {
  290. static char buf[20];
  291. struct iwinfo_hardware_id ids;
  292. if (!iw->hardware_id(ifname, (char *)&ids))
  293. {
  294. snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
  295. ids.vendor_id, ids.device_id,
  296. ids.subsystem_vendor_id, ids.subsystem_device_id);
  297. }
  298. else
  299. {
  300. snprintf(buf, sizeof(buf), "unknown");
  301. }
  302. return buf;
  303. }
  304. static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
  305. {
  306. static char buf[128];
  307. if (iw->hardware_name(ifname, buf))
  308. snprintf(buf, sizeof(buf), "unknown");
  309. return buf;
  310. }
  311. static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
  312. {
  313. int off;
  314. static char buf[12];
  315. if (iw->txpower_offset(ifname, &off))
  316. snprintf(buf, sizeof(buf), "unknown");
  317. else if (off != 0)
  318. snprintf(buf, sizeof(buf), "%d dB", off);
  319. else
  320. snprintf(buf, sizeof(buf), "none");
  321. return buf;
  322. }
  323. static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
  324. {
  325. int off;
  326. static char buf[12];
  327. if (iw->frequency_offset(ifname, &off))
  328. snprintf(buf, sizeof(buf), "unknown");
  329. else if (off != 0)
  330. snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
  331. else
  332. snprintf(buf, sizeof(buf), "none");
  333. return buf;
  334. }
  335. static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
  336. {
  337. char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
  338. if (iw->ssid(ifname, buf))
  339. memset(buf, 0, sizeof(buf));
  340. return format_ssid(buf);
  341. }
  342. static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
  343. {
  344. static char buf[18] = { 0 };
  345. if (iw->bssid(ifname, buf))
  346. snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
  347. return buf;
  348. }
  349. static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
  350. {
  351. int mode;
  352. static char buf[128];
  353. if (iw->mode(ifname, &mode))
  354. mode = IWINFO_OPMODE_UNKNOWN;
  355. snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
  356. return buf;
  357. }
  358. static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
  359. {
  360. int ch;
  361. if (iw->channel(ifname, &ch))
  362. ch = -1;
  363. return format_channel(ch);
  364. }
  365. static char * print_center_chan1(const struct iwinfo_ops *iw, const char *ifname)
  366. {
  367. int ch;
  368. if (iw->center_chan1(ifname, &ch))
  369. ch = -1;
  370. return format_channel(ch);
  371. }
  372. static char * print_center_chan2(const struct iwinfo_ops *iw, const char *ifname)
  373. {
  374. int ch;
  375. if (iw->center_chan2(ifname, &ch))
  376. ch = -1;
  377. return format_channel(ch);
  378. }
  379. static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
  380. {
  381. int freq;
  382. if (iw->frequency(ifname, &freq))
  383. freq = -1;
  384. return format_frequency(freq);
  385. }
  386. static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
  387. {
  388. int pwr, off;
  389. if (iw->txpower_offset(ifname, &off))
  390. off = 0;
  391. if (iw->txpower(ifname, &pwr))
  392. pwr = -1;
  393. else
  394. pwr += off;
  395. return format_txpower(pwr);
  396. }
  397. static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
  398. {
  399. int qual;
  400. if (iw->quality(ifname, &qual))
  401. qual = -1;
  402. return format_quality(qual);
  403. }
  404. static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
  405. {
  406. int qmax;
  407. if (iw->quality_max(ifname, &qmax))
  408. qmax = -1;
  409. return format_quality_max(qmax);
  410. }
  411. static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
  412. {
  413. int sig;
  414. if (iw->signal(ifname, &sig))
  415. sig = 0;
  416. return format_signal(sig);
  417. }
  418. static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
  419. {
  420. int noise;
  421. if (iw->noise(ifname, &noise))
  422. noise = 0;
  423. return format_noise(noise);
  424. }
  425. static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
  426. {
  427. int rate;
  428. if (iw->bitrate(ifname, &rate))
  429. rate = -1;
  430. return format_rate(rate);
  431. }
  432. static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
  433. {
  434. struct iwinfo_crypto_entry c = { 0 };
  435. if (iw->encryption(ifname, (char *)&c))
  436. return format_encryption(NULL);
  437. return format_encryption(&c);
  438. }
  439. static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
  440. {
  441. int modes;
  442. if (iw->hwmodelist(ifname, &modes))
  443. modes = -1;
  444. return format_hwmodes(modes);
  445. }
  446. static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
  447. {
  448. int supp;
  449. static char buf[4];
  450. if (iw->mbssid_support(ifname, &supp))
  451. snprintf(buf, sizeof(buf), "no");
  452. else
  453. snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
  454. return buf;
  455. }
  456. static char * print_phyname(const struct iwinfo_ops *iw, const char *ifname)
  457. {
  458. static char buf[32];
  459. if (!iw->phyname(ifname, buf))
  460. return buf;
  461. return "?";
  462. }
  463. static void print_info(const struct iwinfo_ops *iw, const char *ifname)
  464. {
  465. printf("%-9s ESSID: %s\n",
  466. ifname,
  467. print_ssid(iw, ifname));
  468. printf(" Access Point: %s\n",
  469. print_bssid(iw, ifname));
  470. printf(" Mode: %s Channel: %s (%s)\n",
  471. print_mode(iw, ifname),
  472. print_channel(iw, ifname),
  473. print_frequency(iw, ifname));
  474. if (iw->center_chan1 != NULL) {
  475. printf(" Center Channel 1: %s",
  476. print_center_chan1(iw, ifname));
  477. printf(" 2: %s\n", print_center_chan2(iw, ifname));
  478. }
  479. printf(" Tx-Power: %s Link Quality: %s/%s\n",
  480. print_txpower(iw, ifname),
  481. print_quality(iw, ifname),
  482. print_quality_max(iw, ifname));
  483. printf(" Signal: %s Noise: %s\n",
  484. print_signal(iw, ifname),
  485. print_noise(iw, ifname));
  486. printf(" Bit Rate: %s\n",
  487. print_rate(iw, ifname));
  488. printf(" Encryption: %s\n",
  489. print_encryption(iw, ifname));
  490. printf(" Type: %s HW Mode(s): %s\n",
  491. print_type(iw, ifname),
  492. print_hwmodes(iw, ifname));
  493. printf(" Hardware: %s [%s]\n",
  494. print_hardware_id(iw, ifname),
  495. print_hardware_name(iw, ifname));
  496. printf(" TX power offset: %s\n",
  497. print_txpower_offset(iw, ifname));
  498. printf(" Frequency offset: %s\n",
  499. print_frequency_offset(iw, ifname));
  500. printf(" Supports VAPs: %s PHY name: %s\n",
  501. print_mbssid_supp(iw, ifname),
  502. print_phyname(iw, ifname));
  503. }
  504. static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
  505. {
  506. int i, x, len;
  507. char buf[IWINFO_BUFSIZE];
  508. struct iwinfo_scanlist_entry *e;
  509. if (iw->scanlist(ifname, buf, &len))
  510. {
  511. printf("Scanning not possible\n\n");
  512. return;
  513. }
  514. else if (len <= 0)
  515. {
  516. printf("No scan results\n\n");
  517. return;
  518. }
  519. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
  520. {
  521. e = (struct iwinfo_scanlist_entry *) &buf[i];
  522. printf("Cell %02d - Address: %s\n",
  523. x,
  524. format_bssid(e->mac));
  525. printf(" ESSID: %s\n",
  526. format_ssid(e->ssid));
  527. printf(" Mode: %s Channel: %s\n",
  528. IWINFO_OPMODE_NAMES[e->mode],
  529. format_channel(e->channel));
  530. printf(" Signal: %s Quality: %s/%s\n",
  531. format_signal(e->signal - 0x100),
  532. format_quality(e->quality),
  533. format_quality_max(e->quality_max));
  534. printf(" Encryption: %s\n",
  535. format_encryption(&e->crypto));
  536. printf(" HT Operation:\n");
  537. printf(" Primary Channel: %d\n",
  538. e->ht_chan_info.primary_chan);
  539. printf(" Secondary Channel Offset: %s\n",
  540. ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
  541. printf(" Channel Width: %s\n",
  542. format_chan_width(e->ht_chan_info.chan_width));
  543. if (e->vht_chan_info.center_chan_1) {
  544. printf(" VHT Operation:\n");
  545. printf(" Channel Width: %s\n",
  546. format_chan_width(e->vht_chan_info.chan_width));
  547. printf(" Center Frequency 1: %d\n",
  548. e->vht_chan_info.center_chan_1);
  549. printf(" Center Frequency 2: %d\n",
  550. e->vht_chan_info.center_chan_2);
  551. }
  552. printf("\n");
  553. }
  554. }
  555. static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
  556. {
  557. int len, pwr, off, i;
  558. char buf[IWINFO_BUFSIZE];
  559. struct iwinfo_txpwrlist_entry *e;
  560. if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
  561. {
  562. printf("No TX power information available\n");
  563. return;
  564. }
  565. if (iw->txpower(ifname, &pwr))
  566. pwr = -1;
  567. if (iw->txpower_offset(ifname, &off))
  568. off = 0;
  569. for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
  570. {
  571. e = (struct iwinfo_txpwrlist_entry *) &buf[i];
  572. printf("%s%3d dBm (%4d mW)\n",
  573. (pwr == e->dbm) ? "*" : " ",
  574. e->dbm + off,
  575. iwinfo_dbm2mw(e->dbm + off));
  576. }
  577. }
  578. static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
  579. {
  580. int i, len, ch;
  581. char buf[IWINFO_BUFSIZE];
  582. struct iwinfo_freqlist_entry *e;
  583. if (iw->freqlist(ifname, buf, &len) || len <= 0)
  584. {
  585. printf("No frequency information available\n");
  586. return;
  587. }
  588. if (iw->channel(ifname, &ch))
  589. ch = -1;
  590. for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
  591. {
  592. e = (struct iwinfo_freqlist_entry *) &buf[i];
  593. printf("%s %s (Channel %s)%s\n",
  594. (ch == e->channel) ? "*" : " ",
  595. format_frequency(e->mhz),
  596. format_channel(e->channel),
  597. e->restricted ? " [restricted]" : "");
  598. }
  599. }
  600. static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
  601. {
  602. int i, len;
  603. char buf[IWINFO_BUFSIZE];
  604. struct iwinfo_assoclist_entry *e;
  605. if (iw->assoclist(ifname, buf, &len))
  606. {
  607. printf("No information available\n");
  608. return;
  609. }
  610. else if (len <= 0)
  611. {
  612. printf("No station connected\n");
  613. return;
  614. }
  615. for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
  616. {
  617. e = (struct iwinfo_assoclist_entry *) &buf[i];
  618. printf("%s %s / %s (SNR %d) %d ms ago\n",
  619. format_bssid(e->mac),
  620. format_signal(e->signal),
  621. format_noise(e->noise),
  622. (e->signal - e->noise),
  623. e->inactive);
  624. printf(" RX: %-38s %8d Pkts.\n",
  625. format_assocrate(&e->rx_rate),
  626. e->rx_packets
  627. );
  628. printf(" TX: %-38s %8d Pkts.\n",
  629. format_assocrate(&e->tx_rate),
  630. e->tx_packets
  631. );
  632. printf(" expected throughput: %s\n\n",
  633. format_rate(e->thr));
  634. }
  635. }
  636. static char * lookup_country(char *buf, int len, int iso3166)
  637. {
  638. int i;
  639. struct iwinfo_country_entry *c;
  640. for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
  641. {
  642. c = (struct iwinfo_country_entry *) &buf[i];
  643. if (c->iso3166 == iso3166)
  644. return c->ccode;
  645. }
  646. return NULL;
  647. }
  648. static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
  649. {
  650. int len;
  651. char buf[IWINFO_BUFSIZE];
  652. char *ccode;
  653. char curcode[3];
  654. const struct iwinfo_iso3166_label *l;
  655. if (iw->countrylist(ifname, buf, &len))
  656. {
  657. printf("No country code information available\n");
  658. return;
  659. }
  660. if (iw->country(ifname, curcode))
  661. memset(curcode, 0, sizeof(curcode));
  662. for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
  663. {
  664. if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
  665. {
  666. printf("%s %4s %c%c\n",
  667. strncmp(ccode, curcode, 2) ? " " : "*",
  668. ccode, (l->iso3166 / 256), (l->iso3166 % 256));
  669. }
  670. }
  671. }
  672. static void print_htmodelist(const struct iwinfo_ops *iw, const char *ifname)
  673. {
  674. int i, htmodes = 0;
  675. if (iw->htmodelist(ifname, &htmodes))
  676. {
  677. printf("No HT mode information available\n");
  678. return;
  679. }
  680. for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++)
  681. if (htmodes & (1 << i))
  682. printf("%s ", IWINFO_HTMODE_NAMES[i]);
  683. printf("\n");
  684. }
  685. static void lookup_phy(const struct iwinfo_ops *iw, const char *section)
  686. {
  687. char buf[IWINFO_BUFSIZE];
  688. if (!iw->lookup_phy)
  689. {
  690. fprintf(stderr, "Not supported\n");
  691. return;
  692. }
  693. if (iw->lookup_phy(section, buf))
  694. {
  695. fprintf(stderr, "Phy not found\n");
  696. return;
  697. }
  698. printf("%s\n", buf);
  699. }
  700. static void lookup_path(const struct iwinfo_ops *iw, const char *phy)
  701. {
  702. const char *path;
  703. if (!iw->phy_path || iw->phy_path(phy, &path) || !path)
  704. return;
  705. printf("%s\n", path);
  706. }
  707. int main(int argc, char **argv)
  708. {
  709. int i, rv = 0;
  710. char *p;
  711. const struct iwinfo_ops *iw;
  712. glob_t globbuf;
  713. if (argc > 1 && argc < 3)
  714. {
  715. fprintf(stderr,
  716. "Usage:\n"
  717. " iwinfo <device> info\n"
  718. " iwinfo <device> scan\n"
  719. " iwinfo <device> txpowerlist\n"
  720. " iwinfo <device> freqlist\n"
  721. " iwinfo <device> assoclist\n"
  722. " iwinfo <device> countrylist\n"
  723. " iwinfo <device> htmodelist\n"
  724. " iwinfo <backend> phyname <section>\n"
  725. );
  726. return 1;
  727. }
  728. if (argc == 1)
  729. {
  730. glob("/sys/class/net/*", 0, NULL, &globbuf);
  731. for (i = 0; i < globbuf.gl_pathc; i++)
  732. {
  733. p = strrchr(globbuf.gl_pathv[i], '/');
  734. if (!p)
  735. continue;
  736. iw = iwinfo_backend(++p);
  737. if (!iw)
  738. continue;
  739. print_info(iw, p);
  740. printf("\n");
  741. }
  742. globfree(&globbuf);
  743. return 0;
  744. }
  745. if (argc > 3)
  746. {
  747. iw = iwinfo_backend_by_name(argv[1]);
  748. if (!iw)
  749. {
  750. fprintf(stderr, "No such wireless backend: %s\n", argv[1]);
  751. rv = 1;
  752. }
  753. else
  754. {
  755. if (!strcmp(argv[2], "path")) {
  756. lookup_path(iw, argv[3]);
  757. return 0;
  758. }
  759. switch (argv[2][0])
  760. {
  761. case 'p':
  762. lookup_phy(iw, argv[3]);
  763. break;
  764. default:
  765. fprintf(stderr, "Unknown command: %s\n", argv[2]);
  766. rv = 1;
  767. }
  768. }
  769. }
  770. else
  771. {
  772. iw = iwinfo_backend(argv[1]);
  773. if (!iw)
  774. {
  775. fprintf(stderr, "No such wireless device: %s\n", argv[1]);
  776. rv = 1;
  777. }
  778. else
  779. {
  780. for (i = 2; i < argc; i++)
  781. {
  782. switch(argv[i][0])
  783. {
  784. case 'i':
  785. print_info(iw, argv[1]);
  786. break;
  787. case 's':
  788. print_scanlist(iw, argv[1]);
  789. break;
  790. case 't':
  791. print_txpwrlist(iw, argv[1]);
  792. break;
  793. case 'f':
  794. print_freqlist(iw, argv[1]);
  795. break;
  796. case 'a':
  797. print_assoclist(iw, argv[1]);
  798. break;
  799. case 'c':
  800. print_countrylist(iw, argv[1]);
  801. break;
  802. case 'h':
  803. print_htmodelist(iw, argv[1]);
  804. break;
  805. default:
  806. fprintf(stderr, "Unknown command: %s\n", argv[i]);
  807. rv = 1;
  808. }
  809. }
  810. }
  811. }
  812. iwinfo_finish();
  813. return rv;
  814. }