X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=win%2Finspircd_win32wrapper.cpp;h=3a929d09e375bcc175e0d5fdeebaaf0761a5e54f;hb=6a48a6cc9b6b37c55e0dae5f847a23a5d343be49;hp=ef04b4fc86935bb3f71fd367d6b804e24da6d8b1;hpb=ce92c4d35cad254777c471abfcbc0fd775b2da3f;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/win/inspircd_win32wrapper.cpp b/win/inspircd_win32wrapper.cpp index ef04b4fc8..3a929d09e 100644 --- a/win/inspircd_win32wrapper.cpp +++ b/win/inspircd_win32wrapper.cpp @@ -1,3 +1,16 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2007 InspIRCd Development Team + * See: http://www.inspircd.org/wiki/index.php/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + #include "inspircd_win32wrapper.h" #include "inspircd.h" #include @@ -52,6 +65,13 @@ int inet_pton(int af, const char *src, void *dst) sockaddr_in sa; int len = sizeof(SOCKADDR); int rv = WSAStringToAddress((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len); + if(rv >= 0) + { + if(WSAGetLastError() == 10022) // Invalid Argument + rv = 0; + else + rv = 1; + } memcpy(dst, &sa.sin_addr, sizeof(struct in_addr)); return rv; } @@ -177,72 +197,6 @@ const char * dlerror() return errormessage; } -int printf_c(const char * format, ...) -{ - // Better hope we're not multithreaded, otherwise we'll have chickens crossing the road other side to get the to :P - static char message[500]; - static char temp[10]; - int color1, color2; - - /* parse arguments */ - va_list ap; - va_start(ap, format); - vsnprintf(message, 500, format, ap); - va_end(ap); - - /* search for unix-style escape sequences */ - int t; - int c = 0; - const char * p = message; - while(*p != 0) - { - if (*p == '\033') - { - // Escape sequence -> copy into the temp buffer, and parse the color. - p++; - t = 0; - while(*p != 'm') - { - temp[t++] = *p; - ++p; - } - - temp[t] = 0; - p++; - if (!stricmp(temp, "[0")) - { - // Returning to normal colour. - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); - } - else if (sscanf(temp, "[%u;%u", &color1, &color2) == 2) - { - switch(color2) - { - case 32: // Green - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY); - break; - - default: // Unknown - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); - break; - } - } - else - { - char message[50]; - sprintf("Unknown color code: %s", temp); - MessageBox(0, message, message, MB_OK); - } - } - - putchar(*p); - ++c; - ++p; - } - - return c; -} - int arg_counter = 1; char optarg[514]; int getopt_long_only(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind) @@ -366,3 +320,105 @@ void CloseIPC() CloseHandle(hIPCPipe); } + +/* These three functions were created from looking at how ares does it + * (...and they look far tidier in C++) + */ + +/* Get active nameserver */ +bool GetNameServer(HKEY regkey, const char *key, char* &output) +{ + DWORD size = 0; + DWORD result = RegQueryValueEx(regkey, key, 0, NULL, NULL, &size); + if (((result != ERROR_SUCCESS) && (result != ERROR_MORE_DATA)) || (!size)) + return false; + + output = new char[size+1]; + + if ((RegQueryValueEx(regkey, key, 0, NULL, (LPBYTE)output, &size) != ERROR_SUCCESS) || (!*output)) + { + delete output; + return false; + } + return true; +} + +/* Check a network interface for its nameserver */ +bool GetInterface(HKEY regkey, const char *key, char* &output) +{ + char buf[39]; + DWORD size = 39; + int idx = 0; + HKEY top; + + while (RegEnumKeyEx(regkey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS) + { + size = 39; + if (RegOpenKeyEx(regkey, buf, 0, KEY_QUERY_VALUE, &top) != ERROR_SUCCESS) + continue; + int rc = GetNameServer(top, key, output); + RegCloseKey(top); + if (rc) + return true; + } + return false; +} + + +std::string FindNameServerWin() +{ + std::string returnval = "127.0.0.1"; + HKEY top, key; + char* dns = NULL; + + /* Lets see if the correct registry hive and tree exist */ + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &top) == ERROR_SUCCESS) + { + /* If they do, attempt to get the nameserver name */ + RegOpenKeyEx(top, "Interfaces", 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &key); + if ((GetNameServer(top, "NameServer", dns)) || (GetNameServer(top, "DhcpNameServer", dns)) + || (GetInterface(key, "NameServer", dns)) || (GetInterface(key, "DhcpNameServer", dns))) + { + if (dns) + { + returnval = dns; + delete dns; + } + } + RegCloseKey(key); + RegCloseKey(top); + } + return returnval; +} + + +void ClearConsole() +{ + COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD cCharsWritten; + CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ + DWORD dwConSize; /* number of character cells in the current buffer */ + + /* get the number of character cells in the current buffer */ + + if (GetConsoleScreenBufferInfo( hConsole, &csbi )) + { + dwConSize = csbi.dwSize.X * csbi.dwSize.Y; + /* fill the entire screen with blanks */ + if (FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten )) + { + /* get the current text attribute */ + if (GetConsoleScreenBufferInfo( hConsole, &csbi )) + { + /* now set the buffer's attributes accordingly */ + if (FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten )) + { + /* put the cursor at (0, 0) */ + SetConsoleCursorPosition( hConsole, coordScreen ); + } + } + } + } + return; +}