# HG changeset patch # User rbultje # Date 1263230056 0 # Node ID fa3115c9b043489a0534743d81ce72a854d8ffa3 # Parent 52c7b29eca31ed44d995066866c29d41565a99c7 Use getaddrinfo(), if available, in resolve_host(). Patch by Martin Storsj <$firstname()$firstname,st>. diff -r 52c7b29eca31 -r fa3115c9b043 os_support.c --- a/os_support.c Mon Jan 11 05:51:09 2010 +0000 +++ b/os_support.c Mon Jan 11 17:14:16 2010 +0000 @@ -63,13 +63,34 @@ /* resolve host with also IP address parsing */ int resolve_host(struct in_addr *sin_addr, const char *hostname) { - struct hostent *hp; if (!inet_aton(hostname, sin_addr)) { +#if HAVE_GETADDRINFO + struct addrinfo *ai, *cur; + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + if (getaddrinfo(hostname, NULL, &hints, &ai)) + return -1; + /* getaddrinfo returns a linked list of addrinfo structs. + * Even if we set ai_family = AF_INET above, make sure + * that the returned one actually is of the correct type. */ + for (cur = ai; cur; cur = cur->ai_next) { + if (cur->ai_family == AF_INET) { + *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr; + freeaddrinfo(ai); + return 0; + } + } + freeaddrinfo(ai); + return -1; +#else + struct hostent *hp; hp = gethostbyname(hostname); if (!hp) return -1; memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); +#endif } return 0; }