2
0

iwinfo_cli.c 21 KB

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