Introduction of ares_library_init() and ares_library_cleanup()
authorYang Tse <yangsita@gmail.com>
Sun, 17 May 2009 17:11:28 +0000 (17:11 +0000)
committerYang Tse <yangsita@gmail.com>
Sun, 17 May 2009 17:11:28 +0000 (17:11 +0000)
ares/Makefile.inc
ares/Makefile.vc6
ares/acountry.c
ares/adig.c
ares/ahost.c
ares/ares.h
ares/ares_init.c
ares/ares_strerror.c
ares/vc/areslib/areslib.dsp

index 586fe06..95686c1 100644 (file)
@@ -6,12 +6,13 @@ ares_timeout.c ares_destroy.c ares_mkquery.c ares_version.c           \
 ares_expand_name.c ares_parse_a_reply.c windows_port.c ares_strdup.c   \
 ares_expand_string.c ares_parse_ptr_reply.c ares_parse_aaaa_reply.c    \
 ares_getnameinfo.c inet_net_pton.c bitncmp.c inet_ntop.c ares_writev.c \
-ares_parse_ns_reply.c ares_llist.c ares__timeval.c ares_strcasecmp.c
+ares_parse_ns_reply.c ares_llist.c ares__timeval.c ares_strcasecmp.c   \
+ares_library_init.c
 
 HHEADERS = ares.h ares_private.h setup.h ares_dns.h ares_version.h          \
  nameser.h inet_net_pton.h inet_ntop.h ares_ipv6.h bitncmp.h setup_once.h   \
  ares_llist.h ares_strdup.h ares_strcasecmp.h ares_writev.h ares_build.h    \
- ares_rules.h
+ ares_rules.h ares_library_init.h
 
 MANPAGES= ares_destroy.3 ares_expand_name.3 ares_expand_string.3 ares_fds.3 \
  ares_free_hostent.3 ares_free_string.3 ares_gethostbyaddr.3               \
index 84352db..7fb2754 100644 (file)
@@ -63,6 +63,7 @@ OBJECTS = $(OBJ_DIR)\ares_fds.obj              \
           $(OBJ_DIR)\ares_strerror.obj         \
           $(OBJ_DIR)\ares_cancel.obj           \
           $(OBJ_DIR)\ares_init.obj             \
+          $(OBJ_DIR)\ares_library_init.obj     \
           $(OBJ_DIR)\ares_llist.obj            \
           $(OBJ_DIR)\ares_timeout.obj          \
           $(OBJ_DIR)\ares__timeval.obj         \
@@ -110,6 +111,8 @@ $(DEF_FILE): $(OBJECTS) Makefile.VC6
        @echo   ares_getsock           >> $@
        @echo   ares_init              >> $@
        @echo   ares_init_options      >> $@
+       @echo   ares_library_init      >> $@
+       @echo   ares_library_cleanup   >> $@
        @echo   ares_mkquery           >> $@
        @echo   ares_parse_a_reply     >> $@
        @echo   ares_parse_ptr_reply   >> $@
@@ -203,7 +206,8 @@ $(OBJ_DIR)\ares_cancel.obj: ares_cancel.c setup.h setup_once.h ares.h          \
   ares_private.h ares_ipv6.h ares_build.h ares_rules.h
 
 $(OBJ_DIR)\ares_init.obj: ares_init.c setup.h setup_once.h nameser.h ares.h    \
-  ares_private.h ares_ipv6.h inet_net_pton.h ares_build.h ares_rules.h
+  ares_private.h ares_ipv6.h inet_net_pton.h ares_build.h ares_rules.h         \
+  ares_library_init.h
 
 $(OBJ_DIR)\ares_timeout.obj: ares_timeout.c setup.h setup_once.h ares.h        \
   ares_private.h ares_ipv6.h ares_build.h ares_rules.h
@@ -260,6 +264,9 @@ $(OBJ_DIR)\inet_ntop.obj: inet_ntop.c setup.h setup_once.h nameser.h           \
 $(OBJ_DIR)\ares_getopt.obj: ares_getopt.c ares_getopt.h ares_build.h           \
   ares_rules.h
 
