]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
windows: Provide wrappers for dlopen() function family
authorMartin Willi <martin@revosec.ch>
Fri, 11 Oct 2013 14:58:02 +0000 (16:58 +0200)
committerMartin Willi <martin@revosec.ch>
Tue, 3 Jun 2014 10:24:34 +0000 (12:24 +0200)
src/libstrongswan/plugins/plugin_loader.c
src/libstrongswan/utils/windows.h

index 487fafa014e628640d7b8d38a3f0333f82a5f608..c23f2f03f7d22bd7e1ccccdf27c35915da3feec5 100644 (file)
@@ -21,7 +21,9 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
+#ifdef HAVE_DLADDR
 #include <dlfcn.h>
+#endif
 #include <limits.h>
 #include <stdio.h>
 
index 5306cbc4216abc506ad56e1ffb2afcc9a046724d..2457cff32675966977106ee19fc701649f47342a 100644 (file)
@@ -150,4 +150,85 @@ static inline struct tm *localtime_r(const time_t *timep, struct tm *result)
        return NULL;
 }
 
+/**
+ * dlerror(3) from <dlfcn.h>, printing error to an alloca() buffer
+ */
+#define dlerror() \
+({ \
+       char buf[128], *out;\
+       ssize_t len; \
+       DWORD err; \
+       err = GetLastError(); \
+       len = FormatMessage(0, NULL, err, 0, buf, sizeof(buf), NULL); \
+       if (len <= 0) \
+       { \
+               len = snprintf(buf, sizeof(buf), "(%u)", err); \
+       } \
+       len++; \
+       out = alloca(len); \
+       memcpy(out, buf, len); \
+       out; \
+})
+
+/**
+ * Lazy binding, ignored on Windows
+ */
+#define RTLD_LAZY 1
+
+/**
+ * dlopen(3) from <dlfcn.h>
+ */
+static inline void *dlopen(const char *filename, int flag)
+{
+       return LoadLibrary(filename);
+}
+
+/**
+ * Default handle targeting .exe
+ */
+#define RTLD_DEFAULT (NULL)
+
+/**
+ * Find symbol in next library
+ */
+#define RTLD_NEXT ((void*)~(uintptr_t)0)
+
+/**
+ * dlsym() from <dlfcn.h>
+ */
+static inline void *dlsym(void *handle, const char *symbol)
+{
+       if (handle == RTLD_DEFAULT)
+       {
+               handle = GetModuleHandle(NULL);
+       }
+       else if (handle == RTLD_NEXT)
+       {
+               if (strcmp(symbol, "malloc") == 0 ||
+                       strcmp(symbol, "realloc") == 0 ||
+                       strcmp(symbol, "free") == 0)
+               {
+                       /* for leak-detective */
+                       handle = GetModuleHandle("msvcrt");
+               }
+               else
+               {
+                       return NULL;
+               }
+       }
+       if (handle)
+       {
+               return GetProcAddress((HMODULE)handle, symbol);
+       }
+       return NULL;
+}
+
+/**
+ * dlclose() from <dlfcn.h>
+ */
+static inline int dlclose(void *handle)
+{
+       return FreeLibrary((HMODULE)handle);
+}
+
 #endif /** WINDOWS_H_ @}*/