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( nl80211_probe(ifname
) )
28 lua_pushstring(L
, "nl80211");
33 if( madwifi_probe(ifname
) )
34 lua_pushstring(L
, "madwifi");
39 if( wl_probe(ifname
) )
40 lua_pushstring(L
, "wl");
44 if( wext_probe(ifname
) )
45 lua_pushstring(L
, "wext");
54 * Build a short textual description of the crypto info
57 static char * iwinfo_crypto_print_ciphers(int ciphers
)
59 static char str
[128] = { 0 };
62 if( ciphers
& IWINFO_CIPHER_WEP40
)
63 pos
+= sprintf(pos
, "WEP-40, ");
65 if( ciphers
& IWINFO_CIPHER_WEP104
)
66 pos
+= sprintf(pos
, "WEP-104, ");
68 if( ciphers
& IWINFO_CIPHER_TKIP
)
69 pos
+= sprintf(pos
, "TKIP, ");
71 if( ciphers
& IWINFO_CIPHER_CCMP
)
72 pos
+= sprintf(pos
, "CCMP, ");
74 if( ciphers
& IWINFO_CIPHER_WRAP
)
75 pos
+= sprintf(pos
, "WRAP, ");
77 if( ciphers
& IWINFO_CIPHER_AESOCB
)
78 pos
+= sprintf(pos
, "AES-OCB, ");
80 if( ciphers
& IWINFO_CIPHER_CKIP
)
81 pos
+= sprintf(pos
, "CKIP, ");
83 if( !ciphers
|| (ciphers
& IWINFO_CIPHER_NONE
) )
84 pos
+= sprintf(pos
, "NONE, ");
91 static char * iwinfo_crypto_print_suites(int suites
)
93 static char str
[64] = { 0 };
96 if( suites
& IWINFO_KMGMT_PSK
)
97 pos
+= sprintf(pos
, "PSK/");
99 if( suites
& IWINFO_KMGMT_8021x
)
100 pos
+= sprintf(pos
, "802.1X/");
102 if( !suites
|| (suites
& IWINFO_KMGMT_NONE
) )
103 pos
+= sprintf(pos
, "NONE/");
110 static char * iwinfo_crypto_desc(struct iwinfo_crypto_entry
*c
)
112 static char desc
[512] = { 0 };
119 if( c
->auth_algs
&& !c
->wpa_version
)
121 if( (c
->auth_algs
& IWINFO_AUTH_OPEN
) &&
122 (c
->auth_algs
& IWINFO_AUTH_SHARED
) )
124 sprintf(desc
, "WEP Open/Shared (%s)",
125 iwinfo_crypto_print_ciphers(c
->pair_ciphers
));
127 else if( c
->auth_algs
& IWINFO_AUTH_OPEN
)
129 sprintf(desc
, "WEP Open System (%s)",
130 iwinfo_crypto_print_ciphers(c
->pair_ciphers
));
132 else if( c
->auth_algs
& IWINFO_AUTH_SHARED
)
134 sprintf(desc
, "WEP Shared Auth (%s)",
135 iwinfo_crypto_print_ciphers(c
->pair_ciphers
));
140 else if( c
->wpa_version
)
142 switch(c
->wpa_version
) {
144 sprintf(desc
, "mixed WPA/WPA2 %s (%s)",
145 iwinfo_crypto_print_suites(c
->auth_suites
),
146 iwinfo_crypto_print_ciphers(c
->pair_ciphers
));
150 sprintf(desc
, "WPA2 %s (%s)",
151 iwinfo_crypto_print_suites(c
->auth_suites
),
152 iwinfo_crypto_print_ciphers(c
->pair_ciphers
));
156 sprintf(desc
, "WPA %s (%s)",
157 iwinfo_crypto_print_suites(c
->auth_suites
),
158 iwinfo_crypto_print_ciphers(c
->pair_ciphers
));
165 sprintf(desc
, "None");
170 sprintf(desc
, "Unknown");
176 /* Build Lua table from crypto data */
177 static void iwinfo_L_cryptotable(lua_State
*L
, struct iwinfo_crypto_entry
*c
)
183 lua_pushboolean(L
, c
->enabled
);
184 lua_setfield(L
, -2, "enabled");
186 lua_pushstring(L
, iwinfo_crypto_desc(c
));
187 lua_setfield(L
, -2, "description");
189 lua_pushboolean(L
, (c
->enabled
&& !c
->wpa_version
));
190 lua_setfield(L
, -2, "wep");
192 lua_pushinteger(L
, c
->wpa_version
);
193 lua_setfield(L
, -2, "wpa");
196 for( i
= 0, j
= 1; i
< 8; i
++ )
198 if( c
->pair_ciphers
& (1 << i
) )
200 lua_pushstring(L
, IWINFO_CIPHER_NAMES
[i
]);
201 lua_rawseti(L
, -2, j
++);
204 lua_setfield(L
, -2, "pair_ciphers");
207 for( i
= 0, j
= 1; i
< 8; i
++ )
209 if( c
->group_ciphers
& (1 << i
) )
211 lua_pushstring(L
, IWINFO_CIPHER_NAMES
[i
]);
212 lua_rawseti(L
, -2, j
++);
215 lua_setfield(L
, -2, "group_ciphers");
218 for( i
= 0, j
= 1; i
< 8; i
++ )
220 if( c
->auth_suites
& (1 << i
) )
222 lua_pushstring(L
, IWINFO_KMGMT_NAMES
[i
]);
223 lua_rawseti(L
, -2, j
++);
226 lua_setfield(L
, -2, "auth_suites");
229 for( i
= 0, j
= 1; i
< 8; i
++ )
231 if( c
->auth_algs
& (1 << i
) )
233 lua_pushstring(L
, IWINFO_AUTH_NAMES
[i
]);
234 lua_rawseti(L
, -2, j
++);
237 lua_setfield(L
, -2, "auth_algs");
241 /* Wrapper for assoclist */
242 static int iwinfo_L_assoclist(lua_State
*L
, int (*func
)(const char *, char *, int *))
245 char rv
[IWINFO_BUFSIZE
];
247 const char *ifname
= luaL_checkstring(L
, 1);
248 struct iwinfo_assoclist_entry
*e
;
251 memset(rv
, 0, sizeof(rv
));
253 if( !(*func
)(ifname
, rv
, &len
) )
255 for( i
= 0; i
< len
; i
+= sizeof(struct iwinfo_assoclist_entry
) )
257 e
= (struct iwinfo_assoclist_entry
*) &rv
[i
];
259 sprintf(macstr
, "%02X:%02X:%02X:%02X:%02X:%02X",
260 e
->mac
[0], e
->mac
[1], e
->mac
[2],
261 e
->mac
[3], e
->mac
[4], e
->mac
[5]);
265 lua_pushnumber(L
, e
->signal
);
266 lua_setfield(L
, -2, "signal");
268 lua_pushnumber(L
, e
->noise
);
269 lua_setfield(L
, -2, "noise");
271 lua_setfield(L
, -2, macstr
);
278 /* Wrapper for tx power list */
279 static int iwinfo_L_txpwrlist(lua_State
*L
, int (*func
)(const char *, char *, int *))
282 char rv
[IWINFO_BUFSIZE
];
283 const char *ifname
= luaL_checkstring(L
, 1);
284 struct iwinfo_txpwrlist_entry
*e
;
287 memset(rv
, 0, sizeof(rv
));
289 if( !(*func
)(ifname
, rv
, &len
) )
291 for( i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_txpwrlist_entry
), x
++ )
293 e
= (struct iwinfo_txpwrlist_entry
*) &rv
[i
];
297 lua_pushnumber(L
, e
->mw
);
298 lua_setfield(L
, -2, "mw");
300 lua_pushnumber(L
, e
->dbm
);
301 lua_setfield(L
, -2, "dbm");
303 lua_rawseti(L
, -2, x
);
310 /* Wrapper for scan list */
311 static int iwinfo_L_scanlist(lua_State
*L
, int (*func
)(const char *, char *, int *))
314 char rv
[IWINFO_BUFSIZE
];
316 const char *ifname
= luaL_checkstring(L
, 1);
317 struct iwinfo_scanlist_entry
*e
;
320 memset(rv
, 0, sizeof(rv
));
322 if( !(*func
)(ifname
, rv
, &len
) )
324 for( i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_scanlist_entry
), x
++ )
326 e
= (struct iwinfo_scanlist_entry
*) &rv
[i
];
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]);
335 lua_pushstring(L
, macstr
);
336 lua_setfield(L
, -2, "bssid");
341 lua_pushstring(L
, (char *) e
->ssid
);
342 lua_setfield(L
, -2, "ssid");
346 lua_pushinteger(L
, e
->channel
);
347 lua_setfield(L
, -2, "channel");
350 lua_pushstring(L
, (char *) e
->mode
);
351 lua_setfield(L
, -2, "mode");
353 /* Quality, Signal */
354 lua_pushinteger(L
, e
->quality
);
355 lua_setfield(L
, -2, "quality");
357 lua_pushinteger(L
, e
->quality_max
);
358 lua_setfield(L
, -2, "quality_max");
360 lua_pushnumber(L
, (e
->signal
- 0x100));
361 lua_setfield(L
, -2, "signal");
364 iwinfo_L_cryptotable(L
, &e
->crypto
);
365 lua_setfield(L
, -2, "encryption");
367 lua_rawseti(L
, -2, x
);
374 /* Wrapper for frequency list */
375 static int iwinfo_L_freqlist(lua_State
*L
, int (*func
)(const char *, char *, int *))
378 char rv
[IWINFO_BUFSIZE
];
379 const char *ifname
= luaL_checkstring(L
, 1);
380 struct iwinfo_freqlist_entry
*e
;
383 memset(rv
, 0, sizeof(rv
));
385 if( !(*func
)(ifname
, rv
, &len
) )
387 for( i
= 0, x
= 1; i
< len
; i
+= sizeof(struct iwinfo_freqlist_entry
), x
++ )
389 e
= (struct iwinfo_freqlist_entry
*) &rv
[i
];
394 lua_pushinteger(L
, e
->mhz
);
395 lua_setfield(L
, -2, "mhz");
398 lua_pushinteger(L
, e
->channel
);
399 lua_setfield(L
, -2, "channel");
401 lua_rawseti(L
, -2, x
);
408 /* Wrapper for crypto settings */
409 static int iwinfo_L_encryption(lua_State
*L
, int (*func
)(const char *, char *))
411 const char *ifname
= luaL_checkstring(L
, 1);
412 struct iwinfo_crypto_entry c
= { 0 };
414 if( !(*func
)(ifname
, (char *)&c
) )
416 iwinfo_L_cryptotable(L
, &c
);
426 LUA_WRAP_INT(wl
,channel
)
427 LUA_WRAP_INT(wl
,frequency
)
428 LUA_WRAP_INT(wl
,txpower
)
429 LUA_WRAP_INT(wl
,bitrate
)
430 LUA_WRAP_INT(wl
,signal
)
431 LUA_WRAP_INT(wl
,noise
)
432 LUA_WRAP_INT(wl
,quality
)
433 LUA_WRAP_INT(wl
,quality_max
)
434 LUA_WRAP_INT(wl
,mbssid_support
)
435 LUA_WRAP_STRING(wl
,mode
)
436 LUA_WRAP_STRING(wl
,ssid
)
437 LUA_WRAP_STRING(wl
,bssid
)
438 LUA_WRAP_LIST(wl
,assoclist
)
439 LUA_WRAP_LIST(wl
,txpwrlist
)
440 LUA_WRAP_LIST(wl
,scanlist
)
441 LUA_WRAP_LIST(wl
,freqlist
)
442 LUA_WRAP_LIST(wl
,encryption
)
447 LUA_WRAP_INT(madwifi
,channel
)
448 LUA_WRAP_INT(madwifi
,frequency
)
449 LUA_WRAP_INT(madwifi
,txpower
)
450 LUA_WRAP_INT(madwifi
,bitrate
)
451 LUA_WRAP_INT(madwifi
,signal
)
452 LUA_WRAP_INT(madwifi
,noise
)
453 LUA_WRAP_INT(madwifi
,quality
)
454 LUA_WRAP_INT(madwifi
,quality_max
)
455 LUA_WRAP_INT(madwifi
,mbssid_support
)
456 LUA_WRAP_STRING(madwifi
,mode
)
457 LUA_WRAP_STRING(madwifi
,ssid
)
458 LUA_WRAP_STRING(madwifi
,bssid
)
459 LUA_WRAP_LIST(madwifi
,assoclist
)
460 LUA_WRAP_LIST(madwifi
,txpwrlist
)
461 LUA_WRAP_LIST(madwifi
,scanlist
)
462 LUA_WRAP_LIST(madwifi
,freqlist
)
463 LUA_WRAP_LIST(madwifi
,encryption
)
468 LUA_WRAP_INT(nl80211
,channel
)
469 LUA_WRAP_INT(nl80211
,frequency
)
470 LUA_WRAP_INT(nl80211
,txpower
)
471 LUA_WRAP_INT(nl80211
,bitrate
)
472 LUA_WRAP_INT(nl80211
,signal
)
473 LUA_WRAP_INT(nl80211
,noise
)
474 LUA_WRAP_INT(nl80211
,quality
)
475 LUA_WRAP_INT(nl80211
,quality_max
)
476 LUA_WRAP_INT(nl80211
,mbssid_support
)
477 LUA_WRAP_STRING(nl80211
,mode
)
478 LUA_WRAP_STRING(nl80211
,ssid
)
479 LUA_WRAP_STRING(nl80211
,bssid
)
480 LUA_WRAP_LIST(nl80211
,assoclist
)
481 LUA_WRAP_LIST(nl80211
,txpwrlist
)
482 LUA_WRAP_LIST(nl80211
,scanlist
)
483 LUA_WRAP_LIST(nl80211
,freqlist
)
484 LUA_WRAP_LIST(nl80211
,encryption
)
488 LUA_WRAP_INT(wext
,channel
)
489 LUA_WRAP_INT(wext
,frequency
)
490 LUA_WRAP_INT(wext
,txpower
)
491 LUA_WRAP_INT(wext
,bitrate
)
492 LUA_WRAP_INT(wext
,signal
)
493 LUA_WRAP_INT(wext
,noise
)
494 LUA_WRAP_INT(wext
,quality
)
495 LUA_WRAP_INT(wext
,quality_max
)
496 LUA_WRAP_INT(wext
,mbssid_support
)
497 LUA_WRAP_STRING(wext
,mode
)
498 LUA_WRAP_STRING(wext
,ssid
)
499 LUA_WRAP_STRING(wext
,bssid
)
500 LUA_WRAP_LIST(wext
,assoclist
)
501 LUA_WRAP_LIST(wext
,txpwrlist
)
502 LUA_WRAP_LIST(wext
,scanlist
)
503 LUA_WRAP_LIST(wext
,freqlist
)
504 LUA_WRAP_LIST(wext
,encryption
)
508 static const luaL_reg R_wl
[] = {
510 LUA_REG(wl
,frequency
),
516 LUA_REG(wl
,quality_max
),
520 LUA_REG(wl
,assoclist
),
521 LUA_REG(wl
,txpwrlist
),
522 LUA_REG(wl
,scanlist
),
523 LUA_REG(wl
,freqlist
),
524 LUA_REG(wl
,encryption
),
525 LUA_REG(wl
,mbssid_support
),
532 static const luaL_reg R_madwifi
[] = {
533 LUA_REG(madwifi
,channel
),
534 LUA_REG(madwifi
,frequency
),
535 LUA_REG(madwifi
,txpower
),
536 LUA_REG(madwifi
,bitrate
),
537 LUA_REG(madwifi
,signal
),
538 LUA_REG(madwifi
,noise
),
539 LUA_REG(madwifi
,quality
),
540 LUA_REG(madwifi
,quality_max
),
541 LUA_REG(madwifi
,mode
),
542 LUA_REG(madwifi
,ssid
),
543 LUA_REG(madwifi
,bssid
),
544 LUA_REG(madwifi
,assoclist
),
545 LUA_REG(madwifi
,txpwrlist
),
546 LUA_REG(madwifi
,scanlist
),
547 LUA_REG(madwifi
,freqlist
),
548 LUA_REG(madwifi
,encryption
),
549 LUA_REG(madwifi
,mbssid_support
),
556 static const luaL_reg R_nl80211
[] = {
557 LUA_REG(nl80211
,channel
),
558 LUA_REG(nl80211
,frequency
),
559 LUA_REG(nl80211
,txpower
),
560 LUA_REG(nl80211
,bitrate
),
561 LUA_REG(nl80211
,signal
),
562 LUA_REG(nl80211
,noise
),
563 LUA_REG(nl80211
,quality
),
564 LUA_REG(nl80211
,quality_max
),
565 LUA_REG(nl80211
,mode
),
566 LUA_REG(nl80211
,ssid
),
567 LUA_REG(nl80211
,bssid
),
568 LUA_REG(nl80211
,assoclist
),
569 LUA_REG(nl80211
,txpwrlist
),
570 LUA_REG(nl80211
,scanlist
),
571 LUA_REG(nl80211
,freqlist
),
572 LUA_REG(nl80211
,encryption
),
573 LUA_REG(nl80211
,mbssid_support
),
579 static const luaL_reg R_wext
[] = {
580 LUA_REG(wext
,channel
),
581 LUA_REG(wext
,frequency
),
582 LUA_REG(wext
,txpower
),
583 LUA_REG(wext
,bitrate
),
584 LUA_REG(wext
,signal
),
586 LUA_REG(wext
,quality
),
587 LUA_REG(wext
,quality_max
),
591 LUA_REG(wext
,assoclist
),
592 LUA_REG(wext
,txpwrlist
),
593 LUA_REG(wext
,scanlist
),
594 LUA_REG(wext
,freqlist
),
595 LUA_REG(wext
,encryption
),
596 LUA_REG(wext
,mbssid_support
),
601 static const luaL_reg R_common
[] = {
602 { "type", iwinfo_L_type
},
607 LUALIB_API
int luaopen_iwinfo(lua_State
*L
) {
608 luaL_register(L
, IWINFO_META
, R_common
);
611 luaL_newmetatable(L
, IWINFO_WL_META
);
612 luaL_register(L
, NULL
, R_wl
);
613 lua_pushvalue(L
, -1);
614 lua_setfield(L
, -2, "__index");
615 lua_setfield(L
, -2, "wl");
619 luaL_newmetatable(L
, IWINFO_MADWIFI_META
);
620 luaL_register(L
, NULL
, R_madwifi
);
621 lua_pushvalue(L
, -1);
622 lua_setfield(L
, -2, "__index");
623 lua_setfield(L
, -2, "madwifi");
627 luaL_newmetatable(L
, IWINFO_NL80211_META
);
628 luaL_register(L
, NULL
, R_nl80211
);
629 lua_pushvalue(L
, -1);
630 lua_setfield(L
, -2, "__index");
631 lua_setfield(L
, -2, "nl80211");
634 luaL_newmetatable(L
, IWINFO_WEXT_META
);
635 luaL_register(L
, NULL
, R_wext
);
636 lua_pushvalue(L
, -1);
637 lua_setfield(L
, -2, "__index");
638 lua_setfield(L
, -2, "wext");