]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.cpp
Tidy up source files:
[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                 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
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                 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
52                 return dst;
53         }
54         return NULL;
55 }
56
57 CoreExport int insp_inet_pton(int af, const char *src, void *dst)
58 {
59         sockaddr_in sa;
60         int len = sizeof(SOCKADDR);
61         int rv = WSAStringToAddressA((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len);
62         if(rv >= 0)
63         {
64                 if(WSAGetLastError() == WSAEINVAL)
65                         rv = 0;
66                 else
67                         rv = 1;
68         }
69         memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
70         return rv;
71 }
72
73 CoreExport DIR * opendir(const char * path)
74 {
75         std::string search_path = std::string(path) + "\\*.*";
76         WIN32_FIND_DATAA fd;
77         HANDLE f = FindFirstFileA(search_path.c_str(), &fd);
78         if (f != INVALID_HANDLE_VALUE)
79         {
80                 DIR * d = new DIR;
81                 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
82                 d->find_handle = f;
83                 d->first = true;
84                 return d;
85         }
86         else
87         {
88                 return 0;
89         }
90 }
91
92 CoreExport dirent * readdir(DIR * handle)
93 {
94         if (handle->first)
95                 handle->first = false;
96         else
97         {
98                 if (!FindNextFileA(handle->find_handle, &handle->find_data))
99                         return 0;
100         }
101
102         strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
103         return &handle->dirent_pointer;
104 }
105
106 CoreExport void closedir(DIR * handle)
107 {
108         FindClose(handle->find_handle);
109         delete handle;
110 }
111
112 int optind = 1;
113 char optarg[514];
114 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
115 {
116         // burlex todo: handle the shortops, at the moment it only works with longopts.
117
118         if (___argc == 1 || optind == ___argc)                  // No arguments (apart from filename)
119                 return -1;
120
121         const char * opt = ___argv[optind];
122         optind++;
123
124         // if we're not an option, return an error.
125         if (strnicmp(opt, "--", 2) != 0)
126                 return 1;
127         else
128                 opt += 2;
129
130
131         // parse argument list
132         int i = 0;
133         for (; __longopts[i].name != 0; ++i)
134         {
135                 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
136                 {
137                         // woot, found a valid argument =)
138                         char * par = 0;
139                         if ((optind) != ___argc)
140                         {
141                                 // grab the parameter from the next argument (if its not another argument)
142                                 if (strnicmp(___argv[optind], "--", 2) != 0)
143                                 {
144 //                                      optind++;               // Trash this next argument, we won't be needing it.
145                                         par = ___argv[optind-1];
146                                 }
147                         }                       
148
149                         // increment the argument for next time
150 //                      optind++;
151
152                         // determine action based on type
153                         if (__longopts[i].has_arg == required_argument && !par)
154                         {
155                                 // parameter missing and its a required parameter option
156                                 return 1;
157                         }
158
159                         // store argument in optarg
160                         if (par)
161                                 strncpy(optarg, par, 514);
162
163                         if (__longopts[i].flag != 0)
164                         {
165                                 // this is a variable, we have to set it if this argument is found.
166                                 *__longopts[i].flag = 1;
167                                 return 0;
168                         }
169                         else
170                         {
171                                 if (__longopts[i].val == -1 || par == 0)
172                                         return 1;
173                                 
174                                 return __longopts[i].val;
175                         }                       
176                         break;
177                 }
178         }
179
180         // return 1 (invalid argument)
181         return 1;
182 }
183
184 CWin32Exception::CWin32Exception() : exception()
185 {
186         dwErrorCode = GetLastError();
187         if( FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0 )
188                 sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
189 }
190
191 CWin32Exception::CWin32Exception(const CWin32Exception& other)
192 {
193         strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
194 }
195
196 const char* CWin32Exception::what() const throw()
197 {
198         return szErrorString;
199 }
200
201 DWORD CWin32Exception::GetErrorCode()
202 {
203         return dwErrorCode;
204 }
205
206 #include "../src/modules/m_spanningtree/link.h"
207 #include "modules/ssl.h"
208 template class reference<Link>;
209 template class reference<Autoconnect>;
210 template class reference<ssl_cert>;
211 template class reference<OperInfo>;