iwinfo_lua.c 22 KB

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