about summary refs log tree commit diff
path: root/resolv
diff options
context:
space:
mode:
Diffstat (limited to 'resolv')
-rw-r--r--resolv/gethnamaddr.c48
-rw-r--r--resolv/nss_dns/dns-host.c70
-rw-r--r--resolv/nss_dns/dns-network.c57
-rw-r--r--resolv/res_query.c20
4 files changed, 157 insertions, 38 deletions
diff --git a/resolv/gethnamaddr.c b/resolv/gethnamaddr.c
index 1ab102b420..797af94874 100644
--- a/resolv/gethnamaddr.c
+++ b/resolv/gethnamaddr.c
@@ -510,10 +510,11 @@ gethostbyname2(name, af)
 	const char *name;
 	int af;
 {
-	querybuf buf;
+	querybuf *buf;
 	register const char *cp;
 	char *bp;
-	int n, size, type, len;
+	int n, size, type, len, use_malloc = 0;
+	struct hostent *ret;
 	extern struct hostent *_gethtbyname2();
 
 	if ((_res.options & RES_INIT) == 0 && __res_ninit(&_res) == -1) {
@@ -615,13 +616,26 @@ gethostbyname2(name, af)
 				break;
 		}
 
-	if ((n = res_nsearch(&_res, name, C_IN, type, buf.buf, sizeof(buf.buf))) < 0) {
+	if (!__libc_use_alloca (MAXPACKET)) {
+		buf = (querybuf *) malloc (sizeof (*buf));
+		if (buf == NULL) {
+			__set_h_errno (NETDB_INTERNAL);
+			return NULL;
+		}
+		use_malloc = 1;
+	} else
+		buf = (querybuf *) alloca (sizeof (*buf));
+
+	if ((n = res_nsearch(&_res, name, C_IN, type, buf->buf, sizeof(buf->buf))) < 0) {
 		dprintf("res_nsearch failed (%d)\n", n);
 		if (errno == ECONNREFUSED)
 			return (_gethtbyname2(name, af));
 		return (NULL);
 	}
-	return (getanswer(&buf, n, name, type));
+	ret = getanswer(buf, n, name, type);
+	if (use_malloc)
+		free (buf);
+	return ret;
 }
 
 struct hostent *
@@ -633,9 +647,9 @@ gethostbyaddr(addr, len, af)
 	const u_char *uaddr = (const u_char *)addr;
 	static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
 	static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
-	int n;
+	int n, use_malloc = 0;
 	socklen_t size;
-	querybuf buf;
+	querybuf *buf;
 	register struct hostent *hp;
 	char qbuf[MAXDNAME+1], *qp = NULL;
 #ifdef SUNSECURITY
@@ -696,18 +710,34 @@ gethostbyaddr(addr, len, af)
 	default:
 		abort();
 	}
