2 * iwinfo - Wireless Information Library - Lua Bindings
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
19 #include "iwinfo/lua.h"
23 static int iwinfo_L_type(lua_State *L)
25 const char *ifname = luaL_checkstring(L, 1);
26 const char *type = iwinfo_type(ifname);
29 lua_pushstring(L, type);
36 /* Shutdown backends */
37 static int iwinfo_L__gc(lua_State *L)
44 * Build a short textual description of the crypto info
47 static char * iwinfo_crypto_print_ciphers(int ciphers)
49 static char str[128] = { 0 };
52 if (ciphers & IWINFO_CIPHER_WEP40)
53 pos += sprintf(pos, "WEP-40, ");
55 if (ciphers & IWINFO_CIPHER_WEP104)
56 pos += sprintf(pos, "WEP-104, ");
58 if (ciphers & IWINFO_CIPHER_TKIP)
59 pos += sprintf(pos, "TKIP, ");
61 if (ciphers & IWINFO_CIPHER_CCMP)
62 pos += sprintf(pos, "CCMP, ");
64 if (ciphers & IWINFO_CIPHER_WRAP)
65 pos += sprintf(pos, "WRAP, ");
67 if (ciphers & IWINFO_CIPHER_AESOCB)
68 pos += sprintf(pos, "AES-OCB, ");
70 if (ciphers & IWINFO_CIPHER_CKIP)
71 pos += sprintf(pos, "CKIP, ");
73 if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
74 pos += sprintf(pos, "NONE, ");
81 static char * iwinfo_crypto_print_suites(int suites)
83 static char str[64] = { 0 };
86 if (suites & IWINFO_KMGMT_PSK)
87 pos += sprintf(pos, "PSK/");
89 if (suites & IWINFO_KMGMT_8021x)
90 pos += sprintf(pos, "802.1X/");
92 if (!suites || (suites & IWINFO_KMGMT_NONE))
93 pos += sprintf(pos, "NONE/");
100 static char * iwinfo_crypto_desc(struct iwinfo_crypto_entry *c)
102 static char desc[512] = { 0 };
109 if (c->auth_algs && !c->wpa_version)
111 if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
112 (c->auth_algs & IWINFO_AUTH_SHARED))
114 sprintf(desc, "WEP Open/Shared (%s)",
115 iwinfo_crypto_print_ciphers(c->pair_ciphers));
117 else if (c->auth_algs & IWINFO_AUTH_OPEN)
119 sprintf(desc, "WEP Open System (%s)",
120 iwinfo_crypto_print_ciphers(c->pair_ciphers));
122 else if (c->auth_algs & IWINFO_AUTH_SHARED)
124 sprintf(desc, "WEP Shared Auth (%s)",
125 iwinfo_crypto_print_ciphers(c->pair_ciphers));
130 else if (c->wpa_version)
132 switch (c->wpa_version) {
134 sprintf(desc, "mixed WPA/WPA2 %s (%s)",
135 iwinfo_crypto_print_suites(c->auth_suites),
136 iwinfo_crypto_print_ciphers(
137 c->pair_ciphers & c->group_ciphers));
141 sprintf(desc, "WPA2 %s (%s)",
142 iwinfo_crypto_print_suites(c->auth_suites),
143 iwinfo_crypto_print_ciphers(
144 c->pair_ciphers & c->group_ciphers));
148 sprintf(desc, "WPA %s (%s)",
149 iwinfo_crypto_print_suites(c->auth_suites),
150 iwinfo_crypto_print_ciphers(
151 c->pair_ciphers & c->group_ciphers));
157 sprintf(desc, "None");
162 sprintf(desc, "None");
167 sprintf(desc, "Unknown");
173 /* Build Lua table from crypto data */
174 static void iwinfo_L_cryptotable(lua_State *L, struct iwinfo_crypto_entry *c)
180 lua_pushboolean(L, c->enabled);
181 lua_setfield(L, -2, "enabled");
183 lua_pushstring(L, iwinfo_crypto_desc(c));
184 lua_setfield(L, -2, "description");
186 lua_pushboolean(L, (c->enabled && !c->wpa_version));
187 lua_setfield(L, -2, "wep");
189 lua_pushinteger(L, c->wpa_version);
190 lua_setfield(L, -2, "wpa");
193 for (i = 0, j = 1; i < 8; i++)
195 if (c->pair_ciphers & (1 << i))
197 lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
198 lua_rawseti(L, -2, j++);
201 lua_setfield(L, -2, "pair_ciphers");
204 for (i = 0, j = 1; i < 8; i++)
206 if (c->group_ciphers & (1 << i))
208 lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
209 lua_rawseti(L, -2, j++);
212 lua_setfield(L, -2, "group_ciphers");
215 for (i = 0, j = 1; i < 8; i++)
217 if (c->auth_suites & (1 << i))
219 lua_pushstring(L, IWINFO_KMGMT_NAMES[i]);
220 lua_rawseti(L, -2, j++);
223 lua_setfield(L, -2, "auth_suites");
226 for (i = 0, j = 1; i < 8; i++)
228 if (c->auth_algs & (1 << i))
230 lua_pushstring(L, IWINFO_AUTH_NAMES[i]);
231 lua_rawseti(L, -2, j++);
234 lua_setfield(L, -2, "auth_algs");
238 /* Wrapper for assoclist */
239 static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
242 char rv[IWINFO_BUFSIZE];
244 const char *ifname = luaL_checkstring(L, 1);
245 struct iwinfo_assoclist_entry *e;
248 memset(rv, 0, sizeof(rv));
250 if (!(*func)(ifname, rv, &len))
252 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
254 e = (struct iwinfo_assoclist_entry *) &rv[i];
256 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
257 e->mac[0], e->mac[1], e->mac[2],
258 e->mac[3], e->mac[4], e->mac[5]);
262 lua_pushnumber(L, e->signal);
263 lua_setfield(L, -2, "signal");
265 lua_pushnumber(L, e->noise);
266 lua_setfield(L, -2, "noise");
268 lua_setfield(L, -2, macstr);
275 /* Wrapper for tx power list */
276 static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
279 char rv[IWINFO_BUFSIZE];
280 const char *ifname = luaL_checkstring(L, 1);
281 struct iwinfo_txpwrlist_entry *e;
284 memset(rv, 0, sizeof(rv));
286 if (!(*func)(ifname, rv, &len))
288 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++)
290 e = (struct iwinfo_txpwrlist_entry *) &rv[i];
294 lua_pushnumber(L, e->mw);
295 lua_setfield(L, -2, "mw");
297 lua_pushnumber(L, e->dbm);
298 lua_setfield(L, -2, "dbm");
300 lua_rawseti(L, -2, x);
307 /* Wrapper for scan list */
308 static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
311 char rv[IWINFO_BUFSIZE];
313 const char *ifname = luaL_checkstring(L, 1);
314 struct iwinfo_scanlist_entry *e;
317 memset(rv, 0, sizeof(rv));
319 if (!(*func)(ifname, rv, &len))
321 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
323 e = (struct iwinfo_scanlist_entry *) &rv[i];
328 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
329 e->mac[0], e->mac[1], e->mac[2],
330 e->mac[3], e->mac[4], e->mac[5]);
332 lua_pushstring(L, macstr);
333 lua_setfield(L, -2, "bssid");
338 lua_pushstring(L, (char *) e->ssid);
339 lua_setfield(L, -2, "ssid");
343 lua_pushinteger(L, e->channel);
344 lua_setfield(L, -2, "channel");
347 lua_pushstring(L, (char *) e->mode);
348 lua_setfield(L, -2, "mode");
350 /* Quality, Signal */
351 lua_pushinteger(L, e->quality);
352 lua_setfield(L, -2, "quality");
354 lua_pushinteger(L, e->quality_max);
355 lua_setfield(L, -2, "quality_max");
357 lua_pushnumber(L, (e->signal - 0x100));
358 lua_setfield(L, -2, "signal");
361 iwinfo_L_cryptotable(L, &e->crypto);
362 lua_setfield(L, -2, "encryption");
364 lua_rawseti(L, -2, x);
371 /* Wrapper for frequency list */
372 static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *))
375 char rv[IWINFO_BUFSIZE];
376 const char *ifname = luaL_checkstring(L, 1);
377 struct iwinfo_freqlist_entry *e;
380 memset(rv, 0, sizeof(rv));
382 if (!(*func)(ifname, rv, &len))
384 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++)
386 e = (struct iwinfo_freqlist_entry *) &rv[i];
391 lua_pushinteger(L, e->mhz);
392 lua_setfield(L, -2, "mhz");
395 lua_pushinteger(L, e->channel);
396 lua_setfield(L, -2, "channel");
398 /* Restricted (DFS/TPC/Radar) */
399 lua_pushboolean(L, e->restricted);
400 lua_setfield(L, -2, "restricted");
402 lua_rawseti(L, -2, x);
409 /* Wrapper for crypto settings */
410 static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *))
412 const char *ifname = luaL_checkstring(L, 1);
413 struct iwinfo_crypto_entry c = { 0 };
415 if (!(*func)(ifname, (char *)&c))
417 iwinfo_L_cryptotable(L, &c);
425 /* Wrapper for hwmode list */
426 static int iwinfo_L_hwmodelist(lua_State *L, int (*func)(const char *, int *))
428 const char *ifname = luaL_checkstring(L, 1);
431 if (!(*func)(ifname, &hwmodes))
435 lua_pushboolean(L, hwmodes & IWINFO_80211_A);
436 lua_setfield(L, -2, "a");
438 lua_pushboolean(L, hwmodes & IWINFO_80211_B);
439 lua_setfield(L, -2, "b");
441 lua_pushboolean(L, hwmodes & IWINFO_80211_G);
442 lua_setfield(L, -2, "g");
444 lua_pushboolean(L, hwmodes & IWINFO_80211_N);
445 lua_setfield(L, -2, "n");
454 /* Wrapper for mbbsid_support */
455 static int iwinfo_L_mbssid_support(lua_State *L, int (*func)(const char *, int *))
457 const char *ifname = luaL_checkstring(L, 1);
460 if (!(*func)(ifname, &support))
462 lua_pushboolean(L, support);
470 /* Wrapper for country list */
471 static char * iwinfo_L_country_lookup(char *buf, int len, int iso3166)
474 struct iwinfo_country_entry *c;
476 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
478 c = (struct iwinfo_country_entry *) &buf[i];
480 if (c->iso3166 == iso3166)
487 static int iwinfo_L_countrylist(lua_State *L, int (*func)(const char *, char *, int *))
490 char rv[IWINFO_BUFSIZE], alpha2[3];
492 const char *ifname = luaL_checkstring(L, 1);
493 const struct iwinfo_iso3166_label *l;
496 memset(rv, 0, sizeof(rv));
498 if (!(*func)(ifname, rv, &len))
500 for (l = IWINFO_ISO3166_NAMES, j = 1; l->iso3166; l++)
502 if ((ccode = iwinfo_L_country_lookup(rv, len, l->iso3166)) != NULL)
504 sprintf(alpha2, "%c%c",
505 (l->iso3166 / 256), (l->iso3166 % 256));
509 lua_pushstring(L, alpha2);
510 lua_setfield(L, -2, "alpha2");
512 lua_pushstring(L, ccode);
513 lua_setfield(L, -2, "ccode");
515 lua_pushstring(L, l->name);
516 lua_setfield(L, -2, "name");
518 lua_rawseti(L, -2, j++);
529 LUA_WRAP_INT(wl,channel)
530 LUA_WRAP_INT(wl,frequency)
531 LUA_WRAP_INT(wl,txpower)
532 LUA_WRAP_INT(wl,bitrate)
533 LUA_WRAP_INT(wl,signal)
534 LUA_WRAP_INT(wl,noise)
535 LUA_WRAP_INT(wl,quality)
536 LUA_WRAP_INT(wl,quality_max)
537 LUA_WRAP_STRING(wl,mode)
538 LUA_WRAP_STRING(wl,ssid)
539 LUA_WRAP_STRING(wl,bssid)
540 LUA_WRAP_STRING(wl,country)
541 LUA_WRAP_LIST(wl,assoclist)
542 LUA_WRAP_LIST(wl,txpwrlist)
543 LUA_WRAP_LIST(wl,scanlist)
544 LUA_WRAP_LIST(wl,freqlist)
545 LUA_WRAP_LIST(wl,countrylist)
546 LUA_WRAP_LIST(wl,hwmodelist)
547 LUA_WRAP_LIST(wl,encryption)
548 LUA_WRAP_LIST(wl,mbssid_support)
553 LUA_WRAP_INT(madwifi,channel)
554 LUA_WRAP_INT(madwifi,frequency)
555 LUA_WRAP_INT(madwifi,txpower)
556 LUA_WRAP_INT(madwifi,bitrate)
557 LUA_WRAP_INT(madwifi,signal)
558 LUA_WRAP_INT(madwifi,noise)
559 LUA_WRAP_INT(madwifi,quality)
560 LUA_WRAP_INT(madwifi,quality_max)
561 LUA_WRAP_STRING(madwifi,mode)
562 LUA_WRAP_STRING(madwifi,ssid)
563 LUA_WRAP_STRING(madwifi,bssid)
564 LUA_WRAP_STRING(madwifi,country)
565 LUA_WRAP_LIST(madwifi,assoclist)
566 LUA_WRAP_LIST(madwifi,txpwrlist)
567 LUA_WRAP_LIST(madwifi,scanlist)
568 LUA_WRAP_LIST(madwifi,freqlist)
569 LUA_WRAP_LIST(madwifi,countrylist)
570 LUA_WRAP_LIST(madwifi,hwmodelist)
571 LUA_WRAP_LIST(madwifi,encryption)
572 LUA_WRAP_LIST(madwifi,mbssid_support)
577 LUA_WRAP_INT(nl80211,channel)
578 LUA_WRAP_INT(nl80211,frequency)
579 LUA_WRAP_INT(nl80211,txpower)
580 LUA_WRAP_INT(nl80211,bitrate)
581 LUA_WRAP_INT(nl80211,signal)
582 LUA_WRAP_INT(nl80211,noise)
583 LUA_WRAP_INT(nl80211,quality)
584 LUA_WRAP_INT(nl80211,quality_max)
585 LUA_WRAP_STRING(nl80211,mode)
586 LUA_WRAP_STRING(nl80211,ssid)
587 LUA_WRAP_STRING(nl80211,bssid)
588 LUA_WRAP_STRING(nl80211,country)
589 LUA_WRAP_LIST(nl80211,assoclist)
590 LUA_WRAP_LIST(nl80211,txpwrlist)
591 LUA_WRAP_LIST(nl80211,scanlist)
592 LUA_WRAP_LIST(nl80211,freqlist)
593 LUA_WRAP_LIST(nl80211,countrylist)
594 LUA_WRAP_LIST(nl80211,hwmodelist)
595 LUA_WRAP_LIST(nl80211,encryption)
596 LUA_WRAP_LIST(nl80211,mbssid_support)
600 LUA_WRAP_INT(wext,channel)
601 LUA_WRAP_INT(wext,frequency)
602 LUA_WRAP_INT(wext,txpower)
603 LUA_WRAP_INT(wext,bitrate)
604 LUA_WRAP_INT(wext,signal)
605 LUA_WRAP_INT(wext,noise)
606 LUA_WRAP_INT(wext,quality)
607 LUA_WRAP_INT(wext,quality_max)
608 LUA_WRAP_STRING(wext,mode)
609 LUA_WRAP_STRING(wext,ssid)
610 LUA_WRAP_STRING(wext,bssid)
611 LUA_WRAP_STRING(wext,country)
612 LUA_WRAP_LIST(wext,assoclist)
613 LUA_WRAP_LIST(wext,txpwrlist)
614 LUA_WRAP_LIST(wext,scanlist)
615 LUA_WRAP_LIST(wext,freqlist)
616 LUA_WRAP_LIST(wext,countrylist)
617 LUA_WRAP_LIST(wext,hwmodelist)
618 LUA_WRAP_LIST(wext,encryption)
619 LUA_WRAP_LIST(wext,mbssid_support)
623 static const luaL_reg R_wl[] = {
625 LUA_REG(wl,frequency),
631 LUA_REG(wl,quality_max),
636 LUA_REG(wl,assoclist),
637 LUA_REG(wl,txpwrlist),
638 LUA_REG(wl,scanlist),
639 LUA_REG(wl,freqlist),
640 LUA_REG(wl,countrylist),
641 LUA_REG(wl,hwmodelist),
642 LUA_REG(wl,encryption),
643 LUA_REG(wl,mbssid_support),
650 static const luaL_reg R_madwifi[] = {
651 LUA_REG(madwifi,channel),
652 LUA_REG(madwifi,frequency),
653 LUA_REG(madwifi,txpower),
654 LUA_REG(madwifi,bitrate),
655 LUA_REG(madwifi,signal),
656 LUA_REG(madwifi,noise),
657 LUA_REG(madwifi,quality),
658 LUA_REG(madwifi,quality_max),
659 LUA_REG(madwifi,mode),
660 LUA_REG(madwifi,ssid),
661 LUA_REG(madwifi,bssid),
662 LUA_REG(madwifi,country),
663 LUA_REG(madwifi,assoclist),
664 LUA_REG(madwifi,txpwrlist),
665 LUA_REG(madwifi,scanlist),
666 LUA_REG(madwifi,freqlist),
667 LUA_REG(madwifi,countrylist),
668 LUA_REG(madwifi,hwmodelist),
669 LUA_REG(madwifi,encryption),
670 LUA_REG(madwifi,mbssid_support),
677 static const luaL_reg R_nl80211[] = {
678 LUA_REG(nl80211,channel),
679 LUA_REG(nl80211,frequency),
680 LUA_REG(nl80211,txpower),
681 LUA_REG(nl80211,bitrate),
682 LUA_REG(nl80211,signal),
683 LUA_REG(nl80211,noise),
684 LUA_REG(nl80211,quality),
685 LUA_REG(nl80211,quality_max),
686 LUA_REG(nl80211,mode),
687 LUA_REG(nl80211,ssid),
688 LUA_REG(nl80211,bssid),
689 LUA_REG(nl80211,country),
690 LUA_REG(nl80211,assoclist),
691 LUA_REG(nl80211,txpwrlist),
692 LUA_REG(nl80211,scanlist),
693 LUA_REG(nl80211,freqlist),
694 LUA_REG(nl80211,countrylist),
695 LUA_REG(nl80211,hwmodelist),
696 LUA_REG(nl80211,encryption),
697 LUA_REG(nl80211,mbssid_support),
703 static const luaL_reg R_wext[] = {
704 LUA_REG(wext,channel),
705 LUA_REG(wext,frequency),
706 LUA_REG(wext,txpower),
707 LUA_REG(wext,bitrate),
708 LUA_REG(wext,signal),
710 LUA_REG(wext,quality),
711 LUA_REG(wext,quality_max),
715 LUA_REG(wext,country),
716 LUA_REG(wext,assoclist),
717 LUA_REG(wext,txpwrlist),
718 LUA_REG(wext,scanlist),
719 LUA_REG(wext,freqlist),
720 LUA_REG(wext,countrylist),
721 LUA_REG(wext,hwmodelist),
722 LUA_REG(wext,encryption),
723 LUA_REG(wext,mbssid_support),
728 static const luaL_reg R_common[] = {
729 { "type", iwinfo_L_type },
730 { "__gc", iwinfo_L__gc },
735 LUALIB_API int luaopen_iwinfo(lua_State *L) {
736 luaL_register(L, IWINFO_META, R_common);
739 luaL_newmetatable(L, IWINFO_WL_META);
740 luaL_register(L, NULL, R_wl);
741 lua_pushvalue(L, -1);
742 lua_setfield(L, -2, "__index");
743 lua_setfield(L, -2, "wl");
747 luaL_newmetatable(L, IWINFO_MADWIFI_META);
748 luaL_register(L, NULL, R_madwifi);
749 lua_pushvalue(L, -1);
750 lua_setfield(L, -2, "__index");
751 lua_setfield(L, -2, "madwifi");
755 luaL_newmetatable(L, IWINFO_NL80211_META);
756 luaL_register(L, NULL, R_nl80211);
757 lua_pushvalue(L, -1);
758 lua_setfield(L, -2, "__index");
759 lua_setfield(L, -2, "nl80211");
762 luaL_newmetatable(L, IWINFO_WEXT_META);
763 luaL_register(L, NULL, R_wext);
764 lua_pushvalue(L, -1);
765 lua_setfield(L, -2, "__index");
766 lua_setfield(L, -2, "wext");