From 922ee2c5292ba898ac1ca72c69e877af307b0bc4 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Fri, 11 Oct 2013 14:45:56 +0200 Subject: windows: Add a common Windows header for platform specific wrappers Include some more basic system headers in utils.h, so we can use that common header on the different platforms. --- src/libstrongswan/Makefile.am | 2 +- src/libstrongswan/asn1/oid.pl | 2 +- src/libstrongswan/crypto/crypto_tester.c | 6 +- src/libstrongswan/networking/host.h | 4 +- src/libstrongswan/networking/host_resolver.c | 2 - src/libstrongswan/selectors/traffic_selector.c | 7 +- src/libstrongswan/utils/chunk.h | 2 + src/libstrongswan/utils/identification.c | 5 +- src/libstrongswan/utils/leak_detective.c | 8 +- src/libstrongswan/utils/utils.h | 13 ++- src/libstrongswan/utils/windows.h | 153 +++++++++++++++++++++++++ 11 files changed, 180 insertions(+), 24 deletions(-) create mode 100644 src/libstrongswan/utils/windows.h (limited to 'src') diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index c4d1a5802..68162929e 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -87,7 +87,7 @@ utils/utils.h utils/chunk.h utils/debug.h utils/enum.h utils/identification.h \ utils/lexparser.h utils/optionsfrom.h utils/capabilities.h utils/backtrace.h \ utils/leak_detective.h utils/printf_hook/printf_hook.h \ utils/printf_hook/printf_hook_vstr.h utils/printf_hook/printf_hook_builtin.h \ -utils/parser_helper.h utils/test.h utils/integrity_checker.h \ +utils/parser_helper.h utils/test.h utils/integrity_checker.h utils/windows.h \ utils/utils/strerror.h endif diff --git a/src/libstrongswan/asn1/oid.pl b/src/libstrongswan/asn1/oid.pl index 82100e8aa..c45077a3f 100644 --- a/src/libstrongswan/asn1/oid.pl +++ b/src/libstrongswan/asn1/oid.pl @@ -30,7 +30,7 @@ print OID_H "/* Object identifiers (OIDs) used by strongSwan\n", " * ", $automatic, "\n", " * ", $warning, "\n", " */\n\n", - "#include \n\n", + "#include \n\n", "#ifndef OID_H_\n", "#define OID_H_\n\n", "typedef struct {\n", diff --git a/src/libstrongswan/crypto/crypto_tester.c b/src/libstrongswan/crypto/crypto_tester.c index c6780daf1..9e1d4dd90 100644 --- a/src/libstrongswan/crypto/crypto_tester.c +++ b/src/libstrongswan/crypto/crypto_tester.c @@ -14,8 +14,10 @@ * for more details. */ -#define _GNU_SOURCE -#include +#ifdef HAVE_DLADDR +# define _GNU_SOURCE +# include +#endif #include #include "crypto_tester.h" diff --git a/src/libstrongswan/networking/host.h b/src/libstrongswan/networking/host.h index 4fc6cf35c..9c9b5035f 100644 --- a/src/libstrongswan/networking/host.h +++ b/src/libstrongswan/networking/host.h @@ -30,10 +30,8 @@ typedef struct host_t host_t; #include #include #include -#include -#include -#include +#include #include /** diff --git a/src/libstrongswan/networking/host_resolver.c b/src/libstrongswan/networking/host_resolver.c index 10af11a7f..a7524ac23 100644 --- a/src/libstrongswan/networking/host_resolver.c +++ b/src/libstrongswan/networking/host_resolver.c @@ -14,8 +14,6 @@ */ #include -#include -#include #include "host_resolver.h" diff --git a/src/libstrongswan/selectors/traffic_selector.c b/src/libstrongswan/selectors/traffic_selector.c index b9d9b6556..94b77467a 100644 --- a/src/libstrongswan/selectors/traffic_selector.c +++ b/src/libstrongswan/selectors/traffic_selector.c @@ -15,16 +15,15 @@ * for more details. */ -#include #include -#include #include #include "traffic_selector.h" -#include -#include #include +#include +#include +#include #define NON_SUBNET_ADDRESS_RANGE 255 diff --git a/src/libstrongswan/utils/chunk.h b/src/libstrongswan/utils/chunk.h index 33f66caec..5a052a013 100644 --- a/src/libstrongswan/utils/chunk.h +++ b/src/libstrongswan/utils/chunk.h @@ -30,6 +30,8 @@ #include #endif +#include + typedef struct chunk_t chunk_t; /** diff --git a/src/libstrongswan/utils/identification.c b/src/libstrongswan/utils/identification.c index e7eb63bc6..b8199c885 100644 --- a/src/libstrongswan/utils/identification.c +++ b/src/libstrongswan/utils/identification.c @@ -15,15 +15,12 @@ * for more details. */ -#define _GNU_SOURCE -#include -#include -#include #include #include #include "identification.h" +#include #include #include #include diff --git a/src/libstrongswan/utils/leak_detective.c b/src/libstrongswan/utils/leak_detective.c index af29e2100..a2bca193d 100644 --- a/src/libstrongswan/utils/leak_detective.c +++ b/src/libstrongswan/utils/leak_detective.c @@ -19,14 +19,11 @@ #include #include #include -#include -#include -#include #include -#include -#include #include +#ifdef HAVE_DLADDR #include +#endif #include #include @@ -42,6 +39,7 @@ #include "leak_detective.h" #include +#include #include #include #include diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h index 392f24e63..ca0d6b9a3 100644 --- a/src/libstrongswan/utils/utils.h +++ b/src/libstrongswan/utils/utils.h @@ -26,9 +26,18 @@ #include #include #include -#include #include +#ifdef WIN32 +# include "windows.h" +#else +# define _GNU_SOURCE +# include +# include +# include +# include +#endif + /** * strongSwan program return codes */ @@ -273,7 +282,7 @@ static inline bool memeq(const void *x, const void *y, size_t len) * TODO: since the uintXX_t types are defined by the C99 standard we should * probably use those anyway */ -#ifdef __sun +#if defined __sun || defined WIN32 #include typedef uint8_t u_int8_t; typedef uint16_t u_int16_t; diff --git a/src/libstrongswan/utils/windows.h b/src/libstrongswan/utils/windows.h new file mode 100644 index 000000000..5306cbc42 --- /dev/null +++ b/src/libstrongswan/utils/windows.h @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2013 Martin Willi + * Copyright (C) 2013 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/** + * @defgroup windows windows + * @{ @ingroup utils + */ + +#ifndef WINDOWS_H_ +#define WINDOWS_H_ + +#include +#include +#include + +/* undef Windows variants evaluating values more than once */ +#undef min +#undef max + +/* interface is defined as an alias to "struct" in basetypes.h, but + * we use it here and there as ordinary identifier. */ +#undef interface + +/* used by Windows API, but we have our own */ +#undef CALLBACK + +/* UID/GID types for capabilities, even if not supported */ +typedef u_int uid_t; +typedef u_int gid_t; + +/** + * Replacement for random(3) + */ +static inline long random(void) +{ + return rand(); +} + +/** + * Replacement for srandom(3) + */ +static inline void srandom(unsigned int seed) +{ + srand(seed); +} + +/** + * Provided via ws2_32 + */ +const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); + +/** + * Provided via ws2_32 + */ +int inet_pton(int af, const char *src, void *dst); + +/** + * Provided by printf hook backend + */ +int asprintf(char **strp, const char *fmt, ...); + +/** + * Provided by printf hook backend + */ +int vasprintf(char **strp, const char *fmt, va_list ap); + +/** + * timeradd(3) from + */ +static inline void timeradd(struct timeval *a, struct timeval *b, + struct timeval *res) +{ + res->tv_sec = a->tv_sec + b->tv_sec; + res->tv_usec = a->tv_usec + b->tv_usec; + if (res->tv_usec >= 1000000) + { + res->tv_usec -= 1000000; + res->tv_sec++; + } +} + +/** + * timersub(3) from + */ +static inline void timersub(struct timeval *a, struct timeval *b, + struct timeval *res) +{ + res->tv_sec = a->tv_sec - b->tv_sec; + res->tv_usec = a->tv_usec - b->tv_usec; + if (res->tv_usec < 0) + { + res->tv_usec += 1000000; + res->tv_sec--; + } +} + +/** + * gmtime_r(3) from + */ +static inline struct tm *gmtime_r(const time_t *timep, struct tm *result) +{ + if (sizeof(time_t) == 4) + { + if (_gmtime32_s(result, (__time32_t*)time) == 0) + { + return result; + } + } + else + { + if (_gmtime64_s(result, (__time64_t*)time) == 0) + { + return result; + } + } + return NULL; +} + +/** + * localtime_r(3) from + */ +static inline struct tm *localtime_r(const time_t *timep, struct tm *result) +{ + if (sizeof(time_t) == 4) + { + if (_localtime32_s(result, (__time32_t*)time) == 0) + { + return result; + } + } + else + { + if (_localtime64_s(result, (__time64_t*)time) == 0) + { + return result; + } + } + return NULL; +} + +#endif /** WINDOWS_H_ @}*/ -- cgit v1.2.3