diff options
author | Martin Willi <martin@revosec.ch> | 2013-10-11 16:58:02 +0200 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2014-06-03 12:24:34 +0200 |
commit | 2e6c203bad2b1c7f05584ab6f91eedc23c880b6d (patch) | |
tree | 388a4da52b4ffcbfa2890ae01838e48dc64a1f8d /src | |
parent | 40a924090e0350ac10c5f854e7cb96c04c7e1b9e (diff) | |
download | strongswan-2e6c203bad2b1c7f05584ab6f91eedc23c880b6d.tar.bz2 strongswan-2e6c203bad2b1c7f05584ab6f91eedc23c880b6d.tar.xz |
windows: Provide wrappers for dlopen() function family
Diffstat (limited to 'src')
-rw-r--r-- | src/libstrongswan/plugins/plugin_loader.c | 2 | ||||
-rw-r--r-- | src/libstrongswan/utils/windows.h | 81 |
2 files changed, 83 insertions, 0 deletions
diff --git a/src/libstrongswan/plugins/plugin_loader.c b/src/libstrongswan/plugins/plugin_loader.c index 487fafa01..c23f2f03f 100644 --- a/src/libstrongswan/plugins/plugin_loader.c +++ b/src/libstrongswan/plugins/plugin_loader.c @@ -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> diff --git a/src/libstrongswan/utils/windows.h b/src/libstrongswan/utils/windows.h index 5306cbc42..2457cff32 100644 --- a/src/libstrongswan/utils/windows.h +++ b/src/libstrongswan/utils/windows.h @@ -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_ @}*/ |