summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--configure.in2
-rw-r--r--src/common/compat.c31
2 files changed, 30 insertions, 3 deletions
diff --git a/configure.in b/configure.in
index e8e80fe5ee..57489cae06 100644
--- a/configure.in
+++ b/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])
diff --git a/src/common/compat.c b/src/common/compat.c
index fa30cd8739..c6c218881b 100644
--- a/src/common/compat.c
+++ b/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);
@@ -520,6 +546,7 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
#else
return (h_errno == TRY_AGAIN) ? 1 : -1;
#endif
+#endif
}
}