- if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
-#ifdef IPV6
- if (getaddrinfo(host, NULL, NULL, &ret->ai) == 0) {
- ret->family = ret->ai->ai_family;
- } else
-#endif
- {
- /*
- * Otherwise use the IPv4-only gethostbyname... (NOTE:
- * we don't use gethostbyname as a fallback!)
- */
- if (ret->family == 0) {
- /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
- if ( (h = gethostbyname(host)) )
- ret->family = AF_INET;
- }
- if (ret->family == 0) {
- ret->error = (h_errno == HOST_NOT_FOUND ||
- h_errno == NO_DATA ||
- h_errno == NO_ADDRESS ? "Host does not exist" :
- h_errno == TRY_AGAIN ?
- "Temporary name service failure" :
- "gethostbyname: unknown error");
- return ret;
- }
+#ifndef NO_IPV6
+ hints.ai_flags = AI_CANONNAME;
+ hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
+ address_family == ADDRTYPE_IPV6 ? AF_INET6 :
+ AF_UNSPEC);
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = 0;
+ hints.ai_addrlen = 0;
+ hints.ai_addr = NULL;
+ hints.ai_canonname = NULL;
+ hints.ai_next = NULL;
+ err = getaddrinfo(host, NULL, &hints, &ret->ais);
+ ret->ai = ret->ais;
+ if (err != 0) {
+ ret->error = gai_strerror(err);
+ return ret;
+ }
+ ret->family = ret->ai->ai_family;
+ *realhost = '\0';
+ if (ret->ai->ai_canonname != NULL)
+ strncat(realhost, ret->ai->ai_canonname, sizeof(realhost) - 1);
+ else
+ strncat(realhost, host, sizeof(realhost) - 1);
+#else
+ if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
+ /*
+ * Otherwise use the IPv4-only gethostbyname... (NOTE:
+ * we don't use gethostbyname as a fallback!)
+ */
+ if (ret->family == 0) {
+ /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
+ if ( (h = gethostbyname(host)) )
+ ret->family = AF_INET;