2002-10-15  Jakub Jelinek  <jakub@redhat.com>

	* include/resolv.h (__libc_res_nquery, __libc_res_nsearch,
	__libc_res_nsend): New prototypes.
	* resolv/res_query.c (QUERYSIZE): Define.
	(__libc_res_nquery): Renamed from res_nquery.  Added answerp
	argument.  Allocate only QUERYSIZE bytes first, if res_nmkquery
	fails use MAXPACKET buffer.  Call __libc_res_nsend instead of
	res_nsend, pass answerp.
	(res_nquery): Changed into wrapper around __libc_res_nquery.
	(__libc_res_nsearch): Renamed from res_nsearch.  Added answerp
	argument.  Call __libc_res_nquerydomain and __libc_res_nquery
	instead of the non-__libc_ variants, pass them answerp.
	(res_nsearch): Changed into wrapper around __libc_res_nsearch.
	(__libc_res_nquerydomain): Renamed from res_nquerydomain.
	Added answerp argument.  Call __libc_res_nquery instead of
	res_nquery, pass answerp.
	(res_nquerydomain): Changed into wrapper around
	__libc_res_nquerydomain.
	* resolv/res_send.c: Include sys/ioctl.h.
	(MAXPACKET): Define.
	(send_vc): Change arguments.  Reallocate answer buffer if it is
	too small.
	(send_dg): Likewise.
	(__libc_res_nsend): Renamed from res_nsend.  Added ansp argument.
	Reallocate answer buffer if it is too small and hooks are in use.
	Adjust calls to send_vc and send_dg.
	(res_nsend): Changed into wrapper around __libc_res_nsend.
	* resolv/nss_dns/dns-host.c (_nss_dns_gethostbyname2_r): Allocate
	just 1K answer buffer on the stack, use __libc_res_nsearch instead
	of res_nsearch.
	(_nss_dns_gethostbyaddr_r): Similarly with __libc_res_nquery.
	* resolv/nss_dns/dns-network.c (_nss_dns_getnetbyaddr_r): Likewise.
	(_nss_dns_getnetbyname_r): Similarly with __libc_res_nsearch.
	* resolv/gethnamaddr.c (gethostbyname2): Likewise.
	(gethostbyaddr): Similarly with __libc_res_nquery.
	* resolv/Versions (libresolv): Export __libc_res_nquery and
	__libc_res_nsearch at GLIBC_PRIVATE.
This commit is contained in:
Ulrich Drepper 2002-10-17 21:51:21 +00:00
parent d025fe269a
commit 0420d8885a
8 changed files with 267 additions and 153 deletions

View File

@ -1,3 +1,42 @@
2002-10-15 Jakub Jelinek <jakub@redhat.com>
* include/resolv.h (__libc_res_nquery, __libc_res_nsearch,
__libc_res_nsend): New prototypes.
* resolv/res_query.c (QUERYSIZE): Define.
(__libc_res_nquery): Renamed from res_nquery. Added answerp
argument. Allocate only QUERYSIZE bytes first, if res_nmkquery
fails use MAXPACKET buffer. Call __libc_res_nsend instead of
res_nsend, pass answerp.
(res_nquery): Changed into wrapper around __libc_res_nquery.
(__libc_res_nsearch): Renamed from res_nsearch. Added answerp
argument. Call __libc_res_nquerydomain and __libc_res_nquery
instead of the non-__libc_ variants, pass them answerp.
(res_nsearch): Changed into wrapper around __libc_res_nsearch.
(__libc_res_nquerydomain): Renamed from res_nquerydomain.
Added answerp argument. Call __libc_res_nquery instead of
res_nquery, pass answerp.
(res_nquerydomain): Changed into wrapper around
__libc_res_nquerydomain.
* resolv/res_send.c: Include sys/ioctl.h.
(MAXPACKET): Define.
(send_vc): Change arguments. Reallocate answer buffer if it is
too small.
(send_dg): Likewise.
(__libc_res_nsend): Renamed from res_nsend. Added ansp argument.
Reallocate answer buffer if it is too small and hooks are in use.
Adjust calls to send_vc and send_dg.
(res_nsend): Changed into wrapper around __libc_res_nsend.
* resolv/nss_dns/dns-host.c (_nss_dns_gethostbyname2_r): Allocate
just 1K answer buffer on the stack, use __libc_res_nsearch instead
of res_nsearch.
(_nss_dns_gethostbyaddr_r): Similarly with __libc_res_nquery.
* resolv/nss_dns/dns-network.c (_nss_dns_getnetbyaddr_r): Likewise.
(_nss_dns_getnetbyname_r): Similarly with __libc_res_nsearch.
* resolv/gethnamaddr.c (gethostbyname2): Likewise.
(gethostbyaddr): Similarly with __libc_res_nquery.
* resolv/Versions (libresolv): Export __libc_res_nquery and
__libc_res_nsearch at GLIBC_PRIVATE.
2002-10-17 Roland McGrath <roland@redhat.com> 2002-10-17 Roland McGrath <roland@redhat.com>
* sysdeps/x86_64/dl-machine.h (elf_machine_rela): Do CHECK_STATIC_TLS * sysdeps/x86_64/dl-machine.h (elf_machine_rela): Do CHECK_STATIC_TLS

