]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.cpp
26d1548986b7745399c607f24fe8fb92ec7e0d61
[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 "colors.h"
29 #include <string>
30 #include <errno.h>
31 #include <assert.h>
32 #include <mmsystem.h>
33 #pragma comment(lib, "Winmm.lib")
34
35 CoreExport const char *insp_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
36 {
37
38         if (af == AF_INET)
39         {
40                 struct sockaddr_in in;
41                 memset(&in, 0, sizeof(in));
42                 in.sin_family = AF_INET;
43                 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
44                 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
45                 return dst;
46         }
47         else if (af == AF_INET6)
48         {
49                 struct sockaddr_in6 in;
50                 memset(&in, 0, sizeof(in));
51                 in.sin6_family = AF_INET6;
52                 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
53                 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
54                 return dst;
55         }
56         return NULL;
57 }
58
59 CoreExport int insp_inet_pton(int af, const char *src, void *dst)
60 {
61         sockaddr_in sa;
62         int len = sizeof(SOCKADDR);
63         int rv = WSAStringToAddressA((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len);
64         if(rv >= 0)
65         {
66                 if(WSAGetLastError() == WSAEINVAL)
67                         rv = 0;
68                 else
69                         rv = 1;
70         }
71         memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
72         return rv;
73 }
74
75 CoreExport DIR * opendir(const char * path)
76 {
77         std::string search_path = std::string(path) + "\\*.*";
78         WIN32_FIND_DATAA fd;
79         HANDLE f = FindFirstFileA(search_path.c_str(), &fd);
80         if (f != INVALID_HANDLE_VALUE)
81         {
82                 DIR * d = new DIR;
83                 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
84                 d->find_handle = f;
85                 d->first = true;
86                 return d;
87         }
88         else
89         {
90                 return 0;
91         }
92 }
93
94 CoreExport dirent * readdir(DIR * handle)
95 {
96         if (handle->first)
97                 handle->first = false;
98         else
99         {
100                 if (!FindNextFileA(handle->find_handle, &handle->find_data))
101                         return 0;
102         }
103
104         strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
105         return &handle->dirent_pointer;
106 }
107
108 CoreExport void closedir(DIR * handle)
109 {
110         FindClose(handle->find_handle);
111         delete handle;
112 }
113
114 int optind = 1;
115 char optarg[514];
116 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
117 {
118         // burlex todo: handle the shortops, at the moment it only works with longopts.
119
120         if (___argc == 1 || optind == ___argc)                  // No arguments (apart from filename)
121                 return -1;
122
123         const char * opt = ___argv[optind];
124         optind++;
125
126         // if we're not an option, return an error.
127         if (strnicmp(opt, "--", 2) != 0)
128                 return 1;
129         else
130                 opt += 2;
131
132
133         // parse argument list
134         int i = 0;
135         for (; __longopts[i].name != 0; ++i)
136         {
137                 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
138                 {
139                         // woot, found a valid argument =)
140                         char * par = 0;
141                         if ((optind) != ___argc)
142                         {
143                                 // grab the parameter from the next argument (if its not another argument)
144                                 if (strnicmp(___argv[optind], "--", 2) != 0)
145                                 {
146 //                                      optind++;               // Trash this next argument, we won't be needing it.
147                                         par = ___argv[optind-1];
148                                 }
149                         }                       
150
151                         // increment the argument for next time
152 //                      optind++;
153
154                         // determine action based on type
155                         if (__longopts[i].has_arg == required_argument && !par)
156                         {
157                                 // parameter missing and its a required parameter option
158                                 return 1;
159                         }
160
161                         // store argument in optarg
162                         if (par)
163                                 strncpy(optarg, par, 514);
164
165                         if (__longopts[i].flag != 0)
166                         {
167                                 // this is a variable, we have to set it if this argument is found.
168                                 *__longopts[i].flag = 1;
169                                 return 0;
170                         }
171                         else
172                         {
173                                 if (__longopts[i].val == -1 || par == 0)
174                                         return 1;
175                                 
176                                 return __longopts[i].val;
177                         }                       
178                         break;
179                 }
180         }
181
182         // return 1 (invalid argument)
183         return 1;
184 }
185
186 #include "../src/modules/m_spanningtree/link.h"
187 #include "../src/modules/ssl.h"
188 template class reference<Link>;
189 template class reference<Autoconnect>;
190 template class reference<ssl_cert>;
191 template class reference<OperInfo>;