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_lualib.h"
22 static int iwinfo_L_type(lua_State *L)
24 const char *ifname = luaL_checkstring(L, 1);
26 if( wl_probe(ifname) )
27 lua_pushstring(L, "wl");
29 else if( madwifi_probe(ifname) )
30 lua_pushstring(L, "madwifi");
32 else if( wext_probe(ifname) )
33 lua_pushstring(L, "wext");
41 /* Wrapper for assoclist */
42 static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
45 char rv[IWINFO_BUFSIZE];
47 const char *ifname = luaL_checkstring(L, 1);
48 struct iwinfo_assoclist_entry *e;
51 memset(rv, 0, sizeof(rv));
53 if( !(*func)(ifname, rv, &len) )
55 for( i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry) )
57 e = (struct iwinfo_assoclist_entry *) &rv[i];
59 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
60 e->mac[0], e->mac[1], e->mac[2],
61 e->mac[3], e->mac[4], e->mac[5]);
65 lua_pushnumber(L, e->signal);
66 lua_setfield(L, -2, "signal");
68 lua_pushnumber(L, e->noise);
69 lua_setfield(L, -2, "noise");
71 lua_setfield(L, -2, macstr);
78 /* Wrapper for tx power list */
79 static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
82 char rv[IWINFO_BUFSIZE];
83 const char *ifname = luaL_checkstring(L, 1);
84 struct iwinfo_txpwrlist_entry *e;
87 memset(rv, 0, sizeof(rv));
89 if( !(*func)(ifname, rv, &len) )
91 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++ )
93 e = (struct iwinfo_txpwrlist_entry *) &rv[i];
97 lua_pushnumber(L, e->mw);
98 lua_setfield(L, -2, "mw");
100 lua_pushnumber(L, e->dbm);
101 lua_setfield(L, -2, "dbm");
103 lua_rawseti(L, -2, x);
110 /* Wrapper for scan list */
111 static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
114 char rv[IWINFO_BUFSIZE];
116 const char *ifname = luaL_checkstring(L, 1);
117 struct iwinfo_scanlist_entry *e;
120 memset(rv, 0, sizeof(rv));
122 if( !(*func)(ifname, rv, &len) )
124 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++ )
126 e = (struct iwinfo_scanlist_entry *) &rv[i];
131 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
132 e->mac[0], e->mac[1], e->mac[2],
133 e->mac[3], e->mac[4], e->mac[5]);
135 lua_pushstring(L, macstr);
136 lua_setfield(L, -2, "bssid");
141 lua_pushstring(L, (char *) e->ssid);
142 lua_setfield(L, -2, "ssid");
146 lua_pushinteger(L, e->channel);
147 lua_setfield(L, -2, "channel");
150 lua_pushstring(L, (char *) e->mode);
151 lua_setfield(L, -2, "mode");
154 lua_pushinteger(L, e->crypto.wpa_version);
155 lua_setfield(L, -2, "wpa");
157 lua_pushboolean(L, (!e->crypto.wpa_version && e->crypto.enabled));
158 lua_setfield(L, -2, "wep");
161 for( j = 0, y = 1; j < 8; j++ )
163 if( e->crypto.group_ciphers & (1<<j) )
165 lua_pushstring(L, (j < IW_IE_CYPHER_NUM)
166 ? iw_ie_cypher_name[j] : "Proprietary");
168 lua_rawseti(L, -2, y++);
171 lua_setfield(L, -2, "group_ciphers");
174 for( j = 0, y = 1; j < 8; j++ )
176 if( e->crypto.pair_ciphers & (1<<j) )
178 lua_pushstring(L, (j < IW_IE_CYPHER_NUM)
179 ? iw_ie_cypher_name[j] : "Proprietary");
181 lua_rawseti(L, -2, y++);
184 lua_setfield(L, -2, "pair_ciphers");
187 for( j = 0, y = 1; j < 8; j++ )
189 if( e->crypto.auth_suites & (1<<j) )
191 lua_pushstring(L, (j < IW_IE_KEY_MGMT_NUM)
192 ? iw_ie_key_mgmt_name[j] : "Proprietary");
194 lua_rawseti(L, -2, y++);
197 lua_setfield(L, -2, "auth_suites");
199 lua_rawseti(L, -2, x);
208 LUA_WRAP_INT(wl,channel)
209 LUA_WRAP_INT(wl,frequency)
210 LUA_WRAP_INT(wl,bitrate)
211 LUA_WRAP_INT(wl,signal)
212 LUA_WRAP_INT(wl,noise)
213 LUA_WRAP_INT(wl,quality)
214 LUA_WRAP_INT(wl,quality_max)
215 LUA_WRAP_INT(wl,mbssid_support)
216 LUA_WRAP_STRING(wl,mode)
217 LUA_WRAP_STRING(wl,ssid)
218 LUA_WRAP_STRING(wl,bssid)
219 LUA_WRAP_STRING(wl,enctype)
220 LUA_WRAP_LIST(wl,assoclist)
221 LUA_WRAP_LIST(wl,txpwrlist)
222 LUA_WRAP_LIST(wl,scanlist)
225 LUA_WRAP_INT(madwifi,channel)
226 LUA_WRAP_INT(madwifi,frequency)
227 LUA_WRAP_INT(madwifi,bitrate)
228 LUA_WRAP_INT(madwifi,signal)
229 LUA_WRAP_INT(madwifi,noise)
230 LUA_WRAP_INT(madwifi,quality)
231 LUA_WRAP_INT(madwifi,quality_max)
232 LUA_WRAP_INT(madwifi,mbssid_support)
233 LUA_WRAP_STRING(madwifi,mode)
234 LUA_WRAP_STRING(madwifi,ssid)
235 LUA_WRAP_STRING(madwifi,bssid)
236 LUA_WRAP_STRING(madwifi,enctype)
237 LUA_WRAP_LIST(madwifi,assoclist)
238 LUA_WRAP_LIST(madwifi,txpwrlist)
239 LUA_WRAP_LIST(madwifi,scanlist)
242 LUA_WRAP_INT(wext,channel)
243 LUA_WRAP_INT(wext,frequency)
244 LUA_WRAP_INT(wext,bitrate)
245 LUA_WRAP_INT(wext,signal)
246 LUA_WRAP_INT(wext,noise)
247 LUA_WRAP_INT(wext,quality)
248 LUA_WRAP_INT(wext,quality_max)
249 LUA_WRAP_INT(wext,mbssid_support)
250 LUA_WRAP_STRING(wext,mode)
251 LUA_WRAP_STRING(wext,ssid)
252 LUA_WRAP_STRING(wext,bssid)
253 LUA_WRAP_STRING(wext,enctype)
254 LUA_WRAP_LIST(wext,assoclist)
255 LUA_WRAP_LIST(wext,txpwrlist)
256 LUA_WRAP_LIST(wext,scanlist)
259 static const luaL_reg R_wl[] = {
261 LUA_REG(wl,frequency),
266 LUA_REG(wl,quality_max),
271 LUA_REG(wl,assoclist),
272 LUA_REG(wl,txpwrlist),
273 LUA_REG(wl,scanlist),
274 LUA_REG(wl,mbssid_support),
279 static const luaL_reg R_madwifi[] = {
280 LUA_REG(madwifi,channel),
281 LUA_REG(madwifi,frequency),
282 LUA_REG(madwifi,bitrate),
283 LUA_REG(madwifi,signal),
284 LUA_REG(madwifi,noise),
285 LUA_REG(madwifi,quality),
286 LUA_REG(madwifi,quality_max),
287 LUA_REG(madwifi,mode),
288 LUA_REG(madwifi,ssid),
289 LUA_REG(madwifi,bssid),
290 LUA_REG(madwifi,enctype),
291 LUA_REG(madwifi,assoclist),
292 LUA_REG(madwifi,txpwrlist),
293 LUA_REG(madwifi,scanlist),
294 LUA_REG(madwifi,mbssid_support),
299 static const luaL_reg R_wext[] = {
300 LUA_REG(wext,channel),
301 LUA_REG(wext,frequency),
302 LUA_REG(wext,bitrate),
303 LUA_REG(wext,signal),
305 LUA_REG(wext,quality),
306 LUA_REG(wext,quality_max),
310 LUA_REG(wext,enctype),
311 LUA_REG(wext,assoclist),
312 LUA_REG(wext,txpwrlist),
313 LUA_REG(wext,scanlist),
314 LUA_REG(wext,mbssid_support),
319 static const luaL_reg R_common[] = {
320 { "type", iwinfo_L_type },
325 LUALIB_API int luaopen_iwinfo(lua_State *L) {
326 luaL_register(L, IWINFO_META, R_common);
328 luaL_newmetatable(L, IWINFO_WL_META);
329 luaL_register(L, NULL, R_wl);
330 lua_pushvalue(L, -1);
331 lua_setfield(L, -2, "__index");
332 lua_setfield(L, -2, "wl");
334 luaL_newmetatable(L, IWINFO_MADWIFI_META);
335 luaL_register(L, NULL, R_madwifi);
336 lua_pushvalue(L, -1);
337 lua_setfield(L, -2, "__index");
338 lua_setfield(L, -2, "madwifi");
340 luaL_newmetatable(L, IWINFO_WEXT_META);
341 luaL_register(L, NULL, R_wext);
342 lua_pushvalue(L, -1);
343 lua_setfield(L, -2, "__index");
344 lua_setfield(L, -2, "wext");