]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Use getaddrinfo and gethostbyname_r where available. Note that these are not necessa...
authorNick Mathewson <nickm@torproject.org>
Thu, 3 Feb 2005 21:31:04 +0000 (21:31 +0000)
committerNick Mathewson <nickm@torproject.org>
Thu, 3 Feb 2005 21:31:04 +0000 (21:31 +0000)
svn:r3522

configure.in
src/common/compat.c

index e8e80fe5eed89f2ab72e54ea9f71e6fbaf1c6506..57489cae0665c1edbc095c9eab07939853878d91 100644 (file)
@@ -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])
index fa30cd87399b50d35595c8f9733b67baaf442dc1..c6c218881b9c717826941a4c95c1821d88f2c96a 100644 (file)
@@ -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
   }
 }