0
0
mirror of https://gitlab.torproject.org/tpo/core/tor.git synced 2024-09-20 12:22:14 +02:00

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
This commit is contained in:
Nick Mathewson 2005-02-03 21:31:04 +00:00
parent 4174bf9cbd
commit a035032f09
2 changed files with 30 additions and 3 deletions

View 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])

View 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
}
}