1 /* +------------------------------------+
\r
2 * | Inspire Internet Relay Chat Daemon |
\r
3 * +------------------------------------+
\r
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
\r
6 * See: http://www.inspircd.org/wiki/index.php/Credits
\r
8 * This program is free but copyrighted software; see
\r
9 * the file COPYING for details.
\r
11 * ---------------------------------------------------
\r
14 #include "inspircd_win32wrapper.h"
\r
15 #include "inspircd.h"
\r
19 using namespace std;
\r
22 #define INADDR_NONE 0xffffffff
\r
27 int inet_aton(const char *cp, struct in_addr *addr)
\r
29 unsigned long ip = inet_addr(cp);
\r
31 return (addr->s_addr == INADDR_NONE) ? 0 : 1;
\r
34 const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
\r
39 struct sockaddr_in in;
\r
40 memset(&in, 0, sizeof(in));
\r
41 in.sin_family = AF_INET;
\r
42 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
\r
43 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
\r
46 else if (af == AF_INET6)
\r
48 struct sockaddr_in6 in;
\r
49 memset(&in, 0, sizeof(in));
\r
50 in.sin6_family = AF_INET6;
\r
51 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
\r
52 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
\r
63 int inet_pton(int af, const char *src, void *dst)
\r
66 int len = sizeof(SOCKADDR);
\r
67 int rv = WSAStringToAddress((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len);
\r
70 if(WSAGetLastError() == 10022) // Invalid Argument
\r
75 memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
\r
79 char * strtok_r(char *_String, const char *_Control, char **_Context)
\r
82 const unsigned char *ctl = (const unsigned char*)_Control;
\r
83 unsigned char map[32];
\r
85 if (_Context == 0 || !_Control)
\r
88 if (!(_String != NULL || *_Context != NULL))
\r
94 map[*ctl >> 3] |= (1 << (*ctl & 7));
\r
97 /* If string is NULL, set str to the saved
\r
98 * pointer (i.e., continue breaking tokens out of the string
\r
99 * from the last strtok call) */
\r
100 if (_String != NULL)
\r
102 str = (unsigned char*)_String;
\r
106 str = (unsigned char*)*_Context;
\r
109 /* Find beginning of token (skip over leading delimiters). Note that
\r
110 * there is no token iff this loop sets str to point to the terminal
\r
111 * null (*str == 0) */
\r
112 while ((map[*str >> 3] & (1 << (*str & 7))) && *str != 0)
\r
117 _String = (char*)str;
\r
119 /* Find the end of the token. If it is not the end of the string,
\r
120 * put a null there. */
\r
121 for ( ; *str != 0 ; str++ )
\r
123 if (map[*str >> 3] & (1 << (*str & 7)))
\r
130 /* Update context */
\r
131 *_Context = (char*)str;
\r
133 /* Determine if a token has been found. */
\r
134 if (_String == (char*)str)
\r
144 void setcolor(int color_code)
\r
146 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_code);
\r
149 DIR * opendir(const char * path)
\r
151 std::string search_path = string(path) + "\\*.*";
\r
152 WIN32_FIND_DATA fd;
\r
153 HANDLE f = FindFirstFile(search_path.c_str(), &fd);
\r
154 if (f != INVALID_HANDLE_VALUE)
\r
157 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
\r
158 d->find_handle = f;
\r
168 dirent * readdir(DIR * handle)
\r
171 handle->first = false;
\r
174 if (!FindNextFile(handle->find_handle, &handle->find_data))
\r
178 strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
\r
179 return &handle->dirent_pointer;
\r
182 void closedir(DIR * handle)
\r
184 FindClose(handle->find_handle);
\r
188 const char * dlerror()
\r
190 static char errormessage[500];
\r
191 DWORD error = GetLastError();
\r
196 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)errormessage, 500, 0);
\r
197 return errormessage;
\r
200 int printf_c(const char * format, ...)
\r
202 // Better hope we're not multithreaded, otherwise we'll have chickens crossing the road other side to get the to :P
\r
203 static char message[500];
\r
204 static char temp[10];
\r
205 int color1, color2;
\r
207 /* parse arguments */
\r
209 va_start(ap, format);
\r
210 vsnprintf(message, 500, format, ap);
\r
213 /* search for unix-style escape sequences */
\r
216 const char * p = message;
\r
221 // Escape sequence -> copy into the temp buffer, and parse the color.
\r
232 if (!_stricmp(temp, "[0"))
\r
234 // Returning to normal colour.
\r
235 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
\r
237 else if (sscanf(temp, "[%u;%u", &color1, &color2) == 2)
\r
242 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
\r
245 default: // Unknown
\r
246 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
\r
253 sprintf("Unknown color code: %s", temp);
\r
254 MessageBox(0, message, message, MB_OK);
\r
266 int arg_counter = 1;
\r
268 int getopt_long_only(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
\r
270 // burlex todo: handle the shortops, at the moment it only works with longopts.
\r
272 if (___argc == 1 || arg_counter == ___argc) // No arguments (apart from filename)
\r
275 const char * opt = ___argv[arg_counter];
\r
276 int return_val = 0;
\r
278 // if we're not an option, return an error.
\r
279 if (strnicmp(opt, "--", 2) != 0)
\r
285 // parse argument list
\r
287 for (; __longopts[i].name != 0; ++i)
\r
289 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
\r
291 // woot, found a valid argument =)
\r
293 if ((arg_counter + 1) != ___argc)
\r
295 // grab the parameter from the next argument (if its not another argument)
\r
296 if (strnicmp(___argv[arg_counter+1], "--", 2) != 0)
\r
298 arg_counter++; // Trash this next argument, we won't be needing it.
\r
299 par = ___argv[arg_counter];
\r
303 // increment the argument for next time
\r
306 // determine action based on type
\r
307 if (__longopts[i].has_arg == required_argument && !par)
\r
309 // parameter missing and its a required parameter option
\r
313 // store argument in optarg
\r
315 strncpy(optarg, par, 514);
\r
317 if (__longopts[i].flag != 0)
\r
319 // this is a variable, we have to set it if this argument is found.
\r
320 *__longopts[i].flag = 1;
\r
325 if (__longopts[i].val == -1 || par == 0)
\r
328 return __longopts[i].val;
\r
334 // return 1 (invalid argument)
\r
339 #define IPC_MESSAGE_REHASH 1
\r
340 #define IPC_MESSAGE_DIE 2
\r
341 #define IPC_MESSAGE_RESTART 3
\r
345 static DWORD buflen = 1024;
\r
346 static const char * pipename = "\\\\.\\mailslot\\Inspircd";
\r
347 hIPCPipe = CreateMailslot(pipename, buflen, 0, 0);
\r
348 if (hIPCPipe == INVALID_HANDLE_VALUE)
\r
349 printf("IPC Pipe could not be created. Are you sure you didn't start InspIRCd twice?\n");
\r
352 void CheckIPC(InspIRCd * Instance)
\r
354 if (hIPCPipe == INVALID_HANDLE_VALUE)
\r
360 BOOL res = ReadFile(hIPCPipe, &action, sizeof(DWORD), &bytes, 0);
\r
363 if (GetLastError() != ERROR_SEM_TIMEOUT)
\r
364 Instance->Log(DEFAULT, "IPC Pipe Error %u: %s", GetLastError(), dlerror());
\r
370 case IPC_MESSAGE_REHASH:
\r
371 InspIRCd::Rehash(0);
\r
374 case IPC_MESSAGE_DIE:
\r
378 case IPC_MESSAGE_RESTART:
\r
379 Instance->Restart("IPC_MESSAGE_RESTART received by mailslot.");
\r
386 CloseHandle(hIPCPipe);
\r
390 /* These three functions were created from looking at how ares does it
\r
391 * (...and they look far tidier in C++)
\r
394 /* Get active nameserver */
\r
395 bool GetNameServer(HKEY regkey, const char *key, char* &output)
\r
398 DWORD result = RegQueryValueEx(regkey, key, 0, NULL, NULL, &size);
\r
399 if (((result != ERROR_SUCCESS) && (result != ERROR_MORE_DATA)) || (!size))
\r
402 output = new char[size+1];
\r
404 if ((RegQueryValueEx(regkey, key, 0, NULL, (LPBYTE)output, &size) != ERROR_SUCCESS) || (!*output))
\r
412 /* Check a network interface for its nameserver */
\r
413 bool GetInterface(HKEY regkey, const char *key, char* &output)
\r
420 while (RegEnumKeyEx(regkey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
\r
423 if (RegOpenKeyEx(regkey, buf, 0, KEY_QUERY_VALUE, &top) != ERROR_SUCCESS)
\r
425 int rc = GetNameServer(top, key, output);
\r
434 std::string FindNameServerWin()
\r
436 std::string returnval = "127.0.0.1";
\r
440 /* Lets see if the correct registry hive and tree exist */
\r
441 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &top) == ERROR_SUCCESS)
\r
443 /* If they do, attempt to get the nameserver name */
\r
444 RegOpenKeyEx(top, "Interfaces", 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &key);
\r
445 if ((GetNameServer(top, "NameServer", dns)) || (GetNameServer(top, "DhcpNameServer", dns))
\r
446 || (GetInterface(key, "NameServer", dns)) || (GetInterface(key, "DhcpNameServer", dns)))
\r
461 void ClearConsole()
\r
463 COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
\r
464 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
\r
465 DWORD cCharsWritten;
\r
466 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
\r
467 DWORD dwConSize; /* number of character cells in the current buffer */
\r
469 /* get the number of character cells in the current buffer */
\r
471 if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
\r
473 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
\r
474 /* fill the entire screen with blanks */
\r
475 if (FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))
\r
477 /* get the current text attribute */
\r
478 if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
\r
480 /* now set the buffer's attributes accordingly */
\r
481 if (FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
\r
483 /* put the cursor at (0, 0) */
\r
484 SetConsoleCursorPosition( hConsole, coordScreen );
\r