xref: /freebsd/lib/libc/net/gethostbydns.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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.20 1996/09/28 06:51:07 vixie Exp";
59 static char rcsid[] = "$Id: gethostbydns.c,v 1.20 1997/02/22 15:00:06 peter Exp $";
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 static struct hostent *
136 gethostanswer(answer, anslen, qname, qtype)
137 	const querybuf *answer;
138 	int anslen;
139 	const char *qname;
140 	int qtype;
141 {
142 	register const HEADER *hp;
143 	register const u_char *cp;
144 	register int n;
145 	const u_char *eom;
146 	char *bp, **ap, **hap;
147 	int type, class, buflen, ancount, qdcount;
148 	int haveanswer, had_error;
149 	int toobig = 0;
150 	char tbuf[MAXDNAME];
151 	const char *tname;
152 	int (*name_ok) __P((const char *));
153 
154 	tname = qname;
155 	host.h_name = NULL;
156 	eom = answer->buf + anslen;
157 	switch (qtype) {
158 	case T_A:
159 	case T_AAAA:
160 		name_ok = res_hnok;
161 		break;
162 	case T_PTR:
163 		name_ok = res_dnok;
164 		break;
165 	default:
166 		h_errno = NO_RECOVERY;
167 		return (NULL);	/* XXX should be abort(); */
168 	}
169 	/*
170 	 * find first satisfactory answer
171 	 */
172 	hp = &answer->hdr;
173 	ancount = ntohs(hp->ancount);
174 	qdcount = ntohs(hp->qdcount);
175 	bp = hostbuf;
176 	buflen = sizeof hostbuf;
177 	cp = answer->buf + HFIXEDSZ;
178 	if (qdcount != 1) {
179 		h_errno = NO_RECOVERY;
180 		return (NULL);
181 	}
182 	n = dn_expand(answer->buf, eom, cp, bp, buflen);
183 	if ((n < 0) || !(*name_ok)(bp)) {
184 		h_errno = NO_RECOVERY;
185 		return (NULL);
186 	}
187 	cp += n + QFIXEDSZ;
188 	if (qtype == T_A || qtype == T_AAAA) {
189 		/* res_send() has already verified that the query name is the
190 		 * same as the one we sent; this just gets the expanded name
191 		 * (i.e., with the succeeding search-domain tacked on).
192 		 */
193 		n = strlen(bp) + 1;		/* for the \0 */
194 		host.h_name = bp;
195 		bp += n;
196 		buflen -= n;
197 		/* The qname can be abbreviated, but h_name is now absolute. */
198 		qname = host.h_name;
199 	}
200 	ap = host_aliases;
201 	*ap = NULL;
202 	host.h_aliases = host_aliases;
203 	hap = h_addr_ptrs;
204 	*hap = NULL;
205 	host.h_addr_list = h_addr_ptrs;
206 	haveanswer = 0;
207 	had_error = 0;
208 	_dns_ttl_ = -1;
209 	while (ancount-- > 0 && cp < eom && !had_error) {
210 		n = dn_expand(answer->buf, eom, cp, bp, buflen);
211 		if ((n < 0) || !(*name_ok)(bp)) {
212 			had_error++;
213 			continue;
214 		}
215 		cp += n;			/* name */
216 		type = _getshort(cp);
217  		cp += INT16SZ;			/* type */
218 		class = _getshort(cp);
219  		cp += INT16SZ;			/* class */
220 		if (qtype == T_A  && type == T_A)
221 			_dns_ttl_ = _getlong(cp);
222 		cp += INT32SZ;			/* TTL */
223 		n = _getshort(cp);
224 		cp += INT16SZ;			/* len */
225 		if (class != C_IN) {
226 			/* XXX - debug? syslog? */
227 			cp += n;
228 			continue;		/* XXX - had_error++ ? */
229 		}
230 		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
231 			if (ap >= &host_aliases[MAXALIASES-1])
232 				continue;
233 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
234 			if ((n < 0) || !(*name_ok)(tbuf)) {
235 				had_error++;
236 				continue;
237 			}
238 			cp += n;
239 			/* Store alias. */
240 			*ap++ = bp;
241 			n = strlen(bp) + 1;	/* for the \0 */
242 			bp += n;
243 			buflen -= n;
244 			/* Get canonical name. */
245 			n = strlen(tbuf) + 1;	/* for the \0 */
246 			if (n > buflen) {
247 				had_error++;
248 				continue;
249 			}
250 			strcpy(bp, tbuf);
251 			host.h_name = bp;
252 			bp += n;
253 			buflen -= n;
254 			continue;
255 		}
256 		if (qtype == T_PTR && type == T_CNAME) {
257 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
258 			if ((n < 0) || !res_hnok(tbuf)) {
259 				had_error++;
260 				continue;
261 			}
262 			cp += n;
263 			/* Get canonical name. */
264 			n = strlen(tbuf) + 1;	/* for the \0 */
265 			if (n > buflen) {
266 				had_error++;
267 				continue;
268 			}
269 			strcpy(bp, tbuf);
270 			tname = bp;
271 			bp += n;
272 			buflen -= n;
273 			continue;
274 		}
275 		if (type != qtype) {
276 			syslog(LOG_NOTICE|LOG_AUTH,
277 	"gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
278 			       qname, p_class(C_IN), p_type(qtype),
279 			       p_type(type));
280 			cp += n;
281 			continue;		/* XXX - had_error++ ? */
282 		}
283 		switch (type) {
284 		case T_PTR:
285 			if (strcasecmp(tname, bp) != 0) {
286 				syslog(LOG_NOTICE|LOG_AUTH,
287 				       AskedForGot, qname, bp);
288 				cp += n;
289 				continue;	/* XXX - had_error++ ? */
290 			}
291 			n = dn_expand(answer->buf, eom, cp, bp, buflen);
292 			if ((n < 0) || !res_hnok(bp)) {
293 				had_error++;
294 				break;
295 			}
296 #if MULTI_PTRS_ARE_ALIASES
297 			cp += n;
298 			if (!haveanswer)
299 				host.h_name = bp;
300 			else if (ap < &host_aliases[MAXALIASES-1])
301 				*ap++ = bp;
302 			else
303 				n = -1;
304 			if (n != -1) {
305 				n = strlen(bp) + 1;	/* for the \0 */
306 				bp += n;
307 				buflen -= n;
308 			}
309 			break;
310 #else
311 			host.h_name = bp;
312 			if (_res.options & RES_USE_INET6) {
313 				n = strlen(bp) + 1;	/* for the \0 */
314 				bp += n;
315 				buflen -= n;
316 				_map_v4v6_hostent(&host, &bp, &buflen);
317 			}
318 			h_errno = NETDB_SUCCESS;
319 			return (&host);
320 #endif
321 		case T_A:
322 		case T_AAAA:
323 			if (strcasecmp(host.h_name, bp) != 0) {
324 				syslog(LOG_NOTICE|LOG_AUTH,
325 				       AskedForGot, host.h_name, bp);
326 				cp += n;
327 				continue;	/* XXX - had_error++ ? */
328 			}
329 			if (n != host.h_length) {
330 				cp += n;
331 				continue;
332 			}
333 			if (!haveanswer) {
334 				register int nn;
335 
336 				host.h_name = bp;
337 				nn = strlen(bp) + 1;	/* for the \0 */
338 				bp += nn;
339 				buflen -= nn;
340 			}
341 
342 			bp += sizeof(align) - ((u_long)bp % sizeof(align));
343 
344 			if (bp + n >= &hostbuf[sizeof hostbuf]) {
345 				dprintf("size (%d) too big\n", n);
346 				had_error++;
347 				continue;
348 			}
349 			if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
350 				if (!toobig++)
351 					dprintf("Too many addresses (%d)\n",
352 						MAXADDRS);
353 				cp += n;
354 				continue;
355 			}
356 			bcopy(cp, *hap++ = bp, n);
357 			bp += n;
358 			buflen -= n;
359 			cp += n;
360 			break;
361 		default:
362 			dprintf("Impossible condition (type=%d)\n", type);
363 			h_errno = NO_RECOVERY;
364 			return (NULL);
365 			/* BIND has abort() here, too risky on bad data */
366 		}
367 		if (!had_error)
368 			haveanswer++;
369 	}
370 	if (haveanswer) {
371 		*ap = NULL;
372 		*hap = NULL;
373 # if defined(RESOLVSORT)
374 		/*
375 		 * Note: we sort even if host can take only one address
376 		 * in its return structures - should give it the "best"
377 		 * address in that case, not some random one
378 		 */
379 		if (_res.nsort && haveanswer > 1 && qtype == T_A)
380 			addrsort(h_addr_ptrs, haveanswer);
381 # endif /*RESOLVSORT*/
382 		if (!host.h_name) {
383 			n = strlen(qname) + 1;	/* for the \0 */
384 			if (n > buflen)
385 				goto try_again;
386 			strcpy(bp, qname);
387 			host.h_name = bp;
388 			bp += n;
389 			buflen -= n;
390 		}
391 		if (_res.options & RES_USE_INET6)
392 			_map_v4v6_hostent(&host, &bp, &buflen);
393 		h_errno = NETDB_SUCCESS;
394 		return (&host);
395 	}
396  try_again:
397 	h_errno = TRY_AGAIN;
398 	return (NULL);
399 }
400 
401 struct hostent *
402 __dns_getanswer(answer, anslen, qname, qtype)
403 	const char *answer;
404 	int anslen;
405 	const char *qname;
406 	int qtype;
407 {
408 	switch(qtype) {
409 	case T_AAAA:
410 		host.h_addrtype = AF_INET6;
411 		host.h_length = IN6ADDRSZ;
412 		break;
413 	case T_A:
414 	default:
415 		host.h_addrtype = AF_INET;
416 		host.h_length = INADDRSZ;
417 		break;
418 	}
419 
420 	return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
421 }
422 
423 struct hostent *
424 _gethostbydnsname(name, af)
425 	const char *name;
426 	int af;
427 {
428 	querybuf buf;
429 	register const char *cp;
430 	char *bp;
431 	int n, size, type, len;
432 
433 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
434 		h_errno = NETDB_INTERNAL;
435 		return (NULL);
436 	}
437 
438 	switch (af) {
439 	case AF_INET:
440 		size = INADDRSZ;
441 		type = T_A;
442 		break;
443 	case AF_INET6:
444 		size = IN6ADDRSZ;
445 		type = T_AAAA;
446 		break;
447 	default:
448 		h_errno = NETDB_INTERNAL;
449 		errno = EAFNOSUPPORT;
450 		return (NULL);
451 	}
452 
453 	host.h_addrtype = af;
454 	host.h_length = size;
455 
456 	/*
457 	 * if there aren't any dots, it could be a user-level alias.
458 	 * this is also done in res_query() since we are not the only
459 	 * function that looks up host names.
460 	 */
461 	if (!strchr(name, '.') && (cp = __hostalias(name)))
462 		name = cp;
463 
464 	/*
465 	 * disallow names consisting only of digits/dots, unless
466 	 * they end in a dot.
467 	 */
468 	if (isdigit(name[0]))
469 		for (cp = name;; ++cp) {
470 			if (!*cp) {
471 				if (*--cp == '.')
472 					break;
473 				/*
474 				 * All-numeric, no dot at the end.
475 				 * Fake up a hostent as if we'd actually
476 				 * done a lookup.
477 				 */
478 				if (inet_pton(af, name, host_addr) <= 0) {
479 					h_errno = HOST_NOT_FOUND;
480 					return (NULL);
481 				}
482 				strncpy(hostbuf, name, MAXDNAME);
483 				hostbuf[MAXDNAME] = '\0';
484 				bp = hostbuf + MAXDNAME;
485 				len = sizeof hostbuf - MAXDNAME;
486 				host.h_name = hostbuf;
487 				host.h_aliases = host_aliases;
488 				host_aliases[0] = NULL;
489 				h_addr_ptrs[0] = (char *)host_addr;
490 				h_addr_ptrs[1] = NULL;
491 				host.h_addr_list = h_addr_ptrs;
492 				if (_res.options & RES_USE_INET6)
493 					_map_v4v6_hostent(&host, &bp, &len);
494 				h_errno = NETDB_SUCCESS;
495 				return (&host);
496 			}
497 			if (!isdigit(*cp) && *cp != '.')
498 				break;
499 		}
500 	if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
501 	    name[0] == ':')
502 		for (cp = name;; ++cp) {
503 			if (!*cp) {
504 				if (*--cp == '.')
505 					break;
506 				/*
507 				 * All-IPv6-legal, no dot at the end.
508 				 * Fake up a hostent as if we'd actually
509 				 * done a lookup.
510 				 */
511 				if (inet_pton(af, name, host_addr) <= 0) {
512 					h_errno = HOST_NOT_FOUND;
513 					return (NULL);
514 				}
515 				strncpy(hostbuf, name, MAXDNAME);
516 				hostbuf[MAXDNAME] = '\0';
517 				bp = hostbuf + MAXDNAME;
518 				len = sizeof hostbuf - MAXDNAME;
519 				host.h_name = hostbuf;
520 				host.h_aliases = host_aliases;
521 				host_aliases[0] = NULL;
522 				h_addr_ptrs[0] = (char *)host_addr;
523 				h_addr_ptrs[1] = NULL;
524 				host.h_addr_list = h_addr_ptrs;
525 				h_errno = NETDB_SUCCESS;
526 				return (&host);
527 			}
528 			if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
529 				break;
530 		}
531 
532 	if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
533 		dprintf("res_search failed (%d)\n", n);
534 		return (NULL);
535 	}
536 	return (gethostanswer(&buf, n, name, type));
537 }
538 
539 struct hostent *
540 _gethostbydnsaddr(addr, len, af)
541 	const char *addr;	/* XXX should have been def'd as u_char! */
542 	int len, af;
543 {
544 	const u_char *uaddr = (const u_char *)addr;
545 	static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
546 	static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
547 	int n, size;
548 	querybuf buf;
549 	register struct hostent *hp;
550 	char qbuf[MAXDNAME+1], *qp;
551 #ifdef SUNSECURITY
552 	register struct hostent *rhp;
553 	char **haddr;
554 	u_long old_options;
555 	char hname2[MAXDNAME+1];
556 #endif /*SUNSECURITY*/
557 
558 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
559 		h_errno = NETDB_INTERNAL;
560 		return (NULL);
561 	}
562 	if (af == AF_INET6 && len == IN6ADDRSZ &&
563 	    (!bcmp(uaddr, mapped, sizeof mapped) ||
564 	     !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
565 		/* Unmap. */
566 		addr += sizeof mapped;
567 		uaddr += sizeof mapped;
568 		af = AF_INET;
569 		len = INADDRSZ;
570 	}
571 	switch (af) {
572 	case AF_INET:
573 		size = INADDRSZ;
574 		break;
575 	case AF_INET6:
576 		size = IN6ADDRSZ;
577 		break;
578 	default:
579 		errno = EAFNOSUPPORT;
580 		h_errno = NETDB_INTERNAL;
581 		return (NULL);
582 	}
583 	if (size != len) {
584 		errno = EINVAL;
585 		h_errno = NETDB_INTERNAL;
586 		return (NULL);
587 	}
588 	switch (af) {
589 	case AF_INET:
590 		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
591 			       (uaddr[3] & 0xff),
592 			       (uaddr[2] & 0xff),
593 			       (uaddr[1] & 0xff),
594 			       (uaddr[0] & 0xff));
595 		break;
596 	case AF_INET6:
597 		qp = qbuf;
598 		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
599 			qp += SPRINTF((qp, "%x.%x.",
600 				       uaddr[n] & 0xf,
601 				       (uaddr[n] >> 4) & 0xf));
602 		}
603 		strcpy(qp, "ip6.int");
604 		break;
605 	default:
606 		abort();
607 	}
608 	n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
609 	if (n < 0) {
610 		dprintf("res_query failed (%d)\n", n);
611 		return (NULL);
612 	}
613 	if (!(hp = gethostanswer(&buf, n, qbuf, T_PTR)))
614 		return (NULL);	/* h_errno was set by gethostanswer() */
615 #ifdef SUNSECURITY
616 	if (af == AF_INET) {
617 	    /*
618 	     * turn off search as the name should be absolute,
619 	     * 'localhost' should be matched by defnames
620 	     */
621 	    strncpy(hname2, hp->h_name, MAXDNAME);
622 	    hname2[MAXDNAME] = '\0';
623 	    old_options = _res.options;
624 	    _res.options &= ~RES_DNSRCH;
625 	    _res.options |= RES_DEFNAMES;
626 	    if (!(rhp = gethostbyname(hname2))) {
627 		syslog(LOG_NOTICE|LOG_AUTH,
628 		       "gethostbyaddr: No A record for %s (verifying [%s])",
629 		       hname2, inet_ntoa(*((struct in_addr *)addr)));
630 		_res.options = old_options;
631 		h_errno = HOST_NOT_FOUND;
632 		return (NULL);
633 	    }
634 	    _res.options = old_options;
635 	    for (haddr = rhp->h_addr_list; *haddr; haddr++)
636 		if (!memcmp(*haddr, addr, INADDRSZ))
637 			break;
638 	    if (!*haddr) {
639 		syslog(LOG_NOTICE|LOG_AUTH,
640 		       "gethostbyaddr: A record of %s != PTR record [%s]",
641 		       hname2, inet_ntoa(*((struct in_addr *)addr)));
642 		h_errno = HOST_NOT_FOUND;
643 		return (NULL);
644 	    }
645 	}
646 #endif /*SUNSECURITY*/
647 	hp->h_addrtype = af;
648 	hp->h_length = len;
649 	bcopy(addr, host_addr, len);
650 	h_addr_ptrs[0] = (char *)host_addr;
651 	h_addr_ptrs[1] = NULL;
652 	if (af == AF_INET && (_res.options & RES_USE_INET6)) {
653 		_map_v4v6_address((char*)host_addr, (char*)host_addr);
654 		hp->h_addrtype = AF_INET6;
655 		hp->h_length = IN6ADDRSZ;
656 	}
657 	h_errno = NETDB_SUCCESS;
658 	return (hp);
659 }
660 
661 #ifdef RESOLVSORT
662 static void
663 addrsort(ap, num)
664 	char **ap;
665 	int num;
666 {
667 	int i, j;
668 	char **p;
669 	short aval[MAXADDRS];
670 	int needsort = 0;
671 
672 	p = ap;
673 	for (i = 0; i < num; i++, p++) {
674 	    for (j = 0 ; (unsigned)j < _res.nsort; j++)
675 		if (_res.sort_list[j].addr.s_addr ==
676 		    (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
677 			break;
678 	    aval[i] = j;
679 	    if (needsort == 0 && i > 0 && j < aval[i-1])
680 		needsort = i;
681 	}
682 	if (!needsort)
683 	    return;
684 
685 	while (needsort < num) {
686 	    for (j = needsort - 1; j >= 0; j--) {
687 		if (aval[j] > aval[j+1]) {
688 		    char *hp;
689 
690 		    i = aval[j];
691 		    aval[j] = aval[j+1];
692 		    aval[j+1] = i;
693 
694 		    hp = ap[j];
695 		    ap[j] = ap[j+1];
696 		    ap[j+1] = hp;
697 
698 		} else
699 		    break;
700 	    }
701 	    needsort++;
702 	}
703 }
704 #endif
705 void
706 _sethostdnsent(stayopen)
707 	int stayopen;
708 {
709 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
710 		return;
711 	if (stayopen)
712 		_res.options |= RES_STAYOPEN | RES_USEVC;
713 }
714 
715 void
716 _endhostdnsent()
717 {
718 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
719 	res_close();
720 }
721