iwinfo_cli.c 20 KB

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