]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.cpp
Merge insp20
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
11  *
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.
15  *
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
19  * details.
20  *
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/>.
23  */
24
25
26 #include "inspircd_win32wrapper.h"
27 #include "inspircd.h"
28 #include "configreader.h"
29 #include <string>
30 #include <errno.h>
31 #include <assert.h>
32
33 CoreExport const char *insp_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
34 {
35
36         if (af == AF_INET)
37         {
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)
43                         return dst;
44         }
45         else if (af == AF_INET6)
46         {
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)
52                         return dst;
53         }
54         return NULL;
55 }
56
57 CoreExport int insp_inet_pton(int af, const char *src, void *dst)
58 {
59         int address_length;
60         sockaddr_storage sa;
61         sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&sa);
62         sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(&sa);
63
64         switch (af)
65         {
66                 case AF_INET:
67                         address_length = sizeof(sockaddr_in);
68                         break;
69                 case AF_INET6:
70                         address_length = sizeof(sockaddr_in6);
71                         break;
72                 default:
73                         return -1;
74         }
75
76         if (!WSAStringToAddress(static_cast<LPSTR>(const_cast<char *>(src)), af, NULL, reinterpret_cast<LPSOCKADDR>(&sa), &address_length))
77         {
78                 switch (af)
79                 {
80                         case AF_INET:
81                                 memcpy(dst, &sin->sin_addr, sizeof(in_addr));
82                                 break;
83                         case AF_INET6:
84                                 memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr));
85                                 break;
86                 }
87                 return 1;
88         }
89
90         return 0;
91 }
92
93 CoreExport DIR * opendir(const char * path)
94 {
95         std::string search_path = std::string(path) + "\\*.*";
96         WIN32_FIND_DATAA fd;
97         HANDLE f = FindFirstFileA(search_path.c_str(), &fd);
98         if (f != INVALID_HANDLE_VALUE)
99         {
100                 DIR * d = new DIR;
101                 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
102                 d->find_handle = f;
103                 d->first = true;
104                 return d;
105         }
106         else
107         {
108                 return 0;
109         }
110 }
111
112 CoreExport dirent * readdir(DIR * handle)
113 {
114         if (handle->first)
115                 handle->first = false;
116         else
117         {
118                 if (!FindNextFileA(handle->find_handle, &handle->find_data))
119                         return 0;
120         }
121
122         strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
123         return &handle->dirent_pointer;
124 }
125
126 CoreExport void closedir(DIR * handle)
127 {
128         FindClose(handle->find_handle);
129         delete handle;
130 }
131
132 int optind = 1;
133 char optarg[514];
134 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
135 {
136         // burlex todo: handle the shortops, at the moment it only works with longopts.
137
138         if (___argc == 1 || optind == ___argc)                  // No arguments (apart from filename)
139                 return -1;
140
141         const char * opt = ___argv[optind];
142         optind++;
143
144         // if we're not an option, return an error.
145         if (strnicmp(opt, "--", 2) != 0)
146                 return 1;
147         else
148                 opt += 2;
149
150
151         // parse argument list
152         int i = 0;
153         for (; __longopts[i].name != 0; ++i)
154         {
155                 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
156                 {
157                         // woot, found a valid argument =)
158                         char * par = 0;
159                         if ((optind) != ___argc)
160                         {
161                                 // grab the parameter from the next argument (if its not another argument)
162                                 if (strnicmp(___argv[optind], "--", 2) != 0)
163                                 {
164 //                                      optind++;               // Trash this next argument, we won't be needing it.
165                                         par = ___argv[optind-1];
166                                 }
167                         }
168
169                         // increment the argument for next time
170 //                      optind++;
171
172                         // determine action based on type
173                         if (__longopts[i].has_arg == required_argument && !par)
174                         {
175                                 // parameter missing and its a required parameter option
176                                 return 1;
177                         }
178
179                         // store argument in optarg
180                         if (par)
181                                 strncpy(optarg, par, 514);
182
183                         if (__longopts[i].flag != 0)
184                         {
185                                 // this is a variable, we have to set it if this argument is found.
186                                 *__longopts[i].flag = 1;
187                                 return 0;
188                         }
189                         else
190                         {
191                                 if (__longopts[i].val == -1 || par == 0)
192                                         return 1;
193
194                                 return __longopts[i].val;
195                         }
196                         break;
197                 }
198         }
199
200         // return 1 (invalid argument)
201         return 1;
202 }
203
204 CWin32Exception::CWin32Exception() : exception()
205 {
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++)
210         {
211                 if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
212                         szErrorString[i] = 0;
213         }
214 }
215
216 CWin32Exception::CWin32Exception(const CWin32Exception& other)
217 {
218         strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
219 }
220
221 const char* CWin32Exception::what() const throw()
222 {
223         return szErrorString;
224 }
225
226 DWORD CWin32Exception::GetErrorCode()
227 {
228         return dwErrorCode;
229 }