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