gethostbyname_r is not supported on Mac OS X (but gethostbyname uses thread-local buffers)

This commit is contained in:
Tobias Brunner 2009-05-06 07:23:31 -04:00
parent af05cc564c
commit f52e9c1aa8
2 changed files with 34 additions and 18 deletions

View File

@ -774,6 +774,7 @@ AC_SUBST(DLLIB)
AC_CHECK_FUNCS(backtrace) AC_CHECK_FUNCS(backtrace)
AC_CHECK_FUNCS(prctl) AC_CHECK_FUNCS(prctl)
AC_CHECK_FUNCS(gethostbyname_r)
AC_MSG_CHECKING(for dladdr) AC_MSG_CHECKING(for dladdr)
AC_TRY_COMPILE( AC_TRY_COMPILE(

View File

@ -436,9 +436,12 @@ host_t *host_create_from_string(char *string, u_int16_t port)
host_t *host_create_from_dns(char *string, int af, u_int16_t port) host_t *host_create_from_dns(char *string, int af, u_int16_t port)
{ {
private_host_t *this; private_host_t *this;
struct hostent host, *ptr; struct hostent *ptr;
int ret = 0, err;
#ifdef HAVE_GETHOSTBYNAME_R
struct hostent host;
char buf[512]; char buf[512];
int err, ret; #endif
if (streq(string, "%any")) if (streq(string, "%any"))
{ {
@ -454,6 +457,7 @@ host_t *host_create_from_dns(char *string, int af, u_int16_t port)
return host_create_from_string(string, port); return host_create_from_string(string, port);
} }
#ifdef HAVE_GETHOSTBYNAME_R
if (af) if (af)
{ {
ret = gethostbyname2_r(string, af, &host, buf, sizeof(buf), &ptr, &err); ret = gethostbyname2_r(string, af, &host, buf, sizeof(buf), &ptr, &err);
@ -462,28 +466,39 @@ host_t *host_create_from_dns(char *string, int af, u_int16_t port)
{ {
ret = gethostbyname_r(string, &host, buf, sizeof(buf), &ptr, &err); ret = gethostbyname_r(string, &host, buf, sizeof(buf), &ptr, &err);
} }
if (ret != 0) #else
/* Some systems (e.g. Mac OS X) do not support gethostbyname_r */
if (af)
{
ptr = gethostbyname2(string, af);
}
else
{
ptr = gethostbyname(string);
}
if (ptr == NULL)
{
err = h_errno;
}
#endif
if (ret != 0 || ptr == NULL)
{ {
DBG1("resolving '%s' failed: %s", string, hstrerror(err)); DBG1("resolving '%s' failed: %s", string, hstrerror(err));
return NULL; return NULL;
} }
if (ptr == NULL)
{
DBG1("resolving '%s' failed", string);
}
this = host_create_empty(); this = host_create_empty();
this->address.sa_family = host.h_addrtype; this->address.sa_family = ptr->h_addrtype;
switch (this->address.sa_family) switch (this->address.sa_family)
{ {
case AF_INET: case AF_INET:
memcpy(&this->address4.sin_addr.s_addr, memcpy(&this->address4.sin_addr.s_addr,
host.h_addr_list[0], host.h_length); ptr->h_addr_list[0], ptr->h_length);
this->address4.sin_port = htons(port); this->address4.sin_port = htons(port);
this->socklen = sizeof(struct sockaddr_in); this->socklen = sizeof(struct sockaddr_in);
break; break;
case AF_INET6: case AF_INET6:
memcpy(&this->address6.sin6_addr.s6_addr, memcpy(&this->address6.sin6_addr.s6_addr,
host.h_addr_list[0], host.h_length); ptr->h_addr_list[0], ptr->h_length);
this->address6.sin6_port = htons(port); this->address6.sin6_port = htons(port);
this->socklen = sizeof(struct sockaddr_in6); this->socklen = sizeof(struct sockaddr_in6);
break; break;