mod_s2s: Remove warning about hostname mismatch
[prosody.git] / util-src / windows.c
blob89bec57bc60a25d51ae503d31cd6a6703d1b1212
1 /* Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8 */
11 * windows.c
12 * Windows support functions for Lua
15 #include <stdio.h>
16 #include <windows.h>
17 #include <windns.h>
19 #include "lua.h"
20 #include "lauxlib.h"
22 #if (LUA_VERSION_NUM == 501)
23 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
24 #endif
26 static int Lget_nameservers(lua_State *L) {
27 char stack_buffer[1024]; // stack allocated buffer
28 IP4_ARRAY *ips = (IP4_ARRAY *) stack_buffer;
29 DWORD len = sizeof(stack_buffer);
30 DNS_STATUS status;
32 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
34 if(status == 0) {
35 DWORD i;
36 lua_createtable(L, ips->AddrCount, 0);
38 for(i = 0; i < ips->AddrCount; i++) {
39 DWORD ip = ips->AddrArray[i];
40 char ip_str[16] = "";
41 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
42 lua_pushstring(L, ip_str);
43 lua_rawseti(L, -2, i + 1);
46 return 1;
47 } else {
48 lua_pushnil(L);
49 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
50 return 2;
54 static int lerror(lua_State *L, char *string) {
55 lua_pushnil(L);
56 lua_pushfstring(L, "%s: %d", string, GetLastError());
57 return 2;
60 static int Lget_consolecolor(lua_State *L) {
61 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
62 WORD color;
63 DWORD read_len;
65 CONSOLE_SCREEN_BUFFER_INFO info;
67 if(console == INVALID_HANDLE_VALUE) {
68 return lerror(L, "GetStdHandle");
71 if(!GetConsoleScreenBufferInfo(console, &info)) {
72 return lerror(L, "GetConsoleScreenBufferInfo");
75 if(!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
76 return lerror(L, "ReadConsoleOutputAttribute");
79 lua_pushnumber(L, color);
80 return 1;
82 static int Lset_consolecolor(lua_State *L) {
83 int color = luaL_checkint(L, 1);
84 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
86 if(console == INVALID_HANDLE_VALUE) {
87 return lerror(L, "GetStdHandle");
90 if(!SetConsoleTextAttribute(console, color)) {
91 return lerror(L, "SetConsoleTextAttribute");
94 lua_pushboolean(L, 1);
95 return 1;
98 static const luaL_Reg Reg[] = {
99 { "get_nameservers", Lget_nameservers },
100 { "get_consolecolor", Lget_consolecolor },
101 { "set_consolecolor", Lset_consolecolor },
102 { NULL, NULL }
105 LUALIB_API int luaopen_util_windows(lua_State *L) {
106 #if (LUA_VERSION_NUM > 501)
107 luaL_checkversion(L);
108 #endif
109 lua_newtable(L);
110 luaL_setfuncs(L, Reg, 0);
111 lua_pushliteral(L, "-3.14");
112 lua_setfield(L, -2, "version");
113 return 1;