util.windows: Initial commit. Adds support for querying the windows DNS API for names...
authorWaqas Hussain <waqas20@gmail.com>
Fri, 30 Oct 2009 23:58:23 +0000 (04:58 +0500)
committerWaqas Hussain <waqas20@gmail.com>
Fri, 30 Oct 2009 23:58:23 +0000 (04:58 +0500)
util-src/windows.c [new file with mode: 0644]

diff --git a/util-src/windows.c b/util-src/windows.c
new file mode 100644 (file)
index 0000000..7fb9631
--- /dev/null
@@ -0,0 +1,45 @@
+\r
+#include <stdio.h>\r
+#include <windows.h>\r
+#include <windns.h>\r
+\r
+#include "lua.h"\r
+#include "lauxlib.h"\r
+\r
+static int Lget_nameservers(lua_State *L) {\r
+       char stack_buffer[1024]; // stack allocated buffer\r
+       IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;\r
+       DWORD len = sizeof(stack_buffer);\r
+       DNS_STATUS status;\r
+\r
+       status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);\r
+       if (status == 0) {\r
+               DWORD i;\r
+               lua_createtable(L, ips->AddrCount, 0);\r
+               for (i = 0; i < ips->AddrCount; i++) {\r
+                       DWORD ip = ips->AddrArray[i];\r
+                       char ip_str[16] = "";\r
+                       sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);\r
+                       lua_pushstring(L, ip_str);\r
+                       lua_rawseti(L, -2, i+1);\r
+               }\r
+               return 1;\r
+       } else {\r
+               luaL_error(L, "DnsQueryConfig returned %d", status);\r
+               return 0; // unreachable, but prevents a compiler warning\r
+       }\r
+}\r
+\r
+static const luaL_Reg Reg[] =\r
+{\r
+       { "get_nameservers",    Lget_nameservers        },\r
+       { NULL,         NULL    }\r
+};\r
+\r
+LUALIB_API int luaopen_util_windows(lua_State *L) {\r
+       luaL_register(L, "windows", Reg);\r
+       lua_pushliteral(L, "version");                  /** version */\r
+       lua_pushliteral(L, "-3.14");\r
+       lua_settable(L,-3);\r
+       return 1;\r
+}\r