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
68 memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
\r
72 char * strtok_r(char *_String, const char *_Control, char **_Context)
\r
75 const unsigned char *ctl = (const unsigned char*)_Control;
\r
76 unsigned char map[32];
\r
78 if (_Context == 0 || !_Control)
\r
81 if (!(_String != NULL || *_Context != NULL))
\r
87 map[*ctl >> 3] |= (1 << (*ctl & 7));
\r
90 /* If string is NULL, set str to the saved
\r
91 * pointer (i.e., continue breaking tokens out of the string
\r
92 * from the last strtok call) */
\r
93 if (_String != NULL)
\r
95 str = (unsigned char*)_String;
\r
99 str = (unsigned char*)*_Context;
\r
102 /* Find beginning of token (skip over leading delimiters). Note that
\r
103 * there is no token iff this loop sets str to point to the terminal
\r
104 * null (*str == 0) */
\r
105 while ((map[*str >> 3] & (1 << (*str & 7))) && *str != 0)
\r
110 _String = (char*)str;
\r
112 /* Find the end of the token. If it is not the end of the string,
\r
113 * put a null there. */
\r
114 for ( ; *str != 0 ; str++ )
\r
116 if (map[*str >> 3] & (1 << (*str & 7)))
\r
123 /* Update context */
\r
124 *_Context = (char*)str;
\r
126 /* Determine if a token has been found. */
\r
127 if (_String == (char*)str)
\r
137 void setcolor(int color_code)
\r
139 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_code);
\r
142 DIR * opendir(const char * path)
\r
144 std::string search_path = string(path) + "\\*.*";
\r
145 WIN32_FIND_DATA fd;
\r
146 HANDLE f = FindFirstFile(search_path.c_str(), &fd);
\r
147 if (f != INVALID_HANDLE_VALUE)
\r
150 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
\r
151 d->find_handle = f;
\r
161 dirent * readdir(DIR * handle)
\r
164 handle->first = false;
\r
167 if (!FindNextFile(handle->find_handle, &handle->find_data))
\r
171 strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
\r
172 return &handle->dirent_pointer;
\r
175 void closedir(DIR * handle)
\r
177 FindClose(handle->find_handle);
\r
181 const char * dlerror()
\r
183 static char errormessage[500];
\r
184 DWORD error = GetLastError();
\r
189 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)errormessage, 500, 0);
\r
190 return errormessage;
\r
193 int printf_c(const char * format, ...)
\r
195 // Better hope we're not multithreaded, otherwise we'll have chickens crossing the road other side to get the to :P
\r
196 static char message[500];
\r
197 static char temp[10];
\r
198 int color1, color2;
\r
200 /* parse arguments */
\r
202 va_start(ap, format);
\r
203 vsnprintf(message, 500, format, ap);
\r
206 /* search for unix-style escape sequences */
\r
209 const char * p = message;
\r
214 // Escape sequence -> copy into the temp buffer, and parse the color.
\r
225 if (!_stricmp(temp, "[0"))
\r
227 // Returning to normal colour.
\r
228 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
\r
230 else if (sscanf(temp, "[%u;%u", &color1, &color2) == 2)
\r
235 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
\r
238 default: // Unknown
\r
239 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
\r
246 sprintf("Unknown color code: %s", temp);
\r
247 MessageBox(0, message, message, MB_OK);
\r
259 int arg_counter = 1;
\r
261 int getopt_long_only(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
\r
263 // burlex todo: handle the shortops, at the moment it only works with longopts.
\r
265 if (___argc == 1 || arg_counter == ___argc) // No arguments (apart from filename)
\r
268 const char * opt = ___argv[arg_counter];
\r
269 int return_val = 0;
\r
271 // if we're not an option, return an error.
\r
272 if (strnicmp(opt, "--", 2) != 0)
\r
278 // parse argument list
\r
280 for (; __longopts[i].name != 0; ++i)
\r
282 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
\r
284 // woot, found a valid argument =)
\r
286 if ((arg_counter + 1) != ___argc)
\r
288 // grab the parameter from the next argument (if its not another argument)
\r
289 if (strnicmp(___argv[arg_counter+1], "--", 2) != 0)
\r
291 arg_counter++; // Trash this next argument, we won't be needing it.
\r
292 par = ___argv[arg_counter];
\r
296 // increment the argument for next time
\r
299 // determine action based on type
\r
300 if (__longopts[i].has_arg == required_argument && !par)
\r
302 // parameter missing and its a required parameter option
\r
306 // store argument in optarg
\r
308 strncpy(optarg, par, 514);
\r
310 if (__longopts[i].flag != 0)
\r
312 // this is a variable, we have to set it if this argument is found.
\r
313 *__longopts[i].flag = 1;
\r
318 if (__longopts[i].val == -1 || par == 0)
\r
321 return __longopts[i].val;
\r
327 // return 1 (invalid argument)
\r
332 #define IPC_MESSAGE_REHASH 1
\r
333 #define IPC_MESSAGE_DIE 2
\r
334 #define IPC_MESSAGE_RESTART 3
\r
338 static DWORD buflen = 1024;
\r
339 static const char * pipename = "\\\\.\\mailslot\\Inspircd";
\r
340 hIPCPipe = CreateMailslot(pipename, buflen, 0, 0);
\r
341 if (hIPCPipe == INVALID_HANDLE_VALUE)
\r
342 printf("IPC Pipe could not be created. Are you sure you didn't start InspIRCd twice?\n");
\r
345 void CheckIPC(InspIRCd * Instance)
\r
347 if (hIPCPipe == INVALID_HANDLE_VALUE)
\r
353 BOOL res = ReadFile(hIPCPipe, &action, sizeof(DWORD), &bytes, 0);
\r
356 if (GetLastError() != ERROR_SEM_TIMEOUT)
\r
357 Instance->Log(DEFAULT, "IPC Pipe Error %u: %s", GetLastError(), dlerror());
\r
363 case IPC_MESSAGE_REHASH:
\r
364 InspIRCd::Rehash(0);
\r
367 case IPC_MESSAGE_DIE:
\r
371 case IPC_MESSAGE_RESTART:
\r
372 Instance->Restart("IPC_MESSAGE_RESTART received by mailslot.");
\r
379 CloseHandle(hIPCPipe);
\r
383 /* These three functions were created from looking at how ares does it
\r
384 * (...and they look far tidier in C++)
\r
387 /* Get active nameserver */
\r
388 bool GetNameServer(HKEY regkey, const char *key, char* &output)
\r
391 DWORD result = RegQueryValueEx(regkey, key, 0, NULL, NULL, &size);
\r
392 if (((result != ERROR_SUCCESS) && (result != ERROR_MORE_DATA)) || (!size))
\r
395 output = new char[size+1];
\r
397 if ((RegQueryValueEx(regkey, key, 0, NULL, (LPBYTE)output, &size) != ERROR_SUCCESS) || (!*output))
\r
405 /* Check a network interface for its nameserver */
\r
406 bool GetInterface(HKEY regkey, const char *key, char* &output)
\r
413 while (RegEnumKeyEx(regkey, idx++, buf, &size, 0, NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS)
\r
416 if (RegOpenKeyEx(regkey, buf, 0, KEY_QUERY_VALUE, &top) != ERROR_SUCCESS)
\r
418 int rc = GetNameServer(top, key, output);
\r
427 std::string FindNameServerWin()
\r
429 std::string returnval = "127.0.0.1";
\r
433 /* Lets see if the correct registry hive and tree exist */
\r
434 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &top) == ERROR_SUCCESS)
\r
436 /* If they do, attempt to get the nameserver name */
\r
437 RegOpenKeyEx(top, "Interfaces", 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &key);
\r
438 if ((GetNameServer(top, "NameServer", dns)) || (GetNameServer(top, "DhcpNameServer", dns))
\r
439 || (GetInterface(key, "NameServer", dns)) || (GetInterface(key, "DhcpNameServer", dns)))
\r
454 void ClearConsole()
\r
456 COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
\r
457 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
\r
459 DWORD cCharsWritten;
\r
460 CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
\r
461 DWORD dwConSize; /* number of character cells in the current buffer */
\r
463 /* get the number of character cells in the current buffer */
\r
465 if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
\r
467 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
\r
468 /* fill the entire screen with blanks */
\r
469 if (FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))
\r
471 /* get the current text attribute */
\r
472 if (GetConsoleScreenBufferInfo( hConsole, &csbi ))
\r
474 /* now set the buffer's attributes accordingly */
\r
475 if (FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
\r
477 /* put the cursor at (0, 0) */
\r
478 SetConsoleCursorPosition( hConsole, coordScreen );
\r