+$(OBJ_DIR)\ares_library_init.obj: ares_library_init.c setup.h setup_once.h     \
+  ares.h ares_private.h ares_library_init.h ares_build.h ares_rules.h
+
 $(OBJ_DIR)\ares_llist.obj: ares_llist.c setup.h setup_once.h ares.h            \
   ares_private.h ares_llist.h ares_build.h ares_rules.h
 
index cac9ae5..1899ae9 100644 (file)
@@ -116,6 +116,13 @@ int main(int argc, char **argv)
   WSAStartup(wVersionRequested, &wsaData);
 #endif
 
+  status = ares_library_init(ARES_LIB_INIT_ALL);
+  if (status != ARES_SUCCESS)
+    {
+      fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
+      return 1;
+    }
+
   while ((ch = ares_getopt(argc, argv, "dvh?")) != -1)
     switch (ch)
       {
@@ -178,6 +185,8 @@ int main(int argc, char **argv)
   wait_ares(channel);
   ares_destroy(channel);
 
+  ares_library_cleanup();
+
 #if defined(WIN32) && !defined(WATT32)
   WSACleanup();
 #endif
index 7df32d9..8b5cb2a 100644 (file)
@@ -187,6 +187,13 @@ int main(int argc, char **argv)
   WSAStartup(wVersionRequested, &wsaData);
 #endif
 
+  status = ares_library_init(ARES_LIB_INIT_ALL);
+  if (status != ARES_SUCCESS)
+    {
+      fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
+      return 1;
+    }
+
   options.flags = ARES_FLAG_NOCHECKRESP;
   options.servers = NULL;
   options.nservers = 0;
@@ -325,6 +332,8 @@ int main(int argc, char **argv)
 
   ares_destroy(channel);
 
+  ares_library_cleanup();
+
 #ifdef USE_WINSOCK
   WSACleanup();
 #endif
index 46ec735..0e6797e 100644 (file)
@@ -77,6 +77,13 @@ int main(int argc, char **argv)
   WSAStartup(wVersionRequested, &wsaData);
 #endif
 
+  status = ares_library_init(ARES_LIB_INIT_ALL);
+  if (status != ARES_SUCCESS)
+    {
+      fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
+      return 1;
+    }
+
   while ((c = ares_getopt(argc,argv,"dt:h")) != -1)
     {
       switch (c)
@@ -147,6 +154,8 @@ int main(int argc, char **argv)
 
   ares_destroy(channel);
 
+  ares_library_cleanup();
+
 #ifdef USE_WINSOCK
   WSACleanup();
 #endif
index 38750fe..e942d2d 100644 (file)
@@ -93,6 +93,12 @@ extern "C" {
 #define ARES_ENONAME            19
 #define ARES_EBADHINTS          20
 
+/* ares_library_init error codes */
+#define ARES_ELOADIPHLPAPI           21
+#define ARES_ELOADADVAPI32           22
+#define ARES_EADDRGetNetworkParams   23
+#define ARES_EADDRSYSTEMFUNCTION036  24
+
 /* Flag values */
 #define ARES_FLAG_USEVC         (1 << 0)
 #define ARES_FLAG_PRIMARY       (1 << 1)
@@ -161,6 +167,11 @@ extern "C" {
 #define ARES_GETSOCK_WRITABLE(bits,num) (bits & (1 << ((num) + \
                                          ARES_GETSOCK_MAXNUM)))
 
+/* c-ares library initialization flag values */
+#define ARES_LIB_INIT_NONE   (0)
+#define ARES_LIB_INIT_WIN32  (1 << 0)
+#define ARES_LIB_INIT_ALL    (ARES_LIB_INIT_WIN32)
+
 
 /*
  * Typedef our socket type
@@ -235,6 +246,9 @@ typedef void (*ares_nameinfo_callback)(void *arg, int status, int timeouts,
 typedef int  (*ares_sock_create_callback)(ares_socket_t socket_fd,
                                           int type, void *data);
 
+int ares_library_init(int flags);
+void ares_library_cleanup(void);
+
 int ares_init(ares_channel *channelptr);
 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
                       int optmask);
index da6a04c..68aae2d 100644 (file)
@@ -67,6 +67,7 @@
 #include <errno.h>
 #include "ares.h"
 #include "inet_net_pton.h"
+#include "ares_library_init.h"
 #include "ares_private.h"
 
 #ifdef WATT32
@@ -560,9 +561,6 @@ static int get_iphlpapi_dns_info (char *ret_buf, size_t ret_size)
 {
   FIXED_INFO    *fi, *newfi;
   DWORD          size = sizeof (*fi);
-  typedef DWORD (WINAPI* get_net_param_func) (FIXED_INFO*, DWORD*);
-  get_net_param_func fpGetNetworkParams;  /* available only on Win-98/2000+ */
-  HMODULE        handle;
   IP_ADDR_STRING *ipAddr;
   int            i, count = 0;
   int            debug  = 0;
@@ -573,15 +571,7 @@ static int get_iphlpapi_dns_info (char *ret_buf, size_t ret_size)
 
   fi = malloc(size);
   if (!fi)
-     return (0);
-
-  handle = LoadLibrary ("iphlpapi.dll");
-  if (!handle)
-     goto quit;
-
-  fpGetNetworkParams = (get_net_param_func) GetProcAddress (handle, "GetNetworkParams");
-  if (!fpGetNetworkParams)
-     goto quit;
+     return 0;
 
   res = (*fpGetNetworkParams) (fi, &size);
   if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
@@ -628,14 +618,12 @@ static int get_iphlpapi_dns_info (char *ret_buf, size_t ret_size)
 quit:
   if (fi)
      free(fi);
-  if (handle)
-     FreeLibrary (handle);
 
   if (debug && left <= ip_size)
      printf ("Too many nameservers. Truncating to %d addressess", count);
   if (ret > ret_buf)
      ret[-1] = '\0';
-  return (count);
+  return count;
 }
 #endif
 
