iwinfo_lua.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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_pushnumber(L, r->mhz);
  227. lua_setfield(L, -2, rx ? "rx_mhz" : "tx_mhz");
  228. if (r->is_ht)
  229. {
  230. lua_pushboolean(L, r->is_40mhz);
  231. lua_setfield(L, -2, rx ? "rx_40mhz" : "tx_40mhz");
  232. lua_pushnumber(L, r->mcs);
  233. lua_setfield(L, -2, rx ? "rx_mcs" : "tx_mcs");
  234. lua_pushboolean(L, r->is_short_gi);
  235. lua_setfield(L, -2, rx ? "rx_short_gi" : "tx_short_gi");
  236. }
  237. else if (r->is_vht || r->is_he)
  238. {
  239. lua_pushnumber(L, r->mcs);
  240. lua_setfield(L, -2, rx ? "rx_mcs" : "tx_mcs");
  241. lua_pushnumber(L, r->nss);
  242. lua_setfield(L, -2, rx ? "rx_nss" : "tx_nss");
  243. if (r->is_he) {
  244. lua_pushnumber(L, r->he_gi);
  245. lua_setfield(L, -2, rx ? "rx_he_gi" : "tx_he_gi");
  246. lua_pushnumber(L, r->he_dcm);
  247. lua_setfield(L, -2, rx ? "rx_he_dcm" : "tx_he_dcm");
  248. }
  249. if (r->is_vht) {
  250. lua_pushboolean(L, r->is_short_gi);
  251. lua_setfield(L, -2, rx ? "rx_short_gi" : "tx_short_gi");
  252. }
  253. }
  254. }
  255. /* Wrapper for assoclist */
  256. static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
  257. {
  258. int i, len;
  259. char rv[IWINFO_BUFSIZE];
  260. char macstr[18];
  261. const char *ifname = luaL_checkstring(L, 1);
  262. struct iwinfo_assoclist_entry *e;
  263. lua_newtable(L);
  264. memset(rv, 0, sizeof(rv));
  265. if (!(*func)(ifname, rv, &len))
  266. {
  267. for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
  268. {
  269. e = (struct iwinfo_assoclist_entry *) &rv[i];
  270. sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
  271. e->mac[0], e->mac[1], e->mac[2],
  272. e->mac[3], e->mac[4], e->mac[5]);
  273. lua_newtable(L);
  274. lua_pushnumber(L, e->signal);
  275. lua_setfield(L, -2, "signal");
  276. lua_pushnumber(L, e->noise);
  277. lua_setfield(L, -2, "noise");
  278. lua_pushnumber(L, e->inactive);
  279. lua_setfield(L, -2, "inactive");
  280. lua_pushnumber(L, e->rx_packets);
  281. lua_setfield(L, -2, "rx_packets");
  282. lua_pushnumber(L, e->tx_packets);
  283. lua_setfield(L, -2, "tx_packets");
  284. set_rateinfo(L, &e->rx_rate, true);
  285. set_rateinfo(L, &e->tx_rate, false);
  286. if (e->thr) {
  287. lua_pushnumber(L, e->thr);
  288. lua_setfield(L, -2, "expected_throughput");
  289. }
  290. lua_setfield(L, -2, macstr);
  291. }
  292. }
  293. return 1;
  294. }
  295. /* Wrapper for tx power list */
  296. static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
  297. {
  298. int i, x, len;
  299. char rv[IWINFO_BUFSIZE];
  300. const char *ifname = luaL_checkstring(L, 1);
  301. struct iwinfo_txpwrlist_entry *e;
  302. memset(rv, 0, sizeof(rv));
  303. if (!(*func)(ifname, rv, &len))
  304. {
  305. lua_newtable(L);
  306. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++)
  307. {
  308. e = (struct iwinfo_txpwrlist_entry *) &rv[i];
  309. lua_newtable(L);
  310. lua_pushnumber(L, e->mw);
  311. lua_setfield(L, -2, "mw");
  312. lua_pushnumber(L, e->dbm);
  313. lua_setfield(L, -2, "dbm");
  314. lua_rawseti(L, -2, x);
  315. }
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. /* Wrapper for scan list */
  321. static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
  322. {
  323. int i, x, len = 0;
  324. char rv[IWINFO_BUFSIZE];
  325. char macstr[18];
  326. const char *ifname = luaL_checkstring(L, 1);
  327. struct iwinfo_scanlist_entry *e;
  328. lua_newtable(L);
  329. memset(rv, 0, sizeof(rv));
  330. if (!(*func)(ifname, rv, &len))
  331. {
  332. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
  333. {
  334. e = (struct iwinfo_scanlist_entry *) &rv[i];
  335. lua_newtable(L);
  336. /* BSSID */
  337. sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
  338. e->mac[0], e->mac[1], e->mac[2],
  339. e->mac[3], e->mac[4], e->mac[5]);
  340. lua_pushstring(L, macstr);
  341. lua_setfield(L, -2, "bssid");
  342. /* ESSID */
  343. if (e->ssid[0])
  344. {
  345. lua_pushstring(L, (char *) e->ssid);
  346. lua_setfield(L, -2, "ssid");
  347. }
  348. /* Channel */
  349. lua_pushinteger(L, e->channel);
  350. lua_setfield(L, -2, "channel");
  351. /* Mode */
  352. lua_pushstring(L, IWINFO_OPMODE_NAMES[e->mode]);
  353. lua_setfield(L, -2, "mode");
  354. /* Quality, Signal */
  355. lua_pushinteger(L, e->quality);
  356. lua_setfield(L, -2, "quality");
  357. lua_pushinteger(L, e->quality_max);
  358. lua_setfield(L, -2, "quality_max");
  359. lua_pushnumber(L, (e->signal - 0x100));
  360. lua_setfield(L, -2, "signal");
  361. /* Crypto */
  362. iwinfo_L_cryptotable(L, &e->crypto);
  363. lua_setfield(L, -2, "encryption");
  364. lua_rawseti(L, -2, x);
  365. }
  366. }
  367. return 1;
  368. }
  369. /* Wrapper for frequency list */
  370. static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *))
  371. {
  372. int i, x, len;
  373. char rv[IWINFO_BUFSIZE];
  374. const char *ifname = luaL_checkstring(L, 1);
  375. struct iwinfo_freqlist_entry *e;
  376. lua_newtable(L);
  377. memset(rv, 0, sizeof(rv));
  378. if (!(*func)(ifname, rv, &len))
  379. {
  380. for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++)
  381. {
  382. e = (struct iwinfo_freqlist_entry *) &rv[i];
  383. lua_newtable(L);
  384. /* MHz */
  385. lua_pushinteger(L, e->mhz);
  386. lua_setfield(L, -2, "mhz");
  387. /* Channel */
  388. lua_pushinteger(L, e->channel);
  389. lua_setfield(L, -2, "channel");
  390. /* Restricted (DFS/TPC/Radar) */
  391. lua_pushboolean(L, e->restricted);
  392. lua_setfield(L, -2, "restricted");
  393. lua_rawseti(L, -2, x);
  394. }
  395. }
  396. return 1;
  397. }
  398. /* Wrapper for crypto settings */
  399. static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *))
  400. {
  401. const char *ifname = luaL_checkstring(L, 1);
  402. struct iwinfo_crypto_entry c = { 0 };
  403. if (!(*func)(ifname, (char *)&c))
  404. {
  405. iwinfo_L_cryptotable(L, &c);
  406. return 1;
  407. }
  408. lua_pushnil(L);
  409. return 1;
  410. }
  411. /* Wrapper for hwmode list */
  412. static int iwinfo_L_hwmodelist(lua_State *L, int (*func)(const char *, int *))
  413. {
  414. const char *ifname = luaL_checkstring(L, 1);
  415. int hwmodes = 0;
  416. if (!(*func)(ifname, &hwmodes))
  417. {
  418. lua_newtable(L);
  419. lua_pushboolean(L, hwmodes & IWINFO_80211_A);
  420. lua_setfield(L, -2, "a");
  421. lua_pushboolean(L, hwmodes & IWINFO_80211_B);
  422. lua_setfield(L, -2, "b");
  423. lua_pushboolean(L, hwmodes & IWINFO_80211_G);
  424. lua_setfield(L, -2, "g");
  425. lua_pushboolean(L, hwmodes & IWINFO_80211_N);
  426. lua_setfield(L, -2, "n");
  427. lua_pushboolean(L, hwmodes & IWINFO_80211_AC);
  428. lua_setfield(L, -2, "ac");
  429. lua_pushboolean(L, hwmodes & IWINFO_80211_AD);
  430. lua_setfield(L, -2, "ad");
  431. lua_pushboolean(L, hwmodes & IWINFO_80211_AX);
  432. lua_setfield(L, -2, "ax");
  433. return 1;
  434. }
  435. lua_pushnil(L);
  436. return 1;
  437. }
  438. /* Wrapper for htmode list */
  439. static int iwinfo_L_htmodelist(lua_State *L, int (*func)(const char *, int *))
  440. {
  441. const char *ifname = luaL_checkstring(L, 1);
  442. int i, htmodes = 0;
  443. if (!(*func)(ifname, &htmodes))
  444. {
  445. lua_newtable(L);
  446. for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++)
  447. {
  448. lua_pushboolean(L, htmodes & (1 << i));
  449. lua_setfield(L, -2, IWINFO_HTMODE_NAMES[i]);
  450. }
  451. return 1;
  452. }
  453. lua_pushnil(L);
  454. return 1;
  455. }
  456. /* Wrapper for mbssid_support */
  457. static int iwinfo_L_mbssid_support(lua_State *L, int (*func)(const char *, int *))
  458. {
  459. const char *ifname = luaL_checkstring(L, 1);
  460. int support = 0;
  461. if (!(*func)(ifname, &support))
  462. {
  463. lua_pushboolean(L, support);
  464. return 1;
  465. }
  466. lua_pushnil(L);
  467. return 1;
  468. }
  469. /* Wrapper for hardware_id */
  470. static int iwinfo_L_hardware_id(lua_State *L, int (*func)(const char *, char *))
  471. {
  472. const char *ifname = luaL_checkstring(L, 1);
  473. struct iwinfo_hardware_id ids;
  474. if (!(*func)(ifname, (char *)&ids))
  475. {
  476. lua_newtable(L);
  477. lua_pushnumber(L, ids.vendor_id);
  478. lua_setfield(L, -2, "vendor_id");
  479. lua_pushnumber(L, ids.device_id);
  480. lua_setfield(L, -2, "device_id");
  481. lua_pushnumber(L, ids.subsystem_vendor_id);
  482. lua_setfield(L, -2, "subsystem_vendor_id");
  483. lua_pushnumber(L, ids.subsystem_device_id);
  484. lua_setfield(L, -2, "subsystem_device_id");
  485. }
  486. else
  487. {
  488. lua_pushnil(L);
  489. }
  490. return 1;
  491. }
  492. /* Wrapper for country list */
  493. static char * iwinfo_L_country_lookup(char *buf, int len, int iso3166)
  494. {
  495. int i;
  496. struct iwinfo_country_entry *c;
  497. for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
  498. {
  499. c = (struct iwinfo_country_entry *) &buf[i];
  500. if (c->iso3166 == iso3166)
  501. return c->ccode;
  502. }
  503. return NULL;
  504. }
  505. static int iwinfo_L_countrylist(lua_State *L, int (*func)(const char *, char *, int *))
  506. {
  507. int len, i;
  508. char rv[IWINFO_BUFSIZE], alpha2[3];
  509. char *ccode;
  510. const char *ifname = luaL_checkstring(L, 1);
  511. const struct iwinfo_iso3166_label *l;
  512. lua_newtable(L);
  513. memset(rv, 0, sizeof(rv));
  514. if (!(*func)(ifname, rv, &len))
  515. {
  516. for (l = IWINFO_ISO3166_NAMES, i = 1; l->iso3166; l++)
  517. {
  518. if ((ccode = iwinfo_L_country_lookup(rv, len, l->iso3166)) != NULL)
  519. {
  520. sprintf(alpha2, "%c%c",
  521. (l->iso3166 / 256), (l->iso3166 % 256));
  522. lua_newtable(L);
  523. lua_pushstring(L, alpha2);
  524. lua_setfield(L, -2, "alpha2");
  525. lua_pushstring(L, ccode);
  526. lua_setfield(L, -2, "ccode");
  527. lua_pushstring(L, l->name);
  528. lua_setfield(L, -2, "name");
  529. lua_rawseti(L, -2, i++);
  530. }
  531. }
  532. }
  533. return 1;
  534. }
  535. #ifdef USE_WL
  536. /* Broadcom */
  537. LUA_WRAP_INT_OP(wl,channel)
  538. LUA_WRAP_INT_OP(wl,frequency)
  539. LUA_WRAP_INT_OP(wl,frequency_offset)
  540. LUA_WRAP_INT_OP(wl,txpower)
  541. LUA_WRAP_INT_OP(wl,txpower_offset)
  542. LUA_WRAP_INT_OP(wl,bitrate)
  543. LUA_WRAP_INT_OP(wl,signal)
  544. LUA_WRAP_INT_OP(wl,noise)
  545. LUA_WRAP_INT_OP(wl,quality)
  546. LUA_WRAP_INT_OP(wl,quality_max)
  547. LUA_WRAP_STRING_OP(wl,ssid)
  548. LUA_WRAP_STRING_OP(wl,bssid)
  549. LUA_WRAP_STRING_OP(wl,country)
  550. LUA_WRAP_STRING_OP(wl,hardware_name)
  551. LUA_WRAP_STRING_OP(wl,phyname)
  552. LUA_WRAP_STRUCT_OP(wl,mode)
  553. LUA_WRAP_STRUCT_OP(wl,assoclist)
  554. LUA_WRAP_STRUCT_OP(wl,txpwrlist)
  555. LUA_WRAP_STRUCT_OP(wl,scanlist)
  556. LUA_WRAP_STRUCT_OP(wl,freqlist)
  557. LUA_WRAP_STRUCT_OP(wl,countrylist)
  558. LUA_WRAP_STRUCT_OP(wl,hwmodelist)
  559. LUA_WRAP_STRUCT_OP(wl,htmodelist)
  560. LUA_WRAP_STRUCT_OP(wl,encryption)
  561. LUA_WRAP_STRUCT_OP(wl,mbssid_support)
  562. LUA_WRAP_STRUCT_OP(wl,hardware_id)
  563. #endif
  564. #ifdef USE_MADWIFI
  565. /* Madwifi */
  566. LUA_WRAP_INT_OP(madwifi,channel)
  567. LUA_WRAP_INT_OP(madwifi,frequency)
  568. LUA_WRAP_INT_OP(madwifi,frequency_offset)
  569. LUA_WRAP_INT_OP(madwifi,txpower)
  570. LUA_WRAP_INT_OP(madwifi,txpower_offset)
  571. LUA_WRAP_INT_OP(madwifi,bitrate)
  572. LUA_WRAP_INT_OP(madwifi,signal)
  573. LUA_WRAP_INT_OP(madwifi,noise)
  574. LUA_WRAP_INT_OP(madwifi,quality)
  575. LUA_WRAP_INT_OP(madwifi,quality_max)
  576. LUA_WRAP_STRING_OP(madwifi,ssid)
  577. LUA_WRAP_STRING_OP(madwifi,bssid)
  578. LUA_WRAP_STRING_OP(madwifi,country)
  579. LUA_WRAP_STRING_OP(madwifi,hardware_name)
  580. LUA_WRAP_STRING_OP(madwifi,phyname)
  581. LUA_WRAP_STRUCT_OP(madwifi,mode)
  582. LUA_WRAP_STRUCT_OP(madwifi,assoclist)
  583. LUA_WRAP_STRUCT_OP(madwifi,txpwrlist)
  584. LUA_WRAP_STRUCT_OP(madwifi,scanlist)
  585. LUA_WRAP_STRUCT_OP(madwifi,freqlist)
  586. LUA_WRAP_STRUCT_OP(madwifi,countrylist)
  587. LUA_WRAP_STRUCT_OP(madwifi,hwmodelist)
  588. LUA_WRAP_STRUCT_OP(madwifi,htmodelist)
  589. LUA_WRAP_STRUCT_OP(madwifi,encryption)
  590. LUA_WRAP_STRUCT_OP(madwifi,mbssid_support)
  591. LUA_WRAP_STRUCT_OP(madwifi,hardware_id)
  592. #endif
  593. #ifdef USE_NL80211
  594. /* NL80211 */
  595. LUA_WRAP_INT_OP(nl80211,channel)
  596. LUA_WRAP_INT_OP(nl80211,frequency)
  597. LUA_WRAP_INT_OP(nl80211,frequency_offset)
  598. LUA_WRAP_INT_OP(nl80211,txpower)
  599. LUA_WRAP_INT_OP(nl80211,txpower_offset)
  600. LUA_WRAP_INT_OP(nl80211,bitrate)
  601. LUA_WRAP_INT_OP(nl80211,signal)
  602. LUA_WRAP_INT_OP(nl80211,noise)
  603. LUA_WRAP_INT_OP(nl80211,quality)
  604. LUA_WRAP_INT_OP(nl80211,quality_max)
  605. LUA_WRAP_STRING_OP(nl80211,ssid)
  606. LUA_WRAP_STRING_OP(nl80211,bssid)
  607. LUA_WRAP_STRING_OP(nl80211,country)
  608. LUA_WRAP_STRING_OP(nl80211,hardware_name)
  609. LUA_WRAP_STRING_OP(nl80211,phyname)
  610. LUA_WRAP_STRUCT_OP(nl80211,mode)
  611. LUA_WRAP_STRUCT_OP(nl80211,assoclist)
  612. LUA_WRAP_STRUCT_OP(nl80211,txpwrlist)
  613. LUA_WRAP_STRUCT_OP(nl80211,scanlist)
  614. LUA_WRAP_STRUCT_OP(nl80211,freqlist)
  615. LUA_WRAP_STRUCT_OP(nl80211,countrylist)
  616. LUA_WRAP_STRUCT_OP(nl80211,hwmodelist)
  617. LUA_WRAP_STRUCT_OP(nl80211,htmodelist)
  618. LUA_WRAP_STRUCT_OP(nl80211,encryption)
  619. LUA_WRAP_STRUCT_OP(nl80211,mbssid_support)
  620. LUA_WRAP_STRUCT_OP(nl80211,hardware_id)
  621. #endif
  622. /* Wext */
  623. #ifdef USE_WEXT
  624. LUA_WRAP_INT_OP(wext,channel)
  625. LUA_WRAP_INT_OP(wext,frequency)
  626. LUA_WRAP_INT_OP(wext,frequency_offset)
  627. LUA_WRAP_INT_OP(wext,txpower)
  628. LUA_WRAP_INT_OP(wext,txpower_offset)
  629. LUA_WRAP_INT_OP(wext,bitrate)
  630. LUA_WRAP_INT_OP(wext,signal)
  631. LUA_WRAP_INT_OP(wext,noise)
  632. LUA_WRAP_INT_OP(wext,quality)
  633. LUA_WRAP_INT_OP(wext,quality_max)
  634. LUA_WRAP_STRING_OP(wext,ssid)
  635. LUA_WRAP_STRING_OP(wext,bssid)
  636. LUA_WRAP_STRING_OP(wext,country)
  637. LUA_WRAP_STRING_OP(wext,hardware_name)
  638. LUA_WRAP_STRING_OP(wext,phyname)
  639. LUA_WRAP_STRUCT_OP(wext,mode)
  640. LUA_WRAP_STRUCT_OP(wext,assoclist)
  641. LUA_WRAP_STRUCT_OP(wext,txpwrlist)
  642. LUA_WRAP_STRUCT_OP(wext,scanlist)
  643. LUA_WRAP_STRUCT_OP(wext,freqlist)
  644. LUA_WRAP_STRUCT_OP(wext,countrylist)
  645. LUA_WRAP_STRUCT_OP(wext,hwmodelist)
  646. LUA_WRAP_STRUCT_OP(wext,htmodelist)
  647. LUA_WRAP_STRUCT_OP(wext,encryption)
  648. LUA_WRAP_STRUCT_OP(wext,mbssid_support)
  649. LUA_WRAP_STRUCT_OP(wext,hardware_id)
  650. #endif
  651. #ifdef USE_WL
  652. /* Broadcom table */
  653. static const luaL_reg R_wl[] = {
  654. LUA_REG(wl,channel),
  655. LUA_REG(wl,frequency),
  656. LUA_REG(wl,frequency_offset),
  657. LUA_REG(wl,txpower),
  658. LUA_REG(wl,txpower_offset),
  659. LUA_REG(wl,bitrate),
  660. LUA_REG(wl,signal),
  661. LUA_REG(wl,noise),
  662. LUA_REG(wl,quality),
  663. LUA_REG(wl,quality_max),
  664. LUA_REG(wl,mode),
  665. LUA_REG(wl,ssid),
  666. LUA_REG(wl,bssid),
  667. LUA_REG(wl,country),
  668. LUA_REG(wl,assoclist),
  669. LUA_REG(wl,txpwrlist),
  670. LUA_REG(wl,scanlist),
  671. LUA_REG(wl,freqlist),
  672. LUA_REG(wl,countrylist),
  673. LUA_REG(wl,hwmodelist),
  674. LUA_REG(wl,htmodelist),
  675. LUA_REG(wl,encryption),
  676. LUA_REG(wl,mbssid_support),
  677. LUA_REG(wl,hardware_id),
  678. LUA_REG(wl,hardware_name),
  679. LUA_REG(wl,phyname),
  680. { NULL, NULL }
  681. };
  682. #endif
  683. #ifdef USE_MADWIFI
  684. /* Madwifi table */
  685. static const luaL_reg R_madwifi[] = {
  686. LUA_REG(madwifi,channel),
  687. LUA_REG(madwifi,frequency),
  688. LUA_REG(madwifi,frequency_offset),
  689. LUA_REG(madwifi,txpower),
  690. LUA_REG(madwifi,txpower_offset),
  691. LUA_REG(madwifi,bitrate),
  692. LUA_REG(madwifi,signal),
  693. LUA_REG(madwifi,noise),
  694. LUA_REG(madwifi,quality),
  695. LUA_REG(madwifi,quality_max),
  696. LUA_REG(madwifi,mode),
  697. LUA_REG(madwifi,ssid),
  698. LUA_REG(madwifi,bssid),
  699. LUA_REG(madwifi,country),
  700. LUA_REG(madwifi,assoclist),
  701. LUA_REG(madwifi,txpwrlist),
  702. LUA_REG(madwifi,scanlist),
  703. LUA_REG(madwifi,freqlist),
  704. LUA_REG(madwifi,countrylist),
  705. LUA_REG(madwifi,hwmodelist),
  706. LUA_REG(madwifi,htmodelist),
  707. LUA_REG(madwifi,encryption),
  708. LUA_REG(madwifi,mbssid_support),
  709. LUA_REG(madwifi,hardware_id),
  710. LUA_REG(madwifi,hardware_name),
  711. LUA_REG(madwifi,phyname),
  712. { NULL, NULL }
  713. };
  714. #endif
  715. #ifdef USE_NL80211
  716. /* NL80211 table */
  717. static const luaL_reg R_nl80211[] = {
  718. LUA_REG(nl80211,channel),
  719. LUA_REG(nl80211,frequency),
  720. LUA_REG(nl80211,frequency_offset),
  721. LUA_REG(nl80211,txpower),
  722. LUA_REG(nl80211,txpower_offset),
  723. LUA_REG(nl80211,bitrate),
  724. LUA_REG(nl80211,signal),
  725. LUA_REG(nl80211,noise),
  726. LUA_REG(nl80211,quality),
  727. LUA_REG(nl80211,quality_max),
  728. LUA_REG(nl80211,mode),
  729. LUA_REG(nl80211,ssid),
  730. LUA_REG(nl80211,bssid),
  731. LUA_REG(nl80211,country),
  732. LUA_REG(nl80211,assoclist),
  733. LUA_REG(nl80211,txpwrlist),
  734. LUA_REG(nl80211,scanlist),
  735. LUA_REG(nl80211,freqlist),
  736. LUA_REG(nl80211,countrylist),
  737. LUA_REG(nl80211,hwmodelist),
  738. LUA_REG(nl80211,htmodelist),
  739. LUA_REG(nl80211,encryption),
  740. LUA_REG(nl80211,mbssid_support),
  741. LUA_REG(nl80211,hardware_id),
  742. LUA_REG(nl80211,hardware_name),
  743. LUA_REG(nl80211,phyname),
  744. { NULL, NULL }
  745. };
  746. #endif
  747. /* Wext table */
  748. #ifdef USE_WEXT
  749. static const luaL_reg R_wext[] = {
  750. LUA_REG(wext,channel),
  751. LUA_REG(wext,frequency),
  752. LUA_REG(wext,frequency_offset),
  753. LUA_REG(wext,txpower),
  754. LUA_REG(wext,txpower_offset),
  755. LUA_REG(wext,bitrate),
  756. LUA_REG(wext,signal),
  757. LUA_REG(wext,noise),
  758. LUA_REG(wext,quality),
  759. LUA_REG(wext,quality_max),
  760. LUA_REG(wext,mode),
  761. LUA_REG(wext,ssid),
  762. LUA_REG(wext,bssid),
  763. LUA_REG(wext,country),
  764. LUA_REG(wext,assoclist),
  765. LUA_REG(wext,txpwrlist),
  766. LUA_REG(wext,scanlist),
  767. LUA_REG(wext,freqlist),
  768. LUA_REG(wext,countrylist),
  769. LUA_REG(wext,hwmodelist),
  770. LUA_REG(wext,htmodelist),
  771. LUA_REG(wext,encryption),
  772. LUA_REG(wext,mbssid_support),
  773. LUA_REG(wext,hardware_id),
  774. LUA_REG(wext,hardware_name),
  775. LUA_REG(wext,phyname),
  776. { NULL, NULL }
  777. };
  778. #endif
  779. /* Common */
  780. static const luaL_reg R_common[] = {
  781. { "type", iwinfo_L_type },
  782. { "__gc", iwinfo_L__gc },
  783. { NULL, NULL }
  784. };
  785. LUALIB_API int luaopen_iwinfo(lua_State *L) {
  786. luaL_register(L, IWINFO_META, R_common);
  787. #ifdef USE_WL
  788. luaL_newmetatable(L, IWINFO_WL_META);
  789. luaL_register(L, NULL, R_common);
  790. luaL_register(L, NULL, R_wl);
  791. lua_pushvalue(L, -1);
  792. lua_setfield(L, -2, "__index");
  793. lua_setfield(L, -2, "wl");
  794. #endif
  795. #ifdef USE_MADWIFI
  796. luaL_newmetatable(L, IWINFO_MADWIFI_META);
  797. luaL_register(L, NULL, R_common);
  798. luaL_register(L, NULL, R_madwifi);
  799. lua_pushvalue(L, -1);
  800. lua_setfield(L, -2, "__index");
  801. lua_setfield(L, -2, "madwifi");
  802. #endif
  803. #ifdef USE_NL80211
  804. luaL_newmetatable(L, IWINFO_NL80211_META);
  805. luaL_register(L, NULL, R_common);
  806. luaL_register(L, NULL, R_nl80211);
  807. lua_pushvalue(L, -1);
  808. lua_setfield(L, -2, "__index");
  809. lua_setfield(L, -2, "nl80211");
  810. #endif
  811. #ifdef USE_WEXT
  812. luaL_newmetatable(L, IWINFO_WEXT_META);
  813. luaL_register(L, NULL, R_common);
  814. luaL_register(L, NULL, R_wext);
  815. lua_pushvalue(L, -1);
  816. lua_setfield(L, -2, "__index");
  817. lua_setfield(L, -2, "wext");
  818. #endif
  819. return 1;
  820. }