iwinfo_lua.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * iwinfo - Wireless Information Library - Lua Bindings
  3. *
  4. * Copyright (C) 2009 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/lua.h"
  19. /* Determine type */
  20. static int iwinfo_L_type(lua_State *L)
  21. {
  22. const char *ifname = luaL_checkstring(L, 1);
  23. const char *type = iwinfo_type(ifname);
  24. if (type)
  25. lua_pushstring(L, type);
  26. else
  27. lua_pushnil(L);
  28. return 1;
  29. }
  30. /* Shutdown backends */
  31. static int iwinfo_L__gc(lua_State *L)
  32. {
  33. iwinfo_finish();
  34. return 0;
  35. }
  36. /*
  37. * Build a short textual description of the crypto info
  38. */
  39. static char * iwinfo_crypto_print_ciphers(int ciphers)
  40. {
  41. static char str[128] = { 0 };
  42. char *pos = str;
  43. if (ciphers & IWINFO_CIPHER_WEP40)
  44. pos += sprintf(pos, "WEP-40, ");
  45. if (ciphers & IWINFO_CIPHER_WEP104)
  46. pos += sprintf(pos, "WEP-104, ");
  47. if (ciphers & IWINFO_CIPHER_TKIP)
  48. pos += sprintf(pos, "TKIP, ");
  49. if (ciphers & IWINFO_CIPHER_CCMP)
  50. pos += sprintf(pos, "CCMP, ");
  51. if (ciphers & IWINFO_CIPHER_CCMP256)
  52. pos += sprintf(pos, "CCMP-256, ");
  53. if (ciphers & IWINFO_CIPHER_GCMP)
  54. pos += sprintf(pos, "GCMP, ");
  55. if (ciphers & IWINFO_CIPHER_GCMP256)
  56. pos += sprintf(pos, "GCMP-256, ");
  57. if (ciphers & IWINFO_CIPHER_WRAP)
  58. pos += sprintf(pos, "WRAP, ");
  59. if (ciphers & IWINFO_CIPHER_AESOCB)
  60. pos += sprintf(pos, "AES-OCB, ");
  61. if (ciphers & IWINFO_CIPHER_CKIP)
  62. pos += sprintf(pos, "CKIP, ");
  63. if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
  64. pos += sprintf(pos, "NONE, ");
  65. *(pos - 2) = 0;
  66. return str;
  67. }
  68. static char * iwinfo_crypto_print_suites(int suites)
  69. {
  70. static char str[64] = { 0 };
  71. char *pos = str;
  72. if (suites & IWINFO_KMGMT_PSK)
  73. pos += sprintf(pos, "PSK/");
  74. if (suites & IWINFO_KMGMT_8021x)
  75. pos += sprintf(pos, "802.1X/");
  76. if (suites & IWINFO_KMGMT_SAE)
  77. pos += sprintf(pos, "SAE/");
  78. if (suites & IWINFO_KMGMT_OWE)
  79. pos += sprintf(pos, "OWE/");
  80. if (!suites || (suites & IWINFO_KMGMT_NONE))
  81. pos += sprintf(pos, "NONE/");
  82. *(pos - 1) = 0;
  83. return str;
  84. }
  85. static char * iwinfo_crypto_desc(struct iwinfo_crypto_entry *c)
  86. {
  87. static char desc[512] = { 0 };
  88. char *pos = desc;
  89. int i, n;
  90. if (c)
  91. {
  92. if (c->enabled)
  93. {
  94. /* WEP */
  95. if (c->auth_algs && !c->wpa_version)
  96. {
  97. if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
  98. (c->auth_algs & IWINFO_AUTH_SHARED))
  99. {
  100. sprintf(desc, "WEP Open/Shared (%s)",
  101. iwinfo_crypto_print_ciphers(c->pair_ciphers));
  102. }
  103. else if (c->auth_algs & IWINFO_AUTH_OPEN)
  104. {
  105. sprintf(desc, "WEP Open System (%s)",
  106. iwinfo_crypto_print_ciphers(c->pair_ciphers));
  107. }
  108. else if (c->auth_algs & IWINFO_AUTH_SHARED)
  109. {
  110. sprintf(desc, "WEP Shared Auth (%s)",
  111. iwinfo_crypto_print_ciphers(c->pair_ciphers));
  112. }
  113. }
  114. /* WPA */
  115. else if (c->wpa_version)
  116. {
  117. for (i = 0, n = 0; i < 3; i++)
  118. if (c->wpa_version & (1 << i))
  119. n++;
  120. if (n > 1)
  121. pos += sprintf(pos, "mixed ");
  122. for (i = 0; i < 3; i++)
  123. if (c->wpa_version & (1 << i))
  124. {
  125. if (i)
  126. pos += sprintf(pos, "WPA%d/", i + 1);
  127. else
  128. pos += sprintf(pos, "WPA/");
  129. }
  130. pos--;
  131. sprintf(pos, " %s (%s)",
  132. iwinfo_crypto_print_suites(c->auth_suites),
  133. iwinfo_crypto_print_ciphers(
  134. c->pair_ciphers | c->group_ciphers));
  135. }
  136. else
  137. {
  138. sprintf(desc, "None");
  139. }
  140. }
  141. else
  142. {
  143. sprintf(desc, "None");
  144. }
  145. }
  146. else
  147. {
  148. sprintf(desc, "Unknown");
  149. }
  150. return desc;
  151. }
  152. /* Build Lua table from crypto data */
  153. static void iwinfo_L_cryptotable(lua_State *L, struct iwinfo_crypto_entry *c)
  154. {
  155. int i, j;
  156. lua_newtable(L);
  157. lua_pushboolean(L, c->enabled);
  158. lua_setfield(L, -2, "enabled");
  159. lua_pushstring(L, iwinfo_crypto_desc(c));
  160. lua_setfield(L, -2, "description");
  161. lua_pushboolean(L, (c->enabled && !c->wpa_version));
  162. lua_setfield(L, -2, "wep");
  163. lua_pushinteger(L, c->wpa_version);
  164. lua_setfield(L, -2, "wpa");
  165. lua_newtable(L);
  166. for (i = 0, j = 1; i < ARRAY_SIZE(IWINFO_CIPHER_NAMES); i++)
  167. {
  168. if (c->pair_ciphers & (1 << i))
  169. {
  170. lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
  171. lua_rawseti(L, -2, j++);
  172. }
  173. }
  174. lua_setfield(L, -2, "pair_ciphers");
  175. lua_newtable(L);
  176. for (i = 0, j = 1; i < ARRAY_SIZE(IWINFO_CIPHER_NAMES); i++)
  177. {
  178. if (c->group_ciphers & (1 << i))
  179. {
  180. lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
  181. lua_rawseti(L, -2, j++);
  182. }
  183. }
  184. lua_setfield(L, -2, "group_ciphers");
  185. lua_newtable(L);
  186. for (i = 0, j = 1; i < ARRAY_SIZE(IWINFO_KMGMT_NAMES); i++)
  187. {
  188. if (c->auth_suites & (1 << i))
  189. {
  190. lua_pushstring(L, IWINFO_KMGMT_NAMES[i]);
  191. lua_rawseti(L, -2, j++);
  192. }
  193. }
  194. lua_setfield(L, -2, "auth_suites");
  195. lua_newtable(L);
  196. for (i = 0, j = 1; i < ARRAY_SIZE(IWINFO_AUTH_NAMES); i++)
  197. {
  198. if (c->auth_algs & (1 << i))
  199. {
  200. lua_pushstring(L, IWINFO_AUTH_NAMES[i]);
  201. lua_rawseti(L, -2, j++);
  202. }
  203. }
  204. lua_setfield(L, -2, "auth_algs");
  205. }
  206. /* Wrapper for mode */
  207. static int iwinfo_L_mode(lua_State *L, int (*func)(const char *, int *))
  208. {
  209. int mode;
  210. const char *ifname = luaL_checkstring(L, 1);
  211. if ((*func)(ifname, &mode))
  212. mode = IWINFO_OPMODE_UNKNOWN;
  213. lua_pushstring(L, IWINFO_OPMODE_NAMES[mode]);
  214. return 1;
  215. }
  216. static void set_rateinfo(lua_State *L, struct iwinfo_rate_entry *r, bool rx)
  217. {
  218. lua_pushnumber(L, r->rate);
  219. lua_setfield(L, -2, rx ? "rx_rate" : "tx_rate");
  220. lua_pushboolean(L, r->is_ht);
  221. lua_setfield(L, -2, rx ? "rx_ht" : "tx_ht");
  222. lua_pushboolean(L, r->is_vht);
  223. lua_setfield(L, -2, rx ? "rx_vht" : "tx_vht");
  224. lua_pushboolean(L, r->is_he);
  225. lua_setfield(L, -2, rx ? "rx_he" : "tx_he");
  226. lua_pushboolean(L, r->is_eht);
  227. lua_setfield(L, -2, rx ? "rx_eht" : "tx_eht");
  228. lua_pushnumber(L, r->mhz_hi * 256 + r->mhz);
  229. lua_setfield(L, -2, rx ? "rx_mhz" : "tx_mhz");
  230. if (r->is_ht)
  231. {
  232. lua_pushboolean(L, r->is_40mhz);
  233. lua_setfield(L, -2, rx ? "rx_40mhz" : "tx_40mhz");
  234. lua_pushnumber(L, r->mcs);
  235. lua_setfield(L, -2, rx ? "rx_mcs" : "tx_mcs");
  236. lua_pushboolean(L, r->is_short_gi);
  237. lua_setfield(L, -2, rx ? "rx_short_gi" : "tx_short_gi");
  238. }
  239. else if (r->is_vht || r->is_he | r->is_eht)
  240. {
  241. lua_pushnumber(L, r->mcs);
  242. lua_setfield(L, -2, rx ? "rx_mcs" : "tx_mcs");
  243. lua_pushnumber(L, r->nss);
  244. lua_setfield(L, -2, rx ? "rx_nss" : "tx_nss");
  245. if (r->is_he) {
  246. lua_pushnumber(L, r->he_gi);
  247. lua_setfield(L, -2, rx ? "rx_he_gi" : "tx_he_gi");
  248. lua_pushnumber(L, r->he_dcm);
  249. lua_setfield(L, -2, rx ? "rx_he_dcm" : "tx_he_dcm");
  250. }
  251. if (r->is_eht) {
  252. lua_pushnumber(L, r->eht_gi);
  253. lua_setfield(L, -2, rx ? "rx_eht_gi" : "tx_eht_gi");
  254. }
  255. if (r->is_vht) {
  256. lua_pushboolean(L, r->is_short_gi);
  257. lua_setfield(L, -2, rx ? "rx_short_gi" : "tx_short_gi");
  258. }
  259. }
  260. }
  261. /* Wrapper for assoclist */
  262. static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
  263. {
  264. int i, len;
  265. char rv[IWINFO_BUFSIZE];
  266. char macstr[18];
  267. const char *ifname = luaL_checkstring(L, 1);
  268. struct iwinfo_assoclist_entry *e;
  269. lua_newtable(L);
  270. memset(rv, 0, sizeof(rv));
  271. if (!(*func)(ifname, rv, &len))
  272. {
  273. for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
  274. {
  275. e = (struct iwinfo_assoclist_entry *) &rv[i];
  276. sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
  277. e->mac[0], e->mac[1], e->mac[2],
  278. e->mac[3], e->mac[4], e->mac[5]);
  279. lua_newtable(L);
  280. lua_pushnumber(L, e->signal);
  281. lua_setfield(L, -2, "signal");
  282. lua_pushnumber(L, e->noise);
  283. lua_setfield(L, -2, "noise");
  284. lua_pushnumber(L, e->inactive);
  285. lua_setfield(L, -2, "inactive");
  286. lua_pushnumber(L, e->rx_packets);
  287. lua_setfield(L, -2, "rx_packets");
  288. lua_pushnumber(L, e->tx_packets);
  289. lua_setfield(L, -2, "tx_packets");
  290. set_rateinfo(L, &e->rx_rate, true);
  291. set_rateinfo(L, &e->tx_rate, false);
  292. if (e->thr) {
  293. lua_pushnumber(L, e->thr);
  294. lua_setfield(L, -2, "expected_throughput");
  295. }
  296. lua_setfield(L, -2, macstr);
  297. }
  298. }
  299. return 1;
  300. }
  301. /* Wrapper for tx power list */
  302. static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
  303. {
  304. int i, x, len;
  305. char rv[IWINFO_BUFSIZE];
  306. const char *ifname = luaL_checkstring(L, 1);
  307. struct iwinfo_txpwrlist_entry *e;
  308. memset(rv, 0, sizeof(rv));
  309. if (!(*func)(ifname, rv, &len))
  310. {
  311. lua_newtable(L);
  312. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++)
  313. {
  314. e = (struct iwinfo_txpwrlist_entry *) &rv[i];
  315. lua_newtable(L);
  316. lua_pushnumber(L, e->mw);
  317. lua_setfield(L, -2, "mw");
  318. lua_pushnumber(L, e->dbm);
  319. lua_setfield(L, -2, "dbm");
  320. lua_rawseti(L, -2, x);
  321. }
  322. return 1;
  323. }
  324. return 0;
  325. }
  326. /* Wrapper for scan list */
  327. static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
  328. {
  329. int i, x, len = 0;
  330. char rv[IWINFO_BUFSIZE];
  331. char macstr[18];
  332. const char *ifname = luaL_checkstring(L, 1);
  333. struct iwinfo_scanlist_entry *e;
  334. lua_newtable(L);
  335. memset(rv, 0, sizeof(rv));
  336. if (!(*func)(ifname, rv, &len))
  337. {
  338. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
  339. {
  340. e = (struct iwinfo_scanlist_entry *) &rv[i];
  341. lua_newtable(L);
  342. /* BSSID */
  343. sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
  344. e->mac[0], e->mac[1], e->mac[2],
  345. e->mac[3], e->mac[4], e->mac[5]);
  346. lua_pushstring(L, macstr);
  347. lua_setfield(L, -2, "bssid");
  348. /* ESSID */
  349. if (e->ssid[0])
  350. {
  351. lua_pushstring(L, (char *) e->ssid);
  352. lua_setfield(L, -2, "ssid");
  353. }
  354. /* Channel */
  355. lua_pushinteger(L, e->channel);
  356. lua_setfield(L, -2, "channel");
  357. /* Mode */
  358. lua_pushstring(L, IWINFO_OPMODE_NAMES[e->mode]);
  359. lua_setfield(L, -2, "mode");
  360. /* Quality, Signal */
  361. lua_pushinteger(L, e->quality);
  362. lua_setfield(L, -2, "quality");
  363. lua_pushinteger(L, e->quality_max);
  364. lua_setfield(L, -2, "quality_max");
  365. lua_pushnumber(L, (e->signal - 0x100));
  366. lua_setfield(L, -2, "signal");
  367. /* Crypto */
  368. iwinfo_L_cryptotable(L, &e->crypto);
  369. lua_setfield(L, -2, "encryption");
  370. lua_rawseti(L, -2, x);
  371. }
  372. }
  373. return 1;
  374. }
  375. /* Wrapper for frequency list */
  376. static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *))
  377. {
  378. int i, x, len;
  379. char rv[IWINFO_BUFSIZE];
  380. const char *ifname = luaL_checkstring(L, 1);
  381. struct iwinfo_freqlist_entry *e;
  382. lua_newtable(L);
  383. memset(rv, 0, sizeof(rv));
  384. if (!(*func)(ifname, rv, &len))
  385. {
  386. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++)
  387. {
  388. e = (struct iwinfo_freqlist_entry *) &rv[i];
  389. lua_newtable(L);
  390. /* MHz */
  391. lua_pushinteger(L, e->mhz);
  392. lua_setfield(L, -2, "mhz");
  393. /* Channel */
  394. lua_pushinteger(L, e->channel);
  395. lua_setfield(L, -2, "channel");
  396. /* Restricted (DFS/TPC/Radar) */
  397. lua_pushboolean(L, e->restricted);
  398. lua_setfield(L, -2, "restricted");
  399. lua_rawseti(L, -2, x);
  400. }
  401. }
  402. return 1;
  403. }
  404. /* Wrapper for crypto settings */
  405. static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *))
  406. {
  407. const char *ifname = luaL_checkstring(L, 1);
  408. struct iwinfo_crypto_entry c = { 0 };
  409. if (!(*func)(ifname, (char *)&c))
  410. {
  411. iwinfo_L_cryptotable(L, &c);
  412. return 1;
  413. }
  414. lua_pushnil(L);
  415. return 1;
  416. }
  417. /* Wrapper for hwmode list */
  418. static int iwinfo_L_hwmodelist(lua_State *L, int (*func)(const char *, int *))
  419. {
  420. const char *ifname = luaL_checkstring(L, 1);
  421. int hwmodes = 0;
  422. if (!(*func)(ifname, &hwmodes))
  423. {
  424. lua_newtable(L);
  425. lua_pushboolean(L, hwmodes & IWINFO_80211_A);
  426. lua_setfield(L, -2, "a");
  427. lua_pushboolean(L, hwmodes & IWINFO_80211_B);
  428. lua_setfield(L, -2, "b");
  429. lua_pushboolean(L, hwmodes & IWINFO_80211_G);
  430. lua_setfield(L, -2, "g");
  431. lua_pushboolean(L, hwmodes & IWINFO_80211_N);
  432. lua_setfield(L, -2, "n");
  433. lua_pushboolean(L, hwmodes & IWINFO_80211_AC);
  434. lua_setfield(L, -2, "ac");
  435. lua_pushboolean(L, hwmodes & IWINFO_80211_AD);
  436. lua_setfield(L, -2, "ad");
  437. lua_pushboolean(L, hwmodes & IWINFO_80211_AX);
  438. lua_setfield(L, -2, "ax");
  439. lua_pushboolean(L, hwmodes & IWINFO_80211_BE);
  440. lua_setfield(L, -2, "be");
  441. return 1;
  442. }
  443. lua_pushnil(L);
  444. return 1;
  445. }
  446. /* Wrapper for htmode list */
  447. static int iwinfo_L_htmodelist(lua_State *L, int (*func)(const char *, int *))
  448. {
  449. const char *ifname = luaL_checkstring(L, 1);
  450. int i, htmodes = 0;
  451. if (!(*func)(ifname, &htmodes))
  452. {
  453. lua_newtable(L);
  454. for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++)
  455. {
  456. lua_pushboolean(L, htmodes & (1 << i));
  457. lua_setfield(L, -2, IWINFO_HTMODE_NAMES[i]);
  458. }
  459. return 1;
  460. }
  461. lua_pushnil(L);
  462. return 1;
  463. }
  464. /* Wrapper for mbssid_support */
  465. static int iwinfo_L_mbssid_support(lua_State *L, int (*func)(const char *, int *))
  466. {
  467. const char *ifname = luaL_checkstring(L, 1);
  468. int support = 0;
  469. if (!(*func)(ifname, &support))
  470. {
  471. lua_pushboolean(L, support);
  472. return 1;
  473. }
  474. lua_pushnil(L);
  475. return 1;
  476. }
  477. /* Wrapper for hardware_id */
  478. static int iwinfo_L_hardware_id(lua_State *L, int (*func)(const char *, char *))
  479. {
  480. const char *ifname = luaL_checkstring(L, 1);
  481. struct iwinfo_hardware_id ids;
  482. if (!(*func)(ifname, (char *)&ids))
  483. {
  484. lua_newtable(L);
  485. lua_pushnumber(L, ids.vendor_id);
  486. lua_setfield(L, -2, "vendor_id");
  487. lua_pushnumber(L, ids.device_id);
  488. lua_setfield(L, -2, "device_id");
  489. lua_pushnumber(L, ids.subsystem_vendor_id);
  490. lua_setfield(L, -2, "subsystem_vendor_id");
  491. lua_pushnumber(L, ids.subsystem_device_id);
  492. lua_setfield(L, -2, "subsystem_device_id");
  493. }
  494. else
  495. {
  496. lua_pushnil(L);
  497. }
  498. return 1;
  499. }
  500. /* Wrapper for country list */
  501. static char * iwinfo_L_country_lookup(char *buf, int len, int iso3166)
  502. {
  503. int i;
  504. struct iwinfo_country_entry *c;
  505. for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
  506. {
  507. c = (struct iwinfo_country_entry *) &buf[i];
  508. if (c->iso3166 == iso3166)
  509. return c->ccode;
  510. }
  511. return NULL;
  512. }
  513. static int iwinfo_L_countrylist(lua_State *L, int (*func)(const char *, char *, int *))
  514. {
  515. int len, i;
  516. char rv[IWINFO_BUFSIZE], alpha2[3];
  517. char *ccode;
  518. const char *ifname = luaL_checkstring(L, 1);
  519. const struct iwinfo_iso3166_label *l;
  520. lua_newtable(L);
  521. memset(rv, 0, sizeof(rv));
  522. if (!(*func)(ifname, rv, &len))
  523. {
  524. for (l = IWINFO_ISO3166_NAMES, i = 1; l->iso3166; l++)
  525. {
  526. if ((ccode = iwinfo_L_country_lookup(rv, len, l->iso3166)) != NULL)
  527. {
  528. sprintf(alpha2, "%c%c",
  529. (l->iso3166 / 256), (l->iso3166 % 256));
  530. lua_newtable(L);
  531. lua_pushstring(L, alpha2);
  532. lua_setfield(L, -2, "alpha2");
  533. lua_pushstring(L, ccode);
  534. lua_setfield(L, -2, "ccode");
  535. lua_pushstring(L, l->name);
  536. lua_setfield(L, -2, "name");
  537. lua_rawseti(L, -2, i++);
  538. }
  539. }
  540. }
  541. return 1;
  542. }
  543. #ifdef USE_WL
  544. /* Broadcom */
  545. LUA_WRAP_INT_OP(wl,channel)
  546. LUA_WRAP_INT_OP(wl,frequency)
  547. LUA_WRAP_INT_OP(wl,frequency_offset)
  548. LUA_WRAP_INT_OP(wl,txpower)
  549. LUA_WRAP_INT_OP(wl,txpower_offset)
  550. LUA_WRAP_INT_OP(wl,bitrate)
  551. LUA_WRAP_INT_OP(wl,signal)
  552. LUA_WRAP_INT_OP(wl,noise)
  553. LUA_WRAP_INT_OP(wl,quality)
  554. LUA_WRAP_INT_OP(wl,quality_max)
  555. LUA_WRAP_STRING_OP(wl,ssid)
  556. LUA_WRAP_STRING_OP(wl,bssid)
  557. LUA_WRAP_STRING_OP(wl,country)
  558. LUA_WRAP_STRING_OP(wl,hardware_name)
  559. LUA_WRAP_STRING_OP(wl,phyname)
  560. LUA_WRAP_STRUCT_OP(wl,mode)
  561. LUA_WRAP_STRUCT_OP(wl,assoclist)
  562. LUA_WRAP_STRUCT_OP(wl,txpwrlist)
  563. LUA_WRAP_STRUCT_OP(wl,scanlist)
  564. LUA_WRAP_STRUCT_OP(wl,freqlist)
  565. LUA_WRAP_STRUCT_OP(wl,countrylist)
  566. LUA_WRAP_STRUCT_OP(wl,hwmodelist)
  567. LUA_WRAP_STRUCT_OP(wl,htmodelist)
  568. LUA_WRAP_STRUCT_OP(wl,encryption)
  569. LUA_WRAP_STRUCT_OP(wl,mbssid_support)
  570. LUA_WRAP_STRUCT_OP(wl,hardware_id)
  571. #endif
  572. #ifdef USE_MADWIFI
  573. /* Madwifi */
  574. LUA_WRAP_INT_OP(madwifi,channel)
  575. LUA_WRAP_INT_OP(madwifi,frequency)
  576. LUA_WRAP_INT_OP(madwifi,frequency_offset)
  577. LUA_WRAP_INT_OP(madwifi,txpower)
  578. LUA_WRAP_INT_OP(madwifi,txpower_offset)
  579. LUA_WRAP_INT_OP(madwifi,bitrate)
  580. LUA_WRAP_INT_OP(madwifi,signal)
  581. LUA_WRAP_INT_OP(madwifi,noise)
  582. LUA_WRAP_INT_OP(madwifi,quality)
  583. LUA_WRAP_INT_OP(madwifi,quality_max)
  584. LUA_WRAP_STRING_OP(madwifi,ssid)
  585. LUA_WRAP_STRING_OP(madwifi,bssid)
  586. LUA_WRAP_STRING_OP(madwifi,country)
  587. LUA_WRAP_STRING_OP(madwifi,hardware_name)
  588. LUA_WRAP_STRING_OP(madwifi,phyname)
  589. LUA_WRAP_STRUCT_OP(madwifi,mode)
  590. LUA_WRAP_STRUCT_OP(madwifi,assoclist)
  591. LUA_WRAP_STRUCT_OP(madwifi,txpwrlist)
  592. LUA_WRAP_STRUCT_OP(madwifi,scanlist)
  593. LUA_WRAP_STRUCT_OP(madwifi,freqlist)
  594. LUA_WRAP_STRUCT_OP(madwifi,countrylist)
  595. LUA_WRAP_STRUCT_OP(madwifi,hwmodelist)
  596. LUA_WRAP_STRUCT_OP(madwifi,htmodelist)
  597. LUA_WRAP_STRUCT_OP(madwifi,encryption)
  598. LUA_WRAP_STRUCT_OP(madwifi,mbssid_support)
  599. LUA_WRAP_STRUCT_OP(madwifi,hardware_id)
  600. #endif
  601. #ifdef USE_NL80211
  602. /* NL80211 */
  603. LUA_WRAP_INT_OP(nl80211,channel)
  604. LUA_WRAP_INT_OP(nl80211,frequency)
  605. LUA_WRAP_INT_OP(nl80211,frequency_offset)
  606. LUA_WRAP_INT_OP(nl80211,txpower)
  607. LUA_WRAP_INT_OP(nl80211,txpower_offset)
  608. LUA_WRAP_INT_OP(nl80211,bitrate)
  609. LUA_WRAP_INT_OP(nl80211,signal)
  610. LUA_WRAP_INT_OP(nl80211,noise)
  611. LUA_WRAP_INT_OP(nl80211,quality)
  612. LUA_WRAP_INT_OP(nl80211,quality_max)
  613. LUA_WRAP_STRING_OP(nl80211,ssid)
  614. LUA_WRAP_STRING_OP(nl80211,bssid)
  615. LUA_WRAP_STRING_OP(nl80211,country)
  616. LUA_WRAP_STRING_OP(nl80211,hardware_name)
  617. LUA_WRAP_STRING_OP(nl80211,phyname)
  618. LUA_WRAP_STRUCT_OP(nl80211,mode)
  619. LUA_WRAP_STRUCT_OP(nl80211,assoclist)
  620. LUA_WRAP_STRUCT_OP(nl80211,txpwrlist)
  621. LUA_WRAP_STRUCT_OP(nl80211,scanlist)
  622. LUA_WRAP_STRUCT_OP(nl80211,freqlist)
  623. LUA_WRAP_STRUCT_OP(nl80211,countrylist)
  624. LUA_WRAP_STRUCT_OP(nl80211,hwmodelist)
  625. LUA_WRAP_STRUCT_OP(nl80211,htmodelist)
  626. LUA_WRAP_STRUCT_OP(nl80211,encryption)
  627. LUA_WRAP_STRUCT_OP(nl80211,mbssid_support)
  628. LUA_WRAP_STRUCT_OP(nl80211,hardware_id)
  629. #endif
  630. /* Wext */
  631. #ifdef USE_WEXT
  632. LUA_WRAP_INT_OP(wext,channel)
  633. LUA_WRAP_INT_OP(wext,frequency)
  634. LUA_WRAP_INT_OP(wext,frequency_offset)
  635. LUA_WRAP_INT_OP(wext,txpower)
  636. LUA_WRAP_INT_OP(wext,txpower_offset)
  637. LUA_WRAP_INT_OP(wext,bitrate)
  638. LUA_WRAP_INT_OP(wext,signal)
  639. LUA_WRAP_INT_OP(wext,noise)
  640. LUA_WRAP_INT_OP(wext,quality)
  641. LUA_WRAP_INT_OP(wext,quality_max)
  642. LUA_WRAP_STRING_OP(wext,ssid)
  643. LUA_WRAP_STRING_OP(wext,bssid)
  644. LUA_WRAP_STRING_OP(wext,country)
  645. LUA_WRAP_STRING_OP(wext,hardware_name)
  646. LUA_WRAP_STRING_OP(wext,phyname)
  647. LUA_WRAP_STRUCT_OP(wext,mode)
  648. LUA_WRAP_STRUCT_OP(wext,assoclist)
  649. LUA_WRAP_STRUCT_OP(wext,txpwrlist)
  650. LUA_WRAP_STRUCT_OP(wext,scanlist)
  651. LUA_WRAP_STRUCT_OP(wext,freqlist)
  652. LUA_WRAP_STRUCT_OP(wext,countrylist)
  653. LUA_WRAP_STRUCT_OP(wext,hwmodelist)
  654. LUA_WRAP_STRUCT_OP(wext,htmodelist)
  655. LUA_WRAP_STRUCT_OP(wext,encryption)
  656. LUA_WRAP_STRUCT_OP(wext,mbssid_support)
  657. LUA_WRAP_STRUCT_OP(wext,hardware_id)
  658. #endif
  659. #ifdef USE_WL
  660. /* Broadcom table */
  661. static const luaL_reg R_wl[] = {
  662. LUA_REG(wl,channel),
  663. LUA_REG(wl,frequency),
  664. LUA_REG(wl,frequency_offset),
  665. LUA_REG(wl,txpower),
  666. LUA_REG(wl,txpower_offset),
  667. LUA_REG(wl,bitrate),
  668. LUA_REG(wl,signal),
  669. LUA_REG(wl,noise),
  670. LUA_REG(wl,quality),
  671. LUA_REG(wl,quality_max),
  672. LUA_REG(wl,mode),
  673. LUA_REG(wl,ssid),
  674. LUA_REG(wl,bssid),
  675. LUA_REG(wl,country),
  676. LUA_REG(wl,assoclist),
  677. LUA_REG(wl,txpwrlist),
  678. LUA_REG(wl,scanlist),
  679. LUA_REG(wl,freqlist),
  680. LUA_REG(wl,countrylist),
  681. LUA_REG(wl,hwmodelist),
  682. LUA_REG(wl,htmodelist),
  683. LUA_REG(wl,encryption),
  684. LUA_REG(wl,mbssid_support),
  685. LUA_REG(wl,hardware_id),
  686. LUA_REG(wl,hardware_name),
  687. LUA_REG(wl,phyname),
  688. { NULL, NULL }
  689. };
  690. #endif
  691. #ifdef USE_MADWIFI
  692. /* Madwifi table */
  693. static const luaL_reg R_madwifi[] = {
  694. LUA_REG(madwifi,channel),
  695. LUA_REG(madwifi,frequency),
  696. LUA_REG(madwifi,frequency_offset),
  697. LUA_REG(madwifi,txpower),
  698. LUA_REG(madwifi,txpower_offset),
  699. LUA_REG(madwifi,bitrate),
  700. LUA_REG(madwifi,signal),
  701. LUA_REG(madwifi,noise),
  702. LUA_REG(madwifi,quality),
  703. LUA_REG(madwifi,quality_max),
  704. LUA_REG(madwifi,mode),
  705. LUA_REG(madwifi,ssid),
  706. LUA_REG(madwifi,bssid),
  707. LUA_REG(madwifi,country),
  708. LUA_REG(madwifi,assoclist),
  709. LUA_REG(madwifi,txpwrlist),
  710. LUA_REG(madwifi,scanlist),
  711. LUA_REG(madwifi,freqlist),
  712. LUA_REG(madwifi,countrylist),
  713. LUA_REG(madwifi,hwmodelist),
  714. LUA_REG(madwifi,htmodelist),
  715. LUA_REG(madwifi,encryption),
  716. LUA_REG(madwifi,mbssid_support),
  717. LUA_REG(madwifi,hardware_id),
  718. LUA_REG(madwifi,hardware_name),
  719. LUA_REG(madwifi,phyname),
  720. { NULL, NULL }
  721. };
  722. #endif
  723. #ifdef USE_NL80211
  724. /* NL80211 table */
  725. static const luaL_reg R_nl80211[] = {
  726. LUA_REG(nl80211,channel),
  727. LUA_REG(nl80211,frequency),
  728. LUA_REG(nl80211,frequency_offset),
  729. LUA_REG(nl80211,txpower),
  730. LUA_REG(nl80211,txpower_offset),
  731. LUA_REG(nl80211,bitrate),
  732. LUA_REG(nl80211,signal),
  733. LUA_REG(nl80211,noise),
  734. LUA_REG(nl80211,quality),
  735. LUA_REG(nl80211,quality_max),
  736. LUA_REG(nl80211,mode),
  737. LUA_REG(nl80211,ssid),
  738. LUA_REG(nl80211,bssid),
  739. LUA_REG(nl80211,country),
  740. LUA_REG(nl80211,assoclist),
  741. LUA_REG(nl80211,txpwrlist),
  742. LUA_REG(nl80211,scanlist),
  743. LUA_REG(nl80211,freqlist),
  744. LUA_REG(nl80211,countrylist),
  745. LUA_REG(nl80211,hwmodelist),
  746. LUA_REG(nl80211,htmodelist),
  747. LUA_REG(nl80211,encryption),
  748. LUA_REG(nl80211,mbssid_support),
  749. LUA_REG(nl80211,hardware_id),
  750. LUA_REG(nl80211,hardware_name),
  751. LUA_REG(nl80211,phyname),
  752. { NULL, NULL }
  753. };
  754. #endif
  755. /* Wext table */
  756. #ifdef USE_WEXT
  757. static const luaL_reg R_wext[] = {
  758. LUA_REG(wext,channel),
  759. LUA_REG(wext,frequency),
  760. LUA_REG(wext,frequency_offset),
  761. LUA_REG(wext,txpower),
  762. LUA_REG(wext,txpower_offset),
  763. LUA_REG(wext,bitrate),
  764. LUA_REG(wext,signal),
  765. LUA_REG(wext,noise),
  766. LUA_REG(wext,quality),
  767. LUA_REG(wext,quality_max),
  768. LUA_REG(wext,mode),
  769. LUA_REG(wext,ssid),
  770. LUA_REG(wext,bssid),
  771. LUA_REG(wext,country),
  772. LUA_REG(wext,assoclist),
  773. LUA_REG(wext,txpwrlist),
  774. LUA_REG(wext,scanlist),
  775. LUA_REG(wext,freqlist),
  776. LUA_REG(wext,countrylist),
  777. LUA_REG(wext,hwmodelist),
  778. LUA_REG(wext,htmodelist),
  779. LUA_REG(wext,encryption),
  780. LUA_REG(wext,mbssid_support),
  781. LUA_REG(wext,hardware_id),
  782. LUA_REG(wext,hardware_name),
  783. LUA_REG(wext,phyname),
  784. { NULL, NULL }
  785. };
  786. #endif
  787. /* Common */
  788. static const luaL_reg R_common[] = {
  789. { "type", iwinfo_L_type },
  790. { "__gc", iwinfo_L__gc },
  791. { NULL, NULL }
  792. };
  793. LUALIB_API int luaopen_iwinfo(lua_State *L) {
  794. luaL_register(L, IWINFO_META, R_common);
  795. #ifdef USE_WL
  796. luaL_newmetatable(L, IWINFO_WL_META);
  797. luaL_register(L, NULL, R_common);
  798. luaL_register(L, NULL, R_wl);
  799. lua_pushvalue(L, -1);
  800. lua_setfield(L, -2, "__index");
  801. lua_setfield(L, -2, "wl");
  802. #endif
  803. #ifdef USE_MADWIFI
  804. luaL_newmetatable(L, IWINFO_MADWIFI_META);
  805. luaL_register(L, NULL, R_common);
  806. luaL_register(L, NULL, R_madwifi);
  807. lua_pushvalue(L, -1);
  808. lua_setfield(L, -2, "__index");
  809. lua_setfield(L, -2, "madwifi");
  810. #endif
  811. #ifdef USE_NL80211
  812. luaL_newmetatable(L, IWINFO_NL80211_META);
  813. luaL_register(L, NULL, R_common);
  814. luaL_register(L, NULL, R_nl80211);
  815. lua_pushvalue(L, -1);
  816. lua_setfield(L, -2, "__index");
  817. lua_setfield(L, -2, "nl80211");
  818. #endif
  819. #ifdef USE_WEXT
  820. luaL_newmetatable(L, IWINFO_WEXT_META);
  821. luaL_register(L, NULL, R_common);
  822. luaL_register(L, NULL, R_wext);
  823. lua_pushvalue(L, -1);
  824. lua_setfield(L, -2, "__index");
  825. lua_setfield(L, -2, "wext");
  826. #endif
  827. return 1;
  828. }