/* * iwinfo - Wireless Information Library - Lua Bindings * * Copyright (C) 2009 Jo-Philipp Wich * * The iwinfo library is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * The iwinfo library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with the iwinfo library. If not, see http://www.gnu.org/licenses/. */ #include "iwinfo_lualib.h" /* Determine type */ static int iwinfo_L_type(lua_State *L) { const char *ifname = luaL_checkstring(L, 1); if( wl_probe(ifname) ) lua_pushstring(L, "wl"); else if( madwifi_probe(ifname) ) lua_pushstring(L, "madwifi"); else if( wext_probe(ifname) ) lua_pushstring(L, "wext"); else lua_pushnil(L); return 1; } /* Wrapper for assoclist */ static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *)) { int i, len; char rv[IWINFO_BUFSIZE]; char macstr[18]; const char *ifname = luaL_checkstring(L, 1); struct iwinfo_assoclist_entry *e; lua_newtable(L); memset(rv, 0, sizeof(rv)); if( !(*func)(ifname, rv, &len) ) { for( i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry) ) { e = (struct iwinfo_assoclist_entry *) &rv[i]; sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", e->mac[0], e->mac[1], e->mac[2], e->mac[3], e->mac[4], e->mac[5]); lua_newtable(L); lua_pushnumber(L, e->signal); lua_setfield(L, -2, "signal"); lua_pushnumber(L, e->noise); lua_setfield(L, -2, "noise"); lua_setfield(L, -2, macstr); } } return 1; } /* Wrapper for tx power list */ static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *)) { int i, x, len; char rv[IWINFO_BUFSIZE]; const char *ifname = luaL_checkstring(L, 1); struct iwinfo_txpwrlist_entry *e; lua_newtable(L); memset(rv, 0, sizeof(rv)); if( !(*func)(ifname, rv, &len) ) { for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++ ) { e = (struct iwinfo_txpwrlist_entry *) &rv[i]; lua_newtable(L); lua_pushnumber(L, e->mw); lua_setfield(L, -2, "mw"); lua_pushnumber(L, e->dbm); lua_setfield(L, -2, "dbm"); lua_rawseti(L, -2, x); } } return 1; } /* Wrapper for scan list */ static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *)) { int i, j, x, y, len; char rv[IWINFO_BUFSIZE]; char macstr[18]; const char *ifname = luaL_checkstring(L, 1); struct iwinfo_scanlist_entry *e; lua_newtable(L); memset(rv, 0, sizeof(rv)); if( !(*func)(ifname, rv, &len) ) { for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++ ) { e = (struct iwinfo_scanlist_entry *) &rv[i]; lua_newtable(L); /* BSSID */ sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", e->mac[0], e->mac[1], e->mac[2], e->mac[3], e->mac[4], e->mac[5]); lua_pushstring(L, macstr); lua_setfield(L, -2, "bssid"); /* ESSID */ if( e->ssid[0] ) { lua_pushstring(L, (char *) e->ssid); lua_setfield(L, -2, "ssid"); } /* Channel */ lua_pushinteger(L, e->channel); lua_setfield(L, -2, "channel"); /* Mode */ lua_pushstring(L, (char *) e->mode); lua_setfield(L, -2, "mode"); /* Crypto */ lua_pushinteger(L, e->crypto.wpa_version); lua_setfield(L, -2, "wpa"); lua_pushboolean(L, (!e->crypto.wpa_version && e->crypto.enabled)); lua_setfield(L, -2, "wep"); lua_newtable(L); for( j = 0, y = 1; j < IW_IE_CYPHER_NUM; j++ ) { if( e->crypto.group_ciphers & (1<crypto.pair_ciphers & (1<crypto.auth_suites & (1<