]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
windows: Add a common Windows header for platform specific wrappers
authorMartin Willi <martin@revosec.ch>
Fri, 11 Oct 2013 12:45:56 +0000 (14:45 +0200)
committerMartin Willi <martin@revosec.ch>
Tue, 3 Jun 2014 10:24:34 +0000 (12:24 +0200)
Include some more basic system headers in utils.h, so we can use that common
header on the different platforms.

src/libstrongswan/Makefile.am
src/libstrongswan/asn1/oid.pl
src/libstrongswan/crypto/crypto_tester.c
src/libstrongswan/networking/host.h
src/libstrongswan/networking/host_resolver.c
src/libstrongswan/selectors/traffic_selector.c
src/libstrongswan/utils/chunk.h
src/libstrongswan/utils/identification.c
src/libstrongswan/utils/leak_detective.c
src/libstrongswan/utils/utils.h
src/libstrongswan/utils/windows.h [new file with mode: 0644]

index c4d1a58022c5fc98827b93b11283001c7871ee89..68162929e247e1b5aef3e6265ff197c13bbc5745 100644 (file)
@@ -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
 
index 82100e8aaf89f01a070fb432e6c534b1186472ce..c45077a3f12229e73ec354b9af69da9bab8dd005 100644 (file)
@@ -30,7 +30,7 @@ print OID_H "/* Object identifiers (OIDs) used by strongSwan\n",
            " * ", $automatic, "\n",
            " * ", $warning, "\n",
            " */\n\n",
-           "#include <sys/types.h>\n\n",
+           "#include <utils/utils.h>\n\n",
            "#ifndef OID_H_\n",
            "#define OID_H_\n\n",
            "typedef struct {\n",
index c6780daf174f5993278657394d51faaa51a3e9f8..9e1d4dd90f41b18868d5dfc7f9649553a7324a99 100644 (file)
  * for more details.
  */
 
-#define _GNU_SOURCE
-#include <dlfcn.h>
+#ifdef HAVE_DLADDR
+# define _GNU_SOURCE
+# include <dlfcn.h>
+#endif
 #include <time.h>
 
 #include "crypto_tester.h"
index 4fc6cf35cd8c029e24393fbc2671a21c830ba6ad..9c9b5035fbf66e5900c88c76a16e9cfe8e9f8d9c 100644 (file)
@@ -30,10 +30,8 @@ typedef struct host_t host_t;
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
+#include <utils/utils.h>
 #include <utils/chunk.h>
 
 /**
index 10af11a7f990df7104225e1dfb37733b45a87608..a7524ac23a92d6cd27ee6b6ecf1bf0681d05eeff 100644 (file)
@@ -14,8 +14,6 @@
  */
 
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
 
 #include "host_resolver.h"
 
index b9d9b65564f1c8946aaa4f44d5f1b88f2ccf4770..94b77467aa841c89f31338f6fb967a2990a61de6 100644 (file)
  * for more details.
  */
 
-#include <arpa/inet.h>
 #include <string.h>
-#include <netdb.h>
 #include <stdio.h>
 
 #include "traffic_selector.h"
 
-#include <collections/linked_list.h>
-#include <utils/identification.h>
 #include <utils/debug.h>
+#include <utils/utils.h>
+#include <utils/identification.h>
+#include <collections/linked_list.h>
 
 #define NON_SUBNET_ADDRESS_RANGE       255
 
index 33f66caec0715b450ded39700aac68a32fa2c183..5a052a01309bad62951daf241f144291a441db37 100644 (file)
@@ -30,6 +30,8 @@
 #include <alloca.h>
 #endif
 
+#include <utils/utils.h>
+
 typedef struct chunk_t chunk_t;
 
 /**
index e7eb63bc6843feca59191eb47005e06f40e98c74..b8199c8854d2a9e78509dab6b20f9f9eec7f71a7 100644 (file)
  * for more details.
  */
 
-#define _GNU_SOURCE
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #include <string.h>
 #include <stdio.h>
 
 #include "identification.h"
 
+#include <utils/utils.h>
 #include <asn1/oid.h>
 #include <asn1/asn1.h>
 #include <crypto/hashers/hasher.h>
index af29e2100917c32d2e4f7bc13810b4b88184ae3a..a2bca193d3468b2fb8b31a3d0a592c5fdf8b5bd0 100644 (file)
 #include <string.h>
 #include <stdio.h>
 #include <signal.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #include <unistd.h>
-#include <syslog.h>
-#include <netdb.h>
 #include <locale.h>
+#ifdef HAVE_DLADDR
 #include <dlfcn.h>
+#endif
 #include <time.h>
 #include <errno.h>
 
@@ -42,6 +39,7 @@
 #include "leak_detective.h"
 
 #include <library.h>
+#include <utils/utils.h>
 #include <utils/debug.h>
 #include <utils/backtrace.h>
 #include <collections/hashtable.h>
index 392f24e63c3d5e5f0dfc3b0751d488557afb0df9..ca0d6b9a31948b654973f192175d618624dc4f5a 100644 (file)
 #include <stdlib.h>
 #include <stddef.h>
 #include <sys/time.h>
-#include <arpa/inet.h>
 #include <string.h>
 
+#ifdef WIN32
+# include "windows.h"
+#else
+# define _GNU_SOURCE
+# include <arpa/inet.h>
+# include <sys/socket.h>
+# include <netdb.h>
+# include <netinet/in.h>
+#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 <stdint.h>
         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 (file)
index 0000000..5306cbc
--- /dev/null
@@ -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 <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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 <winsock2.h>
+#include <ws2tcpip.h>
+#include <direct.h>
+
+/* 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 <sys/time.h>
+ */
+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 <sys/time.h>
+ */
+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 <time.h>
+ */
+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 <time.h>
+ */
+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_ @}*/