Browse Source

Use getaddrinfo and gethostbyname_r where available. Note that these are not necessarily threadsafe: this needs more thinking. Perhaps we should back down on this multithreading idea.

svn:r3522
Nick Mathewson 19 years ago
parent
commit
a035032f09
2 changed files with 30 additions and 3 deletions
  1. 1 1
      configure.in
  2. 29 2
      src/common/compat.c

+ 1 - 1
configure.in

@@ -149,7 +149,7 @@ dnl These headers are not essential
 
 AC_CHECK_HEADERS(stdint.h sys/types.h inttypes.h sys/param.h sys/wait.h sys/limits.h netinet/in.h arpa/inet.h machine/limits.h syslog.h sys/time.h sys/resource.h pthread.h)
 
-AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit setrlimit strlcat strlcpy strtoull getpwnam ftello pthread_create)
+AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit setrlimit strlcat strlcpy strtoull getpwnam ftello pthread_create gethostbyname_r getaddrinfo)
 AC_FUNC_FSEEKO
 
 AC_CHECK_MEMBERS([struct timeval.tv_sec])

+ 29 - 2
src/common/compat.c

@@ -497,7 +497,6 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
    * something.
    */
   struct in_addr iaddr;
-  struct hostent *ent;
   tor_assert(addr);
   if (!*name) {
     /* Empty address is an error. */
@@ -507,7 +506,34 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
     memcpy(addr, &iaddr.s_addr, 4);
     return 0;
   } else {
-    ent = gethostbyname(name);
+#ifdef HAVE_GETADDRINFO
+    int err;
+    struct addrinfo *res, *res_p;
+    struct addrinfo hints;
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = PF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+    err = getaddrinfo(name, NULL, &hints, &res);
+    if (!err) {
+      for (res_p = res; res_p; res_p = res_p->ai_next) {
+        if (res_p->ai_family == PF_INET &&
+            res_p->ai_addrlen == 4) {
+          memcpy(addr, res_p->ai_addr, 4);
+          freeaddrinfo(res);
+          return 0;
+        }
+      }
+      return -1;
+    }
+
+    return (err == EAI_AGAIN) ? 1 : -1;
+#else
+    struct hostent *ent;
+#ifdef HAVE_GETHOSTBYNAME_R
+    ent = gethostbyname_r(name);
+#else
+    struct hostent *ent;
+#endif
     if (ent) {
       /* break to remind us if we move away from IPv4 */
       tor_assert(ent->h_length == 4);
@@ -519,6 +545,7 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
     return (WSAGetLastError() == WSATRY_AGAIN) ? 1 : -1;
 #else
     return (h_errno == TRY_AGAIN) ? 1 : -1;
+#endif
 #endif
   }
 }