@@ -1497,15 +1485,13 @@ static void randomize_key(unsigned char* key,int key_data_len)
   int randomized = 0;
   int counter=0;
 #ifdef WIN32
-  HMODULE lib=LoadLibrary("ADVAPI32.DLL");
-  if (lib) {
-    BOOLEAN (APIENTRY *pfn)(void*, ULONG) =
-      (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(lib,"SystemFunction036");
-    if (pfn && pfn(key,key_data_len) )
-      randomized = 1;
-
-    FreeLibrary(lib);
-  }
+  BOOLEAN res;
+  if (fpSystemFunction036)
+    {
+      res = (*fpSystemFunction036) (key, key_data_len);
+      if (res)
+        randomized = 1;
+    }
 #else /* !WIN32 */
 #ifdef RANDOM_FILE
   FILE *f = fopen(RANDOM_FILE, "rb");
index ce2edb1..4a2f453 100644 (file)
@@ -43,7 +43,11 @@ const char *ares_strerror(int code)
     "Misformatted string",
     "Illegal flags specified",
     "Given hostname is not numeric",
-    "Illegal hints flags specified"
+    "Illegal hints flags specified",
+    "Error loading iphlpapi.dll",
+    "Error loading advapi32.dll",
+    "Unknown GetNetworkParams function address",
+    "Unknown SystemFunction036 function address"
   };
 
   if(code >= 0 && code < (int)(sizeof(errtext) / sizeof(*errtext)))
index 92bb4cd..8a8624b 100644 (file)
@@ -145,6 +145,10 @@ SOURCE=..\..\ares_init.c
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=..\..\ares_library_init.c\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=..\..\ares_llist.c\r
 # End Source File\r
 # Begin Source File\r
@@ -237,6 +241,10 @@ SOURCE=..\..\ares_ipv6.h
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=..\..\ares_library_init.h\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=..\..\ares_llist.h\r
 # End Source File\r
 # Begin Source File\r