]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - win/inspircd_win32wrapper.cpp
Fix problem where in certain cases a \0 would be sent out. Thx to network Hak5IRC...
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.cpp
index 27cd15d0d16a3b36708fca4e9404a94ec3e0b6c8..d2dcd7456fb6aa3d7901c81ea94a38a05fb1519c 100644 (file)
@@ -1,3 +1,16 @@
+/*       +------------------------------------+\r
+ *       | Inspire Internet Relay Chat Daemon |\r
+ *       +------------------------------------+\r
+ *\r
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team\r
+ * See: http://www.inspircd.org/wiki/index.php/Credits\r
+ *\r
+ * This program is free but copyrighted software; see\r
+ *            the file COPYING for details.\r
+ *\r
+ * ---------------------------------------------------\r
+ */\r
+\r
 #include "inspircd_win32wrapper.h"\r
 #include "inspircd.h"\r
 #include <string>\r
@@ -9,7 +22,6 @@ using namespace std;
 #define INADDR_NONE 0xffffffff\r
 #endif\r
 \r
-\r
 HANDLE hIPCPipe;\r
 \r
 int inet_aton(const char *cp, struct in_addr *addr)\r
@@ -53,6 +65,13 @@ int inet_pton(int af, const char *src, void *dst)
        sockaddr_in sa;\r
        int len = sizeof(SOCKADDR);\r
        int rv = WSAStringToAddress((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len);\r
+       if(rv >= 0)\r
+       {\r
+               if(WSAGetLastError() == 10022)                  // Invalid Argument\r
+                       rv = 0;\r
+               else\r
+                       rv = 1;\r
+       }\r
        memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));\r
        return rv;\r
 }\r
@@ -368,41 +387,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
@@ -412,16 +433,17 @@ bool GetInterface(HKEY hKey, const char *subkey, char* &obuf)
 \r
 std::string FindNameServerWin()\r
 {\r
-       std::string returnval;\r
-       HKEY mykey;\r
-       HKEY subkey;\r
+       std::string returnval = "127.0.0.1";\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 +451,40 @@ 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
+\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