+\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
+ DWORD size = 0;\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
+ output = new char[size+1];\r
+\r
+ if ((RegQueryValueEx(regkey, key, 0, NULL, (LPBYTE)output, &size) != ERROR_SUCCESS) || (!*output))\r
+ {\r
+ delete output;\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\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 top;\r
+\r
+ while (RegEnumKeyEx(regkey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)\r
+ {\r
+ size = 39;\r
+ if (RegOpenKeyEx(regkey, buf, 0, KEY_QUERY_VALUE, &top) != ERROR_SUCCESS)\r
+ continue;\r
+ int rc = GetNameServer(top, key, output);\r
+ RegCloseKey(top);\r
+ if (rc)\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+\r
+std::string FindNameServerWin()\r
+{\r
+ std::string returnval = "127.0.0.1";\r
+ HKEY top, key;\r
+ char* dns = NULL;\r
+\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
+ /* 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
+ returnval = dns;\r
+ delete dns;\r
+ }\r
+ }\r
+ RegCloseKey(key);\r
+ RegCloseKey(top);\r
+ }\r
+ return returnval;\r
+}\r
+\r
+\r
+void ClearConsole()\r
+{\r
+ COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */\r
+ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);\r
+ DWORD cCharsWritten;\r
+ CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ \r
+ DWORD dwConSize; /* number of character cells in the current buffer */ \r
+\r
+ /* get the number of character cells in the current buffer */ \r
+\r
+ if (GetConsoleScreenBufferInfo( hConsole, &csbi ))\r
+ {\r
+ dwConSize = csbi.dwSize.X * csbi.dwSize.Y;\r
+ /* fill the entire screen with blanks */ \r
+ if (FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))\r
+ {\r
+ /* get the current text attribute */ \r
+ if (GetConsoleScreenBufferInfo( hConsole, &csbi ))\r
+ {\r
+ /* now set the buffer's attributes accordingly */\r
+ if (FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))\r
+ {\r
+ /* put the cursor at (0, 0) */\r
+ SetConsoleCursorPosition( hConsole, coordScreen );\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return;\r
+}\r