2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2011 Adam <Adam@anope.org>
5 * Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
7 * Copyright (C) 2007-2009 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2008 John Brooks <john.brooks@dereferenced.net>
9 * Copyright (C) 2007 Burlex <???@???>
10 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "inspircd_win32wrapper.h"
27 #include "configreader.h"
32 CoreExport const char *insp_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
37 struct sockaddr_in in;
38 memset(&in, 0, sizeof(in));
39 in.sin_family = AF_INET;
40 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
41 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
44 else if (af == AF_INET6)
46 struct sockaddr_in6 in;
47 memset(&in, 0, sizeof(in));
48 in.sin6_family = AF_INET6;
49 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
50 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
56 CoreExport int insp_inet_pton(int af, const char *src, void *dst)
60 sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&sa);
61 sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(&sa);
66 address_length = sizeof(sockaddr_in);
69 address_length = sizeof(sockaddr_in6);
75 if (!WSAStringToAddress(static_cast<LPSTR>(const_cast<char *>(src)), af, NULL, reinterpret_cast<LPSOCKADDR>(&sa), &address_length))
80 memcpy(dst, &sin->sin_addr, sizeof(in_addr));
83 memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr));
92 CoreExport DIR * opendir(const char * path)
94 std::string search_path = std::string(path) + "\\*.*";
96 HANDLE f = FindFirstFileA(search_path.c_str(), &fd);
97 if (f != INVALID_HANDLE_VALUE)
100 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
111 CoreExport dirent * readdir(DIR * handle)
114 handle->first = false;
117 if (!FindNextFileA(handle->find_handle, &handle->find_data))
121 strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
122 return &handle->dirent_pointer;
125 CoreExport void closedir(DIR * handle)
127 FindClose(handle->find_handle);
133 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
135 // burlex todo: handle the shortops, at the moment it only works with longopts.
137 if (___argc == 1 || optind == ___argc) // No arguments (apart from filename)
140 const char * opt = ___argv[optind];
143 // if we're not an option, return an error.
144 if (strnicmp(opt, "--", 2) != 0)
150 // parse argument list
152 for (; __longopts[i].name != 0; ++i)
154 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
156 // woot, found a valid argument =)
158 if ((optind) != ___argc)
160 // grab the parameter from the next argument (if its not another argument)
161 if (strnicmp(___argv[optind], "--", 2) != 0)
163 // optind++; // Trash this next argument, we won't be needing it.
164 par = ___argv[optind-1];
168 // increment the argument for next time
171 // determine action based on type
172 if (__longopts[i].has_arg == required_argument && !par)
174 // parameter missing and its a required parameter option
178 // store argument in optarg
180 strncpy(optarg, par, 514);
182 if (__longopts[i].flag != 0)
184 // this is a variable, we have to set it if this argument is found.
185 *__longopts[i].flag = 1;
190 if (__longopts[i].val == -1 || par == 0)
193 return __longopts[i].val;
199 // return 1 (invalid argument)
203 CWin32Exception::CWin32Exception() : exception()
205 dwErrorCode = GetLastError();
206 if( FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0 )
207 sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
208 for (size_t i = 0; i < _countof(szErrorString); i++)
210 if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
211 szErrorString[i] = 0;
215 CWin32Exception::CWin32Exception(const CWin32Exception& other)
217 strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
220 const char* CWin32Exception::what() const throw()
222 return szErrorString;
225 DWORD CWin32Exception::GetErrorCode()