| 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 mode */ |
| 239 | static int iwinfo_L_mode(lua_State *L, int (*func)(const char *, int *)) |
| 240 | { |
| 241 | int mode; |
| 242 | const char *ifname = luaL_checkstring(L, 1); |
| 243 | |
| 244 | if ((*func)(ifname, &mode)) |
| 245 | mode = IWINFO_OPMODE_UNKNOWN; |
| 246 | |
| 247 | lua_pushstring(L, IWINFO_OPMODE_NAMES[mode]); |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | /* Wrapper for assoclist */ |
| 252 | static int iwinfo_L_assoclist(lua_State *L, int (*func)(const char *, char *, int *)) |
| 253 | { |
| 254 | int i, len; |
| 255 | char rv[IWINFO_BUFSIZE]; |
| 256 | char macstr[18]; |
| 257 | const char *ifname = luaL_checkstring(L, 1); |
| 258 | struct iwinfo_assoclist_entry *e; |
| 259 | |
| 260 | lua_newtable(L); |
| 261 | memset(rv, 0, sizeof(rv)); |
| 262 | |
| 263 | if (!(*func)(ifname, rv, &len)) |
| 264 | { |
| 265 | for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry)) |
| 266 | { |
| 267 | e = (struct iwinfo_assoclist_entry *) &rv[i]; |
| 268 | |
| 269 | sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 270 | e->mac[0], e->mac[1], e->mac[2], |
| 271 | e->mac[3], e->mac[4], e->mac[5]); |
| 272 | |
| 273 | lua_newtable(L); |
| 274 | |
| 275 | lua_pushnumber(L, e->signal); |
| 276 | lua_setfield(L, -2, "signal"); |
| 277 | |
| 278 | lua_pushnumber(L, e->noise); |
| 279 | lua_setfield(L, -2, "noise"); |
| 280 | |
| 281 | lua_pushnumber(L, e->inactive); |
| 282 | lua_setfield(L, -2, "inactive"); |
| 283 | |
| 284 | lua_pushnumber(L, e->rx_packets); |
| 285 | lua_setfield(L, -2, "rx_packets"); |
| 286 | |
| 287 | lua_pushnumber(L, e->tx_packets); |
| 288 | lua_setfield(L, -2, "tx_packets"); |
| 289 | |
| 290 | lua_pushnumber(L, e->rx_rate.rate); |
| 291 | lua_setfield(L, -2, "rx_rate"); |
| 292 | |
| 293 | lua_pushnumber(L, e->tx_rate.rate); |
| 294 | lua_setfield(L, -2, "tx_rate"); |
| 295 | |
| 296 | if (e->rx_rate.mcs >= 0) |
| 297 | { |
| 298 | lua_pushnumber(L, e->rx_rate.mcs); |
| 299 | lua_setfield(L, -2, "rx_mcs"); |
| 300 | |
| 301 | lua_pushboolean(L, e->rx_rate.is_40mhz); |
| 302 | lua_setfield(L, -2, "rx_40mhz"); |
| 303 | |
| 304 | lua_pushboolean(L, e->rx_rate.is_short_gi); |
| 305 | lua_setfield(L, -2, "rx_short_gi"); |
| 306 | } |
| 307 | |
| 308 | if (e->tx_rate.mcs >= 0) |
| 309 | { |
| 310 | lua_pushnumber(L, e->tx_rate.mcs); |
| 311 | lua_setfield(L, -2, "tx_mcs"); |
| 312 | |
| 313 | lua_pushboolean(L, e->tx_rate.is_40mhz); |
| 314 | lua_setfield(L, -2, "tx_40mhz"); |
| 315 | |
| 316 | lua_pushboolean(L, e->tx_rate.is_short_gi); |
| 317 | lua_setfield(L, -2, "tx_short_gi"); |
| 318 | } |
| 319 | |
| 320 | lua_setfield(L, -2, macstr); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return 1; |
| 325 | } |
| 326 | |
| 327 | /* Wrapper for tx power list */ |
| 328 | static int iwinfo_L_txpwrlist(lua_State *L, int (*func)(const char *, char *, int *)) |
| 329 | { |
| 330 | int i, x, len; |
| 331 | char rv[IWINFO_BUFSIZE]; |
| 332 | const char *ifname = luaL_checkstring(L, 1); |
| 333 | struct iwinfo_txpwrlist_entry *e; |
| 334 | |
| 335 | memset(rv, 0, sizeof(rv)); |
| 336 | |
| 337 | if (!(*func)(ifname, rv, &len)) |
| 338 | { |
| 339 | lua_newtable(L); |
| 340 | |
| 341 | for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_txpwrlist_entry), x++) |
| 342 | { |
| 343 | e = (struct iwinfo_txpwrlist_entry *) &rv[i]; |
| 344 | |
| 345 | lua_newtable(L); |
| 346 | |
| 347 | lua_pushnumber(L, e->mw); |
| 348 | lua_setfield(L, -2, "mw"); |
| 349 | |
| 350 | lua_pushnumber(L, e->dbm); |
| 351 | lua_setfield(L, -2, "dbm"); |
| 352 | |
| 353 | lua_rawseti(L, -2, x); |
| 354 | } |
| 355 | |
| 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /* Wrapper for scan list */ |
| 363 | static int iwinfo_L_scanlist(lua_State *L, int (*func)(const char *, char *, int *)) |
| 364 | { |
| 365 | int i, x, len; |
| 366 | char rv[IWINFO_BUFSIZE]; |
| 367 | char macstr[18]; |
| 368 | const char *ifname = luaL_checkstring(L, 1); |
| 369 | struct iwinfo_scanlist_entry *e; |
| 370 | |
| 371 | lua_newtable(L); |
| 372 | memset(rv, 0, sizeof(rv)); |
| 373 | |
| 374 | if (!(*func)(ifname, rv, &len)) |
| 375 | { |
| 376 | for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++) |
| 377 | { |
| 378 | e = (struct iwinfo_scanlist_entry *) &rv[i]; |
| 379 | |
| 380 | lua_newtable(L); |
| 381 | |
| 382 | /* BSSID */ |
| 383 | sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 384 | e->mac[0], e->mac[1], e->mac[2], |
| 385 | e->mac[3], e->mac[4], e->mac[5]); |
| 386 | |
| 387 | lua_pushstring(L, macstr); |
| 388 | lua_setfield(L, -2, "bssid"); |
| 389 | |
| 390 | /* ESSID */ |
| 391 | if (e->ssid[0]) |
| 392 | { |
| 393 | lua_pushstring(L, (char *) e->ssid); |
| 394 | lua_setfield(L, -2, "ssid"); |
| 395 | } |
| 396 | |
| 397 | /* Channel */ |
| 398 | lua_pushinteger(L, e->channel); |
| 399 | lua_setfield(L, -2, "channel"); |
| 400 | |
| 401 | /* Mode */ |
| 402 | lua_pushstring(L, IWINFO_OPMODE_NAMES[e->mode]); |
| 403 | lua_setfield(L, -2, "mode"); |
| 404 | |
| 405 | /* Quality, Signal */ |
| 406 | lua_pushinteger(L, e->quality); |
| 407 | lua_setfield(L, -2, "quality"); |
| 408 | |
| 409 | lua_pushinteger(L, e->quality_max); |
| 410 | lua_setfield(L, -2, "quality_max"); |
| 411 | |
| 412 | lua_pushnumber(L, (e->signal - 0x100)); |
| 413 | lua_setfield(L, -2, "signal"); |
| 414 | |
| 415 | /* Crypto */ |
| 416 | iwinfo_L_cryptotable(L, &e->crypto); |
| 417 | lua_setfield(L, -2, "encryption"); |
| 418 | |
| 419 | lua_rawseti(L, -2, x); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return 1; |
| 424 | } |
| 425 | |
| 426 | /* Wrapper for frequency list */ |
| 427 | static int iwinfo_L_freqlist(lua_State *L, int (*func)(const char *, char *, int *)) |
| 428 | { |
| 429 | int i, x, len; |
| 430 | char rv[IWINFO_BUFSIZE]; |
| 431 | const char *ifname = luaL_checkstring(L, 1); |
| 432 | struct iwinfo_freqlist_entry *e; |
| 433 | |
| 434 | lua_newtable(L); |
| 435 | memset(rv, 0, sizeof(rv)); |
| 436 | |
| 437 | if (!(*func)(ifname, rv, &len)) |
| 438 | { |
| 439 | for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_freqlist_entry), x++) |
| 440 | { |
| 441 | e = (struct iwinfo_freqlist_entry *) &rv[i]; |
| 442 | |
| 443 | lua_newtable(L); |
| 444 | |
| 445 | /* MHz */ |
| 446 | lua_pushinteger(L, e->mhz); |
| 447 | lua_setfield(L, -2, "mhz"); |
| 448 | |
| 449 | /* Channel */ |
| 450 | lua_pushinteger(L, e->channel); |
| 451 | lua_setfield(L, -2, "channel"); |
| 452 | |
| 453 | /* Restricted (DFS/TPC/Radar) */ |
| 454 | lua_pushboolean(L, e->restricted); |
| 455 | lua_setfield(L, -2, "restricted"); |
| 456 | |
| 457 | lua_rawseti(L, -2, x); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | /* Wrapper for crypto settings */ |
| 465 | static int iwinfo_L_encryption(lua_State *L, int (*func)(const char *, char *)) |
| 466 | { |
| 467 | const char *ifname = luaL_checkstring(L, 1); |
| 468 | struct iwinfo_crypto_entry c = { 0 }; |
| 469 | |
| 470 | if (!(*func)(ifname, (char *)&c)) |
| 471 | { |
| 472 | iwinfo_L_cryptotable(L, &c); |
| 473 | return 1; |
| 474 | } |
| 475 | |
| 476 | lua_pushnil(L); |
| 477 | return 1; |
| 478 | } |
| 479 | |
| 480 | /* Wrapper for hwmode list */ |
| 481 | static int iwinfo_L_hwmodelist(lua_State *L, int (*func)(const char *, int *)) |
| 482 | { |
| 483 | const char *ifname = luaL_checkstring(L, 1); |
| 484 | int hwmodes = 0; |
| 485 | |
| 486 | if (!(*func)(ifname, &hwmodes)) |
| 487 | { |
| 488 | lua_newtable(L); |
| 489 | |
| 490 | lua_pushboolean(L, hwmodes & IWINFO_80211_A); |
| 491 | lua_setfield(L, -2, "a"); |
| 492 | |
| 493 | lua_pushboolean(L, hwmodes & IWINFO_80211_B); |
| 494 | lua_setfield(L, -2, "b"); |
| 495 | |
| 496 | lua_pushboolean(L, hwmodes & IWINFO_80211_G); |
| 497 | lua_setfield(L, -2, "g"); |
| 498 | |
| 499 | lua_pushboolean(L, hwmodes & IWINFO_80211_N); |
| 500 | lua_setfield(L, -2, "n"); |
| 501 | |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | lua_pushnil(L); |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | /* Wrapper for mbssid_support */ |
| 510 | static int iwinfo_L_mbssid_support(lua_State *L, int (*func)(const char *, int *)) |
| 511 | { |
| 512 | const char *ifname = luaL_checkstring(L, 1); |
| 513 | int support = 0; |
| 514 | |
| 515 | if (!(*func)(ifname, &support)) |
| 516 | { |
| 517 | lua_pushboolean(L, support); |
| 518 | return 1; |
| 519 | } |
| 520 | |
| 521 | lua_pushnil(L); |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | /* Wrapper for hardware_id */ |
| 526 | static int iwinfo_L_hardware_id(lua_State *L, int (*func)(const char *, char *)) |
| 527 | { |
| 528 | const char *ifname = luaL_checkstring(L, 1); |
| 529 | struct iwinfo_hardware_id ids; |
| 530 | |
| 531 | if (!(*func)(ifname, (char *)&ids)) |
| 532 | { |
| 533 | lua_newtable(L); |
| 534 | |
| 535 | lua_pushnumber(L, ids.vendor_id); |
| 536 | lua_setfield(L, -2, "vendor_id"); |
| 537 | |
| 538 | lua_pushnumber(L, ids.device_id); |
| 539 | lua_setfield(L, -2, "device_id"); |
| 540 | |
| 541 | lua_pushnumber(L, ids.subsystem_vendor_id); |
| 542 | lua_setfield(L, -2, "subsystem_vendor_id"); |
| 543 | |
| 544 | lua_pushnumber(L, ids.subsystem_device_id); |
| 545 | lua_setfield(L, -2, "subsystem_device_id"); |
| 546 | } |
| 547 | else |
| 548 | { |
| 549 | lua_pushnil(L); |
| 550 | } |
| 551 | |
| 552 | return 1; |
| 553 | } |
| 554 | |
| 555 | /* Wrapper for country list */ |
| 556 | static char * iwinfo_L_country_lookup(char *buf, int len, int iso3166) |
| 557 | { |
| 558 | int i; |
| 559 | struct iwinfo_country_entry *c; |
| 560 | |
| 561 | for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry)) |
| 562 | { |
| 563 | c = (struct iwinfo_country_entry *) &buf[i]; |
| 564 | |
| 565 | if (c->iso3166 == iso3166) |
| 566 | return c->ccode; |
| 567 | } |
| 568 | |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | static int iwinfo_L_countrylist(lua_State *L, int (*func)(const char *, char *, int *)) |
| 573 | { |
| 574 | int len, i, j; |
| 575 | char rv[IWINFO_BUFSIZE], alpha2[3]; |
| 576 | char *ccode; |
| 577 | const char *ifname = luaL_checkstring(L, 1); |
| 578 | const struct iwinfo_iso3166_label *l; |
| 579 | |
| 580 | lua_newtable(L); |
| 581 | memset(rv, 0, sizeof(rv)); |
| 582 | |
| 583 | if (!(*func)(ifname, rv, &len)) |
| 584 | { |
| 585 | for (l = IWINFO_ISO3166_NAMES, j = 1; l->iso3166; l++) |
| 586 | { |
| 587 | if ((ccode = iwinfo_L_country_lookup(rv, len, l->iso3166)) != NULL) |
| 588 | { |
| 589 | sprintf(alpha2, "%c%c", |
| 590 | (l->iso3166 / 256), (l->iso3166 % 256)); |
| 591 | |
| 592 | lua_newtable(L); |
| 593 | |
| 594 | lua_pushstring(L, alpha2); |
| 595 | lua_setfield(L, -2, "alpha2"); |
| 596 | |
| 597 | lua_pushstring(L, ccode); |
| 598 | lua_setfield(L, -2, "ccode"); |
| 599 | |
| 600 | lua_pushstring(L, l->name); |
| 601 | lua_setfield(L, -2, "name"); |
| 602 | |
| 603 | lua_rawseti(L, -2, j++); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | return 1; |
| 609 | } |
| 610 | |
| 611 | |
| 612 | #ifdef USE_WL |
| 613 | /* Broadcom */ |
| 614 | LUA_WRAP_INT(wl,channel) |
| 615 | LUA_WRAP_INT(wl,frequency) |
| 616 | LUA_WRAP_INT(wl,frequency_offset) |
| 617 | LUA_WRAP_INT(wl,txpower) |
| 618 | LUA_WRAP_INT(wl,txpower_offset) |
| 619 | LUA_WRAP_INT(wl,bitrate) |
| 620 | LUA_WRAP_INT(wl,signal) |
| 621 | LUA_WRAP_INT(wl,noise) |
| 622 | LUA_WRAP_INT(wl,quality) |
| 623 | LUA_WRAP_INT(wl,quality_max) |
| 624 | LUA_WRAP_STRING(wl,ssid) |
| 625 | LUA_WRAP_STRING(wl,bssid) |
| 626 | LUA_WRAP_STRING(wl,country) |
| 627 | LUA_WRAP_STRING(wl,hardware_name) |
| 628 | LUA_WRAP_STRUCT(wl,mode) |
| 629 | LUA_WRAP_STRUCT(wl,assoclist) |
| 630 | LUA_WRAP_STRUCT(wl,txpwrlist) |
| 631 | LUA_WRAP_STRUCT(wl,scanlist) |
| 632 | LUA_WRAP_STRUCT(wl,freqlist) |
| 633 | LUA_WRAP_STRUCT(wl,countrylist) |
| 634 | LUA_WRAP_STRUCT(wl,hwmodelist) |
| 635 | LUA_WRAP_STRUCT(wl,encryption) |
| 636 | LUA_WRAP_STRUCT(wl,mbssid_support) |
| 637 | LUA_WRAP_STRUCT(wl,hardware_id) |
| 638 | #endif |
| 639 | |
| 640 | #ifdef USE_MADWIFI |
| 641 | /* Madwifi */ |
| 642 | LUA_WRAP_INT(madwifi,channel) |
| 643 | LUA_WRAP_INT(madwifi,frequency) |
| 644 | LUA_WRAP_INT(madwifi,frequency_offset) |
| 645 | LUA_WRAP_INT(madwifi,txpower) |
| 646 | LUA_WRAP_INT(madwifi,txpower_offset) |
| 647 | LUA_WRAP_INT(madwifi,bitrate) |
| 648 | LUA_WRAP_INT(madwifi,signal) |
| 649 | LUA_WRAP_INT(madwifi,noise) |
| 650 | LUA_WRAP_INT(madwifi,quality) |
| 651 | LUA_WRAP_INT(madwifi,quality_max) |
| 652 | LUA_WRAP_STRING(madwifi,ssid) |
| 653 | LUA_WRAP_STRING(madwifi,bssid) |
| 654 | LUA_WRAP_STRING(madwifi,country) |
| 655 | LUA_WRAP_STRING(madwifi,hardware_name) |
| 656 | LUA_WRAP_STRUCT(madwifi,mode) |
| 657 | LUA_WRAP_STRUCT(madwifi,assoclist) |
| 658 | LUA_WRAP_STRUCT(madwifi,txpwrlist) |
| 659 | LUA_WRAP_STRUCT(madwifi,scanlist) |
| 660 | LUA_WRAP_STRUCT(madwifi,freqlist) |
| 661 | LUA_WRAP_STRUCT(madwifi,countrylist) |
| 662 | LUA_WRAP_STRUCT(madwifi,hwmodelist) |
| 663 | LUA_WRAP_STRUCT(madwifi,encryption) |
| 664 | LUA_WRAP_STRUCT(madwifi,mbssid_support) |
| 665 | LUA_WRAP_STRUCT(madwifi,hardware_id) |
| 666 | #endif |
| 667 | |
| 668 | #ifdef USE_NL80211 |
| 669 | /* NL80211 */ |
| 670 | LUA_WRAP_INT(nl80211,channel) |
| 671 | LUA_WRAP_INT(nl80211,frequency) |
| 672 | LUA_WRAP_INT(nl80211,frequency_offset) |
| 673 | LUA_WRAP_INT(nl80211,txpower) |
| 674 | LUA_WRAP_INT(nl80211,txpower_offset) |
| 675 | LUA_WRAP_INT(nl80211,bitrate) |
| 676 | LUA_WRAP_INT(nl80211,signal) |
| 677 | LUA_WRAP_INT(nl80211,noise) |
| 678 | LUA_WRAP_INT(nl80211,quality) |
| 679 | LUA_WRAP_INT(nl80211,quality_max) |
| 680 | LUA_WRAP_STRING(nl80211,ssid) |
| 681 | LUA_WRAP_STRING(nl80211,bssid) |
| 682 | LUA_WRAP_STRING(nl80211,country) |
| 683 | LUA_WRAP_STRING(nl80211,hardware_name) |
| 684 | LUA_WRAP_STRUCT(nl80211,mode) |
| 685 | LUA_WRAP_STRUCT(nl80211,assoclist) |
| 686 | LUA_WRAP_STRUCT(nl80211,txpwrlist) |
| 687 | LUA_WRAP_STRUCT(nl80211,scanlist) |
| 688 | LUA_WRAP_STRUCT(nl80211,freqlist) |
| 689 | LUA_WRAP_STRUCT(nl80211,countrylist) |
| 690 | LUA_WRAP_STRUCT(nl80211,hwmodelist) |
| 691 | LUA_WRAP_STRUCT(nl80211,encryption) |
| 692 | LUA_WRAP_STRUCT(nl80211,mbssid_support) |
| 693 | LUA_WRAP_STRUCT(nl80211,hardware_id) |
| 694 | #endif |
| 695 | |
| 696 | /* Wext */ |
| 697 | LUA_WRAP_INT(wext,channel) |
| 698 | LUA_WRAP_INT(wext,frequency) |
| 699 | LUA_WRAP_INT(wext,frequency_offset) |
| 700 | LUA_WRAP_INT(wext,txpower) |
| 701 | LUA_WRAP_INT(wext,txpower_offset) |
| 702 | LUA_WRAP_INT(wext,bitrate) |
| 703 | LUA_WRAP_INT(wext,signal) |
| 704 | LUA_WRAP_INT(wext,noise) |
| 705 | LUA_WRAP_INT(wext,quality) |
| 706 | LUA_WRAP_INT(wext,quality_max) |
| 707 | LUA_WRAP_STRING(wext,ssid) |
| 708 | LUA_WRAP_STRING(wext,bssid) |
| 709 | LUA_WRAP_STRING(wext,country) |
| 710 | LUA_WRAP_STRING(wext,hardware_name) |
| 711 | LUA_WRAP_STRUCT(wext,mode) |
| 712 | LUA_WRAP_STRUCT(wext,assoclist) |
| 713 | LUA_WRAP_STRUCT(wext,txpwrlist) |
| 714 | LUA_WRAP_STRUCT(wext,scanlist) |
| 715 | LUA_WRAP_STRUCT(wext,freqlist) |
| 716 | LUA_WRAP_STRUCT(wext,countrylist) |
| 717 | LUA_WRAP_STRUCT(wext,hwmodelist) |
| 718 | LUA_WRAP_STRUCT(wext,encryption) |
| 719 | LUA_WRAP_STRUCT(wext,mbssid_support) |
| 720 | LUA_WRAP_STRUCT(wext,hardware_id) |
| 721 | |
| 722 | #ifdef USE_WL |
| 723 | /* Broadcom table */ |
| 724 | static const luaL_reg R_wl[] = { |
| 725 | LUA_REG(wl,channel), |
| 726 | LUA_REG(wl,frequency), |
| 727 | LUA_REG(wl,frequency_offset), |
| 728 | LUA_REG(wl,txpower), |
| 729 | LUA_REG(wl,txpower_offset), |
| 730 | LUA_REG(wl,bitrate), |
| 731 | LUA_REG(wl,signal), |
| 732 | LUA_REG(wl,noise), |
| 733 | LUA_REG(wl,quality), |
| 734 | LUA_REG(wl,quality_max), |
| 735 | LUA_REG(wl,mode), |
| 736 | LUA_REG(wl,ssid), |
| 737 | LUA_REG(wl,bssid), |
| 738 | LUA_REG(wl,country), |
| 739 | LUA_REG(wl,assoclist), |
| 740 | LUA_REG(wl,txpwrlist), |
| 741 | LUA_REG(wl,scanlist), |
| 742 | LUA_REG(wl,freqlist), |
| 743 | LUA_REG(wl,countrylist), |
| 744 | LUA_REG(wl,hwmodelist), |
| 745 | LUA_REG(wl,encryption), |
| 746 | LUA_REG(wl,mbssid_support), |
| 747 | LUA_REG(wl,hardware_id), |
| 748 | LUA_REG(wl,hardware_name), |
| 749 | { NULL, NULL } |
| 750 | }; |
| 751 | #endif |
| 752 | |
| 753 | #ifdef USE_MADWIFI |
| 754 | /* Madwifi table */ |
| 755 | static const luaL_reg R_madwifi[] = { |
| 756 | LUA_REG(madwifi,channel), |
| 757 | LUA_REG(madwifi,frequency), |
| 758 | LUA_REG(madwifi,frequency_offset), |
| 759 | LUA_REG(madwifi,txpower), |
| 760 | LUA_REG(madwifi,txpower_offset), |
| 761 | LUA_REG(madwifi,bitrate), |
| 762 | LUA_REG(madwifi,signal), |
| 763 | LUA_REG(madwifi,noise), |
| 764 | LUA_REG(madwifi,quality), |
| 765 | LUA_REG(madwifi,quality_max), |
| 766 | LUA_REG(madwifi,mode), |
| 767 | LUA_REG(madwifi,ssid), |
| 768 | LUA_REG(madwifi,bssid), |
| 769 | LUA_REG(madwifi,country), |
| 770 | LUA_REG(madwifi,assoclist), |
| 771 | LUA_REG(madwifi,txpwrlist), |
| 772 | LUA_REG(madwifi,scanlist), |
| 773 | LUA_REG(madwifi,freqlist), |
| 774 | LUA_REG(madwifi,countrylist), |
| 775 | LUA_REG(madwifi,hwmodelist), |
| 776 | LUA_REG(madwifi,encryption), |
| 777 | LUA_REG(madwifi,mbssid_support), |
| 778 | LUA_REG(madwifi,hardware_id), |
| 779 | LUA_REG(madwifi,hardware_name), |
| 780 | { NULL, NULL } |
| 781 | }; |
| 782 | #endif |
| 783 | |
| 784 | #ifdef USE_NL80211 |
| 785 | /* NL80211 table */ |
| 786 | static const luaL_reg R_nl80211[] = { |
| 787 | LUA_REG(nl80211,channel), |
| 788 | LUA_REG(nl80211,frequency), |
| 789 | LUA_REG(nl80211,frequency_offset), |
| 790 | LUA_REG(nl80211,txpower), |
| 791 | LUA_REG(nl80211,txpower_offset), |
| 792 | LUA_REG(nl80211,bitrate), |
| 793 | LUA_REG(nl80211,signal), |
| 794 | LUA_REG(nl80211,noise), |
| 795 | LUA_REG(nl80211,quality), |
| 796 | LUA_REG(nl80211,quality_max), |
| 797 | LUA_REG(nl80211,mode), |
| 798 | LUA_REG(nl80211,ssid), |
| 799 | LUA_REG(nl80211,bssid), |
| 800 | LUA_REG(nl80211,country), |
| 801 | LUA_REG(nl80211,assoclist), |
| 802 | LUA_REG(nl80211,txpwrlist), |
| 803 | LUA_REG(nl80211,scanlist), |
| 804 | LUA_REG(nl80211,freqlist), |
| 805 | LUA_REG(nl80211,countrylist), |
| 806 | LUA_REG(nl80211,hwmodelist), |
| 807 | LUA_REG(nl80211,encryption), |
| 808 | LUA_REG(nl80211,mbssid_support), |
| 809 | LUA_REG(nl80211,hardware_id), |
| 810 | LUA_REG(nl80211,hardware_name), |
| 811 | { NULL, NULL } |
| 812 | }; |
| 813 | #endif |
| 814 | |
| 815 | /* Wext table */ |
| 816 | static const luaL_reg R_wext[] = { |
| 817 | LUA_REG(wext,channel), |
| 818 | LUA_REG(wext,frequency), |
| 819 | LUA_REG(wext,frequency_offset), |
| 820 | LUA_REG(wext,txpower), |
| 821 | LUA_REG(wext,txpower_offset), |
| 822 | LUA_REG(wext,bitrate), |
| 823 | LUA_REG(wext,signal), |
| 824 | LUA_REG(wext,noise), |
| 825 | LUA_REG(wext,quality), |
| 826 | LUA_REG(wext,quality_max), |
| 827 | LUA_REG(wext,mode), |
| 828 | LUA_REG(wext,ssid), |
| 829 | LUA_REG(wext,bssid), |
| 830 | LUA_REG(wext,country), |
| 831 | LUA_REG(wext,assoclist), |
| 832 | LUA_REG(wext,txpwrlist), |
| 833 | LUA_REG(wext,scanlist), |
| 834 | LUA_REG(wext,freqlist), |
| 835 | LUA_REG(wext,countrylist), |
| 836 | LUA_REG(wext,hwmodelist), |
| 837 | LUA_REG(wext,encryption), |
| 838 | LUA_REG(wext,mbssid_support), |
| 839 | LUA_REG(wext,hardware_id), |
| 840 | LUA_REG(wext,hardware_name), |
| 841 | { NULL, NULL } |
| 842 | }; |
| 843 | |
| 844 | /* Common */ |
| 845 | static const luaL_reg R_common[] = { |
| 846 | { "type", iwinfo_L_type }, |
| 847 | { "__gc", iwinfo_L__gc }, |
| 848 | { NULL, NULL } |
| 849 | }; |
| 850 | |
| 851 | |
| 852 | LUALIB_API int luaopen_iwinfo(lua_State *L) { |
| 853 | luaL_register(L, IWINFO_META, R_common); |
| 854 | |
| 855 | #ifdef USE_WL |
| 856 | luaL_newmetatable(L, IWINFO_WL_META); |
| 857 | luaL_register(L, NULL, R_wl); |
| 858 | lua_pushvalue(L, -1); |
| 859 | lua_setfield(L, -2, "__index"); |
| 860 | lua_setfield(L, -2, "wl"); |
| 861 | #endif |
| 862 | |
| 863 | #ifdef USE_MADWIFI |
| 864 | luaL_newmetatable(L, IWINFO_MADWIFI_META); |
| 865 | luaL_register(L, NULL, R_madwifi); |
| 866 | lua_pushvalue(L, -1); |
| 867 | lua_setfield(L, -2, "__index"); |
| 868 | lua_setfield(L, -2, "madwifi"); |
| 869 | #endif |
| 870 | |
| 871 | #ifdef USE_NL80211 |
| 872 | luaL_newmetatable(L, IWINFO_NL80211_META); |
| 873 | luaL_register(L, NULL, R_nl80211); |
| 874 | lua_pushvalue(L, -1); |
| 875 | lua_setfield(L, -2, "__index"); |
| 876 | lua_setfield(L, -2, "nl80211"); |
| 877 | #endif |
| 878 | |
| 879 | luaL_newmetatable(L, IWINFO_WEXT_META); |
| 880 | luaL_register(L, NULL, R_wext); |
| 881 | lua_pushvalue(L, -1); |
| 882 | lua_setfield(L, -2, "__index"); |
| 883 | lua_setfield(L, -2, "wext"); |
| 884 | |
| 885 | return 1; |
| 886 | } |
| 887 | |