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/>.
26 #include "inspircd_win32wrapper.h"
28 #include "configreader.h"
33 CoreExport const char *insp_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
38 struct sockaddr_in in;
39 memset(&in, 0, sizeof(in));
40 in.sin_family = AF_INET;
41 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
42 if (getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST) == 0)
45 else if (af == AF_INET6)
47 struct sockaddr_in6 in;
48 memset(&in, 0, sizeof(in));
49 in.sin6_family = AF_INET6;
50 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
51 if (getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST) == 0)
57 CoreExport int insp_inet_pton(int af, const char *src, void *dst)
61 sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&sa);
62 sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(&sa);
67 address_length = sizeof(sockaddr_in);
70 address_length = sizeof(sockaddr_in6);
76 if (!WSAStringToAddress(static_cast<LPSTR>(const_cast<char *>(src)), af, NULL, reinterpret_cast<LPSOCKADDR>(&sa), &address_length))
81 memcpy(dst, &sin->sin_addr, sizeof(in_addr));
84 memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr));
93 CoreExport DIR * opendir(const char * path)
95 std::string search_path = std::string(path) + "\\*.*";
97 HANDLE f = FindFirstFileA(search_path.c_str(), &fd);
98 if (f != INVALID_HANDLE_VALUE)
101 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
112 CoreExport dirent * readdir(DIR * handle)
115 handle->first = false;
118 if (!FindNextFileA(handle->find_handle, &handle->find_data))
122 strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
123 return &handle->dirent_pointer;
126 CoreExport void closedir(DIR * handle)
128 FindClose(handle->find_handle);
134 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
136 // burlex todo: handle the shortops, at the moment it only works with longopts.
138 if (___argc == 1 || optind == ___argc) // No arguments (apart from filename)
141 const char * opt = ___argv[optind];
144 // if we're not an option, return an error.
145 if (strnicmp(opt, "--", 2) != 0)
151 // parse argument list
153 for (; __longopts[i].name != 0; ++i)
155 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
157 // woot, found a valid argument =)
159 if ((optind) != ___argc)
161 // grab the parameter from the next argument (if its not another argument)
162 if (strnicmp(___argv[optind], "--", 2) != 0)
164 // optind++; // Trash this next argument, we won't be needing it.
165 par = ___argv[optind-1];
169 // increment the argument for next time
172 // determine action based on type
173 if (__longopts[i].has_arg == required_argument && !par)
175 // parameter missing and its a required parameter option
179 // store argument in optarg
181 strncpy(optarg, par, 514);
183 if (__longopts[i].flag != 0)
185 // this is a variable, we have to set it if this argument is found.
186 *__longopts[i].flag = 1;
191 if (__longopts[i].val == -1 || par == 0)
194 return __longopts[i].val;
200 // return 1 (invalid argument)
204 CWin32Exception::CWin32Exception() : exception()
206 dwErrorCode = GetLastError();
207 if( FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0 )
208 sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
209 for (size_t i = 0; i < _countof(szErrorString); i++)
211 if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
212 szErrorString[i] = 0;
216 CWin32Exception::CWin32Exception(const CWin32Exception& other)
218 strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
221 const char* CWin32Exception::what() const throw()
223 return szErrorString;
226 DWORD CWin32Exception::GetErrorCode()