summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYang Tse <yangsita@gmail.com>2009-05-17 17:10:41 +0000
committerYang Tse <yangsita@gmail.com>2009-05-17 17:10:41 +0000
commitc03c7251c9822de3989513a7b927550f6d7bb757 (patch)
tree8998c1f66619d6dea8b08dc4497a0cbf0480478d
parent4b5589ab99eaee489fc0b87e7d90339512e552bf (diff)
downloadc-ares-c03c7251c9822de3989513a7b927550f6d7bb757.tar.gz
c-ares-c03c7251c9822de3989513a7b927550f6d7bb757.tar.bz2
c-ares-c03c7251c9822de3989513a7b927550f6d7bb757.zip
Introduction of ares_library_init() and ares_library_cleanup()
-rw-r--r--ares_library_init.c109
-rw-r--r--ares_library_init.h24
2 files changed, 133 insertions, 0 deletions
diff --git a/ares_library_init.c b/ares_library_init.c
new file mode 100644
index 0000000..92bcedf
--- /dev/null
+++ b/ares_library_init.c
@@ -0,0 +1,109 @@
+/* $Id$ */
+
+#include "setup.h"
+
+#include "ares.h"
+#include "ares_library_init.h"
+#include "ares_private.h"
+
+/* library-private global and unique instance vars */
+
+#ifdef WIN32
+fpGetNetworkParams_t fpGetNetworkParams = ZERO_NULL;
+fpSystemFunction036_t fpSystemFunction036 = ZERO_NULL;
+#endif
+
+/* library-private global vars with source visibility restricted to this file */
+
+static unsigned int ares_initialized;
+static int ares_init_flags;
+
+#ifdef WIN32
+static HMODULE hnd_iphlpapi;
+static HMODULE hnd_advapi32;
+#endif
+
+
+static int ares_win32_init(void)
+{
+#ifdef WIN32
+
+ hnd_iphlpapi = 0;
+ hnd_iphlpapi = LoadLibrary("iphlpapi.dll");
+ if (!hnd_iphlpapi)
+ return ARES_ELOADIPHLPAPI;
+
+ fpGetNetworkParams = (fpGetNetworkParams_t)
+ GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
+ if (!fpGetNetworkParams)
+ {
+ FreeLibrary(hnd_iphlpapi);
+ return ARES_EADDRGetNetworkParams;
+ }
+
+ hnd_advapi32 = 0;
+ hnd_advapi32 = LoadLibrary("advapi32.dll");
+ if (!hnd_advapi32)
+ {
+ FreeLibrary(hnd_iphlpapi);
+ return ARES_ELOADADVAPI32;
+ }
+
+ fpSystemFunction036 = (fpSystemFunction036_t)
+ GetProcAddress(hnd_advapi32, "SystemFunction036");
+ if (!fpSystemFunction036)
+ {
+ FreeLibrary(hnd_advapi32);
+ FreeLibrary(hnd_iphlpapi);
+ return ARES_EADDRSYSTEMFUNCTION036;
+ }
+
+#endif
+ return ARES_SUCCESS;
+}
+
+
+static void ares_win32_cleanup(void)
+{
+#ifdef WIN32
+ if (hnd_advapi32)
+ FreeLibrary(hnd_advapi32);
+ if (hnd_iphlpapi)
+ FreeLibrary(hnd_iphlpapi);
+#endif
+}
+
+
+int ares_library_init(int flags)
+{
+ int res;
+
+ if (ares_initialized)
+ return ARES_SUCCESS;
+ ares_initialized++;
+
+ if (flags & ARES_LIB_INIT_WIN32)
+ {
+ res = ares_win32_init();
+ if (res != ARES_SUCCESS)
+ return res;
+ }
+
+ ares_init_flags = flags;
+
+ return ARES_SUCCESS;
+}
+
+
+void ares_library_cleanup(void)
+{
+ if (!ares_initialized)
+ return;
+ ares_initialized--;
+
+ if (ares_init_flags & ARES_LIB_INIT_WIN32)
+ ares_win32_cleanup();
+
+ ares_init_flags = ARES_LIB_INIT_NONE;
+}
+
diff --git a/ares_library_init.h b/ares_library_init.h
new file mode 100644
index 0000000..1b4c760
--- /dev/null
+++ b/ares_library_init.h
@@ -0,0 +1,24 @@
+#ifndef HEADER_CARES_LIBRARY_INIT_H
+#define HEADER_CARES_LIBRARY_INIT_H
+
+/* $Id$ */
+
+#include "setup.h"
+
+#ifdef WIN32
+
+#include <iphlpapi.h>
+
+typedef DWORD (WINAPI *fpGetNetworkParams_t) (FIXED_INFO*, DWORD*);
+typedef BOOLEAN (APIENTRY *fpSystemFunction036_t) (void*, ULONG);
+
+/* Forward-declaration of variables defined in ares_library_init.c */
+/* that are global and unique instances for whole c-ares library. */
+
+extern fpGetNetworkParams_t fpGetNetworkParams;
+extern fpSystemFunction036_t fpSystemFunction036;
+
+#endif /* WIN32 */
+
+#endif /* HEADER_CARES_LIBRARY_INIT_H */
+