-	n = res_nquery(&_res, qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
+
+	if (!__libc_use_alloca (MAXPACKET)) {
+		buf = (querybuf *) malloc (sizeof (*buf));
+		if (buf == NULL) {
+			__set_h_errno (NETDB_INTERNAL);
+			return NULL;
+		}
+		use_malloc = 1;
+	} else
+		buf = (querybuf *) alloca (sizeof (*buf));
+
+	n = res_nquery(&_res, qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
 	if (n < 0 && af == AF_INET6) {
 		strcpy(qp, "ip6.int");
-		n = res_nquery(&_res, qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
+		n = res_nquery(&_res, qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
 	}
 	if (n < 0) {
+		if (use_malloc)
+			free (buf);
 		dprintf("res_nquery failed (%d)\n", n);
 		if (errno == ECONNREFUSED)
 			return (_gethtbyaddr(addr, len, af));
 		return (NULL);
 	}
-	if (!(hp = getanswer(&buf, n, qbuf, T_PTR)))
+	hp = getanswer(buf, n, qbuf, T_PTR);
+	if (use_malloc)
+		free (buf);
+	if (!hp)
 		return (NULL);	/* h_errno was set by getanswer() */
 #ifdef SUNSECURITY
 	if (af == AF_INET) {
diff --git a/resolv/nss_dns/dns-host.c b/resolv/nss_dns/dns-host.c
index c7bfc13aab..39eccabbdf 100644
--- a/resolv/nss_dns/dns-host.c
+++ b/resolv/nss_dns/dns-host.c
@@ -132,12 +132,13 @@ _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
 			   char *buffer, size_t buflen, int *errnop,
 			   int *h_errnop)
 {
-  querybuf host_buffer;
+  querybuf *host_buffer;
   char tmp[NS_MAXDNAME];
   int size, type, n;
   const char *cp;
-  int map = 0;
+  int map = 0, use_malloc = 0;
   int olderr = errno;
+  enum nss_status status;
 
   if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
     return NSS_STATUS_UNAVAIL;
@@ -169,8 +170,21 @@ _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
       && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
     name = cp;
 
-  n = res_nsearch (&_res, name, C_IN, type, host_buffer.buf,
-		   sizeof (host_buffer.buf));
+  if (!__libc_use_alloca (MAXPACKET))
+    {
+      host_buffer = (querybuf *) malloc (sizeof (querybuf));
+      if (host_buffer == NULL)
+	{
+	  *errnop = ENOMEM;
+	  return NSS_STATUS_UNAVAIL;
+	}
+      use_malloc = 1;
+    }
+  else
+    host_buffer = (querybuf *) alloca (sizeof (querybuf));
+
+  n = res_nsearch (&_res, name, C_IN, type, host_buffer->buf,
+		   sizeof (host_buffer->buf));
   if (n < 0)
     {
       enum nss_status status = (errno == ECONNREFUSED
@@ -185,11 +199,15 @@ _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
 	 by having the RES_USE_INET6 bit in _res.options set, we try
 	 another lookup.  */
       if (af == AF_INET6 && (_res.options & RES_USE_INET6))
-	n = res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf,
-			 sizeof (host_buffer.buf));
+	n = res_nsearch (&_res, name, C_IN, T_A, host_buffer->buf,
+			 sizeof (host_buffer->buf));
 
       if (n < 0)
-	return status;
+	{
+	  if (use_malloc)
+	    free (host_buffer);
+	  return status;
+	}
 
       map = 1;
 
@@ -197,8 +215,11 @@ _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
       result->h_length = INADDRSZ;;
     }
 
-  return getanswer_r (&host_buffer, n, name, type, result, buffer, buflen,
-		      errnop, h_errnop, map);
+  status = getanswer_r (host_buffer, n, name, type, result, buffer, buflen,
+			errnop, h_errnop, map);
+  if (use_malloc)
+    free (host_buffer);
+  return status;
 }
 
 
@@ -236,10 +257,10 @@ _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
     char *h_addr_ptrs[MAX_NR_ADDRS + 1];
     char linebuffer[0];
   } *host_data = (struct host_data *) buffer;
-  querybuf host_buffer;
+  querybuf *host_buffer;
   char qbuf[MAXDNAME+1], *qp = NULL;
   size_t size;
-  int n, status;
+  int n, status, use_malloc = 0;
   int olderr = errno;
 
   if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
@@ -294,23 +315,40 @@ _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
       break;
     }
 
-  n = res_nquery (&_res, qbuf, C_IN, T_PTR, (u_char *)host_buffer.buf,
-		  sizeof host_buffer);
+  if (!__libc_use_alloca (MAXPACKET))
+    {
+      host_buffer = (querybuf *) malloc (sizeof (querybuf));
+      if (host_buffer == NULL)
+	{
+	  *errnop = ENOMEM;
+	  return NSS_STATUS_UNAVAIL;
+	}
+      use_malloc = 1;
+    }
+  else
+    host_buffer = (querybuf *) alloca (sizeof (querybuf));
+
+  n = res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer->buf,
+		  sizeof (host_buffer->buf));
   if (n < 0 && af == AF_INET6)
     {
       strcpy (qp, "ip6.int");
-      n = res_nquery (&_res, qbuf, C_IN, T_PTR, (u_char *)host_buffer.buf,
-		      sizeof host_buffer);
+      n = res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer->buf,
+		      sizeof (host_buffer->buf));
     }
   if (n < 0)
     {
       *h_errnop = h_errno;
       __set_errno (olderr);
+      if (use_malloc)
+	free (host_buffer);
       return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
     }
 
