Comparison

util-src/windows.c @ 2062:699686c8d016

util.windows: Initial commit. Adds support for querying the windows DNS API for nameservers.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 31 Oct 2009 04:58:23 +0500
child 2923:b7049746bd29
comparison
equal deleted inserted replaced
2061:e34fdca432a9 2062:699686c8d016
1
2 #include <stdio.h>
3 #include <windows.h>
4 #include <windns.h>
5
6 #include "lua.h"
7 #include "lauxlib.h"
8
9 static int Lget_nameservers(lua_State *L) {
10 char stack_buffer[1024]; // stack allocated buffer
11 IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;
12 DWORD len = sizeof(stack_buffer);
13 DNS_STATUS status;
14
15 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
16 if (status == 0) {
17 DWORD i;
18 lua_createtable(L, ips->AddrCount, 0);
19 for (i = 0; i < ips->AddrCount; i++) {
20 DWORD ip = ips->AddrArray[i];
21 char ip_str[16] = "";
22 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
23 lua_pushstring(L, ip_str);
24 lua_rawseti(L, -2, i+1);
25 }
26 return 1;
27 } else {
28 luaL_error(L, "DnsQueryConfig returned %d", status);
29 return 0; // unreachable, but prevents a compiler warning
30 }
31 }
32
33 static const luaL_Reg Reg[] =
34 {
35 { "get_nameservers", Lget_nameservers },
36 { NULL, NULL }
37 };
38
39 LUALIB_API int luaopen_util_windows(lua_State *L) {
40 luaL_register(L, "windows", Reg);
41 lua_pushliteral(L, "version"); /** version */
42 lua_pushliteral(L, "-3.14");
43 lua_settable(L,-3);
44 return 1;
45 }