fb633ea
From bf18f175afdb59cdcd1016995b351e2bbb1d6ba7 Mon Sep 17 00:00:00 2001
fb633ea
From: Andreas Schwab <schwab@redhat.com>
fb633ea
Date: Tue, 21 Jun 2011 17:28:57 +0200
fb633ea
Subject: [PATCH] Don't use gethostbyaddr to determine canonical name
fb633ea
fb633ea
* sysdeps/posix/getaddrinfo.c (gaih_inet): Don't use gethostbyaddr
fb633ea
to determine canonical name.
fb633ea
fb633ea
---
fb633ea
 ChangeLog                   |    5 +++
fb633ea
 sysdeps/posix/getaddrinfo.c |   77 +++----------------------------------------
fb633ea
 2 files changed, 10 insertions(+), 72 deletions(-)
fb633ea
fb633ea
--- a/sysdeps/posix/getaddrinfo.c
fb633ea
+++ b/sysdeps/posix/getaddrinfo.c
fb633ea
@@ -565,8 +565,8 @@ gaih_inet (const char *name, const struct gaih_service *service,
fb633ea
 
fb633ea
 	  /* If we do not have to look for IPv6 addresses, use
fb633ea
 	     the simple, old functions, which do not support
fb633ea
-	     IPv6 scope ids. */
fb633ea
-	  if (req->ai_family == AF_INET)
fb633ea
+	     IPv6 scope ids, nor retrieving the canonical name.  */
fb633ea
+	  if (req->ai_family == AF_INET && (req->ai_flags & AI_CANONNAME) == 0)
fb633ea
 	    {
fb633ea
 	      /* Allocate additional room for struct host_data.  */
fb633ea
 	      size_t tmpbuflen = (512 + MAX_NR_ALIASES * sizeof(char*)
fb633ea
@@ -1107,70 +1107,10 @@ gaih_inet (const char *name, const struct gaih_service *service,
fb633ea
 	/* Only the first entry gets the canonical name.  */
fb633ea
 	if (at2 == at && (req->ai_flags & AI_CANONNAME) != 0)
fb633ea
 	  {
fb633ea
-	    char *tmpbuf2 = NULL;
fb633ea
-	    bool malloc_tmpbuf2 = false;
fb633ea
-
fb633ea
 	    if (canon == NULL)
fb633ea
-	      {
fb633ea
-		struct hostent *h = NULL;
fb633ea
-		int herrno;
fb633ea
-		struct hostent th;
fb633ea
-		/* Add room for struct host_data.  */
fb633ea
-		size_t tmpbuf2len = (512 + (MAX_NR_ALIASES+MAX_NR_ADDRS+1)
fb633ea
-				     * sizeof(char*) + 16 * sizeof(char));
fb633ea
-
fb633ea
-		do
fb633ea
-		  {
fb633ea
-		    if (__libc_use_alloca (alloca_used + 2 * tmpbuf2len))
fb633ea
-		      tmpbuf2 = extend_alloca_account (tmpbuf2, tmpbuf2len,
fb633ea
-						       tmpbuf2len * 2,
fb633ea
-						       alloca_used);
fb633ea
-		    else
fb633ea
-		      {
fb633ea
-			char *newp = realloc (malloc_tmpbuf2 ? tmpbuf2 : NULL,
fb633ea
-					      2 * tmpbuf2len);
fb633ea
-			if (newp == NULL)
fb633ea
-			  {
fb633ea
-			    if (malloc_tmpbuf2)
fb633ea
-			      free (tmpbuf2);
fb633ea
-			    result = -EAI_MEMORY;
fb633ea
-			    goto free_and_return;
fb633ea
-			  }
fb633ea
-
fb633ea
-			tmpbuf2 = newp;
fb633ea
-			tmpbuf2len = 2 * tmpbuf2len;
fb633ea
-			malloc_tmpbuf2 = true;
fb633ea
-		      }
fb633ea
-
fb633ea
-		    rc = __gethostbyaddr_r (at2->addr,
fb633ea
-					    ((at2->family == AF_INET6)
fb633ea
-					     ? sizeof (struct in6_addr)
fb633ea
-					     : sizeof (struct in_addr)),
fb633ea
-					    at2->family, &th, tmpbuf2,
fb633ea
-					    tmpbuf2len, &h, &herrno);
fb633ea
-		  }
fb633ea
-		while (rc == ERANGE && herrno == NETDB_INTERNAL);
fb633ea
-
fb633ea
-		if (rc != 0 && herrno == NETDB_INTERNAL)
fb633ea
-		  {
fb633ea
-		    if (malloc_tmpbuf2)
fb633ea
-		      free (tmpbuf2);
fb633ea
-
fb633ea
-		    __set_h_errno (herrno);
fb633ea
-		    result = -EAI_SYSTEM;
fb633ea
-		    goto free_and_return;
fb633ea
-		  }
fb633ea
-
fb633ea
-		if (h != NULL)
fb633ea
-		  canon = h->h_name;
fb633ea
-		else
fb633ea
-		  {
fb633ea
-		    assert (orig_name != NULL);
fb633ea
-		    /* If the canonical name cannot be determined, use
fb633ea
-		       the passed in string.  */
fb633ea
-		    canon = orig_name;
fb633ea
-		  }
fb633ea
-	      }
fb633ea
+	      /* If the canonical name cannot be determined, use
fb633ea
+		 the passed in string.  */
fb633ea
+	      canon = orig_name;
fb633ea
 
fb633ea
 #ifdef HAVE_LIBIDN
fb633ea
 	    if (req->ai_flags & AI_CANONIDN)
fb633ea
@@ -1185,9 +1125,6 @@ gaih_inet (const char *name, const struct gaih_service *service,
fb633ea
 		int rc = __idna_to_unicode_lzlz (canon, &out, idn_flags);
fb633ea
 		if (rc != IDNA_SUCCESS)
fb633ea
 		  {
fb633ea
-		    if (malloc_tmpbuf2)
fb633ea
-		      free (tmpbuf2);
fb633ea
-
fb633ea
 		    if (rc == IDNA_MALLOC_ERROR)
fb633ea
 		      result = -EAI_MEMORY;
fb633ea
 		    else if (rc == IDNA_DLOPEN_ERROR)
fb633ea
@@ -1217,17 +1154,11 @@ gaih_inet (const char *name, const struct gaih_service *service,
fb633ea
 		    canon = strdup (canon);
fb633ea
 		    if (canon == NULL)
fb633ea
 		      {
fb633ea
-			if (malloc_tmpbuf2)
fb633ea
-			  free (tmpbuf2);
fb633ea
-
fb633ea
 			result = -EAI_MEMORY;
fb633ea
 			goto free_and_return;
fb633ea
 		      }
fb633ea
 		  }
fb633ea
 	      }
fb633ea
-
fb633ea
-	    if (malloc_tmpbuf2)
fb633ea
-	      free (tmpbuf2);
fb633ea
 	  }
fb633ea
 
fb633ea
 	family = at2->family;