]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - win/inspircd_win32wrapper.cpp
- Fix two minor warnings here (catch (ModuleException& e) -> catch (ModuleException...
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.cpp
index 27cd15d0d16a3b36708fca4e9404a94ec3e0b6c8..1cdd46444096c2b981a81fca4375328728fce334 100644 (file)
@@ -368,41 +368,43 @@ void CloseIPC()
 }\r
 \r
 \r
-bool GetNameServer(HKEY hKey, const char *subkey, char* &obuf)\r
+/* These three functions were created from looking at how ares does it\r
+ * (...and they look far tidier in C++)\r
+ */\r
+\r
+/* Get active nameserver */\r
+bool GetNameServer(HKEY regkey, const char *key, char* &output)\r
 {\r
-       /* Test for the size we need */\r
        DWORD size = 0;\r
-       DWORD result;\r
-\r
-       result = RegQueryValueEx(hKey, subkey, 0, NULL, NULL, &size);\r
+       DWORD result = RegQueryValueEx(regkey, key, 0, NULL, NULL, &size);\r
        if (((result != ERROR_SUCCESS) && (result != ERROR_MORE_DATA)) || (!size))\r
                return false;\r
 \r
-       obuf = new char[size+1];\r
+       output = new char[size+1];\r
 \r
-       if ((RegQueryValueEx(hKey, subkey, 0, NULL, (LPBYTE)obuf, &size) != ERROR_SUCCESS) || (!*obuf))\r
+       if ((RegQueryValueEx(regkey, key, 0, NULL, (LPBYTE)output, &size) != ERROR_SUCCESS) || (!*output))\r
        {\r
-               delete obuf;\r
+               delete output;\r
                return false;\r
        }\r
        return true;\r
 }\r
 \r
-bool GetInterface(HKEY hKey, const char *subkey, char* &obuf)\r
+/* Check a network interface for its nameserver */\r
+bool GetInterface(HKEY regkey, const char *key, char* &output)\r
 {\r
        char buf[39];\r
        DWORD size = 39;\r
        int idx = 0;\r
-       HKEY hVal;\r
+       HKEY top;\r
 \r
-       while (RegEnumKeyEx(hKey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)\r
+       while (RegEnumKeyEx(regkey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)\r
        {\r
-               int rc;\r
                size = 39;\r
-               if (RegOpenKeyEx(hKey, buf, 0, KEY_QUERY_VALUE, &hVal) != ERROR_SUCCESS)\r
+               if (RegOpenKeyEx(regkey, buf, 0, KEY_QUERY_VALUE, &top) != ERROR_SUCCESS)\r
                        continue;\r
-               rc = GetNameServer(hVal, subkey, obuf);\r
-               RegCloseKey(hVal);\r
+               int rc = GetNameServer(top, key, output);\r
+               RegCloseKey(top);\r
                if (rc)\r
                        return true;\r
        }\r
@@ -413,15 +415,16 @@ bool GetInterface(HKEY hKey, const char *subkey, char* &obuf)
 std::string FindNameServerWin()\r
 {\r
        std::string returnval;\r
-       HKEY mykey;\r
-       HKEY subkey;\r
+       HKEY top, key;\r
        char* dns = NULL;\r
 \r
-       if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &mykey) == ERROR_SUCCESS)\r
+       /* Lets see if the correct registry hive and tree exist */\r
+       if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &top) == ERROR_SUCCESS)\r
        {\r
-               RegOpenKeyEx(mykey, "Interfaces", 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &subkey);\r
-               if ((GetNameServer(mykey, "NameServer", dns)) || (GetNameServer(mykey, "DhcpNameServer", dns))\r
-                       || (GetInterface(subkey, "NameServer", dns)) || (GetInterface(subkey, "DhcpNameServer", dns)))\r
+               /* If they do, attempt to get the nameserver name */\r
+               RegOpenKeyEx(top, "Interfaces", 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &key);\r
+               if ((GetNameServer(top, "NameServer", dns)) || (GetNameServer(top, "DhcpNameServer", dns))\r
+                       || (GetInterface(key, "NameServer", dns)) || (GetInterface(key, "DhcpNameServer", dns)))\r
                {\r
                        if (dns)\r
                        {\r
@@ -429,8 +432,9 @@ std::string FindNameServerWin()
                                delete dns;\r
                        }\r
                }\r
-               RegCloseKey(subkey);\r
-               RegCloseKey(mykey);\r
+               RegCloseKey(key);\r
+               RegCloseKey(top);\r
        }\r
        return returnval;\r
 }\r
+\r