iwinfo_cli.c 21 KB

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