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);
27 if( madwifi_probe(ifname) )
28 lua_pushstring(L, "madwifi");
33 if( wl_probe(ifname) )
34 lua_pushstring(L, "wl");
38 if( wext_probe(ifname) )
39 lua_pushstring(L, "wext");
47 /* Wrapper for assoclist */
48 static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
51 char rv[IWINFO_BUFSIZE];
53 const char *ifname = luaL_checkstring(L, 1);
54 struct iwinfo_assoclist_entry *e;
57 memset(rv, 0, sizeof(rv));
59 if( !(*func)(ifname, rv, &len) )
61 for( i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry) )
63 e = (struct iwinfo_assoclist_entry *) &rv[i];
65 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
66 e->mac[0], e->mac[1], e->mac[2],
67 e->mac[3], e->mac[4], e->mac[5]);
71 lua_pushnumber(L, e->signal);
72 lua_setfield(L, -2, "signal");
74 lua_pushnumber(L, e->noise);
75 lua_setfield(L, -2, "noise");
77 lua_setfield(L, -2, macstr);
84 /* Wrapper for tx power list */
85 static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
88 char rv[IWINFO_BUFSIZE];
89 const char *ifname = luaL_checkstring(L, 1);
90 struct iwinfo_txpwrlist_entry *e;
93 memset(rv, 0, sizeof(rv));
95 if( !(*func)(ifname, rv, &len) )
97 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++ )
99 e = (struct iwinfo_txpwrlist_entry *) &rv[i];
103 lua_pushnumber(L, e->mw);
104 lua_setfield(L, -2, "mw");
106 lua_pushnumber(L, e->dbm);
107 lua_setfield(L, -2, "dbm");
109 lua_rawseti(L, -2, x);
116 /* Wrapper for scan list */
117 static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
120 char rv[IWINFO_BUFSIZE];
122 const char *ifname = luaL_checkstring(L, 1);
123 struct iwinfo_scanlist_entry *e;
126 memset(rv, 0, sizeof(rv));
128 if( !(*func)(ifname, rv, &len) )
130 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++ )
132 e = (struct iwinfo_scanlist_entry *) &rv[i];
137 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
138 e->mac[0], e->mac[1], e->mac[2],
139 e->mac[3], e->mac[4], e->mac[5]);
141 lua_pushstring(L, macstr);
142 lua_setfield(L, -2, "bssid");
147 lua_pushstring(L, (char *) e->ssid);
148 lua_setfield(L, -2, "ssid");
152 lua_pushinteger(L, e->channel);
153 lua_setfield(L, -2, "channel");
156 lua_pushstring(L, (char *) e->mode);
157 lua_setfield(L, -2, "mode");
159 /* Quality, Signal */
160 lua_pushinteger(L, e->quality);
161 lua_setfield(L, -2, "quality");
163 lua_pushinteger(L, e->quality_max);
164 lua_setfield(L, -2, "quality_max");
166 lua_pushnumber(L, (e->signal - 0x100));
167 lua_setfield(L, -2, "signal");
170 lua_pushboolean(L, (!e->crypto.wpa_version && e->crypto.enabled));
171 lua_setfield(L, -2, "wep");
173 if( e->crypto.wpa_version )
175 lua_pushinteger(L, e->crypto.wpa_version);
176 lua_setfield(L, -2, "wpa");
179 for( j = 0, y = 1; j < IW_IE_CYPHER_NUM; j++ )
181 if( e->crypto.group_ciphers & (1<<j) )
183 lua_pushstring(L, iw_ie_cypher_name[j]);
184 lua_rawseti(L, -2, y++);
187 lua_setfield(L, -2, "group_ciphers");
190 for( j = 0, y = 1; j < IW_IE_CYPHER_NUM; j++ )
192 if( e->crypto.pair_ciphers & (1<<j) )
194 lua_pushstring(L, iw_ie_cypher_name[j]);
195 lua_rawseti(L, -2, y++);
198 lua_setfield(L, -2, "pair_ciphers");
201 for( j = 0, y = 1; j < IW_IE_KEY_MGMT_NUM; j++ )
203 if( e->crypto.auth_suites & (1<<j) )
205 lua_pushstring(L, iw_ie_key_mgmt_name[j]);
206 lua_rawseti(L, -2, y++);
209 lua_setfield(L, -2, "auth_suites");
212 lua_rawseti(L, -2, x);
219 /* Wrapper for frequency list */
220 static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *))
223 char rv[IWINFO_BUFSIZE];
224 const char *ifname = luaL_checkstring(L, 1);
225 struct iwinfo_freqlist_entry *e;
228 memset(rv, 0, sizeof(rv));
230 if( !(*func)(ifname, rv, &len) )
232 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++ )
234 e = (struct iwinfo_freqlist_entry *) &rv[i];
239 lua_pushinteger(L, e->mhz);
240 lua_setfield(L, -2, "mhz");
243 lua_pushinteger(L, e->channel);
244 lua_setfield(L, -2, "channel");
246 lua_rawseti(L, -2, x);
255 LUA_WRAP_INT(wl,channel)
256 LUA_WRAP_INT(wl,frequency)
257 LUA_WRAP_INT(wl,bitrate)
258 LUA_WRAP_INT(wl,signal)
259 LUA_WRAP_INT(wl,noise)
260 LUA_WRAP_INT(wl,quality)
261 LUA_WRAP_INT(wl,quality_max)
262 LUA_WRAP_INT(wl,mbssid_support)
263 LUA_WRAP_STRING(wl,mode)
264 LUA_WRAP_STRING(wl,ssid)
265 LUA_WRAP_STRING(wl,bssid)
266 LUA_WRAP_STRING(wl,enctype)
267 LUA_WRAP_LIST(wl,assoclist)
268 LUA_WRAP_LIST(wl,txpwrlist)
269 LUA_WRAP_LIST(wl,scanlist)
270 LUA_WRAP_LIST(wl,freqlist)
275 LUA_WRAP_INT(madwifi,channel)
276 LUA_WRAP_INT(madwifi,frequency)
277 LUA_WRAP_INT(madwifi,bitrate)
278 LUA_WRAP_INT(madwifi,signal)
279 LUA_WRAP_INT(madwifi,noise)
280 LUA_WRAP_INT(madwifi,quality)
281 LUA_WRAP_INT(madwifi,quality_max)
282 LUA_WRAP_INT(madwifi,mbssid_support)
283 LUA_WRAP_STRING(madwifi,mode)
284 LUA_WRAP_STRING(madwifi,ssid)
285 LUA_WRAP_STRING(madwifi,bssid)
286 LUA_WRAP_STRING(madwifi,enctype)
287 LUA_WRAP_LIST(madwifi,assoclist)
288 LUA_WRAP_LIST(madwifi,txpwrlist)
289 LUA_WRAP_LIST(madwifi,scanlist)
290 LUA_WRAP_LIST(madwifi,freqlist)
294 LUA_WRAP_INT(wext,channel)
295 LUA_WRAP_INT(wext,frequency)
296 LUA_WRAP_INT(wext,bitrate)
297 LUA_WRAP_INT(wext,signal)
298 LUA_WRAP_INT(wext,noise)
299 LUA_WRAP_INT(wext,quality)
300 LUA_WRAP_INT(wext,quality_max)
301 LUA_WRAP_INT(wext,mbssid_support)
302 LUA_WRAP_STRING(wext,mode)
303 LUA_WRAP_STRING(wext,ssid)
304 LUA_WRAP_STRING(wext,bssid)
305 LUA_WRAP_STRING(wext,enctype)
306 LUA_WRAP_LIST(wext,assoclist)
307 LUA_WRAP_LIST(wext,txpwrlist)
308 LUA_WRAP_LIST(wext,scanlist)
309 LUA_WRAP_LIST(wext,freqlist)
313 static const luaL_reg R_wl[] = {
315 LUA_REG(wl,frequency),
320 LUA_REG(wl,quality_max),
325 LUA_REG(wl,assoclist),
326 LUA_REG(wl,txpwrlist),
327 LUA_REG(wl,scanlist),
328 LUA_REG(wl,freqlist),
329 LUA_REG(wl,mbssid_support),
336 static const luaL_reg R_madwifi[] = {
337 LUA_REG(madwifi,channel),
338 LUA_REG(madwifi,frequency),
339 LUA_REG(madwifi,bitrate),
340 LUA_REG(madwifi,signal),
341 LUA_REG(madwifi,noise),
342 LUA_REG(madwifi,quality),
343 LUA_REG(madwifi,quality_max),
344 LUA_REG(madwifi,mode),
345 LUA_REG(madwifi,ssid),
346 LUA_REG(madwifi,bssid),
347 LUA_REG(madwifi,enctype),
348 LUA_REG(madwifi,assoclist),
349 LUA_REG(madwifi,txpwrlist),
350 LUA_REG(madwifi,scanlist),
351 LUA_REG(madwifi,freqlist),
352 LUA_REG(madwifi,mbssid_support),
358 static const luaL_reg R_wext[] = {
359 LUA_REG(wext,channel),
360 LUA_REG(wext,frequency),
361 LUA_REG(wext,bitrate),
362 LUA_REG(wext,signal),
364 LUA_REG(wext,quality),
365 LUA_REG(wext,quality_max),
369 LUA_REG(wext,enctype),
370 LUA_REG(wext,assoclist),
371 LUA_REG(wext,txpwrlist),
372 LUA_REG(wext,scanlist),
373 LUA_REG(wext,freqlist),
374 LUA_REG(wext,mbssid_support),
379 static const luaL_reg R_common[] = {
380 { "type", iwinfo_L_type },
385 LUALIB_API int luaopen_iwinfo(lua_State *L) {
386 luaL_register(L, IWINFO_META, R_common);
389 luaL_newmetatable(L, IWINFO_WL_META);
390 luaL_register(L, NULL, R_wl);
391 lua_pushvalue(L, -1);
392 lua_setfield(L, -2, "__index");
393 lua_setfield(L, -2, "wl");
397 luaL_newmetatable(L, IWINFO_MADWIFI_META);
398 luaL_register(L, NULL, R_madwifi);
399 lua_pushvalue(L, -1);
400 lua_setfield(L, -2, "__index");
401 lua_setfield(L, -2, "madwifi");
404 luaL_newmetatable(L, IWINFO_WEXT_META);
405 luaL_register(L, NULL, R_wext);
406 lua_pushvalue(L, -1);
407 lua_setfield(L, -2, "__index");
408 lua_setfield(L, -2, "wext");