View File

@ -46,6 +46,15 @@ extern int res_ourserver_p (const res_state __statp,
const struct sockaddr_in6 *__inp); const struct sockaddr_in6 *__inp);
libc_hidden_proto (__res_ninit) libc_hidden_proto (__res_ninit)
libc_hidden_proto (__res_randomid) libc_hidden_proto (__res_randomid)
int __libc_res_nquery (res_state, const char *, int, int, u_char *, int,
u_char **);
int __libc_res_nsearch (res_state, const char *, int, int, u_char *, int,
u_char **);
int __libc_res_nsend (res_state, const u_char *, int, u_char *, int,
u_char **)
attribute_hidden;
#endif #endif
#endif #endif

View File

@ -72,6 +72,7 @@ libresolv {
# Needed in libnss_dns. # Needed in libnss_dns.
__ns_name_unpack; __ns_name_ntop; __ns_name_unpack; __ns_name_ntop;
__ns_get16; __ns_samename; __ns_get16; __ns_samename;
__libc_res_nquery; __libc_res_nsearch;
} }
} }

View File

@ -510,10 +510,10 @@ gethostbyname2(name, af)
const char *name; const char *name;
int af; int af;
{ {
querybuf *buf; querybuf *buf, *origbuf;
register const char *cp; register const char *cp;
char *bp; char *bp;
int n, size, type, len, use_malloc = 0; int n, size, type, len;
struct hostent *ret; struct hostent *ret;
extern struct hostent *_gethtbyname2(); extern struct hostent *_gethtbyname2();
@ -616,24 +616,19 @@ gethostbyname2(name, af)
break; break;
} }
if (!__libc_use_alloca (MAXPACKET)) { buf = origbuf = (querybuf *) alloca (1024);
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) { if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf->buf, 1024,
(u_char **) &buf)) < 0) {
if (buf != origbuf)
free (buf);
dprintf("res_nsearch failed (%d)\n", n); dprintf("res_nsearch failed (%d)\n", n);
if (errno == ECONNREFUSED) if (errno == ECONNREFUSED)
return (_gethtbyname2(name, af)); return (_gethtbyname2(name, af));
return (NULL); return (NULL);
} }
ret = getanswer(buf, n, name, type); ret = getanswer(buf, n, name, type);
if (use_malloc) if (buf != origbuf)
free (buf); free (buf);
return ret; return ret;
} }
@ -647,9 +642,9 @@ gethostbyaddr(addr, len, af)
const u_char *uaddr = (const u_char *)addr; 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 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 }; static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
int n, use_malloc = 0; int n;
socklen_t size; socklen_t size;
querybuf *buf; querybuf *buf, *orig_buf;
register struct hostent *hp; register struct hostent *hp;
char qbuf[MAXDNAME+1], *qp = NULL; char qbuf[MAXDNAME+1], *qp = NULL;
#ifdef SUNSECURITY #ifdef SUNSECURITY
@ -711,23 +706,18 @@ gethostbyaddr(addr, len, af)
abort(); abort();
} }
if (!__libc_use_alloca (MAXPACKET)) { buf = orig_buf = (querybuf *) alloca (1024);
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); n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf->buf, 1024,
(u_char **) &buf);
if (n < 0 && af == AF_INET6) { if (n < 0 && af == AF_INET6) {
strcpy(qp, "ip6.int"); strcpy(qp, "ip6.int");
n = res_nquery(&_res, qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf); n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf->buf,
buf != orig_buf ? MAXPACKET : 1024,
(u_char **) &buf);
} }
if (n < 0) { if (n < 0) {
if (use_malloc) if (buf != orig_buf)
free (buf); free (buf);
dprintf("res_nquery failed (%d)\n", n); dprintf("res_nquery failed (%d)\n", n);
if (errno == ECONNREFUSED) if (errno == ECONNREFUSED)
@ -735,7 +725,7 @@ gethostbyaddr(addr, len, af)
return (NULL); return (NULL);
} }
hp = getanswer(buf, n, qbuf, T_PTR); hp = getanswer(buf, n, qbuf, T_PTR);
if (use_malloc) if (buf != orig_buf)
free (buf); free (buf);
if (!hp) if (!hp)
return (NULL); /* h_errno was set by getanswer() */ return (NULL); /* h_errno was set by getanswer() */

