iwinfo_cli.c 20 KB

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