-  status = getanswer_r (&host_buffer, n, qbuf, T_PTR, result, buffer, buflen,
+  status = getanswer_r (host_buffer, n, qbuf, T_PTR, result, buffer, buflen,
 			errnop, h_errnop, 0 /* XXX */);
+  if (use_malloc)
+    free (host_buffer);
   if (status != NSS_STATUS_SUCCESS)
     {
       *h_errnop = h_errno;
diff --git a/resolv/nss_dns/dns-network.c b/resolv/nss_dns/dns-network.c
index 774c868d2f..9a366aca65 100644
--- a/resolv/nss_dns/dns-network.c
+++ b/resolv/nss_dns/dns-network.c
@@ -110,27 +110,47 @@ _nss_dns_getnetbyname_r (const char *name, struct netent *result,
 			 int *herrnop)
 {
   /* Return entry for network with NAME.  */
-  querybuf net_buffer;
-  int anslen;
+  querybuf *net_buffer;
+  int anslen, use_malloc = 0;
   char *qbuf;
+  enum nss_status status;
 
   if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
     return NSS_STATUS_UNAVAIL;
 
   qbuf = strdupa (name);
-  anslen = res_nsearch (&_res, qbuf, C_IN, T_PTR, (u_char *) &net_buffer,
-			sizeof (querybuf));
+
+  if (!__libc_use_alloca (MAXPACKET))
+    {
+      net_buffer = (querybuf *) malloc (sizeof (querybuf));
+      if (net_buffer == NULL)
+	{
+	  *errnop = ENOMEM;
+	  return NSS_STATUS_UNAVAIL;
+	}
+      use_malloc = 1;
+    }
+  else
+    net_buffer = (querybuf *) alloca (sizeof (querybuf));
+
+  anslen = res_nsearch (&_res, qbuf, C_IN, T_PTR, net_buffer->buf,
+			sizeof (net_buffer->buf));
   if (anslen < 0)
     {
       /* Nothing found.  */
       *errnop = errno;
+      if (use_malloc)
+	free (net_buffer);
       return (errno == ECONNREFUSED
 	      || errno == EPFNOSUPPORT
 	      || errno == EAFNOSUPPORT)
 	? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
     }
 
-  return getanswer_r (&net_buffer, anslen, result, buffer, buflen, BYNAME);
+  status = getanswer_r (net_buffer, anslen, result, buffer, buflen, BYNAME);
+  if (use_malloc)
+    free (net_buffer);
+  return status;
 }
 
 
@@ -141,10 +161,10 @@ _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
 {
   /* Return entry for network with NAME.  */
   enum nss_status status;
-  querybuf net_buffer;
+  querybuf *net_buffer;
   unsigned int net_bytes[4];
   char qbuf[MAXDNAME];
-  int cnt, anslen;
+  int cnt, anslen, use_malloc = 0;
   u_int32_t net2;
   int olderr = errno;
 
@@ -181,20 +201,37 @@ _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
       break;
     }
 
-  anslen = res_nquery (&_res, qbuf, C_IN, T_PTR, (u_char *) &net_buffer,
-		       sizeof (querybuf));
+  if (!__libc_use_alloca (MAXPACKET))
+    {
+      net_buffer = (querybuf *) malloc (sizeof (querybuf));
+      if (net_buffer == NULL)
+	{
+	  *errnop = ENOMEM;
+	  return NSS_STATUS_UNAVAIL;
+	}
+      use_malloc = 1;
+    }
+  else
+    net_buffer = (querybuf *) alloca (sizeof (querybuf));
+
+  anslen = res_nquery (&_res, qbuf, C_IN, T_PTR, net_buffer->buf,
+		       sizeof (net_buffer->buf));
   if (anslen < 0)
     {
       /* Nothing found.  */
       int err = errno;
       __set_errno (olderr);
+      if (use_malloc)
+	free (net_buffer);
       return (err == ECONNREFUSED
 	      || err == EPFNOSUPPORT
 	      || err == EAFNOSUPPORT)
 	? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
     }
 
-  status = getanswer_r (&net_buffer, anslen, result, buffer, buflen, BYADDR);
+  status = getanswer_r (net_buffer, anslen, result, buffer, buflen, BYADDR);
+  if (use_malloc)
+    free (net_buffer);
   if (status == NSS_STATUS_SUCCESS)
     {
       /* Strip trailing zeros.  */
diff --git a/resolv/res_query.c b/resolv/res_query.c
index 866a2bba55..968ba8b4e8 100644
--- a/resolv/res_query.c
+++ b/resolv/res_query.c
@@ -108,28 +108,42 @@ res_nquery(res_state statp,
 	   u_char *answer,	/* buffer to put answer */
 	   int anslen)		/* size of answer buffer */
 {
-	u_char buf[MAXPACKET];
+	u_char *buf;
 	HEADER *hp = (HEADER *) answer;
-	int n;
+	int n, use_malloc = 0;
 
 	hp->rcode = NOERROR;	/* default */
 
+	if (!__libc_use_alloca (MAXPACKET)) {
+		buf = malloc (MAXPACKET);
+		if (buf == NULL) {
+			__set_h_errno (NETDB_INTERNAL);
+			return -1;
+		}
+		use_malloc = 1;
+	} else
+		buf = alloca (MAXPACKET);
+
 #ifdef DEBUG
 	if (statp->options & RES_DEBUG)
 		printf(";; res_query(%s, %d, %d)\n", name, class, type);
 #endif
 
 	n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
-			 buf, sizeof(buf));
+			 buf, MAXPACKET);
 	if (n <= 0) {
 #ifdef DEBUG
 		if (statp->options & RES_DEBUG)
 			printf(";; res_query: mkquery failed\n");
 #endif
 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
+		if (use_malloc)
+			free (buf);
 		return (n);
 	}
 	n = res_nsend(statp, buf, n, answer, anslen);
+	if (use_malloc)
+		free (buf);
 	if (n < 0) {
 #ifdef DEBUG
 		if (statp->options & RES_DEBUG)