Mirror of the official iwinfo project repository by OpenWrt https://git.openwrt.org/project/iwinfo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

930 lines
18 KiB

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