windows: Provide a strndup(3) replacement
[strongswan.git] / src / libstrongswan / utils / windows.h
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup windows windows
18 * @{ @ingroup utils
19 */
20
21 #ifndef WINDOWS_H_
22 #define WINDOWS_H_
23
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 #include <direct.h>
27 #include <inttypes.h>
28
29 /* undef Windows variants evaluating values more than once */
30 #undef min
31 #undef max
32
33 /* interface is defined as an alias to "struct" in basetypes.h, but
34 * we use it here and there as ordinary identifier. */
35 #undef interface
36
37 /* used by Windows API, but we have our own */
38 #undef CALLBACK
39
40 /* UID/GID types for capabilities, even if not supported */
41 typedef u_int uid_t;
42 typedef u_int gid_t;
43
44 /**
45 * Initialize Windows libraries
46 */
47 void windows_init();
48
49 /**
50 * Deinitialize windows libraries
51 */
52 void windows_deinit();
53
54 /**
55 * Replacement for random(3)
56 */
57 static inline long random(void)
58 {
59 return rand();
60 }
61
62 /**
63 * Replacement for srandom(3)
64 */
65 static inline void srandom(unsigned int seed)
66 {
67 srand(seed);
68 }
69
70 /**
71 * Replacement of sched_yield(2) from <sched.h>
72 */
73 static inline int sched_yield(void)
74 {
75 Sleep(0);
76 return 0;
77 }
78
79 /**
80 * Replacement of sleep(3), cancellable by thread_cancel()
81 */
82 static inline int sleep(unsigned int seconds)
83 {
84 SleepEx(seconds * 1000, TRUE);
85 return 0;
86 }
87
88 /**
89 * Replacement of usleep(3), cancellable, ms resolution only
90 */
91 int usleep(useconds_t usec);
92
93 /**
94 * strdup(3), the Windows variant can't free(strdup("")) and others
95 */
96 #define strdup strdup_windows
97 static inline char* strdup_windows(const char *src)
98 {
99 size_t len;
100 char *dst;
101
102 len = strlen(src) + 1;
103 dst = malloc(len);
104 memcpy(dst, src, len);
105 return dst;
106 }
107
108 /**
109 * strndup(3)
110 */
111 char* strndup(const char *s, size_t n);
112
113 /**
114 * Provided via ws2_32
115 */
116 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
117
118 /**
119 * Provided via ws2_32
120 */
121 int inet_pton(int af, const char *src, void *dst);
122
123 /**
124 * Provided by printf hook backend
125 */
126 int asprintf(char **strp, const char *fmt, ...);
127
128 /**
129 * Provided by printf hook backend
130 */
131 int vasprintf(char **strp, const char *fmt, va_list ap);
132
133 /**
134 * timeradd(3) from <sys/time.h>
135 */
136 static inline void timeradd(struct timeval *a, struct timeval *b,
137 struct timeval *res)
138 {
139 res->tv_sec = a->tv_sec + b->tv_sec;
140 res->tv_usec = a->tv_usec + b->tv_usec;
141 if (res->tv_usec >= 1000000)
142 {
143 res->tv_usec -= 1000000;
144 res->tv_sec++;
145 }
146 }
147
148 /**
149 * timersub(3) from <sys/time.h>
150 */
151 static inline void timersub(struct timeval *a, struct timeval *b,
152 struct timeval *res)
153 {
154 res->tv_sec = a->tv_sec - b->tv_sec;
155 res->tv_usec = a->tv_usec - b->tv_usec;
156 if (res->tv_usec < 0)
157 {
158 res->tv_usec += 1000000;
159 res->tv_sec--;
160 }
161 }
162
163 /**
164 * gmtime_r(3) from <time.h>
165 */
166 static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
167 {
168 struct tm *ret;
169
170 /* gmtime_s() and friends seem not to be implemented/functioning.
171 * Relying on gmtime() on Windows works as well, as it uses thread
172 * specific buffers. */
173 ret = gmtime(timep);
174 if (ret)
175 {
176 memcpy(result, ret, sizeof(*result));
177 }
178 return ret;
179 }
180
181 /**
182 * localtime_r(3) from <time.h>
183 */
184 static inline struct tm *localtime_r(const time_t *timep, struct tm *result)
185 {
186 struct tm *ret;
187
188 /* localtime_s() and friends seem not to be implemented/functioning.
189 * Relying on localtime() on Windows works as well, as it uses thread
190 * specific buffers. */
191 ret = localtime(timep);
192 if (ret)
193 {
194 memcpy(result, ret, sizeof(*result));
195 }
196 return ret;
197 }
198
199 /**
200 * setenv(3) from <stdlib.h>, overwrite flag is ignored
201 */
202 static inline int setenv(const char *name, const char *value, int overwrite)
203 {
204 if (SetEnvironmentVariableA(name, value) == 0)
205 { /* failed */
206 return -1;
207 }
208 return 0;
209 }
210
211 /**
212 * Lazy binding, ignored on Windows
213 */
214 #define RTLD_LAZY 1
215
216 /**
217 * Default handle targeting .exe
218 */
219 #define RTLD_DEFAULT (NULL)
220
221 /**
222 * Find symbol in next library
223 */
224 #define RTLD_NEXT ((void*)~(uintptr_t)0)
225
226 /**
227 * dlopen(3) from <dlfcn.h>
228 */
229 void* dlopen(const char *filename, int flag);
230
231 /**
232 * dlsym() from <dlfcn.h>
233 */
234 void* dlsym(void *handle, const char *symbol);
235
236 /**
237 * dlerror(3) from <dlfcn.h>, currently not thread save
238 */
239 char* dlerror(void);
240
241 /**
242 * dlclose() from <dlfcn.h>
243 */
244 int dlclose(void *handle);
245
246 /**
247 * socketpair(2) for SOCK_STREAM, uses TCP on loopback
248 */
249 int socketpair(int domain, int type, int protocol, int sv[2]);
250
251 /**
252 * Map MSG_DONTWAIT to the reserved, but deprecated MSG_INTERRUPT
253 */
254 #define MSG_DONTWAIT MSG_INTERRUPT
255
256 /**
257 * EWOULDBLOCK is EAGAIN on other systems as well
258 */
259 #define EWOULDBLOCK EAGAIN
260
261 /**
262 * ECONNRESET is mapped to something arbitrary. It is returned by
263 * stream->read_all() but should not be mapped from a send/recv WSA error.
264 */
265 #define ECONNRESET ENXIO
266
267 /**
268 * recv(2) with support for MSG_DONTWAIT
269 */
270 #define recv(...) windows_recv(__VA_ARGS__)
271 ssize_t windows_recv(int sockfd, void *buf, size_t len, int flags);
272
273 /**
274 * recvfrom(2) with support for MSG_DONTWAIT
275 */
276 #define recvfrom(...) windows_recvfrom(__VA_ARGS__)
277 ssize_t windows_recvfrom(int sockfd, void *buf, size_t len, int flags,
278 struct sockaddr *src_addr, socklen_t *addrlen);
279
280 /**
281 * recvfrom(2) with support for MSG_DONTWAIT
282 */
283 #define send(...) windows_send(__VA_ARGS__)
284 ssize_t windows_send(int sockfd, const void *buf, size_t len, int flags);
285
286 /**
287 * recvfrom(2) with support for MSG_DONTWAIT
288 */
289 #define sendto(...) windows_send(__VA_ARGS__)
290 ssize_t windows_sendto(int sockfd, const void *buf, size_t len, int flags,
291 const struct sockaddr *dest_addr, socklen_t addrlen);
292
293 /* Windows does not support "ll" format printf length modifiers. Mingw
294 * therefore maps these to the Windows specific I64 length modifier. That
295 * won't work for us, as we use our own printf backend on Windows, which works
296 * just fine with "ll". */
297 #undef PRId64
298 #define PRId64 "lld"
299 #undef PRId64
300 #define PRId64 "lld"
301 #undef PRIdLEAST64
302 #define PRIdLEAST64 "lld"
303 #undef PRIdFAST64
304 #define PRIdFAST64 "lld"
305 #undef PRIdMAX
306 #define PRIdMAX "lld"
307 #undef PRIi64
308 #define PRIi64 "lli"
309 #undef PRIiLEAST64
310 #define PRIiLEAST64 "lli"
311 #undef PRIiFAST64
312 #define PRIiFAST64 "lli"
313 #undef PRIiMAX
314 #define PRIiMAX "lli"
315 #undef PRIo64
316 #define PRIo64 "llo"
317 #undef PRIoLEAST64
318 #define PRIoLEAST64 "llo"
319 #undef PRIoFAST64
320 #define PRIoFAST64 "llo"
321 #undef PRIoMAX
322 #define PRIoMAX "llo"
323 #undef PRIu64
324 #define PRIu64 "llu"
325 #undef PRIuLEAST64
326 #define PRIuLEAST64 "llu"
327 #undef PRIuFAST64
328 #define PRIuFAST64 "llu"
329 #undef PRIuMAX
330 #define PRIuMAX "llu"
331 #undef PRIx64
332 #define PRIx64 "llx"
333 #undef PRIxLEAST64
334 #define PRIxLEAST64 "llx"
335 #undef PRIxFAST64
336 #define PRIxFAST64 "llx"
337 #undef PRIxMAX
338 #define PRIxMAX "llx"
339 #undef PRIX64
340 #define PRIX64 "llX"
341 #undef PRIXLEAST64
342 #define PRIXLEAST64 "llX"
343 #undef PRIXFAST64
344 #define PRIXFAST64 "llX"
345 #undef PRIXMAX
346 #define PRIXMAX "llX"
347
348 #ifdef _WIN64
349 # undef PRIdPTR
350 # define PRIdPTR "lld"
351 # undef PRIiPTR
352 # define PRIiPTR "lli"
353 # undef PRIoPTR
354 # define PRIoPTR "llo"
355 # undef PRIuPTR
356 # define PRIuPTR "llu"
357 # undef PRIxPTR
358 # define PRIxPTR "llx"
359 # undef PRIXPTR
360 # define PRIXPTR "llX"
361 #endif /* _WIN64 */
362
363 #endif /** WINDOWS_H_ @}*/