From 428aa503684e0f3d79730a1c7336db734846ffb6 Mon Sep 17 00:00:00 2001 From: brain Date: Tue, 22 May 2007 20:03:33 +0000 Subject: Code to find nameservers in the windows registry - largely untested git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@7113 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/configreader.cpp | 4 +++ win/inspircd_win32wrapper.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++ win/inspircd_win32wrapper.h | 4 +++ 3 files changed, 76 insertions(+) diff --git a/src/configreader.cpp b/src/configreader.cpp index 36cafd111..4702ce816 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -221,6 +221,9 @@ bool ValidateDnsServer(ServerConfig* conf, const char* tag, const char* value, V { if (!*(data.GetString())) { +#ifdef WINDOWS + data.Set(FindNameServerWin().c_str()); +#else // attempt to look up their nameserver from /etc/resolv.conf conf->GetInstance()->Log(DEFAULT,"WARNING: not defined, attempting to find working server in /etc/resolv.conf..."); ifstream resolv("/etc/resolv.conf"); @@ -251,6 +254,7 @@ bool ValidateDnsServer(ServerConfig* conf, const char* tag, const char* value, V conf->GetInstance()->Log(DEFAULT,"/etc/resolv.conf can't be opened! Defaulting to nameserver '127.0.0.1'!"); data.Set("127.0.0.1"); } +#endif } return true; } diff --git a/win/inspircd_win32wrapper.cpp b/win/inspircd_win32wrapper.cpp index ece823a4d..27cd15d0d 100644 --- a/win/inspircd_win32wrapper.cpp +++ b/win/inspircd_win32wrapper.cpp @@ -9,6 +9,7 @@ using namespace std; #define INADDR_NONE 0xffffffff #endif + HANDLE hIPCPipe; int inet_aton(const char *cp, struct in_addr *addr) @@ -366,3 +367,70 @@ void CloseIPC() CloseHandle(hIPCPipe); } + +bool GetNameServer(HKEY hKey, const char *subkey, char* &obuf) +{ + /* Test for the size we need */ + DWORD size = 0; + DWORD result; + + result = RegQueryValueEx(hKey, subkey, 0, NULL, NULL, &size); + if (((result != ERROR_SUCCESS) && (result != ERROR_MORE_DATA)) || (!size)) + return false; + + obuf = new char[size+1]; + + if ((RegQueryValueEx(hKey, subkey, 0, NULL, (LPBYTE)obuf, &size) != ERROR_SUCCESS) || (!*obuf)) + { + delete obuf; + return false; + } + return true; +} + +bool GetInterface(HKEY hKey, const char *subkey, char* &obuf) +{ + char buf[39]; + DWORD size = 39; + int idx = 0; + HKEY hVal; + + while (RegEnumKeyEx(hKey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS) + { + int rc; + size = 39; + if (RegOpenKeyEx(hKey, buf, 0, KEY_QUERY_VALUE, &hVal) != ERROR_SUCCESS) + continue; + rc = GetNameServer(hVal, subkey, obuf); + RegCloseKey(hVal); + if (rc) + return true; + } + return false; +} + + +std::string FindNameServerWin() +{ + std::string returnval; + HKEY mykey; + HKEY subkey; + char* dns = NULL; + + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &mykey) == ERROR_SUCCESS) + { + RegOpenKeyEx(mykey, "Interfaces", 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &subkey); + if ((GetNameServer(mykey, "NameServer", dns)) || (GetNameServer(mykey, "DhcpNameServer", dns)) + || (GetInterface(subkey, "NameServer", dns)) || (GetInterface(subkey, "DhcpNameServer", dns))) + { + if (dns) + { + returnval = dns; + delete dns; + } + } + RegCloseKey(subkey); + RegCloseKey(mykey); + } + return returnval; +} diff --git a/win/inspircd_win32wrapper.h b/win/inspircd_win32wrapper.h index f8506c97b..dc2fbb705 100644 --- a/win/inspircd_win32wrapper.h +++ b/win/inspircd_win32wrapper.h @@ -42,6 +42,8 @@ #define _CRT_SECURE_NO_DEPRECATE #define _SCL_SECURE_NO_DEPRECATE +#include + /* Say we're building on windows 2000. Anyone running something older than this * reeeeeeeally needs to upgrade! */ @@ -168,5 +170,7 @@ void InitIPC(); void CheckIPC(InspIRCd * Instance); void CloseIPC(); +std::string FindNameServerWin(); + #endif -- cgit v1.2.3