1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #include "inspircd_win32wrapper.h"
22 #define INADDR_NONE 0xffffffff
27 int inet_aton(const char *cp, struct in_addr *addr)
29 unsigned long ip = inet_addr(cp);
31 return (addr->s_addr == INADDR_NONE) ? 0 : 1;
34 const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
39 struct sockaddr_in in;
40 memset(&in, 0, sizeof(in));
41 in.sin_family = AF_INET;
42 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
43 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
46 else if (af == AF_INET6)
48 struct sockaddr_in6 in;
49 memset(&in, 0, sizeof(in));
50 in.sin6_family = AF_INET6;
51 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
52 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
63 int inet_pton(int af, const char *src, void *dst)
66 int len = sizeof(SOCKADDR);
67 int rv = WSAStringToAddress((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len);
70 if(WSAGetLastError() == 10022) // Invalid Argument
75 memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
79 char * strtok_r(char *_String, const char *_Control, char **_Context)
82 const unsigned char *ctl = (const unsigned char*)_Control;
83 unsigned char map[32];
85 if (_Context == 0 || !_Control)
88 if (!(_String != NULL || *_Context != NULL))
94 map[*ctl >> 3] |= (1 << (*ctl & 7));
97 /* If string is NULL, set str to the saved
98 * pointer (i.e., continue breaking tokens out of the string
99 * from the last strtok call) */
102 str = (unsigned char*)_String;
106 str = (unsigned char*)*_Context;
109 /* Find beginning of token (skip over leading delimiters). Note that
110 * there is no token iff this loop sets str to point to the terminal
111 * null (*str == 0) */
112 while ((map[*str >> 3] & (1 << (*str & 7))) && *str != 0)
117 _String = (char*)str;
119 /* Find the end of the token. If it is not the end of the string,
120 * put a null there. */
121 for ( ; *str != 0 ; str++ )
123 if (map[*str >> 3] & (1 << (*str & 7)))
131 *_Context = (char*)str;
133 /* Determine if a token has been found. */
134 if (_String == (char*)str)
144 void setcolor(int color_code)
146 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_code);
149 DIR * opendir(const char * path)
151 std::string search_path = string(path) + "\\*.*";
153 HANDLE f = FindFirstFile(search_path.c_str(), &fd);
154 if (f != INVALID_HANDLE_VALUE)
157 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
168 dirent * readdir(DIR * handle)
171 handle->first = false;
174 if (!FindNextFile(handle->find_handle, &handle->find_data))
178 strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
179 return &handle->dirent_pointer;
182 void closedir(DIR * handle)
184 FindClose(handle->find_handle);
188 const char * dlerror()
190 static char errormessage[500];
191 DWORD error = GetLastError();
196 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)errormessage, 500, 0);
200 #define TRED FOREGROUND_RED | FOREGROUND_INTENSITY
201 #define TGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
202 #define TYELLOW FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
203 #define TNORMAL FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE
204 #define TWHITE TNORMAL | FOREGROUND_INTENSITY
205 #define TBLUE FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
207 /* Handles colors in printf */
208 int printf_c(const char * format, ...)
210 // Better hope we're not multithreaded, otherwise we'll have chickens crossing the road other side to get the to :P
211 static char message[MAXBUF];
212 static char temp[MAXBUF];
215 /* parse arguments */
217 va_start(ap, format);
218 vsnprintf(message, 500, format, ap);
221 /* search for unix-style escape sequences */
224 const char * p = message;
229 // Escape sequence -> copy into the temp buffer, and parse the color.
232 while ((*p) && (*p != 'm'))
243 if (sscanf(temp, "[%u;%u", &color1, &color2) == 2)
248 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Yellow
253 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
262 // Returning to normal colour.
263 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
268 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), TWHITE);
273 sprintf(message, "Unknown color code: %s", temp);
274 MessageBox(0, message, message, MB_OK);
291 int getopt_long_only(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
293 // burlex todo: handle the shortops, at the moment it only works with longopts.
295 if (___argc == 1 || arg_counter == ___argc) // No arguments (apart from filename)
298 const char * opt = ___argv[arg_counter];
301 // if we're not an option, return an error.
302 if (strnicmp(opt, "--", 2) != 0)
308 // parse argument list
310 for (; __longopts[i].name != 0; ++i)
312 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
314 // woot, found a valid argument =)
316 if ((arg_counter + 1) != ___argc)
318 // grab the parameter from the next argument (if its not another argument)
319 if (strnicmp(___argv[arg_counter+1], "--", 2) != 0)
321 arg_counter++; // Trash this next argument, we won't be needing it.
322 par = ___argv[arg_counter];
326 // increment the argument for next time
329 // determine action based on type
330 if (__longopts[i].has_arg == required_argument && !par)
332 // parameter missing and its a required parameter option
336 // store argument in optarg
338 strncpy(optarg, par, 514);
340 if (__longopts[i].flag != 0)
342 // this is a variable, we have to set it if this argument is found.
343 *__longopts[i].flag = 1;
348 if (__longopts[i].val == -1 || par == 0)
351 return __longopts[i].val;
357 // return 1 (invalid argument)
362 #define IPC_MESSAGE_REHASH 1
363 #define IPC_MESSAGE_DIE 2
364 #define IPC_MESSAGE_RESTART 3
368 static DWORD buflen = 1024;
369 static const char * pipename = "\\\\.\\mailslot\\Inspircd";
370 hIPCPipe = CreateMailslot(pipename, buflen, 0, 0);
371 if (hIPCPipe == INVALID_HANDLE_VALUE)
372 printf("IPC Pipe could not be created. Are you sure you didn't start InspIRCd twice?\n");
375 void CheckIPC(InspIRCd * Instance)
377 if (hIPCPipe == INVALID_HANDLE_VALUE)
383 BOOL res = ReadFile(hIPCPipe, &action, sizeof(DWORD), &bytes, 0);
386 if (GetLastError() != ERROR_SEM_TIMEOUT)
387 Instance->Log(DEFAULT, "IPC Pipe Error %u: %s", GetLastError(), dlerror());
393 case IPC_MESSAGE_REHASH:
397 case IPC_MESSAGE_DIE:
401 case IPC_MESSAGE_RESTART:
402 Instance->Restart("IPC_MESSAGE_RESTART received by mailslot.");
409 CloseHandle(hIPCPipe);
413 /* These three functions were created from looking at how ares does it
414 * (...and they look far tidier in C++)
417 /* Get active nameserver */
418 bool GetNameServer(HKEY regkey, const char *key, char* &output)
421 DWORD result = RegQueryValueEx(regkey, key, 0, NULL, NULL, &size);
422 if (((result != ERROR_SUCCESS) && (result != ERROR_MORE_DATA)) || (!size))
425 output = new char[size+1];
427 if ((RegQueryValueEx(regkey, key, 0, NULL, (LPBYTE)output, &size) != ERROR_SUCCESS) || (!*output))
435 /* Check a network interface for its nameserver */
436 bool GetInterface(HKEY regkey, const char *key, char* &output)
443 while (RegEnumKeyEx(regkey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
446 if (RegOpenKeyEx(regkey, buf, 0, KEY_QUERY_VALUE, &top) != ERROR_SUCCESS)
448 int rc = GetNameServer(top, key, output);
457 std::string FindNameServerWin()
459 std::string returnval = "127.0.0.1";
463 /* Lets see if the correct registry hive and tree exist */
464 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &top) == ERROR_SUCCESS)
466 /* If they do, attempt to get the nameserver name */
467 RegOpenKeyEx(top, "Interfaces", 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &key);
468 if ((GetNameServer(top, "NameServer", dns)) || (GetNameServer(top, "DhcpNameServer", dns))
469 || (GetInterface(key, "NameServer", dns)) || (GetInterface(key, "DhcpNameServer", dns)))
486 COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
487 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
489 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
490 DWORD dwConSize; /* number of character cells in the current buffer */
492 /* get the number of character cells in the current buffer */
494 if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
496 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
497 /* fill the entire screen with blanks */
498 if (FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))
500 /* get the current text attribute */
501 if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
503 /* now set the buffer's attributes accordingly */
504 if (FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
506 /* put the cursor at (0, 0) */
507 SetConsoleCursorPosition( hConsole, coordScreen );