xref: /freebsd/lib/libc/net/gethostbydns.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * ++Copyright++ 1985, 1988, 1993
3  * -
4  * Copyright (c) 1985, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  * -
35  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36  *
37  * Permission to use, copy, modify, and distribute this software for any
38  * purpose with or without fee is hereby granted, provided that the above
39  * copyright notice and this permission notice appear in all copies, and that
40  * the name of Digital Equipment Corporation not be used in advertising or
41  * publicity pertaining to distribution of the document or software without
42  * specific, written prior permission.
43  *
44  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
47  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51  * SOFTWARE.
52  * -
53  * --Copyright--
54  */
55 
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
58 static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
59 static char rcsid[] = "$FreeBSD$";
60 #endif /* LIBC_SCCS and not lint */
61 
62 #include <sys/types.h>
63 #include <sys/param.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <arpa/nameser.h>
68 
69 #include <stdio.h>
70 #include <unistd.h>
71 #include <string.h>
72 #include <netdb.h>
73 #include <resolv.h>
74 #include <ctype.h>
75 #include <errno.h>
76 #include <syslog.h>
77 
78 #include "res_config.h"
79 
80 #define SPRINTF(x) ((size_t)sprintf x)
81 
82 #define	MAXALIASES	35
83 #define	MAXADDRS	35
84 
85 static const char AskedForGot[] =
86 		"gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
87 
88 static char *h_addr_ptrs[MAXADDRS + 1];
89 
90 static struct hostent host;
91 static char *host_aliases[MAXALIASES];
92 static char hostbuf[8*1024];
93 static u_char host_addr[16];	/* IPv4 or IPv6 */
94 
95 #ifdef RESOLVSORT
96 static void addrsort __P((char **, int));
97 #endif
98 
99 #if PACKETSZ > 1024
100 #define	MAXPACKET	PACKETSZ
101 #else
102 #define	MAXPACKET	1024
103 #endif
104 
105 typedef union {
106     HEADER hdr;
107     u_char buf[MAXPACKET];
108 } querybuf;
109 
110 typedef union {
111     int32_t al;
112     char ac;
113 } align;
114 
115 extern int h_errno;
116 int _dns_ttl_;
117 
118 #ifdef DEBUG
119 static void
120 dprintf(msg, num)
121 	char *msg;
122 	int num;
123 {
124 	if (_res.options & RES_DEBUG) {
125 		int save = errno;
126 
127 		printf(msg, num);
128 		errno = save;
129 	}
130 }
131 #else
132 # define dprintf(msg, num) /*nada*/
133 #endif
134 
135 #define BOUNDED_INCR(x) \
136 	do { \
137 		cp += x; \
138 		if (cp > eom) { \
139 			h_errno = NO_RECOVERY; \
140 			return (NULL); \
141 		} \
142 	} while (0)
143 
144 #define BOUNDS_CHECK(ptr, count) \
145 	do { \
146 		if ((ptr) + (count) > eom) { \
147 			h_errno = NO_RECOVERY; \
148 			return (NULL); \
149 		} \
150 	} while (0)
151 
152 static struct hostent *
153 gethostanswer(answer, anslen, qname, qtype)
154 	const querybuf *answer;
155 	int anslen;
156 	const char *qname;
157 	int qtype;
158 {
159 	register const HEADER *hp;
160 	register const u_char *cp;
161 	register int n;
162 	const u_char *eom, *erdata;
163 	char *bp, **ap, **hap;
164 	int type, class, buflen, ancount, qdcount;
165 	int haveanswer, had_error;
166 	int toobig = 0;
167 	char tbuf[MAXDNAME];
168 	const char *tname;
169 	int (*name_ok) __P((const char *));
170 
171 	tname = qname;
172 	host.h_name = NULL;
173 	eom = answer->buf + anslen;
174 	switch (qtype) {
175 	case T_A:
176 	case T_AAAA:
177 		name_ok = res_hnok;
178 		break;
179 	case T_PTR:
180 		name_ok = res_dnok;
181 		break;
182 	default:
183 		h_errno = NO_RECOVERY;
184 		return (NULL);	/* XXX should be abort(); */
185 	}
186 	/*
187 	 * find first satisfactory answer
188 	 */
189 	hp = &answer->hdr;
190 	ancount = ntohs(hp->ancount);
191 	qdcount = ntohs(hp->qdcount);
192 	bp = hostbuf;
193 	buflen = sizeof hostbuf;
194 	cp = answer->buf;
195 	BOUNDED_INCR(HFIXEDSZ);
196 	if (qdcount != 1) {
197 		h_errno = NO_RECOVERY;
198 		return (NULL);
199 	}
200 	n = dn_expand(answer->buf, eom, cp, bp, buflen);
201 	if ((n < 0) || !(*name_ok)(bp)) {
202 		h_errno = NO_RECOVERY;
203 		return (NULL);
204 	}
205 	BOUNDED_INCR(n + QFIXEDSZ);
206 	if (qtype == T_A || qtype == T_AAAA) {
207 		/* res_send() has already verified that the query name is the
208 		 * same as the one we sent; this just gets the expanded name
209 		 * (i.e., with the succeeding search-domain tacked on).
210 		 */
211 		n = strlen(bp) + 1;		/* for the \0 */
212 		if (n >= MAXHOSTNAMELEN) {
213 			h_errno = NO_RECOVERY;
214 			return (NULL);
215 		}
216 		host.h_name = bp;
217 		bp += n;
218 		buflen -= n;
219 		/* The qname can be abbreviated, but h_name is now absolute. */
220 		qname = host.h_name;
221 	}
222 	ap = host_aliases;
223 	*ap = NULL;
224 	host.h_aliases = host_aliases;
225 	hap = h_addr_ptrs;
226 	*hap = NULL;
227 	host.h_addr_list = h_addr_ptrs;
228 	haveanswer = 0;
229 	had_error = 0;
230 	_dns_ttl_ = -1;
231 	while (ancount-- > 0 && cp < eom && !had_error) {
232 		n = dn_expand(answer->buf, eom, cp, bp, buflen);
233 		if ((n < 0) || !(*name_ok)(bp)) {
234 			had_error++;
235 			continue;
236 		}
237 		cp += n;			/* name */
238 		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
239 		type = _getshort(cp);
240  		cp += INT16SZ;			/* type */
241 		class = _getshort(cp);
242  		cp += INT16SZ;			/* class */
243 		if (qtype == T_A  && type == T_A)
244 			_dns_ttl_ = _getlong(cp);
245 		cp += INT32SZ;			/* TTL */
246 		n = _getshort(cp);
247 		cp += INT16SZ;			/* len */
248 		BOUNDS_CHECK(cp, n);
249 		erdata = cp + n;
250 		if (class != C_IN) {
251 			/* XXX - debug? syslog? */
252 			cp += n;
253 			continue;		/* XXX - had_error++ ? */
254 		}
255 		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
256 			if (ap >= &host_aliases[MAXALIASES-1])
257 				continue;
258 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
259 			if ((n < 0) || !(*name_ok)(tbuf)) {
260 				had_error++;
261 				continue;
262 			}
263 			cp += n;
264 			if (cp != erdata) {
265 				h_errno = NO_RECOVERY;
266 				return (NULL);
267 			}
268 			/* Store alias. */
269 			*ap++ = bp;
270 			n = strlen(bp) + 1;	/* for the \0 */
271 			if (n >= MAXHOSTNAMELEN) {
272 				had_error++;
273 				continue;
274 			}
275 			bp += n;
276 			buflen -= n;
277 			/* Get canonical name. */
278 			n = strlen(tbuf) + 1;	/* for the \0 */
279 			if (n > buflen || n >= MAXHOSTNAMELEN) {
280 				had_error++;
281 				continue;
282 			}
283 			strcpy(bp, tbuf);
284 			host.h_name = bp;
285 			bp += n;
286 			buflen -= n;
287 			continue;
288 		}
289 		if (qtype == T_PTR && type == T_CNAME) {
290 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
291 			if (n < 0 || !res_dnok(tbuf)) {
292 				had_error++;
293 				continue;
294 			}
295 			cp += n;
296 			if (cp != erdata) {
297 				h_errno = NO_RECOVERY;
298 				return (NULL);
299 			}
300 			/* Get canonical name. */
301 			n = strlen(tbuf) + 1;	/* for the \0 */
302 			if (n > buflen || n >= MAXHOSTNAMELEN) {
303 				had_error++;
304 				continue;
305 			}
306 			strcpy(bp, tbuf);
307 			tname = bp;
308 			bp += n;
309 			buflen -= n;
310 			continue;
311 		}
312 		if (type != qtype) {
313 			if (type != T_SIG)
314 				syslog(LOG_NOTICE|LOG_AUTH,
315 	"gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
316 				       qname, p_class(C_IN), p_type(qtype),
317 				       p_type(type));
318 			cp += n;
319 			continue;		/* XXX - had_error++ ? */
320 		}
321 		switch (type) {
322 		case T_PTR:
323 			if (strcasecmp(tname, bp) != 0) {
324 				syslog(LOG_NOTICE|LOG_AUTH,
325 				       AskedForGot, qname, bp);
326 				cp += n;
327 				continue;	/* XXX - had_error++ ? */
328 			}
329 			n = dn_expand(answer->buf, eom, cp, bp, buflen);
330 			if ((n < 0) || !res_hnok(bp)) {
331 				had_error++;
332 				break;
333 			}
334 #if MULTI_PTRS_ARE_ALIASES
335 			cp += n;
336 			if (cp != erdata) {
337 				h_errno = NO_RECOVERY;
338 				return (NULL);
339 			}
340 			if (!haveanswer)
341 				host.h_name = bp;
342 			else if (ap < &host_aliases[MAXALIASES-1])
343 				*ap++ = bp;
344 			else
345 				n = -1;
346 			if (n != -1) {
347 				n = strlen(bp) + 1;	/* for the \0 */
348 				if (n >= MAXHOSTNAMELEN) {
349 					had_error++;
350 					break;
351 				}
352 				bp += n;
353 				buflen -= n;
354 			}
355 			break;
356 #else
357 			host.h_name = bp;
358 			if (_res.options & RES_USE_INET6) {
359 				n = strlen(bp) + 1;	/* for the \0 */
360 				if (n >= MAXHOSTNAMELEN) {
361 					had_error++;
362 					break;
363 				}
364 				bp += n;
365 				buflen -= n;
366 				_map_v4v6_hostent(&host, &bp, &buflen);
367 			}
368 			h_errno = NETDB_SUCCESS;
369 			return (&host);
370 #endif
371 		case T_A:
372 		case T_AAAA:
373 			if (strcasecmp(host.h_name, bp) != 0) {
374 				syslog(LOG_NOTICE|LOG_AUTH,
375 				       AskedForGot, host.h_name, bp);
376 				cp += n;
377 				continue;	/* XXX - had_error++ ? */
378 			}
379 			if (n != host.h_length) {
380 				cp += n;
381 				continue;
382 			}
383 			if (!haveanswer) {
384 				register int nn;
385 
386 				host.h_name = bp;
387 				nn = strlen(bp) + 1;	/* for the \0 */
388 				bp += nn;
389 				buflen -= nn;
390 			}
391 
392 			bp += sizeof(align) - ((u_long)bp % sizeof(align));
393 
394 			if (bp + n >= &hostbuf[sizeof hostbuf]) {
395 				dprintf("size (%d) too big\n", n);
396 				had_error++;
397 				continue;
398 			}
399 			if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
400 				if (!toobig++)
401 					dprintf("Too many addresses (%d)\n",
402 						MAXADDRS);
403 				cp += n;
404 				continue;
405 			}
406 			bcopy(cp, *hap++ = bp, n);
407 			bp += n;
408 			buflen -= n;
409 			cp += n;
410 			if (cp != erdata) {
411 				h_errno = NO_RECOVERY;
412 				return (NULL);
413 			}
414 			break;
415 		default:
416 			dprintf("Impossible condition (type=%d)\n", type);
417 			h_errno = NO_RECOVERY;
418 			return (NULL);
419 			/* BIND has abort() here, too risky on bad data */
420 		}
421 		if (!had_error)
422 			haveanswer++;
423 	}
424 	if (haveanswer) {
425 		*ap = NULL;
426 		*hap = NULL;
427 # if defined(RESOLVSORT)
428 		/*
429 		 * Note: we sort even if host can take only one address
430 		 * in its return structures - should give it the "best"
431 		 * address in that case, not some random one
432 		 */
433 		if (_res.nsort && haveanswer > 1 && qtype == T_A)
434 			addrsort(h_addr_ptrs, haveanswer);
435 # endif /*RESOLVSORT*/
436 		if (!host.h_name) {
437 			n = strlen(qname) + 1;	/* for the \0 */
438 			if (n > buflen || n >= MAXHOSTNAMELEN)
439 				goto no_recovery;
440 			strcpy(bp, qname);
441 			host.h_name = bp;
442 			bp += n;
443 			buflen -= n;
444 		}
445 		if (_res.options & RES_USE_INET6)
446 			_map_v4v6_hostent(&host, &bp, &buflen);
447 		h_errno = NETDB_SUCCESS;
448 		return (&host);
449 	}
450  no_recovery:
451 	h_errno = NO_RECOVERY;
452 	return (NULL);
453 }
454 
455 struct hostent *
456 __dns_getanswer(answer, anslen, qname, qtype)
457 	const char *answer;
458 	int anslen;
459 	const char *qname;
460 	int qtype;
461 {
462 	switch(qtype) {
463 	case T_AAAA:
464 		host.h_addrtype = AF_INET6;
465 		host.h_length = IN6ADDRSZ;
466 		break;
467 	case T_A:
468 	default:
469 		host.h_addrtype = AF_INET;
470 		host.h_length = INADDRSZ;
471 		break;
472 	}
473 
474 	return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
475 }
476 
477 struct hostent *
478 _gethostbydnsname(name, af)
479 	const char *name;
480 	int af;
481 {
482 	querybuf buf;
483 	register const char *cp;
484 	char *bp;
485 	int n, size, type, len;
486 
487 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
488 		h_errno = NETDB_INTERNAL;
489 		return (NULL);
490 	}
491 
492 	switch (af) {
493 	case AF_INET:
494 		size = INADDRSZ;
495 		type = T_A;
496 		break;
497 	case AF_INET6:
498 		size = IN6ADDRSZ;
499 		type = T_AAAA;
500 		break;
501 	default:
502 		h_errno = NETDB_INTERNAL;
503 		errno = EAFNOSUPPORT;
504 		return (NULL);
505 	}
506 
507 	host.h_addrtype = af;
508 	host.h_length = size;
509 
510 	/*
511 	 * if there aren't any dots, it could be a user-level alias.
512 	 * this is also done in res_query() since we are not the only
513 	 * function that looks up host names.
514 	 */
515 	if (!strchr(name, '.') && (cp = __hostalias(name)))
516 		name = cp;
517 
518 	/*
519 	 * disallow names consisting only of digits/dots, unless
520 	 * they end in a dot.
521 	 */
522 	if (isdigit((unsigned char)name[0]))
523 		for (cp = name;; ++cp) {
524 			if (!*cp) {
525 				if (*--cp == '.')
526 					break;
527 				/*
528 				 * All-numeric, no dot at the end.
529 				 * Fake up a hostent as if we'd actually
530 				 * done a lookup.
531 				 */
532 				if (inet_pton(af, name, host_addr) <= 0) {
533 					h_errno = HOST_NOT_FOUND;
534 					return (NULL);
535 				}
536 				strncpy(hostbuf, name, MAXDNAME);
537 				hostbuf[MAXDNAME] = '\0';
538 				bp = hostbuf + MAXDNAME;
539 				len = sizeof hostbuf - MAXDNAME;
540 				host.h_name = hostbuf;
541 				host.h_aliases = host_aliases;
542 				host_aliases[0] = NULL;
543 				h_addr_ptrs[0] = (char *)host_addr;
544 				h_addr_ptrs[1] = NULL;
545 				host.h_addr_list = h_addr_ptrs;
546 				if (_res.options & RES_USE_INET6)
547 					_map_v4v6_hostent(&host, &bp, &len);
548 				h_errno = NETDB_SUCCESS;
549 				return (&host);
550 			}
551 			if (!isdigit((unsigned char)*cp) && *cp != '.')
552 				break;
553 		}
554 	if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
555 	    name[0] == ':')
556 		for (cp = name;; ++cp) {
557 			if (!*cp) {
558 				if (*--cp == '.')
559 					break;
560 				/*
561 				 * All-IPv6-legal, no dot at the end.
562 				 * Fake up a hostent as if we'd actually
563 				 * done a lookup.
564 				 */
565 				if (inet_pton(af, name, host_addr) <= 0) {
566 					h_errno = HOST_NOT_FOUND;
567 					return (NULL);
568 				}
569 				strncpy(hostbuf, name, MAXDNAME);
570 				hostbuf[MAXDNAME] = '\0';
571 				bp = hostbuf + MAXDNAME;
572 				len = sizeof hostbuf - MAXDNAME;
573 				host.h_name = hostbuf;
574 				host.h_aliases = host_aliases;
575 				host_aliases[0] = NULL;
576 				h_addr_ptrs[0] = (char *)host_addr;
577 				h_addr_ptrs[1] = NULL;
578 				host.h_addr_list = h_addr_ptrs;
579 				h_errno = NETDB_SUCCESS;
580 				return (&host);
581 			}
582 			if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
583 				break;
584 		}
585 
586 	if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
587 		dprintf("res_search failed (%d)\n", n);
588 		return (NULL);
589 	}
590 	return (gethostanswer(&buf, n, name, type));
591 }
592 
593 struct hostent *
594 _gethostbydnsaddr(addr, len, af)
595 	const char *addr;	/* XXX should have been def'd as u_char! */
596 	int len, af;
597 {
598 	const u_char *uaddr = (const u_char *)addr;
599 	static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
600 	static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
601 	int n, size;
602 	querybuf buf;
603 	register struct hostent *hp;
604 	char qbuf[MAXDNAME+1], *qp;
605 #ifdef SUNSECURITY
606 	register struct hostent *rhp;
607 	char **haddr;
608 	u_long old_options;
609 	char hname2[MAXDNAME+1];
610 #endif /*SUNSECURITY*/
611 
612 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
613 		h_errno = NETDB_INTERNAL;
614 		return (NULL);
615 	}
616 	if (af == AF_INET6 && len == IN6ADDRSZ &&
617 	    (!bcmp(uaddr, mapped, sizeof mapped) ||
618 	     !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
619 		/* Unmap. */
620 		addr += sizeof mapped;
621 		uaddr += sizeof mapped;
622 		af = AF_INET;
623 		len = INADDRSZ;
624 	}
625 	switch (af) {
626 	case AF_INET:
627 		size = INADDRSZ;
628 		break;
629 	case AF_INET6:
630 		size = IN6ADDRSZ;
631 		break;
632 	default:
633 		errno = EAFNOSUPPORT;
634 		h_errno = NETDB_INTERNAL;
635 		return (NULL);
636 	}
637 	if (size != len) {
638 		errno = EINVAL;
639 		h_errno = NETDB_INTERNAL;
640 		return (NULL);
641 	}
642 	switch (af) {
643 	case AF_INET:
644 		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
645 			       (uaddr[3] & 0xff),
646 			       (uaddr[2] & 0xff),
647 			       (uaddr[1] & 0xff),
648 			       (uaddr[0] & 0xff));
649 		break;
650 	case AF_INET6:
651 		qp = qbuf;
652 		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
653 			qp += SPRINTF((qp, "%x.%x.",
654 				       uaddr[n] & 0xf,
655 				       (uaddr[n] >> 4) & 0xf));
656 		}
657 		strcpy(qp, "ip6.int");
658 		break;
659 	default:
660 		abort();
661 	}
662 	n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
663 	if (n < 0) {
664 		dprintf("res_query failed (%d)\n", n);
665 		return (NULL);
666 	}
667 	if (n > sizeof buf.buf) {
668 		dprintf("static buffer is too small (%d)\n", n);
669 		return (NULL);
670 	}
671 	if (!(hp = gethostanswer(&buf, n, qbuf, T_PTR)))
672 		return (NULL);	/* h_errno was set by gethostanswer() */
673 #ifdef SUNSECURITY
674 	if (af == AF_INET) {
675 	    /*
676 	     * turn off search as the name should be absolute,
677 	     * 'localhost' should be matched by defnames
678 	     */
679 	    strncpy(hname2, hp->h_name, MAXDNAME);
680 	    hname2[MAXDNAME] = '\0';
681 	    old_options = _res.options;
682 	    _res.options &= ~RES_DNSRCH;
683 	    _res.options |= RES_DEFNAMES;
684 	    if (!(rhp = gethostbyname(hname2))) {
685 		syslog(LOG_NOTICE|LOG_AUTH,
686 		       "gethostbyaddr: No A record for %s (verifying [%s])",
687 		       hname2, inet_ntoa(*((struct in_addr *)addr)));
688 		_res.options = old_options;
689 		h_errno = HOST_NOT_FOUND;
690 		return (NULL);
691 	    }
692 	    _res.options = old_options;
693 	    for (haddr = rhp->h_addr_list; *haddr; haddr++)
694 		if (!memcmp(*haddr, addr, INADDRSZ))
695 			break;
696 	    if (!*haddr) {
697 		syslog(LOG_NOTICE|LOG_AUTH,
698 		       "gethostbyaddr: A record of %s != PTR record [%s]",
699 		       hname2, inet_ntoa(*((struct in_addr *)addr)));
700 		h_errno = HOST_NOT_FOUND;
701 		return (NULL);
702 	    }
703 	}
704 #endif /*SUNSECURITY*/
705 	hp->h_addrtype = af;
706 	hp->h_length = len;
707 	bcopy(addr, host_addr, len);
708 	h_addr_ptrs[0] = (char *)host_addr;
709 	h_addr_ptrs[1] = NULL;
710 	if (af == AF_INET && (_res.options & RES_USE_INET6)) {
711 		_map_v4v6_address((char*)host_addr, (char*)host_addr);
712 		hp->h_addrtype = AF_INET6;
713 		hp->h_length = IN6ADDRSZ;
714 	}
715 	h_errno = NETDB_SUCCESS;
716 	return (hp);
717 }
718 
719 #ifdef RESOLVSORT
720 static void
721 addrsort(ap, num)
722 	char **ap;
723 	int num;
724 {
725 	int i, j;
726 	char **p;
727 	short aval[MAXADDRS];
728 	int needsort = 0;
729 
730 	p = ap;
731 	for (i = 0; i < num; i++, p++) {
732 	    for (j = 0 ; (unsigned)j < _res.nsort; j++)
733 		if (_res.sort_list[j].addr.s_addr ==
734 		    (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
735 			break;
736 	    aval[i] = j;
737 	    if (needsort == 0 && i > 0 && j < aval[i-1])
738 		needsort = i;
739 	}
740 	if (!needsort)
741 	    return;
742 
743 	while (needsort < num) {
744 	    for (j = needsort - 1; j >= 0; j--) {
745 		if (aval[j] > aval[j+1]) {
746 		    char *hp;
747 
748 		    i = aval[j];
749 		    aval[j] = aval[j+1];
750 		    aval[j+1] = i;
751 
752 		    hp = ap[j];
753 		    ap[j] = ap[j+1];
754 		    ap[j+1] = hp;
755 
756 		} else
757 		    break;
758 	    }
759 	    needsort++;
760 	}
761 }
762 #endif
763 void
764 _sethostdnsent(stayopen)
765 	int stayopen;
766 {
767 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
768 		return;
769 	if (stayopen)
770 		_res.options |= RES_STAYOPEN | RES_USEVC;
771 }
772 
773 void
774 _endhostdnsent()
775 {
776 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
777 	res_close();
778 }
779