iwinfo_lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * iwinfo - Wireless Information Library - Lua Bindings
  3. *
  4. * Copyright (C) 2009-2013 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 "iwinfo.h"
  19. /*
  20. * name constants
  21. */
  22. const char * const IWINFO_80211_NAMES[IWINFO_80211_COUNT] = {
  23. "a",
  24. "b",
  25. "g",
  26. "n",
  27. "ac",
  28. "ad",
  29. "ax",
  30. };
  31. const char * const IWINFO_BAND_NAMES[IWINFO_BAND_COUNT] = {
  32. "2.4 GHz",
  33. "5 GHz",
  34. "6 GHz",
  35. "60 GHz",
  36. };
  37. const char * const IWINFO_CIPHER_NAMES[IWINFO_CIPHER_COUNT] = {
  38. "NONE",
  39. "WEP-40",
  40. "TKIP",
  41. "WRAP",
  42. "CCMP",
  43. "WEP-104",
  44. "AES-OCB",
  45. "CKIP",
  46. "GCMP",
  47. "CCMP-256",
  48. "GCMP-256",
  49. };
  50. const char * const IWINFO_KMGMT_NAMES[IWINFO_KMGMT_COUNT] = {
  51. "NONE",
  52. "802.1X",
  53. "PSK",
  54. "SAE",
  55. "OWE",
  56. };
  57. const char * const IWINFO_AUTH_NAMES[IWINFO_AUTH_COUNT] = {
  58. "OPEN",
  59. "SHARED",
  60. };
  61. const char * const IWINFO_OPMODE_NAMES[IWINFO_OPMODE_COUNT] = {
  62. "Unknown",
  63. "Master",
  64. "Ad-Hoc",
  65. "Client",
  66. "Monitor",
  67. "Master (VLAN)",
  68. "WDS",
  69. "Mesh Point",
  70. "P2P Client",
  71. "P2P Go",
  72. };
  73. const char * const IWINFO_HTMODE_NAMES[IWINFO_HTMODE_COUNT] = {
  74. "HT20",
  75. "HT40",
  76. "VHT20",
  77. "VHT40",
  78. "VHT80",
  79. "VHT80+80",
  80. "VHT160",
  81. "NOHT",
  82. "HE20",
  83. "HE40",
  84. "HE80",
  85. "HE80+80",
  86. "HE160"
  87. };
  88. const char * const IWINFO_FREQ_FLAG_NAMES[IWINFO_FREQ_FLAG_COUNT] = {
  89. "NO_10MHZ",
  90. "NO_20MHZ",
  91. "NO_HT40+",
  92. "NO_HT40-",
  93. "NO_80MHZ",
  94. "NO_160MHZ",
  95. "NO_HE",
  96. "NO_IR",
  97. "INDOOR_ONLY",
  98. };
  99. const char * const ht_secondary_offset[4] = {
  100. "no secondary",
  101. "above",
  102. "[reserved!]",
  103. "below",
  104. };
  105. const uint16_t ht_chan_width[2] = {
  106. 20,
  107. 2040,
  108. };
  109. const uint16_t vht_chan_width[4] = {
  110. 40, /* 40 MHz or lower (refer to ht to a more precise width) */
  111. 80, /* 80 MHz */
  112. 160, /* 160 MHz */
  113. 8080, /* 80+80 MHz */
  114. };
  115. /*
  116. * ISO3166 country labels
  117. */
  118. const struct iwinfo_iso3166_label IWINFO_ISO3166_NAMES[] = {
  119. { 0x3030 /* 00 */, "World" },
  120. { 0x4144 /* AD */, "Andorra" },
  121. { 0x4145 /* AE */, "United Arab Emirates" },
  122. { 0x4146 /* AF */, "Afghanistan" },
  123. { 0x4147 /* AG */, "Antigua and Barbuda" },
  124. { 0x4149 /* AI */, "Anguilla" },
  125. { 0x414C /* AL */, "Albania" },
  126. { 0x414D /* AM */, "Armenia" },
  127. { 0x414E /* AN */, "Netherlands Antilles" },
  128. { 0x414F /* AO */, "Angola" },
  129. { 0x4151 /* AQ */, "Antarctica" },
  130. { 0x4152 /* AR */, "Argentina" },
  131. { 0x4153 /* AS */, "American Samoa" },
  132. { 0x4154 /* AT */, "Austria" },
  133. { 0x4155 /* AU */, "Australia" },
  134. { 0x4157 /* AW */, "Aruba" },
  135. { 0x4158 /* AX */, "Aland Islands" },
  136. { 0x415A /* AZ */, "Azerbaijan" },
  137. { 0x4241 /* BA */, "Bosnia and Herzegovina" },
  138. { 0x4242 /* BB */, "Barbados" },
  139. { 0x4244 /* BD */, "Bangladesh" },
  140. { 0x4245 /* BE */, "Belgium" },
  141. { 0x4246 /* BF */, "Burkina Faso" },
  142. { 0x4247 /* BG */, "Bulgaria" },
  143. { 0x4248 /* BH */, "Bahrain" },
  144. { 0x4249 /* BI */, "Burundi" },
  145. { 0x424A /* BJ */, "Benin" },
  146. { 0x424C /* BL */, "Saint Barthelemy" },
  147. { 0x424D /* BM */, "Bermuda" },
  148. { 0x424E /* BN */, "Brunei Darussalam" },
  149. { 0x424F /* BO */, "Bolivia" },
  150. { 0x4252 /* BR */, "Brazil" },
  151. { 0x4253 /* BS */, "Bahamas" },
  152. { 0x4254 /* BT */, "Bhutan" },
  153. { 0x4256 /* BV */, "Bouvet Island" },
  154. { 0x4257 /* BW */, "Botswana" },
  155. { 0x4259 /* BY */, "Belarus" },
  156. { 0x425A /* BZ */, "Belize" },
  157. { 0x4341 /* CA */, "Canada" },
  158. { 0x4343 /* CC */, "Cocos (Keeling) Islands" },
  159. { 0x4344 /* CD */, "Congo" },
  160. { 0x4346 /* CF */, "Central African Republic" },
  161. { 0x4347 /* CG */, "Congo" },
  162. { 0x4348 /* CH */, "Switzerland" },
  163. { 0x4349 /* CI */, "Cote d'Ivoire" },
  164. { 0x434B /* CK */, "Cook Islands" },
  165. { 0x434C /* CL */, "Chile" },
  166. { 0x434D /* CM */, "Cameroon" },
  167. { 0x434E /* CN */, "China" },
  168. { 0x434F /* CO */, "Colombia" },
  169. { 0x4352 /* CR */, "Costa Rica" },
  170. { 0x4355 /* CU */, "Cuba" },
  171. { 0x4356 /* CV */, "Cape Verde" },
  172. { 0x4358 /* CX */, "Christmas Island" },
  173. { 0x4359 /* CY */, "Cyprus" },
  174. { 0x435A /* CZ */, "Czech Republic" },
  175. { 0x4445 /* DE */, "Germany" },
  176. { 0x444A /* DJ */, "Djibouti" },
  177. { 0x444B /* DK */, "Denmark" },
  178. { 0x444D /* DM */, "Dominica" },
  179. { 0x444F /* DO */, "Dominican Republic" },
  180. { 0x445A /* DZ */, "Algeria" },
  181. { 0x4543 /* EC */, "Ecuador" },
  182. { 0x4545 /* EE */, "Estonia" },
  183. { 0x4547 /* EG */, "Egypt" },
  184. { 0x4548 /* EH */, "Western Sahara" },
  185. { 0x4552 /* ER */, "Eritrea" },
  186. { 0x4553 /* ES */, "Spain" },
  187. { 0x4554 /* ET */, "Ethiopia" },
  188. { 0x4649 /* FI */, "Finland" },
  189. { 0x464A /* FJ */, "Fiji" },
  190. { 0x464B /* FK */, "Falkland Islands" },
  191. { 0x464D /* FM */, "Micronesia" },
  192. { 0x464F /* FO */, "Faroe Islands" },
  193. { 0x4652 /* FR */, "France" },
  194. { 0x4741 /* GA */, "Gabon" },
  195. { 0x4742 /* GB */, "United Kingdom" },
  196. { 0x4744 /* GD */, "Grenada" },
  197. { 0x4745 /* GE */, "Georgia" },
  198. { 0x4746 /* GF */, "French Guiana" },
  199. { 0x4747 /* GG */, "Guernsey" },
  200. { 0x4748 /* GH */, "Ghana" },
  201. { 0x4749 /* GI */, "Gibraltar" },
  202. { 0x474C /* GL */, "Greenland" },
  203. { 0x474D /* GM */, "Gambia" },
  204. { 0x474E /* GN */, "Guinea" },
  205. { 0x4750 /* GP */, "Guadeloupe" },
  206. { 0x4751 /* GQ */, "Equatorial Guinea" },
  207. { 0x4752 /* GR */, "Greece" },
  208. { 0x4753 /* GS */, "South Georgia" },
  209. { 0x4754 /* GT */, "Guatemala" },
  210. { 0x4755 /* GU */, "Guam" },
  211. { 0x4757 /* GW */, "Guinea-Bissau" },
  212. { 0x4759 /* GY */, "Guyana" },
  213. { 0x484B /* HK */, "Hong Kong" },
  214. { 0x484D /* HM */, "Heard and McDonald Islands" },
  215. { 0x484E /* HN */, "Honduras" },
  216. { 0x4852 /* HR */, "Croatia" },
  217. { 0x4854 /* HT */, "Haiti" },
  218. { 0x4855 /* HU */, "Hungary" },
  219. { 0x4944 /* ID */, "Indonesia" },
  220. { 0x4945 /* IE */, "Ireland" },
  221. { 0x494C /* IL */, "Israel" },
  222. { 0x494D /* IM */, "Isle of Man" },
  223. { 0x494E /* IN */, "India" },
  224. { 0x494F /* IO */, "Chagos Islands" },
  225. { 0x4951 /* IQ */, "Iraq" },
  226. { 0x4952 /* IR */, "Iran" },
  227. { 0x4953 /* IS */, "Iceland" },
  228. { 0x4954 /* IT */, "Italy" },
  229. { 0x4A45 /* JE */, "Jersey" },
  230. { 0x4A4D /* JM */, "Jamaica" },
  231. { 0x4A4F /* JO */, "Jordan" },
  232. { 0x4A50 /* JP */, "Japan" },
  233. { 0x4B45 /* KE */, "Kenya" },
  234. { 0x4B47 /* KG */, "Kyrgyzstan" },
  235. { 0x4B48 /* KH */, "Cambodia" },
  236. { 0x4B49 /* KI */, "Kiribati" },
  237. { 0x4B4D /* KM */, "Comoros" },
  238. { 0x4B4E /* KN */, "Saint Kitts and Nevis" },
  239. { 0x4B50 /* KP */, "North Korea" },
  240. { 0x4B52 /* KR */, "South Korea" },
  241. { 0x4B57 /* KW */, "Kuwait" },
  242. { 0x4B59 /* KY */, "Cayman Islands" },
  243. { 0x4B5A /* KZ */, "Kazakhstan" },
  244. { 0x4C41 /* LA */, "Laos" },
  245. { 0x4C42 /* LB */, "Lebanon" },
  246. { 0x4C43 /* LC */, "Saint Lucia" },
  247. { 0x4C49 /* LI */, "Liechtenstein" },
  248. { 0x4C4B /* LK */, "Sri Lanka" },
  249. { 0x4C52 /* LR */, "Liberia" },
  250. { 0x4C53 /* LS */, "Lesotho" },
  251. { 0x4C54 /* LT */, "Lithuania" },
  252. { 0x4C55 /* LU */, "Luxembourg" },
  253. { 0x4C56 /* LV */, "Latvia" },
  254. { 0x4C59 /* LY */, "Libyan Arab Jamahiriya" },
  255. { 0x4D41 /* MA */, "Morocco" },
  256. { 0x4D43 /* MC */, "Monaco" },
  257. { 0x4D44 /* MD */, "Moldova" },
  258. { 0x4D45 /* ME */, "Montenegro" },
  259. { 0x4D46 /* MF */, "Saint Martin (French part)" },
  260. { 0x4D47 /* MG */, "Madagascar" },
  261. { 0x4D48 /* MH */, "Marshall Islands" },
  262. { 0x4D4B /* MK */, "Macedonia" },
  263. { 0x4D4C /* ML */, "Mali" },
  264. { 0x4D4D /* MM */, "Myanmar" },
  265. { 0x4D4E /* MN */, "Mongolia" },
  266. { 0x4D4F /* MO */, "Macao" },
  267. { 0x4D50 /* MP */, "Northern Mariana Islands" },
  268. { 0x4D51 /* MQ */, "Martinique" },
  269. { 0x4D52 /* MR */, "Mauritania" },
  270. { 0x4D53 /* MS */, "Montserrat" },
  271. { 0x4D54 /* MT */, "Malta" },
  272. { 0x4D55 /* MU */, "Mauritius" },
  273. { 0x4D56 /* MV */, "Maldives" },
  274. { 0x4D57 /* MW */, "Malawi" },
  275. { 0x4D58 /* MX */, "Mexico" },
  276. { 0x4D59 /* MY */, "Malaysia" },
  277. { 0x4D5A /* MZ */, "Mozambique" },
  278. { 0x4E41 /* NA */, "Namibia" },
  279. { 0x4E43 /* NC */, "New Caledonia" },
  280. { 0x4E45 /* NE */, "Niger" },
  281. { 0x4E46 /* NF */, "Norfolk Island" },
  282. { 0x4E47 /* NG */, "Nigeria" },
  283. { 0x4E49 /* NI */, "Nicaragua" },
  284. { 0x4E4C /* NL */, "Netherlands" },
  285. { 0x4E4F /* NO */, "Norway" },
  286. { 0x4E50 /* NP */, "Nepal" },
  287. { 0x4E52 /* NR */, "Nauru" },
  288. { 0x4E55 /* NU */, "Niue" },
  289. { 0x4E5A /* NZ */, "New Zealand" },
  290. { 0x4F4D /* OM */, "Oman" },
  291. { 0x5041 /* PA */, "Panama" },
  292. { 0x5045 /* PE */, "Peru" },
  293. { 0x5046 /* PF */, "French Polynesia" },
  294. { 0x5047 /* PG */, "Papua New Guinea" },
  295. { 0x5048 /* PH */, "Philippines" },
  296. { 0x504B /* PK */, "Pakistan" },
  297. { 0x504C /* PL */, "Poland" },
  298. { 0x504D /* PM */, "Saint Pierre and Miquelon" },
  299. { 0x504E /* PN */, "Pitcairn" },
  300. { 0x5052 /* PR */, "Puerto Rico" },
  301. { 0x5053 /* PS */, "Palestinian Territory" },
  302. { 0x5054 /* PT */, "Portugal" },
  303. { 0x5057 /* PW */, "Palau" },
  304. { 0x5059 /* PY */, "Paraguay" },
  305. { 0x5141 /* QA */, "Qatar" },
  306. { 0x5245 /* RE */, "Reunion" },
  307. { 0x524F /* RO */, "Romania" },
  308. { 0x5253 /* RS */, "Serbia" },
  309. { 0x5255 /* RU */, "Russian Federation" },
  310. { 0x5257 /* RW */, "Rwanda" },
  311. { 0x5341 /* SA */, "Saudi Arabia" },
  312. { 0x5342 /* SB */, "Solomon Islands" },
  313. { 0x5343 /* SC */, "Seychelles" },
  314. { 0x5344 /* SD */, "Sudan" },
  315. { 0x5345 /* SE */, "Sweden" },
  316. { 0x5347 /* SG */, "Singapore" },
  317. { 0x5348 /* SH */, "St. Helena and Dependencies" },
  318. { 0x5349 /* SI */, "Slovenia" },
  319. { 0x534A /* SJ */, "Svalbard and Jan Mayen" },
  320. { 0x534B /* SK */, "Slovakia" },
  321. { 0x534C /* SL */, "Sierra Leone" },
  322. { 0x534D /* SM */, "San Marino" },
  323. { 0x534E /* SN */, "Senegal" },
  324. { 0x534F /* SO */, "Somalia" },
  325. { 0x5352 /* SR */, "Suriname" },
  326. { 0x5354 /* ST */, "Sao Tome and Principe" },
  327. { 0x5356 /* SV */, "El Salvador" },
  328. { 0x5359 /* SY */, "Syrian Arab Republic" },
  329. { 0x535A /* SZ */, "Swaziland" },
  330. { 0x5443 /* TC */, "Turks and Caicos Islands" },
  331. { 0x5444 /* TD */, "Chad" },
  332. { 0x5446 /* TF */, "French Southern Territories" },
  333. { 0x5447 /* TG */, "Togo" },
  334. { 0x5448 /* TH */, "Thailand" },
  335. { 0x544A /* TJ */, "Tajikistan" },
  336. { 0x544B /* TK */, "Tokelau" },
  337. { 0x544C /* TL */, "Timor-Leste" },
  338. { 0x544D /* TM */, "Turkmenistan" },
  339. { 0x544E /* TN */, "Tunisia" },
  340. { 0x544F /* TO */, "Tonga" },
  341. { 0x5452 /* TR */, "Turkey" },
  342. { 0x5454 /* TT */, "Trinidad and Tobago" },
  343. { 0x5456 /* TV */, "Tuvalu" },
  344. { 0x5457 /* TW */, "Taiwan" },
  345. { 0x545A /* TZ */, "Tanzania" },
  346. { 0x5541 /* UA */, "Ukraine" },
  347. { 0x5547 /* UG */, "Uganda" },
  348. { 0x554D /* UM */, "U.S. Minor Outlying Islands" },
  349. { 0x5553 /* US */, "United States" },
  350. { 0x5559 /* UY */, "Uruguay" },
  351. { 0x555A /* UZ */, "Uzbekistan" },
  352. { 0x5641 /* VA */, "Vatican City State" },
  353. { 0x5643 /* VC */, "St. Vincent and Grenadines" },
  354. { 0x5645 /* VE */, "Venezuela" },
  355. { 0x5647 /* VG */, "Virgin Islands, British" },
  356. { 0x5649 /* VI */, "Virgin Islands, U.S." },
  357. { 0x564E /* VN */, "Viet Nam" },
  358. { 0x5655 /* VU */, "Vanuatu" },
  359. { 0x5746 /* WF */, "Wallis and Futuna" },
  360. { 0x5753 /* WS */, "Samoa" },
  361. { 0x5945 /* YE */, "Yemen" },
  362. { 0x5954 /* YT */, "Mayotte" },
  363. { 0x5A41 /* ZA */, "South Africa" },
  364. { 0x5A4D /* ZM */, "Zambia" },
  365. { 0x5A57 /* ZW */, "Zimbabwe" },
  366. { 0, "" }
  367. };
  368. static const struct iwinfo_ops *backends[] = {
  369. #ifdef USE_NL80211
  370. &nl80211_ops,
  371. #endif
  372. #ifdef USE_MADWIFI
  373. &madwifi_ops,
  374. #endif
  375. #ifdef USE_WL
  376. &wl_ops,
  377. #endif
  378. #ifdef USE_WEXT
  379. &wext_ops,
  380. #endif
  381. };
  382. const char * iwinfo_type(const char *ifname)
  383. {
  384. const struct iwinfo_ops *ops = iwinfo_backend(ifname);
  385. if (!ops)
  386. return NULL;
  387. return ops->name;
  388. }
  389. const struct iwinfo_ops * iwinfo_backend(const char *ifname)
  390. {
  391. int i;
  392. for (i = 0; i < ARRAY_SIZE(backends); i++)
  393. if (backends[i]->probe(ifname))
  394. return backends[i];
  395. return NULL;
  396. }
  397. const struct iwinfo_ops * iwinfo_backend_by_name(const char *name)
  398. {
  399. int i;
  400. for (i = 0; i < ARRAY_SIZE(backends); i++)
  401. if (!strcmp(backends[i]->name, name))
  402. return backends[i];
  403. return NULL;
  404. }
  405. void iwinfo_finish(void)
  406. {
  407. int i;
  408. for (i = 0; i < ARRAY_SIZE(backends); i++)
  409. backends[i]->close();
  410. iwinfo_close();
  411. }