Makefile.am: increment -version-info for 1.10.0 release
[platform/upstream/c-ares.git] / ares_library_init.c
index b3e8dc9..770e7c2 100644 (file)
@@ -1,6 +1,21 @@
-/* $Id$ */
 
-#include "setup.h"
+/* Copyright 1998 by the Massachusetts Institute of Technology.
+ * Copyright (C) 2004-2009 by Daniel Stenberg
+ *
+ * Permission to use, copy, modify, and distribute this
+ * software and its documentation for any purpose and without
+ * fee is hereby granted, provided that the above copyright
+ * notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting
+ * documentation, and that the name of M.I.T. not be used in
+ * advertising or publicity pertaining to distribution of the
+ * software without specific, written prior permission.
+ * M.I.T. makes no representations about the suitability of
+ * this software for any purpose.  It is provided "as is"
+ * without express or implied warranty.
+ */
+
+#include "ares_setup.h"
 
 #include "ares.h"
 #include "ares_library_init.h"
@@ -8,9 +23,10 @@
 
 /* library-private global and unique instance vars */
 
-#ifdef WIN32
-fpGetNetworkParams_t fpGetNetworkParams = ZERO_NULL;
-fpSystemFunction036_t fpSystemFunction036 = ZERO_NULL;
+#ifdef USE_WINSOCK
+fpGetNetworkParams_t ares_fpGetNetworkParams = ZERO_NULL;
+fpSystemFunction036_t ares_fpSystemFunction036 = ZERO_NULL;
+fpGetAdaptersAddresses_t ares_fpGetAdaptersAddresses = ZERO_NULL;
 #endif
 
 /* library-private global vars with source visibility restricted to this file */
@@ -18,7 +34,7 @@ fpSystemFunction036_t fpSystemFunction036 = ZERO_NULL;
 static unsigned int ares_initialized;
 static int          ares_init_flags;
 
-#ifdef WIN32
+#ifdef USE_WINSOCK
 static HMODULE hnd_iphlpapi;
 static HMODULE hnd_advapi32;
 #endif
@@ -26,19 +42,28 @@ static HMODULE hnd_advapi32;
 
 static int ares_win32_init(void)
 {
-#ifdef WIN32
+#ifdef USE_WINSOCK
 
   hnd_iphlpapi = 0;
   hnd_iphlpapi = LoadLibrary("iphlpapi.dll");
   if (!hnd_iphlpapi)
     return ARES_ELOADIPHLPAPI;
 
-  fpGetNetworkParams = (fpGetNetworkParams_t)
+  ares_fpGetNetworkParams = (fpGetNetworkParams_t)
     GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
-  if (!fpGetNetworkParams)
+  if (!ares_fpGetNetworkParams)
     {
       FreeLibrary(hnd_iphlpapi);
-      return ARES_EADDRGetNetworkParams;
+      return ARES_EADDRGETNETWORKPARAMS;
+    }
+
+  ares_fpGetAdaptersAddresses = (fpGetAdaptersAddresses_t)
+    GetProcAddress(hnd_iphlpapi, "GetAdaptersAddresses");
+  if (!ares_fpGetAdaptersAddresses)
+    {
+      /* This can happen on clients before WinXP, I don't
+         think it should be an error, unless we don't want to
+         support Windows 2000 anymore */
     }
 
   /*
@@ -51,7 +76,7 @@ static int ares_win32_init(void)
   hnd_advapi32 = LoadLibrary("advapi32.dll");
   if (hnd_advapi32)
     {
-      fpSystemFunction036 = (fpSystemFunction036_t)
+      ares_fpSystemFunction036 = (fpSystemFunction036_t)
         GetProcAddress(hnd_advapi32, "SystemFunction036");
     }
 
@@ -62,7 +87,7 @@ static int ares_win32_init(void)
 
 static void ares_win32_cleanup(void)
 {
-#ifdef WIN32
+#ifdef USE_WINSOCK
   if (hnd_advapi32)
     FreeLibrary(hnd_advapi32);
   if (hnd_iphlpapi)
@@ -76,7 +101,10 @@ int ares_library_init(int flags)
   int res;
 
   if (ares_initialized)
-    return ARES_SUCCESS;
+    {
+      ares_initialized++;
+      return ARES_SUCCESS;
+    }
   ares_initialized++;
 
   if (flags & ARES_LIB_INIT_WIN32)
@@ -97,6 +125,8 @@ void ares_library_cleanup(void)
   if (!ares_initialized)
     return;
   ares_initialized--;
+  if (ares_initialized)
+    return;
 
   if (ares_init_flags & ARES_LIB_INIT_WIN32)
     ares_win32_cleanup();
@@ -104,3 +134,14 @@ void ares_library_cleanup(void)
   ares_init_flags = ARES_LIB_INIT_NONE;
 }
 
+
+int ares_library_initialized(void)
+{
+#ifdef USE_WINSOCK
+  if (!ares_initialized)
+    return ARES_ENOTINITIALIZED;
+#endif
+  return ARES_SUCCESS;
+}
+
+