View File

@ -132,11 +132,11 @@ _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
char *buffer, size_t buflen, int *errnop, char *buffer, size_t buflen, int *errnop,
int *h_errnop) int *h_errnop)
{ {
querybuf *host_buffer; querybuf *host_buffer, *orig_host_buffer;
char tmp[NS_MAXDNAME]; char tmp[NS_MAXDNAME];
int size, type, n; int size, type, n;
const char *cp; const char *cp;
int map = 0, use_malloc = 0; int map = 0;
int olderr = errno; int olderr = errno;
enum nss_status status; enum nss_status status;
@ -170,21 +170,10 @@ _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
&& (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL) && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
name = cp; name = cp;
if (!__libc_use_alloca (MAXPACKET)) host_buffer = orig_host_buffer = (querybuf *) alloca (1024);
{
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, n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer->buf,
sizeof (host_buffer->buf)); 1024, (u_char **) &host_buffer);
if (n < 0) if (n < 0)
{ {
enum nss_status status = (errno == ECONNREFUSED enum nss_status status = (errno == ECONNREFUSED
@ -199,12 +188,13 @@ _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 by having the RES_USE_INET6 bit in _res.options set, we try
another lookup. */ another lookup. */
if (af == AF_INET6 && (_res.options & RES_USE_INET6)) if (af == AF_INET6 && (_res.options & RES_USE_INET6))
n = res_nsearch (&_res, name, C_IN, T_A, host_buffer->buf, n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer->buf,
sizeof (host_buffer->buf)); host_buffer != orig_host_buffer
? MAXPACKET : 1024, (u_char **) &host_buffer);
if (n < 0) if (n < 0)
{ {
if (use_malloc) if (host_buffer != orig_host_buffer)
free (host_buffer); free (host_buffer);
return status; return status;
} }
@ -217,7 +207,7 @@ _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
status = getanswer_r (host_buffer, n, name, type, result, buffer, buflen, status = getanswer_r (host_buffer, n, name, type, result, buffer, buflen,
errnop, h_errnop, map); errnop, h_errnop, map);
if (use_malloc) if (host_buffer != orig_host_buffer)
free (host_buffer); free (host_buffer);
return status; return status;
} }
@ -257,10 +247,10 @@ _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
char *h_addr_ptrs[MAX_NR_ADDRS + 1]; char *h_addr_ptrs[MAX_NR_ADDRS + 1];
char linebuffer[0]; char linebuffer[0];
} *host_data = (struct host_data *) buffer; } *host_data = (struct host_data *) buffer;
querybuf *host_buffer; querybuf *host_buffer, *orig_host_buffer;
char qbuf[MAXDNAME+1], *qp = NULL; char qbuf[MAXDNAME+1], *qp = NULL;
size_t size; size_t size;
int n, status, use_malloc = 0; int n, status;
int olderr = errno; int olderr = errno;
if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1) if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
@ -315,39 +305,29 @@ _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
break; break;
} }
if (!__libc_use_alloca (MAXPACKET)) host_buffer = orig_host_buffer = (querybuf *) alloca (1024);
{
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, n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer->buf,
sizeof (host_buffer->buf)); 1024, (u_char **) &host_buffer);
if (n < 0 && af == AF_INET6) if (n < 0 && af == AF_INET6)
{ {
strcpy (qp, "ip6.int"); strcpy (qp, "ip6.int");
n = res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer->buf, n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer->buf,
sizeof (host_buffer->buf)); host_buffer != orig_host_buffer
? MAXPACKET : 1024, (u_char **) &host_buffer);
} }
if (n < 0) if (n < 0)
{ {
*h_errnop = h_errno; *h_errnop = h_errno;
__set_errno (olderr); __set_errno (olderr);
if (use_malloc) if (host_buffer != orig_host_buffer)
free (host_buffer); free (host_buffer);
return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND; 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 */); errnop, h_errnop, 0 /* XXX */);
if (use_malloc) if (host_buffer != orig_host_buffer)
free (host_buffer); free (host_buffer);
if (status != NSS_STATUS_SUCCESS) if (status != NSS_STATUS_SUCCESS)
{ {

View File

@ -110,8 +110,8 @@ _nss_dns_getnetbyname_r (const char *name, struct netent *result,
int *herrnop) int *herrnop)
{ {
/* Return entry for network with NAME. */ /* Return entry for network with NAME. */
querybuf *net_buffer; querybuf *net_buffer, *orig_net_buffer;
int anslen, use_malloc = 0; int anslen;
char *qbuf; char *qbuf;
enum nss_status status; enum nss_status status;
@ -120,26 +120,15 @@ _nss_dns_getnetbyname_r (const char *name, struct netent *result,
qbuf = strdupa (name); qbuf = strdupa (name);
if (!__libc_use_alloca (MAXPACKET)) net_buffer = orig_net_buffer = (querybuf *) alloca (1024);
{
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, anslen = __libc_res_nsearch (&_res, qbuf, C_IN, T_PTR, net_buffer->buf,
sizeof (net_buffer->buf)); 1024, (u_char **) &net_buffer);
if (anslen < 0) if (anslen < 0)
{ {
/* Nothing found. */ /* Nothing found. */
*errnop = errno; *errnop = errno;
if (use_malloc) if (net_buffer != orig_net_buffer)
free (net_buffer); free (net_buffer);
return (errno == ECONNREFUSED return (errno == ECONNREFUSED
|| errno == EPFNOSUPPORT || errno == EPFNOSUPPORT
@ -148,7 +137,7 @@ _nss_dns_getnetbyname_r (const char *name, struct netent *result,
} }
status = getanswer_r (net_buffer, anslen, result, buffer, buflen, BYNAME); status = getanswer_r (net_buffer, anslen, result, buffer, buflen, BYNAME);
if (use_malloc) if (net_buffer != orig_net_buffer)
free (net_buffer); free (net_buffer);
return status; return status;
} }
@ -161,10 +150,10 @@ _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
{ {
/* Return entry for network with NAME. */ /* Return entry for network with NAME. */
enum nss_status status; enum nss_status status;
querybuf *net_buffer; querybuf *net_buffer, *orig_net_buffer;
unsigned int net_bytes[4]; unsigned int net_bytes[4];
char qbuf[MAXDNAME]; char qbuf[MAXDNAME];
int cnt, anslen, use_malloc = 0; int cnt, anslen;
u_int32_t net2; u_int32_t net2;
int olderr = errno; int olderr = errno;
@ -201,27 +190,16 @@ _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
break; break;
} }
if (!__libc_use_alloca (MAXPACKET)) net_buffer = orig_net_buffer = (querybuf *) alloca (1024);
{
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, anslen = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, net_buffer->buf,
sizeof (net_buffer->buf)); 1024, (u_char **) &net_buffer);
if (anslen < 0) if (anslen < 0)
{ {
/* Nothing found. */ /* Nothing found. */
int err = errno; int err = errno;
__set_errno (olderr); __set_errno (olderr);
if (use_malloc) if (net_buffer != orig_net_buffer)
free (net_buffer); free (net_buffer);
return (err == ECONNREFUSED return (err == ECONNREFUSED
|| err == EPFNOSUPPORT || err == EPFNOSUPPORT
@ -230,7 +208,7 @@ _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
} }
status = getanswer_r (net_buffer, anslen, result, buffer, buflen, BYADDR); status = getanswer_r (net_buffer, anslen, result, buffer, buflen, BYADDR);
if (use_malloc) if (net_buffer != orig_net_buffer)
free (net_buffer); free (net_buffer);
if (status == NSS_STATUS_SUCCESS) if (status == NSS_STATUS_SUCCESS)
{ {

View File

@ -91,6 +91,13 @@ static const char rcsid[] = "$BINDId: res_query.c,v 8.20 2000/02/29 05:39:12 vix
#define MAXPACKET 65536 #define MAXPACKET 65536
#endif #endif
#define QUERYSIZE (HFIXEDSZ + QFIXEDSZ + MAXCDNAME + 1)
static int
__libc_res_nquerydomain(res_state statp, const char *name, const char *domain,
int class, int type, u_char *answer, int anslen,
u_char **answerp);
/* /*
* Formulate a normal query, send, and await answer. * Formulate a normal query, send, and await answer.
* Returned answer is placed in supplied buffer "answer". * Returned answer is placed in supplied buffer "answer".
@ -102,11 +109,12 @@ static const char rcsid[] = "$BINDId: res_query.c,v 8.20 2000/02/29 05:39:12 vix
* Caller must parse answer and determine whether it answers the question. * Caller must parse answer and determine whether it answers the question.
*/ */
int int
res_nquery(res_state statp, __libc_res_nquery(res_state statp,
const char *name, /* domain name */ const char *name, /* domain name */
int class, int type, /* class and type of query */ int class, int type, /* class and type of query */
u_char *answer, /* buffer to put answer */ u_char *answer, /* buffer to put answer */
int anslen) /* size of answer buffer */ int anslen, /* size of answer buffer */
u_char **answerp) /* if buffer needs to be enlarged */
{ {
u_char *buf; u_char *buf;
HEADER *hp = (HEADER *) answer; HEADER *hp = (HEADER *) answer;
@ -114,15 +122,7 @@ res_nquery(res_state statp,
hp->rcode = NOERROR; /* default */ hp->rcode = NOERROR; /* default */
if (!__libc_use_alloca (MAXPACKET)) { buf = alloca (QUERYSIZE);
buf = malloc (MAXPACKET);
if (buf == NULL) {
__set_h_errno (NETDB_INTERNAL);
return -1;
}
use_malloc = 1;
} else
buf = alloca (MAXPACKET);
#ifdef DEBUG #ifdef DEBUG
if (statp->options & RES_DEBUG) if (statp->options & RES_DEBUG)
@ -130,8 +130,18 @@ res_nquery(res_state statp,
#endif #endif
n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL, n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
buf, MAXPACKET); buf, QUERYSIZE);
if (n <= 0) { if (__builtin_expect (n <= 0, 0)) {
/* Retry just in case res_nmkquery failed because of too
short buffer. Shouldn't happen. */
buf = malloc (MAXPACKET);
if (buf != NULL) {
use_malloc = 1;
n = res_nmkquery(statp, QUERY, name, class, type, NULL,
0, NULL, buf, MAXPACKET);
}
}
if (__builtin_expect (n <= 0, 0)) {
#ifdef DEBUG #ifdef DEBUG
if (statp->options & RES_DEBUG) if (statp->options & RES_DEBUG)
printf(";; res_query: mkquery failed\n"); printf(";; res_query: mkquery failed\n");
@ -141,7 +151,7 @@ res_nquery(res_state statp,
free (buf); free (buf);
return (n); return (n);
} }
n = res_nsend(statp, buf, n, answer, anslen); n = __libc_res_nsend(statp, buf, n, answer, anslen, answerp);
if (use_malloc) if (use_malloc)
free (buf); free (buf);
if (n < 0) { if (n < 0) {
@ -181,6 +191,17 @@ res_nquery(res_state statp,
return (n); return (n);
} }
int
res_nquery(res_state statp,
const char *name, /* domain name */
int class, int type, /* class and type of query */
u_char *answer, /* buffer to put answer */
int anslen) /* size of answer buffer */
{
return __libc_res_nquery(statp, name, class, type, answer, anslen,
NULL);
}
/* /*
* Formulate a normal query, send, and retrieve answer in supplied buffer. * Formulate a normal query, send, and retrieve answer in supplied buffer.
* Return the size of the response on success, -1 on error. * Return the size of the response on success, -1 on error.
@ -188,11 +209,12 @@ res_nquery(res_state statp,
* is detected. Error code, if any, is left in H_ERRNO. * is detected. Error code, if any, is left in H_ERRNO.
*/ */
int int
res_nsearch(res_state statp, __libc_res_nsearch(res_state statp,
const char *name, /* domain name */ const char *name, /* domain name */
int class, int type, /* class and type of query */ int class, int type, /* class and type of query */
u_char *answer, /* buffer to put answer */ u_char *answer, /* buffer to put answer */
int anslen) /* size of answer */ int anslen, /* size of answer */
u_char **answerp)
{ {
const char *cp, * const *domain; const char *cp, * const *domain;
HEADER *hp = (HEADER *) answer; HEADER *hp = (HEADER *) answer;
@ -214,7 +236,8 @@ res_nsearch(res_state statp,
/* If there aren't any dots, it could be a user-level alias. */ /* If there aren't any dots, it could be a user-level alias. */
if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL) if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
return (res_nquery(statp, cp, class, type, answer, anslen)); return (__libc_res_nquery(statp, cp, class, type, answer,
anslen, answerp));
/* /*
* If there are enough dots in the name, let's just give it a * If there are enough dots in the name, let's just give it a
@ -223,12 +246,16 @@ res_nsearch(res_state statp,
*/ */
saved_herrno = -1; saved_herrno = -1;
if (dots >= statp->ndots || trailing_dot) { if (dots >= statp->ndots || trailing_dot) {
ret = res_nquerydomain(statp, name, NULL, class, type, ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
answer, anslen); answer, anslen, answerp);
if (ret > 0 || trailing_dot) if (ret > 0 || trailing_dot)
return (ret); return (ret);
saved_herrno = h_errno; saved_herrno = h_errno;
tried_as_is++; tried_as_is++;
if (answerp && *answerp != answer) {
answer = *answerp;
anslen = MAXPACKET;
}
} }
/* /*
@ -249,12 +276,17 @@ res_nsearch(res_state statp,
(domain[0][0] == '.' && domain[0][1] == '\0')) (domain[0][0] == '.' && domain[0][1] == '\0'))
root_on_list++; root_on_list++;
ret = res_nquerydomain(statp, name, *domain, ret = __libc_res_nquerydomain(statp, name, *domain,
class, type, class, type,
answer, anslen); answer, anslen, answerp);
if (ret > 0) if (ret > 0)
return (ret); return (ret);
if (answerp && *answerp != answer) {
answer = *answerp;
anslen = MAXPACKET;
}
/* /*
* If no server present, give up. * If no server present, give up.
* If name isn't found in this domain, * If name isn't found in this domain,
@ -306,8 +338,8 @@ res_nsearch(res_state statp,
* query now. * query now.
*/ */
if (statp->ndots && !(tried_as_is || root_on_list)) { if (statp->ndots && !(tried_as_is || root_on_list)) {
ret = res_nquerydomain(statp, name, NULL, class, type, ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
answer, anslen); answer, anslen, answerp);
if (ret > 0) if (ret > 0)
return (ret); return (ret);
} }
@ -328,17 +360,29 @@ res_nsearch(res_state statp,
return (-1); return (-1);
} }
int
res_nsearch(res_state statp,
const char *name, /* domain name */
int class, int type, /* class and type of query */
u_char *answer, /* buffer to put answer */
int anslen) /* size of answer */
{
return __libc_res_nsearch(statp, name, class, type, answer,
anslen, NULL);
}
/* /*
* Perform a call on res_query on the concatenation of name and domain, * Perform a call on res_query on the concatenation of name and domain,
* removing a trailing dot from name if domain is NULL. * removing a trailing dot from name if domain is NULL.
*/ */
int static int
res_nquerydomain(res_state statp, __libc_res_nquerydomain(res_state statp,
const char *name, const char *name,
const char *domain, const char *domain,
int class, int type, /* class and type of query */ int class, int type, /* class and type of query */
u_char *answer, /* buffer to put answer */ u_char *answer, /* buffer to put answer */
int anslen) /* size of answer */ int anslen, /* size of answer */
u_char **answerp)
{ {
char nbuf[MAXDNAME]; char nbuf[MAXDNAME];
const char *longname = nbuf; const char *longname = nbuf;
@ -374,7 +418,20 @@ res_nquerydomain(res_state statp,
} }
sprintf(nbuf, "%s.%s", name, domain); sprintf(nbuf, "%s.%s", name, domain);
} }
return (res_nquery(statp, longname, class, type, answer, anslen)); return (__libc_res_nquery(statp, longname, class, type, answer,
anslen, answerp));
}
int
res_nquerydomain(res_state statp,
const char *name,
const char *domain,
int class, int type, /* class and type of query */
u_char *answer, /* buffer to put answer */
int anslen) /* size of answer */
{
return __libc_res_nquerydomain(statp, name, domain, class, type,
answer, anslen, NULL);
} }
const char * const char *

View File

@ -85,6 +85,7 @@ static const char rcsid[] = "$BINDId: res_send.c,v 8.38 2000/03/30 20:16:51 vixi
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/nameser.h> #include <arpa/nameser.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/ioctl.h>
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
@ -95,6 +96,12 @@ static const char rcsid[] = "$BINDId: res_send.c,v 8.38 2000/03/30 20:16:51 vixi
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#if PACKETSZ > 65536
#define MAXPACKET PACKETSZ
#else
#define MAXPACKET 65536
#endif
#ifndef _LIBC #ifndef _LIBC
#include <isc/eventlib.h> #include <isc/eventlib.h>
#else #else
@ -193,10 +200,10 @@ static const int highestFD = FD_SETSIZE - 1;
/* Forward. */ /* Forward. */
static int send_vc(res_state, const u_char *, int, static int send_vc(res_state, const u_char *, int,
u_char *, int, int *, int); u_char **, int *, int *, int, u_char **);
static int send_dg(res_state, const u_char *, int, static int send_dg(res_state, const u_char *, int,
u_char *, int, int *, int, u_char **, int *, int *, int,
int *, int *); int *, int *, u_char **);
#ifdef DEBUG #ifdef DEBUG
static void Aerror(const res_state, FILE *, const char *, int, static void Aerror(const res_state, FILE *, const char *, int,
struct sockaddr_in); struct sockaddr_in);
@ -371,8 +378,8 @@ res_queriesmatch(const u_char *buf1, const u_char *eom1,
} }
int int
res_nsend(res_state statp, __libc_res_nsend(res_state statp, const u_char *buf, int buflen,
const u_char *buf, int buflen, u_char *ans, int anssiz) u_char *ans, int anssiz, u_char **ansp)
{ {
int gotsomewhere, terrno, try, v_circuit, resplen, ns, n; int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
@ -380,10 +387,22 @@ res_nsend(res_state statp,
__set_errno (ESRCH); __set_errno (ESRCH);
return (-1); return (-1);
} }
if (anssiz < HFIXEDSZ) { if (anssiz < HFIXEDSZ) {
__set_errno (EINVAL); __set_errno (EINVAL);
return (-1); return (-1);
} }
if ((statp->qhook || statp->rhook) && anssiz < MAXPACKET && ansp) {
u_char *buf = malloc (MAXPACKET);
if (buf == NULL)
return (-1);
memcpy (buf, ans, HFIXEDSZ);
*ansp = buf;
ans = buf;
anssiz = MAXPACKET;
}
DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY), DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
(stdout, ";; res_send()\n"), buf, buflen); (stdout, ";; res_send()\n"), buf, buflen);
v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ; v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
@ -578,8 +597,8 @@ res_nsend(res_state statp,
if (v_circuit) { if (v_circuit) {
/* Use VC; at most one attempt per server. */ /* Use VC; at most one attempt per server. */
try = statp->retry; try = statp->retry;
n = send_vc(statp, buf, buflen, ans, anssiz, &terrno, n = send_vc(statp, buf, buflen, &ans, &anssiz, &terrno,
ns); ns, ansp);
if (n < 0) if (n < 0)
return (-1); return (-1);
if (n == 0) if (n == 0)
@ -587,8 +606,8 @@ res_nsend(res_state statp,
resplen = n; resplen = n;
} else { } else {
/* Use datagrams. */ /* Use datagrams. */
n = send_dg(statp, buf, buflen, ans, anssiz, &terrno, n = send_dg(statp, buf, buflen, &ans, &anssiz, &terrno,
ns, &v_circuit, &gotsomewhere); ns, &v_circuit, &gotsomewhere, ansp);
if (n < 0) if (n < 0)
return (-1); return (-1);
if (n == 0) if (n == 0)
@ -667,14 +686,23 @@ res_nsend(res_state statp,
return (-1); return (-1);
} }
int
res_nsend(res_state statp,
const u_char *buf, int buflen, u_char *ans, int anssiz)
{
return __libc_res_nsend(statp, buf, buflen, ans, anssiz, NULL);
}
/* Private */ /* Private */
static int static int
send_vc(res_state statp, send_vc(res_state statp,
const u_char *buf, int buflen, u_char *ans, int anssiz, const u_char *buf, int buflen, u_char **ansp, int *anssizp,
int *terrno, int ns) int *terrno, int ns, u_char **anscp)
{ {
const HEADER *hp = (HEADER *) buf; const HEADER *hp = (HEADER *) buf;
u_char *ans = *ansp;
int anssiz = *anssizp;
HEADER *anhp = (HEADER *) ans; HEADER *anhp = (HEADER *) ans;
#ifdef _LIBC #ifdef _LIBC
struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns]; struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
@ -782,11 +810,26 @@ send_vc(res_state statp,
} }
resplen = ns_get16(ans); resplen = ns_get16(ans);
if (resplen > anssiz) { if (resplen > anssiz) {
if (anscp) {
ans = malloc (MAXPACKET);
if (ans == NULL) {
*terrno = ENOMEM;
res_nclose(statp);
return (0);
}
anssiz = MAXPACKET;
*anssizp = MAXPACKET;
*ansp = ans;
*anscp = ans;
anhp = (HEADER *) ans;
len = resplen;
} else {
Dprint(statp->options & RES_DEBUG, Dprint(statp->options & RES_DEBUG,
(stdout, ";; response truncated\n") (stdout, ";; response truncated\n")
); );
truncating = 1; truncating = 1;
len = anssiz; len = anssiz;
}
} else } else
len = resplen; len = resplen;
if (len < HFIXEDSZ) { if (len < HFIXEDSZ) {
@ -851,10 +894,12 @@ send_vc(res_state statp,
static int static int
send_dg(res_state statp, send_dg(res_state statp,
const u_char *buf, int buflen, u_char *ans, int anssiz, const u_char *buf, int buflen, u_char **ansp, int *anssizp,
int *terrno, int ns, int *v_circuit, int *gotsomewhere) int *terrno, int ns, int *v_circuit, int *gotsomewhere, u_char **anscp)
{ {
const HEADER *hp = (HEADER *) buf; const HEADER *hp = (HEADER *) buf;
u_char *ans = *ansp;
int anssiz = *anssizp;
HEADER *anhp = (HEADER *) ans; HEADER *anhp = (HEADER *) ans;
#ifdef _LIBC #ifdef _LIBC
struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns]; struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
@ -992,6 +1037,21 @@ send_dg(res_state statp,
#else #else
fromlen = sizeof(struct sockaddr_in); fromlen = sizeof(struct sockaddr_in);
#endif #endif
if (anssiz < MAXPACKET
&& anscp
&& (ioctl (s, FIONREAD, &resplen) < 0
|| anssiz < resplen)) {
ans = malloc (MAXPACKET);
if (ans == NULL)
ans = *ansp;
else {
anssiz = MAXPACKET;
*anssizp = MAXPACKET;
*ansp = ans;
*anscp = ans;
anhp = (HEADER *) ans;
}
}
resplen = recvfrom(s, (char*)ans, anssiz,0, resplen = recvfrom(s, (char*)ans, anssiz,0,
(struct sockaddr *)&from, &fromlen); (struct sockaddr *)&from, &fromlen);
if (resplen <= 0) { if (resplen <= 0) {