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