iwinfo_cli.c 18 KB

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