[package] iwinfo: add initial hardware detection capabilities
[openwrt.git] / package / iwinfo / src / iwinfo_lua.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/lua.h"
20
21
22 /* Determine type */
23 static int iwinfo_L_type(lua_State *L)
24 {
25         const char *ifname = luaL_checkstring(L, 1);
26         const char *type = iwinfo_type(ifname);
27
28         if (type)
29                 lua_pushstring(L, type);
30         else
31                 lua_pushnil(L);
32
33         return 1;
34 }
35
36 /* Shutdown backends */
37 static int iwinfo_L__gc(lua_State *L)
38 {
39         iwinfo_finish();
40         return 0;
41 }
42
43 /*
44  * Build a short textual description of the crypto info
45  */
46
47 static char * iwinfo_crypto_print_ciphers(int ciphers)
48 {
49         static char str[128] = { 0 };
50         char *pos = str;
51
52         if (ciphers & IWINFO_CIPHER_WEP40)
53                 pos += sprintf(pos, "WEP-40, ");
54
55         if (ciphers & IWINFO_CIPHER_WEP104)
56                 pos += sprintf(pos, "WEP-104, ");
57
58         if (ciphers & IWINFO_CIPHER_TKIP)
59                 pos += sprintf(pos, "TKIP, ");
60
61         if (ciphers & IWINFO_CIPHER_CCMP)
62                 pos += sprintf(pos, "CCMP, ");
63
64         if (ciphers & IWINFO_CIPHER_WRAP)
65                 pos += sprintf(pos, "WRAP, ");
66
67         if (ciphers & IWINFO_CIPHER_AESOCB)
68                 pos += sprintf(pos, "AES-OCB, ");
69
70         if (ciphers & IWINFO_CIPHER_CKIP)
71                 pos += sprintf(pos, "CKIP, ");
72
73         if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
74                 pos += sprintf(pos, "NONE, ");
75
76         *(pos - 2) = 0;
77
78         return str;
79 }
80
81 static char * iwinfo_crypto_print_suites(int suites)
82 {
83         static char str[64] = { 0 };
84         char *pos = str;
85
86         if (suites & IWINFO_KMGMT_PSK)
87                 pos += sprintf(pos, "PSK/");
88
89         if (suites & IWINFO_KMGMT_8021x)
90                 pos += sprintf(pos, "802.1X/");
91
92         if (!suites || (suites & IWINFO_KMGMT_NONE))
93                 pos += sprintf(pos, "NONE/");
94
95         *(pos - 1) = 0;
96
97         return str;
98 }
99
100 static char * iwinfo_crypto_desc(struct iwinfo_crypto_entry *c)
101 {
102         static char desc[512] = { 0 };
103
104         if (c)
105         {
106                 if (c->enabled)
107                 {
108                         /* WEP */
109                         if (c->auth_algs && !c->wpa_version)
110                         {
111                                 if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
112                                     (c->auth_algs & IWINFO_AUTH_SHARED))
113                                 {
114                                         sprintf(desc, "WEP Open/Shared (%s)",
115                                                 iwinfo_crypto_print_ciphers(c->pair_ciphers));
116                                 }
117                                 else if (c->auth_algs & IWINFO_AUTH_OPEN)
118                                 {
119                                         sprintf(desc, "WEP Open System (%s)",
120                                                 iwinfo_crypto_print_ciphers(c->pair_ciphers));
121                                 }
122                                 else if (c->auth_algs & IWINFO_AUTH_SHARED)
123                                 {
124                                         sprintf(desc, "WEP Shared Auth (%s)",
125                                                 iwinfo_crypto_print_ciphers(c->pair_ciphers));
126                                 }
127                         }
128
129                         /* WPA */
130                         else if (c->wpa_version)
131                         {
132                                 switch (c->wpa_version) {
133                                         case 3:
134                                                 sprintf(desc, "mixed WPA/WPA2 %s (%s)",
135                                                         iwinfo_crypto_print_suites(c->auth_suites),
136                                                         iwinfo_crypto_print_ciphers(
137                                                                 c->pair_ciphers & c->group_ciphers));
138                                                 break;
139
140                                         case 2:
141                                                 sprintf(desc, "WPA2 %s (%s)",
142                                                         iwinfo_crypto_print_suites(c->auth_suites),
143                                                         iwinfo_crypto_print_ciphers(
144                                                                 c->pair_ciphers & c->group_ciphers));
145                                                 break;
146
147                                         case 1:
148                                                 sprintf(desc, "WPA %s (%s)",
149                                                         iwinfo_crypto_print_suites(c->auth_suites),
150                                                         iwinfo_crypto_print_ciphers(
151                                                                 c->pair_ciphers & c->group_ciphers));
152                                                 break;
153                                 }
154                         }
155                         else
156                         {
157                                 sprintf(desc, "None");
158                         }
159                 }
160                 else
161                 {
162                         sprintf(desc, "None");
163                 }
164         }
165         else
166         {
167                 sprintf(desc, "Unknown");
168         }
169
170         return desc;
171 }
172
173 /* Build Lua table from crypto data */
174 static void iwinfo_L_cryptotable(lua_State *L, struct iwinfo_crypto_entry *c)
175 {
176         int i, j;
177
178         lua_newtable(L);
179
180         lua_pushboolean(L, c->enabled);
181         lua_setfield(L, -2, "enabled");
182
183         lua_pushstring(L, iwinfo_crypto_desc(c));
184         lua_setfield(L, -2, "description");
185
186         lua_pushboolean(L, (c->enabled && !c->wpa_version));
187         lua_setfield(L, -2, "wep");
188
189         lua_pushinteger(L, c->wpa_version);
190         lua_setfield(L, -2, "wpa");
191
192         lua_newtable(L);
193         for (i = 0, j = 1; i < 8; i++)
194         {
195                 if (c->pair_ciphers & (1 << i))
196                 {
197                         lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
198                         lua_rawseti(L, -2, j++);
199                 }
200         }
201         lua_setfield(L, -2, "pair_ciphers");
202
203         lua_newtable(L);
204         for (i = 0, j = 1; i < 8; i++)
205         {
206                 if (c->group_ciphers & (1 << i))
207                 {
208                         lua_pushstring(L, IWINFO_CIPHER_NAMES[i]);
209                         lua_rawseti(L, -2, j++);
210                 }
211         }
212         lua_setfield(L, -2, "group_ciphers");
213
214         lua_newtable(L);
215         for (i = 0, j = 1; i < 8; i++)
216         {
217                 if (c->auth_suites & (1 << i))
218                 {
219                         lua_pushstring(L, IWINFO_KMGMT_NAMES[i]);
220                         lua_rawseti(L, -2, j++);
221                 }
222         }
223         lua_setfield(L, -2, "auth_suites");
224
225         lua_newtable(L);
226         for (i = 0, j = 1; i < 8; i++)
227         {
228                 if (c->auth_algs & (1 << i))
229                 {
230                         lua_pushstring(L, IWINFO_AUTH_NAMES[i]);
231                         lua_rawseti(L, -2, j++);
232                 }
233         }
234         lua_setfield(L, -2, "auth_algs");
235 }
236
237
238 /* Wrapper for assoclist */
239 static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *))
240 {
241         int i, len;
242         char rv[IWINFO_BUFSIZE];
243         char macstr[18];
244         const char *ifname = luaL_checkstring(L, 1);
245         struct iwinfo_assoclist_entry *e;
246
247         lua_newtable(L);
248         memset(rv, 0, sizeof(rv));
249
250         if (!(*func)(ifname, rv, &len))
251         {
252                 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
253                 {
254                         e = (struct iwinfo_assoclist_entry *) &rv[i];
255
256                         sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
257                                 e->mac[0], e->mac[1], e->mac[2],
258                                 e->mac[3], e->mac[4], e->mac[5]);
259
260                         lua_newtable(L);
261
262                         lua_pushnumber(L, e->signal);
263                         lua_setfield(L, -2, "signal");
264
265                         lua_pushnumber(L, e->noise);
266                         lua_setfield(L, -2, "noise");
267
268                         lua_setfield(L, -2, macstr);
269                 }
270         }
271
272         return 1;
273 }
274
275 /* Wrapper for tx power list */
276 static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *))
277 {
278         int i, x, len;
279         char rv[IWINFO_BUFSIZE];
280         const char *ifname = luaL_checkstring(L, 1);
281         struct iwinfo_txpwrlist_entry *e;
282
283         lua_newtable(L);
284         memset(rv, 0, sizeof(rv));
285
286         if (!(*func)(ifname, rv, &len))
287         {
288                 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++)
289                 {
290                         e = (struct iwinfo_txpwrlist_entry *) &rv[i];
291
292                         lua_newtable(L);
293
294                         lua_pushnumber(L, e->mw);
295                         lua_setfield(L, -2, "mw");
296
297                         lua_pushnumber(L, e->dbm);
298                         lua_setfield(L, -2, "dbm");
299
300                         lua_rawseti(L, -2, x);
301                 }
302         }
303
304         return 1;
305 }
306
307 /* Wrapper for scan list */
308 static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *))
309 {
310         int i, x, len;
311         char rv[IWINFO_BUFSIZE];
312         char macstr[18];
313         const char *ifname = luaL_checkstring(L, 1);
314         struct iwinfo_scanlist_entry *e;
315
316         lua_newtable(L);
317         memset(rv, 0, sizeof(rv));
318
319         if (!(*func)(ifname, rv, &len))
320         {
321                 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
322                 {
323                         e = (struct iwinfo_scanlist_entry *) &rv[i];
324
325                         lua_newtable(L);
326
327                         /* BSSID */
328                         sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
329                                 e->mac[0], e->mac[1], e->mac[2],
330                                 e->mac[3], e->mac[4], e->mac[5]);
331
332                         lua_pushstring(L, macstr);
333                         lua_setfield(L, -2, "bssid");
334
335                         /* ESSID */
336                         if (e->ssid[0])
337                         {
338                                 lua_pushstring(L, (char *) e->ssid);
339                                 lua_setfield(L, -2, "ssid");
340                         }
341
342                         /* Channel */
343                         lua_pushinteger(L, e->channel);
344                         lua_setfield(L, -2, "channel");
345
346                         /* Mode */
347                         lua_pushstring(L, (char *) e->mode);
348                         lua_setfield(L, -2, "mode");
349
350                         /* Quality, Signal */
351                         lua_pushinteger(L, e->quality);
352                         lua_setfield(L, -2, "quality");
353
354                         lua_pushinteger(L, e->quality_max);
355                         lua_setfield(L, -2, "quality_max");
356
357                         lua_pushnumber(L, (e->signal - 0x100));
358                         lua_setfield(L, -2, "signal");
359
360                         /* Crypto */
361                         iwinfo_L_cryptotable(L, &e->crypto);
362                         lua_setfield(L, -2, "encryption");
363
364                         lua_rawseti(L, -2, x);
365                 }
366         }
367
368         return 1;
369 }
370
371 /* Wrapper for frequency list */
372 static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *))
373 {
374         int i, x, len;
375         char rv[IWINFO_BUFSIZE];
376         const char *ifname = luaL_checkstring(L, 1);
377         struct iwinfo_freqlist_entry *e;
378
379         lua_newtable(L);
380         memset(rv, 0, sizeof(rv));
381
382         if (!(*func)(ifname, rv, &len))
383         {
384                 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++)
385                 {
386                         e = (struct iwinfo_freqlist_entry *) &rv[i];
387
388                         lua_newtable(L);
389
390                         /* MHz */
391                         lua_pushinteger(L, e->mhz);
392                         lua_setfield(L, -2, "mhz");
393
394                         /* Channel */
395                         lua_pushinteger(L, e->channel);
396                         lua_setfield(L, -2, "channel");
397
398                         /* Restricted (DFS/TPC/Radar) */
399                         lua_pushboolean(L, e->restricted);
400                         lua_setfield(L, -2, "restricted");
401
402                         lua_rawseti(L, -2, x);
403                 }
404         }
405
406         return 1;
407 }
408
409 /* Wrapper for crypto settings */
410 static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *))
411 {
412         const char *ifname = luaL_checkstring(L, 1);
413         struct iwinfo_crypto_entry c = { 0 };
414
415         if (!(*func)(ifname, (char *)&c))
416         {
417                 iwinfo_L_cryptotable(L, &c);
418                 return 1;
419         }
420
421         lua_pushnil(L);
422         return 1;
423 }
424
425 /* Wrapper for hwmode list */
426 static int iwinfo_L_hwmodelist(lua_State *L, int (*func)(const char *, int *))
427 {
428         const char *ifname = luaL_checkstring(L, 1);
429         int hwmodes = 0;
430
431         if (!(*func)(ifname, &hwmodes))
432         {
433                 lua_newtable(L);
434
435                 lua_pushboolean(L, hwmodes & IWINFO_80211_A);
436                 lua_setfield(L, -2, "a");
437
438                 lua_pushboolean(L, hwmodes & IWINFO_80211_B);
439                 lua_setfield(L, -2, "b");
440
441                 lua_pushboolean(L, hwmodes & IWINFO_80211_G);
442                 lua_setfield(L, -2, "g");
443
444                 lua_pushboolean(L, hwmodes & IWINFO_80211_N);
445                 lua_setfield(L, -2, "n");
446
447                 return 1;
448         }
449
450         lua_pushnil(L);
451         return 1;
452 }
453
454 /* Wrapper for mbssid_support */
455 static int iwinfo_L_mbssid_support(lua_State *L, int (*func)(const char *, int *))
456 {
457         const char *ifname = luaL_checkstring(L, 1);
458         int support = 0;
459
460         if (!(*func)(ifname, &support))
461         {
462                 lua_pushboolean(L, support);
463                 return 1;
464         }
465
466         lua_pushnil(L);
467         return 1;
468 }
469
470 /* Wrapper for hardware_id */
471 static int iwinfo_L_hardware_id(lua_State *L, int (*func)(const char *, char *))
472 {
473         const char *ifname = luaL_checkstring(L, 1);
474         struct iwinfo_hardware_id ids;
475
476         if (!(*func)(ifname, (char *)&ids))
477         {
478                 lua_newtable(L);
479
480                 lua_pushnumber(L, ids.vendor_id);
481                 lua_setfield(L, -2, "vendor_id");
482
483                 lua_pushnumber(L, ids.device_id);
484                 lua_setfield(L, -2, "device_id");
485
486                 lua_pushnumber(L, ids.subsystem_vendor_id);
487                 lua_setfield(L, -2, "subsystem_vendor_id");
488
489                 lua_pushnumber(L, ids.subsystem_device_id);
490                 lua_setfield(L, -2, "subsystem_device_id");
491         }
492         else
493         {
494                 lua_pushnil(L);
495         }
496
497         return 1;
498 }
499
500 /* Wrapper for country list */
501 static char * iwinfo_L_country_lookup(char *buf, int len, int iso3166)
502 {
503         int i;
504         struct iwinfo_country_entry *c;
505
506         for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
507         {
508                 c = (struct iwinfo_country_entry *) &buf[i];
509
510                 if (c->iso3166 == iso3166)
511                         return c->ccode;
512         }
513
514         return NULL;
515 }
516
517 static int iwinfo_L_countrylist(lua_State *L, int (*func)(const char *, char *, int *))
518 {
519         int len, i, j;
520         char rv[IWINFO_BUFSIZE], alpha2[3];
521         char *ccode;
522         const char *ifname = luaL_checkstring(L, 1);
523         const struct iwinfo_iso3166_label *l;
524
525         lua_newtable(L);
526         memset(rv, 0, sizeof(rv));
527
528         if (!(*func)(ifname, rv, &len))
529         {
530                 for (l = IWINFO_ISO3166_NAMES, j = 1; l->iso3166; l++)
531                 {
532                         if ((ccode = iwinfo_L_country_lookup(rv, len, l->iso3166)) != NULL)
533                         {
534                                 sprintf(alpha2, "%c%c",
535                                         (l->iso3166 / 256), (l->iso3166 % 256));
536
537                                 lua_newtable(L);
538
539                                 lua_pushstring(L, alpha2);
540                                 lua_setfield(L, -2, "alpha2");
541
542                                 lua_pushstring(L, ccode);
543                                 lua_setfield(L, -2, "ccode");
544
545                                 lua_pushstring(L, l->name);
546                                 lua_setfield(L, -2, "name");
547
548                                 lua_rawseti(L, -2, j++);
549                         }
550                 }
551         }
552
553         return 1;
554 }
555
556
557 #ifdef USE_WL
558 /* Broadcom */
559 LUA_WRAP_INT(wl,channel)
560 LUA_WRAP_INT(wl,frequency)
561 LUA_WRAP_INT(wl,txpower)
562 LUA_WRAP_INT(wl,bitrate)
563 LUA_WRAP_INT(wl,signal)
564 LUA_WRAP_INT(wl,noise)
565 LUA_WRAP_INT(wl,quality)
566 LUA_WRAP_INT(wl,quality_max)
567 LUA_WRAP_STRING(wl,mode)
568 LUA_WRAP_STRING(wl,ssid)
569 LUA_WRAP_STRING(wl,bssid)
570 LUA_WRAP_STRING(wl,country)
571 LUA_WRAP_STRING(wl,hardware_name)
572 LUA_WRAP_STRUCT(wl,assoclist)
573 LUA_WRAP_STRUCT(wl,txpwrlist)
574 LUA_WRAP_STRUCT(wl,scanlist)
575 LUA_WRAP_STRUCT(wl,freqlist)
576 LUA_WRAP_STRUCT(wl,countrylist)
577 LUA_WRAP_STRUCT(wl,hwmodelist)
578 LUA_WRAP_STRUCT(wl,encryption)
579 LUA_WRAP_STRUCT(wl,mbssid_support)
580 LUA_WRAP_STRUCT(wl,hardware_id)
581 #endif
582
583 #ifdef USE_MADWIFI
584 /* Madwifi */
585 LUA_WRAP_INT(madwifi,channel)
586 LUA_WRAP_INT(madwifi,frequency)
587 LUA_WRAP_INT(madwifi,txpower)
588 LUA_WRAP_INT(madwifi,bitrate)
589 LUA_WRAP_INT(madwifi,signal)
590 LUA_WRAP_INT(madwifi,noise)
591 LUA_WRAP_INT(madwifi,quality)
592 LUA_WRAP_INT(madwifi,quality_max)
593 LUA_WRAP_STRING(madwifi,mode)
594 LUA_WRAP_STRING(madwifi,ssid)
595 LUA_WRAP_STRING(madwifi,bssid)
596 LUA_WRAP_STRING(madwifi,country)
597 LUA_WRAP_STRING(madwifi,hardware_name)
598 LUA_WRAP_STRUCT(madwifi,assoclist)
599 LUA_WRAP_STRUCT(madwifi,txpwrlist)
600 LUA_WRAP_STRUCT(madwifi,scanlist)
601 LUA_WRAP_STRUCT(madwifi,freqlist)
602 LUA_WRAP_STRUCT(madwifi,countrylist)
603 LUA_WRAP_STRUCT(madwifi,hwmodelist)
604 LUA_WRAP_STRUCT(madwifi,encryption)
605 LUA_WRAP_STRUCT(madwifi,mbssid_support)
606 LUA_WRAP_STRUCT(madwifi,hardware_id)
607 #endif
608
609 #ifdef USE_NL80211
610 /* NL80211 */
611 LUA_WRAP_INT(nl80211,channel)
612 LUA_WRAP_INT(nl80211,frequency)
613 LUA_WRAP_INT(nl80211,txpower)
614 LUA_WRAP_INT(nl80211,bitrate)
615 LUA_WRAP_INT(nl80211,signal)
616 LUA_WRAP_INT(nl80211,noise)
617 LUA_WRAP_INT(nl80211,quality)
618 LUA_WRAP_INT(nl80211,quality_max)
619 LUA_WRAP_STRING(nl80211,mode)
620 LUA_WRAP_STRING(nl80211,ssid)
621 LUA_WRAP_STRING(nl80211,bssid)
622 LUA_WRAP_STRING(nl80211,country)
623 LUA_WRAP_STRING(nl80211,hardware_name)
624 LUA_WRAP_STRUCT(nl80211,assoclist)
625 LUA_WRAP_STRUCT(nl80211,txpwrlist)
626 LUA_WRAP_STRUCT(nl80211,scanlist)
627 LUA_WRAP_STRUCT(nl80211,freqlist)
628 LUA_WRAP_STRUCT(nl80211,countrylist)
629 LUA_WRAP_STRUCT(nl80211,hwmodelist)
630 LUA_WRAP_STRUCT(nl80211,encryption)
631 LUA_WRAP_STRUCT(nl80211,mbssid_support)
632 LUA_WRAP_STRUCT(nl80211,hardware_id)
633 #endif
634
635 /* Wext */
636 LUA_WRAP_INT(wext,channel)
637 LUA_WRAP_INT(wext,frequency)
638 LUA_WRAP_INT(wext,txpower)
639 LUA_WRAP_INT(wext,bitrate)
640 LUA_WRAP_INT(wext,signal)
641 LUA_WRAP_INT(wext,noise)
642 LUA_WRAP_INT(wext,quality)
643 LUA_WRAP_INT(wext,quality_max)
644 LUA_WRAP_STRING(wext,mode)
645 LUA_WRAP_STRING(wext,ssid)
646 LUA_WRAP_STRING(wext,bssid)
647 LUA_WRAP_STRING(wext,country)
648 LUA_WRAP_STRING(wext,hardware_name)
649 LUA_WRAP_STRUCT(wext,assoclist)
650 LUA_WRAP_STRUCT(wext,txpwrlist)
651 LUA_WRAP_STRUCT(wext,scanlist)
652 LUA_WRAP_STRUCT(wext,freqlist)
653 LUA_WRAP_STRUCT(wext,countrylist)
654 LUA_WRAP_STRUCT(wext,hwmodelist)
655 LUA_WRAP_STRUCT(wext,encryption)
656 LUA_WRAP_STRUCT(wext,mbssid_support)
657 LUA_WRAP_STRUCT(wext,hardware_id)
658
659 #ifdef USE_WL
660 /* Broadcom table */
661 static const luaL_reg R_wl[] = {
662         LUA_REG(wl,channel),
663         LUA_REG(wl,frequency),
664         LUA_REG(wl,txpower),
665         LUA_REG(wl,bitrate),
666         LUA_REG(wl,signal),
667         LUA_REG(wl,noise),
668         LUA_REG(wl,quality),
669         LUA_REG(wl,quality_max),
670         LUA_REG(wl,mode),
671         LUA_REG(wl,ssid),
672         LUA_REG(wl,bssid),
673         LUA_REG(wl,country),
674         LUA_REG(wl,assoclist),
675         LUA_REG(wl,txpwrlist),
676         LUA_REG(wl,scanlist),
677         LUA_REG(wl,freqlist),
678         LUA_REG(wl,countrylist),
679         LUA_REG(wl,hwmodelist),
680         LUA_REG(wl,encryption),
681         LUA_REG(wl,mbssid_support),
682         LUA_REG(wl,hardware_id),
683         LUA_REG(wl,hardware_name),
684         { NULL, NULL }
685 };
686 #endif
687
688 #ifdef USE_MADWIFI
689 /* Madwifi table */
690 static const luaL_reg R_madwifi[] = {
691         LUA_REG(madwifi,channel),
692         LUA_REG(madwifi,frequency),
693         LUA_REG(madwifi,txpower),
694         LUA_REG(madwifi,bitrate),
695         LUA_REG(madwifi,signal),
696         LUA_REG(madwifi,noise),
697         LUA_REG(madwifi,quality),
698         LUA_REG(madwifi,quality_max),
699         LUA_REG(madwifi,mode),
700         LUA_REG(madwifi,ssid),
701         LUA_REG(madwifi,bssid),
702         LUA_REG(madwifi,country),
703         LUA_REG(madwifi,assoclist),
704         LUA_REG(madwifi,txpwrlist),
705         LUA_REG(madwifi,scanlist),
706         LUA_REG(madwifi,freqlist),
707         LUA_REG(madwifi,countrylist),
708         LUA_REG(madwifi,hwmodelist),
709         LUA_REG(madwifi,encryption),
710         LUA_REG(madwifi,mbssid_support),
711         LUA_REG(madwifi,hardware_id),
712         LUA_REG(madwifi,hardware_name),
713         { NULL, NULL }
714 };
715 #endif
716
717 #ifdef USE_NL80211
718 /* NL80211 table */
719 static const luaL_reg R_nl80211[] = {
720         LUA_REG(nl80211,channel),
721         LUA_REG(nl80211,frequency),
722         LUA_REG(nl80211,txpower),
723         LUA_REG(nl80211,bitrate),
724         LUA_REG(nl80211,signal),
725         LUA_REG(nl80211,noise),
726         LUA_REG(nl80211,quality),
727         LUA_REG(nl80211,quality_max),
728         LUA_REG(nl80211,mode),
729         LUA_REG(nl80211,ssid),
730         LUA_REG(nl80211,bssid),
731         LUA_REG(nl80211,country),
732         LUA_REG(nl80211,assoclist),
733         LUA_REG(nl80211,txpwrlist),
734         LUA_REG(nl80211,scanlist),
735         LUA_REG(nl80211,freqlist),
736         LUA_REG(nl80211,countrylist),
737         LUA_REG(nl80211,hwmodelist),
738         LUA_REG(nl80211,encryption),
739         LUA_REG(nl80211,mbssid_support),
740         LUA_REG(nl80211,hardware_id),
741         LUA_REG(nl80211,hardware_name),
742         { NULL, NULL }
743 };
744 #endif
745
746 /* Wext table */
747 static const luaL_reg R_wext[] = {
748         LUA_REG(wext,channel),
749         LUA_REG(wext,frequency),
750         LUA_REG(wext,txpower),
751         LUA_REG(wext,bitrate),
752         LUA_REG(wext,signal),
753         LUA_REG(wext,noise),
754         LUA_REG(wext,quality),
755         LUA_REG(wext,quality_max),
756         LUA_REG(wext,mode),
757         LUA_REG(wext,ssid),
758         LUA_REG(wext,bssid),
759         LUA_REG(wext,country),
760         LUA_REG(wext,assoclist),
761         LUA_REG(wext,txpwrlist),
762         LUA_REG(wext,scanlist),
763         LUA_REG(wext,freqlist),
764         LUA_REG(wext,countrylist),
765         LUA_REG(wext,hwmodelist),
766         LUA_REG(wext,encryption),
767         LUA_REG(wext,mbssid_support),
768         LUA_REG(wext,hardware_id),
769         LUA_REG(wext,hardware_name),
770         { NULL, NULL }
771 };
772
773 /* Common */
774 static const luaL_reg R_common[] = {
775         { "type", iwinfo_L_type },
776         { "__gc", iwinfo_L__gc  },
777         { NULL, NULL }
778 };
779
780
781 LUALIB_API int luaopen_iwinfo(lua_State *L) {
782         luaL_register(L, IWINFO_META, R_common);
783
784 #ifdef USE_WL
785         luaL_newmetatable(L, IWINFO_WL_META);
786         luaL_register(L, NULL, R_wl);
787         lua_pushvalue(L, -1);
788         lua_setfield(L, -2, "__index");
789         lua_setfield(L, -2, "wl");
790 #endif
791
792 #ifdef USE_MADWIFI
793         luaL_newmetatable(L, IWINFO_MADWIFI_META);
794         luaL_register(L, NULL, R_madwifi);
795         lua_pushvalue(L, -1);
796         lua_setfield(L, -2, "__index");
797         lua_setfield(L, -2, "madwifi");
798 #endif
799
800 #ifdef USE_NL80211
801         luaL_newmetatable(L, IWINFO_NL80211_META);
802         luaL_register(L, NULL, R_nl80211);
803         lua_pushvalue(L, -1);
804         lua_setfield(L, -2, "__index");
805         lua_setfield(L, -2, "nl80211");
806 #endif
807
808         luaL_newmetatable(L, IWINFO_WEXT_META);
809         luaL_register(L, NULL, R_wext);
810         lua_pushvalue(L, -1);
811         lua_setfield(L, -2, "__index");
812         lua_setfield(L, -2, "wext");
813
814         return 1;
815 }