[libiwinfo]
[project/luci.git] / contrib / package / iwinfo / src / iwinfo_lualib.c
1 /*
2 * iwinfo - Wireless Information Library - Lua Bindings
3 *
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5 *
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.
9 *
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.
14 *
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/.
17 */
18
19 #include "iwinfo_lualib.h"
20
21 /* Determine type */
22 static int iwinfo_L_type(lua_State *L)
23 {
24 const char *ifname = luaL_checkstring(L, 1);
25
26 #ifdef USE_NL80211
27 if( nl80211_probe(ifname) )
28 lua_pushstring(L, "nl80211");
29 else
30 #endif
31
32 #ifdef USE_MADWIFI
33 if( madwifi_probe(ifname) )
34 lua_pushstring(L, "madwifi");
35 else
36 #endif
37
38 #ifdef USE_WL
39 if( wl_probe(ifname) )
40 lua_pushstring(L, "wl");
41 else
42 #endif
43
44 if( wext_probe(ifname) )
45 lua_pushstring(L, "wext");
46
47 else
48 lua_pushnil(L);
49
50 return 1;
51 }
52
53 /*
54 * Build a short textual description of the crypto info
55 */
56
57 static char * iwinfo_crypto_print_ciphers(int ciphers)
58 {
59 static char str[128] = { 0 };
60 char *pos = str;
61
62 if( ciphers & IWINFO_CIPHER_WEP40 )
63 pos += sprintf(pos, "WEP-40, ");
64
65 if( ciphers & IWINFO_CIPHER_WEP104 )
66 pos += sprintf(pos, "WEP-104, ");
67
68 if( ciphers & IWINFO_CIPHER_TKIP )
69 pos += sprintf(pos, "TKIP, ");
70
71 if( ciphers & IWINFO_CIPHER_CCMP )
72 pos += sprintf(pos, "CCMP, ");
73
74 if( ciphers & IWINFO_CIPHER_WRAP )
75 pos += sprintf(pos, "WRAP, ");
76
77 if( ciphers & IWINFO_CIPHER_AESOCB )
78 pos += sprintf(pos, "AES-OCB, ");
79
80 if( ciphers & IWINFO_CIPHER_CKIP )
81 pos += sprintf(pos, "CKIP, ");
82
83 if( !ciphers || (ciphers & IWINFO_CIPHER_NONE) )
84 pos += sprintf(pos, "NONE, ");
85
86 *(pos - 2) = 0;
87
88 return str;
89 }
90
91 static char * iwinfo_crypto_print_suites(int suites)
92 {
93 static char str[64] = { 0 };
94 char *pos = str;
95
96 if( suites & IWINFO_KMGMT_PSK )
97 pos += sprintf(pos, "PSK/");
98
99 if( suites & IWINFO_KMGMT_8021x )
100 pos += sprintf(pos, "802.1X/");
101
102 if( !suites || (suites & IWINFO_KMGMT_NONE) )
103 pos += sprintf(pos, "NONE/");
104
105 *(pos - 1) = 0;
106
107 return str;
108 }
109
110 static char * iwinfo_crypto_desc(struct iwinfo_crypto_entry *c)
111 {
112 static char desc[512] = { 0 };
113
114 if( c )
115 {
116 if( c->enabled )
117 {
118 /* WEP */
119 if( c->auth_algs && !c->wpa_version )
120 {
121 if( (c->auth_algs & IWINFO_AUTH_OPEN) &&
122 (c->auth_algs & IWINFO_AUTH_SHARED) )
123 {
124 sprintf(desc, "WEP Open/Shared (%s)",
125 iwinfo_crypto_print_ciphers(c->pair_ciphers));
126 }
127 else if( c->auth_algs & IWINFO_AUTH_OPEN )
128 {
129 sprintf(desc, "WEP Open System (%s)",
130 iwinfo_crypto_print_ciphers(c->pair_ciphers));
131 }
132 else if( c->auth_algs & IWINFO_AUTH_SHARED )
133 {
134 sprintf(desc, "WEP Shared Auth (%s)",
135 iwinfo_crypto_print_ciphers(c->pair_ciphers));
136 }
137 }
138
139 /* WPA */
140 else if( c->wpa_version )
141 {
142 switch(c->wpa_version) {
143 case 3:
144 sprintf(desc, "mixed WPA/WPA2 %s (%s)",
145 iwinfo_crypto_print_suites(c->auth_suites),
146 iwinfo_crypto_print_ciphers(c->pair_ciphers));
147 break;
148
149 case 2:
150 sprintf(desc, "WPA2 %s (%s)",
151 iwinfo_crypto_print_suites(c->auth_suites),
152 iwinfo_crypto_print_ciphers(c->pair_ciphers));
153 break;
154
155 case 1:
156 sprintf(desc, "WPA %s (%s)",
157 iwinfo_crypto_print_suites(c->auth_suites),
158 iwinfo_crypto_print_ciphers(c->pair_ciphers));
159 break;
160 }
161 }
162 }
163 else
164 {
165 sprintf(desc, "None");
166 }
167 }
168 else
169 {
170 sprintf(desc, "Unknown");
171 }
172
173 return desc;
174 }
175
176 /* Build Lua table from crypto data */
177 static void iwinfo_L_cryptotable(lua_State *L, struct iwinfo_crypto_entry *c)
178 {
179 int i, j;
180
181 lua_newtable(L);
182
183 lua_pushboolean(L, c->enabled);
184 lua_setfield(L, -2, "enabled");
185
186 lua_pushstring(L, iwinfo_crypto_desc(c));
187 lua_setfield(L, -2, "description");
188
189 lua_pushboolean(L, (c->enabled && !c->wpa_version));
190 lua_setfield(L, -2, "wep");
191
192 lua_pushinteger(L, c->wpa_version);
193 lua_setfield(L, -2, "wpa");
194
195 lua_newtable(L);
196 for( i = 0, j = 1; i < 8; i++ )
197 {
198 if( c->pair_ciphers & (1 << i) )
199 {
200 lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
201 lua_rawseti(L, -2, j++);
202 }
203 }
204 lua_setfield(L, -2, "pair_ciphers");
205
206 lua_newtable(L);
207 for( i = 0, j = 1; i < 8; i++ )
208 {
209 if( c->group_ciphers & (1 << i) )
210 {
211 lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
212 lua_rawseti(L, -2, j++);
213 }
214 }
215 lua_setfield(L, -2, "group_ciphers");
216
217 lua_newtable(L);
218 for( i = 0, j = 1; i < 8; i++ )
219 {
220 if( c->auth_suites & (1 << i) )
221 {
222 lua_pushstring(L, IWINFO_KMGMT_NAMES[i]);
223 lua_rawseti(L, -2, j++);
224 }
225 }
226 lua_setfield(L, -2, "auth_suites");
227
228 lua_newtable(L);
229 for( i = 0, j = 1; i < 8; i++ )
230 {
231 if( c->auth_algs & (1 << i) )
232 {
233 lua_pushstring(L, IWINFO_AUTH_NAMES[i]);
234 lua_rawseti(L, -2, j++);
235 }
236 }
237 lua_setfield(L, -2, "auth_algs");
238 }
239
240
241 /* Wrapper for assoclist */
242 static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
243 {
244 int i, len;
245 char rv[IWINFO_BUFSIZE];
246 char macstr[18];
247 const char *ifname = luaL_checkstring(L, 1);
248 struct iwinfo_assoclist_entry *e;
249
250 lua_newtable(L);
251 memset(rv, 0, sizeof(rv));
252
253 if( !(*func)(ifname, rv, &len) )
254 {
255 for( i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry) )
256 {
257 e = (struct iwinfo_assoclist_entry *) &rv[i];
258
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]);
262
263 lua_newtable(L);
264
265 lua_pushnumber(L, e->signal);
266 lua_setfield(L, -2, "signal");
267
268 lua_pushnumber(L, e->noise);
269 lua_setfield(L, -2, "noise");
270
271 lua_setfield(L, -2, macstr);
272 }
273 }
274
275 return 1;
276 }
277
278 /* Wrapper for tx power list */
279 static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
280 {
281 int i, x, len;
282 char rv[IWINFO_BUFSIZE];
283 const char *ifname = luaL_checkstring(L, 1);
284 struct iwinfo_txpwrlist_entry *e;
285
286 lua_newtable(L);
287 memset(rv, 0, sizeof(rv));
288
289 if( !(*func)(ifname, rv, &len) )
290 {
291 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++ )
292 {
293 e = (struct iwinfo_txpwrlist_entry *) &rv[i];
294
295 lua_newtable(L);
296
297 lua_pushnumber(L, e->mw);
298 lua_setfield(L, -2, "mw");
299
300 lua_pushnumber(L, e->dbm);
301 lua_setfield(L, -2, "dbm");
302
303 lua_rawseti(L, -2, x);
304 }
305 }
306
307 return 1;
308 }
309
310 /* Wrapper for scan list */
311 static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
312 {
313 int i, x, len;
314 char rv[IWINFO_BUFSIZE];
315 char macstr[18];
316 const char *ifname = luaL_checkstring(L, 1);
317 struct iwinfo_scanlist_entry *e;
318
319 lua_newtable(L);
320 memset(rv, 0, sizeof(rv));
321
322 if( !(*func)(ifname, rv, &len) )
323 {
324 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++ )
325 {
326 e = (struct iwinfo_scanlist_entry *) &rv[i];
327
328 lua_newtable(L);
329
330 /* BSSID */
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]);
334
335 lua_pushstring(L, macstr);
336 lua_setfield(L, -2, "bssid");
337
338 /* ESSID */
339 if( e->ssid[0] )
340 {
341 lua_pushstring(L, (char *) e->ssid);
342 lua_setfield(L, -2, "ssid");
343 }
344
345 /* Channel */
346 lua_pushinteger(L, e->channel);
347 lua_setfield(L, -2, "channel");
348
349 /* Mode */
350 lua_pushstring(L, (char *) e->mode);
351 lua_setfield(L, -2, "mode");
352
353 /* Quality, Signal */
354 lua_pushinteger(L, e->quality);
355 lua_setfield(L, -2, "quality");
356
357 lua_pushinteger(L, e->quality_max);
358 lua_setfield(L, -2, "quality_max");
359
360 lua_pushnumber(L, (e->signal - 0x100));
361 lua_setfield(L, -2, "signal");
362
363 /* Crypto */
364 iwinfo_L_cryptotable(L, &e->crypto);
365 lua_setfield(L, -2, "encryption");
366
367 lua_rawseti(L, -2, x);
368 }
369 }
370
371 return 1;
372 }
373
374 /* Wrapper for frequency list */
375 static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *))
376 {
377 int i, x, len;
378 char rv[IWINFO_BUFSIZE];
379 const char *ifname = luaL_checkstring(L, 1);
380 struct iwinfo_freqlist_entry *e;
381
382 lua_newtable(L);
383 memset(rv, 0, sizeof(rv));
384
385 if( !(*func)(ifname, rv, &len) )
386 {
387 for( i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++ )
388 {
389 e = (struct iwinfo_freqlist_entry *) &rv[i];
390
391 lua_newtable(L);
392
393 /* MHz */
394 lua_pushinteger(L, e->mhz);
395 lua_setfield(L, -2, "mhz");
396
397 /* Channel */
398 lua_pushinteger(L, e->channel);
399 lua_setfield(L, -2, "channel");
400
401 lua_rawseti(L, -2, x);
402 }
403 }
404
405 return 1;
406 }
407
408 /* Wrapper for crypto settings */
409 static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *))
410 {
411 const char *ifname = luaL_checkstring(L, 1);
412 struct iwinfo_crypto_entry c = { 0 };
413
414 if( !(*func)(ifname, (char *)&c) )
415 {
416 iwinfo_L_cryptotable(L, &c);
417 return 1;
418 }
419
420 return 0;
421 }
422
423
424 #ifdef USE_WL
425 /* Broadcom */
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)
443 #endif
444
445 #ifdef USE_MADWIFI
446 /* Madwifi */
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)
464 #endif
465
466 #ifdef USE_NL80211
467 /* NL80211 */
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)
485 #endif
486
487 /* Wext */
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)
505
506 #ifdef USE_WL
507 /* Broadcom table */
508 static const luaL_reg R_wl[] = {
509 LUA_REG(wl,channel),
510 LUA_REG(wl,frequency),
511 LUA_REG(wl,txpower),
512 LUA_REG(wl,bitrate),
513 LUA_REG(wl,signal),
514 LUA_REG(wl,noise),
515 LUA_REG(wl,quality),
516 LUA_REG(wl,quality_max),
517 LUA_REG(wl,mode),
518 LUA_REG(wl,ssid),
519 LUA_REG(wl,bssid),
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),
526 { NULL, NULL }
527 };
528 #endif
529
530 #ifdef USE_MADWIFI
531 /* Madwifi table */
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),
550 { NULL, NULL }
551 };
552 #endif
553
554 #ifdef USE_NL80211
555 /* NL80211 table */
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),
574 { NULL, NULL }
575 };
576 #endif
577
578 /* Wext table */
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),
585 LUA_REG(wext,noise),
586 LUA_REG(wext,quality),
587 LUA_REG(wext,quality_max),
588 LUA_REG(wext,mode),
589 LUA_REG(wext,ssid),
590 LUA_REG(wext,bssid),
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),
597 { NULL, NULL }
598 };
599
600 /* Common */
601 static const luaL_reg R_common[] = {
602 { "type", iwinfo_L_type },
603 { NULL, NULL }
604 };
605
606
607 LUALIB_API int luaopen_iwinfo(lua_State *L) {
608 luaL_register(L, IWINFO_META, R_common);
609
610 #ifdef USE_WL
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");
616 #endif
617
618 #ifdef USE_MADWIFI
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");
624 #endif
625
626 #ifdef USE_NL80211
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");
632 #endif
633
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");
639
640 return 1;
641 }
642