xref: /freebsd/lib/libc/net/gethostbydns.c (revision 3d11b6c8f01e1fca5936a11d6996448467851a94)
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 #endif /* LIBC_SCCS and not lint */
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
69 
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <unistd.h>
73 #include <string.h>
74 #include <netdb.h>
75 #include <resolv.h>
76 #include <ctype.h>
77 #include <errno.h>
78 #include <syslog.h>
79 #include <stdarg.h>
80 #include <nsswitch.h>
81 
82 #include "netdb_private.h"
83 #include "res_config.h"
84 
85 #define SPRINTF(x) ((size_t)sprintf x)
86 
87 static const char AskedForGot[] =
88 		"gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
89 
90 #ifdef RESOLVSORT
91 static void addrsort(char **, int, res_state);
92 #endif
93 
94 #ifdef DEBUG
95 static void dprintf(char *, int, res_state) __printflike(1, 0);
96 #endif
97 
98 #define MAXPACKET	(64*1024)
99 
100 typedef union {
101     HEADER hdr;
102     u_char buf[MAXPACKET];
103 } querybuf;
104 
105 typedef union {
106     int32_t al;
107     char ac;
108 } align;
109 
110 int _dns_ttl_;
111 
112 #ifdef DEBUG
113 static void
114 dprintf(msg, num, res)
115 	char *msg;
116 	int num;
117 	res_state res;
118 {
119 	if (res->options & RES_DEBUG) {
120 		int save = errno;
121 
122 		printf(msg, num);
123 		errno = save;
124 	}
125 }
126 #else
127 # define dprintf(msg, num, res) /*nada*/
128 #endif
129 
130 #define BOUNDED_INCR(x) \
131 	do { \
132 		cp += x; \
133 		if (cp > eom) { \
134 			RES_SET_H_ERRNO(hed->res, NO_RECOVERY); \
135 			return -1; \
136 		} \
137 	} while (0)
138 
139 #define BOUNDS_CHECK(ptr, count) \
140 	do { \
141 		if ((ptr) + (count) > eom) { \
142 			RES_SET_H_ERRNO(hed->res, NO_RECOVERY); \
143 			return -1; \
144 		} \
145 	} while (0)
146 
147 static int
148 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
149     struct hostent *he, struct hostent_data *hed)
150 {
151 	const HEADER *hp;
152 	const u_char *cp;
153 	int n;
154 	const u_char *eom, *erdata;
155 	char *bp, *ep, **ap, **hap;
156 	int type, class, ancount, qdcount;
157 	int haveanswer, had_error;
158 	int toobig = 0;
159 	char tbuf[MAXDNAME];
160 	const char *tname;
161 	int (*name_ok)(const char *);
162 
163 	tname = qname;
164 	he->h_name = NULL;
165 	eom = answer->buf + anslen;
166 	switch (qtype) {
167 	case T_A:
168 	case T_AAAA:
169 		name_ok = res_hnok;
170 		break;
171 	case T_PTR:
172 		name_ok = res_dnok;
173 		break;
174 	default:
175 		RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
176 		return -1;	/* XXX should be abort(); */
177 	}
178 	/*
179 	 * find first satisfactory answer
180 	 */
181 	hp = &answer->hdr;
182 	ancount = ntohs(hp->ancount);
183 	qdcount = ntohs(hp->qdcount);
184 	bp = hed->hostbuf;
185 	ep = hed->hostbuf + sizeof hed->hostbuf;
186 	cp = answer->buf;
187 	BOUNDED_INCR(HFIXEDSZ);
188 	if (qdcount != 1) {
189 		RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
190 		return -1;
191 	}
192 	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
193 	if ((n < 0) || !(*name_ok)(bp)) {
194 		RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
195 		return -1;
196 	}
197 	BOUNDED_INCR(n + QFIXEDSZ);
198 	if (qtype == T_A || qtype == T_AAAA) {
199 		/* res_send() has already verified that the query name is the
200 		 * same as the one we sent; this just gets the expanded name
201 		 * (i.e., with the succeeding search-domain tacked on).
202 		 */
203 		n = strlen(bp) + 1;		/* for the \0 */
204 		if (n >= MAXHOSTNAMELEN) {
205 			RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
206 			return -1;
207 		}
208 		he->h_name = bp;
209 		bp += n;
210 		/* The qname can be abbreviated, but h_name is now absolute. */
211 		qname = he->h_name;
212 	}
213 	ap = hed->host_aliases;
214 	*ap = NULL;
215 	he->h_aliases = hed->host_aliases;
216 	hap = hed->h_addr_ptrs;
217 	*hap = NULL;
218 	he->h_addr_list = hed->h_addr_ptrs;
219 	haveanswer = 0;
220 	had_error = 0;
221 	_dns_ttl_ = -1;
222 	while (ancount-- > 0 && cp < eom && !had_error) {
223 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
224 		if ((n < 0) || !(*name_ok)(bp)) {
225 			had_error++;
226 			continue;
227 		}
228 		cp += n;			/* name */
229 		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
230 		type = _getshort(cp);
231  		cp += INT16SZ;			/* type */
232 		class = _getshort(cp);
233  		cp += INT16SZ;			/* class */
234 		if (qtype == T_A  && type == T_A)
235 			_dns_ttl_ = _getlong(cp);
236 		cp += INT32SZ;			/* TTL */
237 		n = _getshort(cp);
238 		cp += INT16SZ;			/* len */
239 		BOUNDS_CHECK(cp, n);
240 		erdata = cp + n;
241 		if (class != C_IN) {
242 			/* XXX - debug? syslog? */
243 			cp += n;
244 			continue;		/* XXX - had_error++ ? */
245 		}
246 		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
247 			if (ap >= &hed->host_aliases[_MAXALIASES-1])
248 				continue;
249 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
250 			if ((n < 0) || !(*name_ok)(tbuf)) {
251 				had_error++;
252 				continue;
253 			}
254 			cp += n;
255 			if (cp != erdata) {
256 				RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
257 				return -1;
258 			}
259 			/* Store alias. */
260 			*ap++ = bp;
261 			n = strlen(bp) + 1;	/* for the \0 */
262 			if (n >= MAXHOSTNAMELEN) {
263 				had_error++;
264 				continue;
265 			}
266 			bp += n;
267 			/* Get canonical name. */
268 			n = strlen(tbuf) + 1;	/* for the \0 */
269 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
270 				had_error++;
271 				continue;
272 			}
273 			strcpy(bp, tbuf);
274 			he->h_name = bp;
275 			bp += n;
276 			continue;
277 		}
278 		if (qtype == T_PTR && type == T_CNAME) {
279 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
280 			if (n < 0 || !res_dnok(tbuf)) {
281 				had_error++;
282 				continue;
283 			}
284 			cp += n;
285 			if (cp != erdata) {
286 				RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
287 				return -1;
288 			}
289 			/* Get canonical name. */
290 			n = strlen(tbuf) + 1;	/* for the \0 */
291 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
292 				had_error++;
293 				continue;
294 			}
295 			strcpy(bp, tbuf);
296 			tname = bp;
297 			bp += n;
298 			continue;
299 		}
300 		if (type != qtype) {
301 			if (type != T_SIG)
302 				syslog(LOG_NOTICE|LOG_AUTH,
303 	"gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
304 				       qname, p_class(C_IN), p_type(qtype),
305 				       p_type(type));
306 			cp += n;
307 			continue;		/* XXX - had_error++ ? */
308 		}
309 		switch (type) {
310 		case T_PTR:
311 			if (strcasecmp(tname, bp) != 0) {
312 				syslog(LOG_NOTICE|LOG_AUTH,
313 				       AskedForGot, qname, bp);
314 				cp += n;
315 				continue;	/* XXX - had_error++ ? */
316 			}
317 			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
318 			if ((n < 0) || !res_hnok(bp)) {
319 				had_error++;
320 				break;
321 			}
322 #if MULTI_PTRS_ARE_ALIASES
323 			cp += n;
324 			if (cp != erdata) {
325 				RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
326 				return -1;
327 			}
328 			if (!haveanswer)
329 				he->h_name = bp;
330 			else if (ap < &hed->host_aliases[_MAXALIASES-1])
331 				*ap++ = bp;
332 			else
333 				n = -1;
334 			if (n != -1) {
335 				n = strlen(bp) + 1;	/* for the \0 */
336 				if (n >= MAXHOSTNAMELEN) {
337 					had_error++;
338 					break;
339 				}
340 				bp += n;
341 			}
342 			break;
343 #else
344 			he->h_name = bp;
345 			if (hed->res->options & RES_USE_INET6) {
346 				n = strlen(bp) + 1;	/* for the \0 */
347 				if (n >= MAXHOSTNAMELEN) {
348 					had_error++;
349 					break;
350 				}
351 				bp += n;
352 				_map_v4v6_hostent(he, &bp, ep);
353 			}
354 			RES_SET_H_ERRNO(hed->res, NETDB_SUCCESS);
355 			return 0;
356 #endif
357 		case T_A:
358 		case T_AAAA:
359 			if (strcasecmp(he->h_name, bp) != 0) {
360 				syslog(LOG_NOTICE|LOG_AUTH,
361 				       AskedForGot, he->h_name, bp);
362 				cp += n;
363 				continue;	/* XXX - had_error++ ? */
364 			}
365 			if (n != he->h_length) {
366 				cp += n;
367 				continue;
368 			}
369 			if (!haveanswer) {
370 				int nn;
371 
372 				he->h_name = bp;
373 				nn = strlen(bp) + 1;	/* for the \0 */
374 				bp += nn;
375 			}
376 
377 			bp += sizeof(align) - ((u_long)bp % sizeof(align));
378 
379 			if (bp + n >= ep) {
380 				dprintf("size (%d) too big\n", n, hed->res);
381 				had_error++;
382 				continue;
383 			}
384 			if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
385 				if (!toobig++)
386 					dprintf("Too many addresses (%d)\n",
387 						_MAXADDRS, hed->res);
388 				cp += n;
389 				continue;
390 			}
391 			memcpy(*hap++ = bp, cp, n);
392 			bp += n;
393 			cp += n;
394 			if (cp != erdata) {
395 				RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
396 				return -1;
397 			}
398 			break;
399 		default:
400 			dprintf("Impossible condition (type=%d)\n", type,
401 			    hed->res);
402 			RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
403 			return -1;
404 			/* BIND has abort() here, too risky on bad data */
405 		}
406 		if (!had_error)
407 			haveanswer++;
408 	}
409 	if (haveanswer) {
410 		*ap = NULL;
411 		*hap = NULL;
412 # if defined(RESOLVSORT)
413 		/*
414 		 * Note: we sort even if host can take only one address
415 		 * in its return structures - should give it the "best"
416 		 * address in that case, not some random one
417 		 */
418 		if (hed->res->nsort && haveanswer > 1 && qtype == T_A)
419 			addrsort(hed->h_addr_ptrs, haveanswer, hed->res);
420 # endif /*RESOLVSORT*/
421 		if (!he->h_name) {
422 			n = strlen(qname) + 1;	/* for the \0 */
423 			if (n > ep - bp || n >= MAXHOSTNAMELEN)
424 				goto no_recovery;
425 			strcpy(bp, qname);
426 			he->h_name = bp;
427 			bp += n;
428 		}
429 		if (hed->res->options & RES_USE_INET6)
430 			_map_v4v6_hostent(he, &bp, ep);
431 		RES_SET_H_ERRNO(hed->res, NETDB_SUCCESS);
432 		return 0;
433 	}
434  no_recovery:
435 	RES_SET_H_ERRNO(hed->res, NO_RECOVERY);
436 	return -1;
437 }
438 
439 /* XXX: for async DNS resolver in ypserv */
440 struct hostent *
441 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
442 {
443 	struct hostdata *hd;
444 	int error;
445 	res_state statp;
446 
447 	statp = __res_state();
448 	if ((hd = __hostdata_init()) == NULL) {
449 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
450 		return NULL;
451 	}
452 	hd->data.res = statp;
453 	switch (qtype) {
454 	case T_AAAA:
455 		hd->host.h_addrtype = AF_INET6;
456 		hd->host.h_length = IN6ADDRSZ;
457 		break;
458 	case T_A:
459 	default:
460 		hd->host.h_addrtype = AF_INET;
461 		hd->host.h_length = INADDRSZ;
462 		break;
463 	}
464 
465 	error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
466 	    &hd->host, &hd->data);
467 	return (error == 0) ? &hd->host : NULL;
468 }
469 
470 int
471 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
472 {
473 	const char *name;
474 	int af;
475 	struct hostent *he;
476 	struct hostent_data *hed;
477 	querybuf *buf;
478 	int n, size, type, error;
479 
480 	name = va_arg(ap, const char *);
481 	af = va_arg(ap, int);
482 	he = va_arg(ap, struct hostent *);
483 	hed = va_arg(ap, struct hostent_data *);
484 
485 	switch (af) {
486 	case AF_INET:
487 		size = INADDRSZ;
488 		type = T_A;
489 		break;
490 	case AF_INET6:
491 		size = IN6ADDRSZ;
492 		type = T_AAAA;
493 		break;
494 	default:
495 		RES_SET_H_ERRNO(hed->res, NETDB_INTERNAL);
496 		errno = EAFNOSUPPORT;
497 		return NS_UNAVAIL;
498 	}
499 
500 	he->h_addrtype = af;
501 	he->h_length = size;
502 
503 	if ((buf = malloc(sizeof(*buf))) == NULL) {
504 		RES_SET_H_ERRNO(hed->res, NETDB_INTERNAL);
505 		return NS_NOTFOUND;
506 	}
507 	n = res_nsearch(hed->res, name, C_IN, type, buf->buf, sizeof(buf->buf));
508 	if (n < 0) {
509 		free(buf);
510 		dprintf("res_nsearch failed (%d)\n", n, hed->res);
511 		return (0);
512 	} else if (n > sizeof(buf->buf)) {
513 		free(buf);
514 		dprintf("static buffer is too small (%d)\n", n, hed->res);
515 		return (0);
516 	}
517 	error = gethostanswer(buf, n, name, type, he, hed);
518 	free(buf);
519 	return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
520 }
521 
522 int
523 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
524 {
525 	const u_char *uaddr;
526 	int len, af;
527 	struct hostent *he;
528 	struct hostent_data *hed;
529 	int n, error;
530 	querybuf *buf;
531 	char qbuf[MAXDNAME+1], *qp;
532 #ifdef SUNSECURITY
533 	struct hostdata rhd;
534 	struct hostent *rhe;
535 	char **haddr;
536 	u_long old_options;
537 	char hname2[MAXDNAME+1], numaddr[46];
538 #endif /*SUNSECURITY*/
539 
540 	uaddr = va_arg(ap, const u_char *);
541 	len = va_arg(ap, int);
542 	af = va_arg(ap, int);
543 	he = va_arg(ap, struct hostent *);
544 	hed = va_arg(ap, struct hostent_data *);
545 
546 	switch (af) {
547 	case AF_INET:
548 		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
549 			       (uaddr[3] & 0xff),
550 			       (uaddr[2] & 0xff),
551 			       (uaddr[1] & 0xff),
552 			       (uaddr[0] & 0xff));
553 		break;
554 	case AF_INET6:
555 		qp = qbuf;
556 		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
557 			qp += SPRINTF((qp, "%x.%x.",
558 				       uaddr[n] & 0xf,
559 				       (uaddr[n] >> 4) & 0xf));
560 		}
561 		strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
562 		break;
563 	default:
564 		abort();
565 	}
566 	if ((buf = malloc(sizeof(*buf))) == NULL) {
567 		RES_SET_H_ERRNO(hed->res, NETDB_INTERNAL);
568 		return NS_NOTFOUND;
569 	}
570 	n = res_nquery(hed->res, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
571 	    sizeof buf->buf);
572 	if (n < 0) {
573 		free(buf);
574 		dprintf("res_nquery failed (%d)\n", n, hed->res);
575 		return NS_UNAVAIL;
576 	}
577 	if (n > sizeof buf->buf) {
578 		free(buf);
579 		dprintf("static buffer is too small (%d)\n", n, hed->res);
580 		return NS_UNAVAIL;
581 	}
582 	if ((error = gethostanswer(buf, n, qbuf, T_PTR, he, hed)) != 0) {
583 		free(buf);
584 		return NS_NOTFOUND;   /* h_errno was set by gethostanswer() */
585 	}
586 	free(buf);
587 #ifdef SUNSECURITY
588 	if (af == AF_INET) {
589 	    /*
590 	     * turn off search as the name should be absolute,
591 	     * 'localhost' should be matched by defnames
592 	     */
593 	    strncpy(hname2, he->h_name, MAXDNAME);
594 	    hname2[MAXDNAME] = '\0';
595 	    old_options = hed->res->options;
596 	    hed->res->options &= ~RES_DNSRCH;
597 	    hed->res->options |= RES_DEFNAMES;
598 	    memset(&rhd, 0, sizeof rhd);
599 	    if (!(rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data))) {
600 		if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
601 		    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
602 		syslog(LOG_NOTICE|LOG_AUTH,
603 		       "gethostbyaddr: No A record for %s (verifying [%s])",
604 		       hname2, numaddr);
605 		hed->res->options = old_options;
606 		RES_SET_H_ERRNO(hed->res, HOST_NOT_FOUND);
607 		return NS_NOTFOUND;
608 	    }
609 	    hed->res->options = old_options;
610 	    for (haddr = rhe->h_addr_list; *haddr; haddr++)
611 		if (!memcmp(*haddr, addr, INADDRSZ))
612 			break;
613 	    if (!*haddr) {
614 		if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
615 		    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
616 		syslog(LOG_NOTICE|LOG_AUTH,
617 		       "gethostbyaddr: A record of %s != PTR record [%s]",
618 		       hname2, numaddr);
619 		RES_SET_H_ERRNO(hed->res, HOST_NOT_FOUND);
620 		return NS_NOTFOUND;
621 	    }
622 	}
623 #endif /*SUNSECURITY*/
624 	he->h_addrtype = af;
625 	he->h_length = len;
626 	memcpy(hed->host_addr, uaddr, len);
627 	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
628 	hed->h_addr_ptrs[1] = NULL;
629 	if (af == AF_INET && (hed->res->options & RES_USE_INET6)) {
630 		_map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
631 		he->h_addrtype = AF_INET6;
632 		he->h_length = IN6ADDRSZ;
633 	}
634 	RES_SET_H_ERRNO(hed->res, NETDB_SUCCESS);
635 	return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
636 }
637 
638 #ifdef RESOLVSORT
639 static void
640 addrsort(ap, num, res)
641 	char **ap;
642 	int num;
643 	res_state res;
644 {
645 	int i, j;
646 	char **p;
647 	short aval[_MAXADDRS];
648 	int needsort = 0;
649 
650 	p = ap;
651 	for (i = 0; i < num; i++, p++) {
652 	    for (j = 0 ; (unsigned)j < res->nsort; j++)
653 		if (res->sort_list[j].addr.s_addr ==
654 		    (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
655 			break;
656 	    aval[i] = j;
657 	    if (needsort == 0 && i > 0 && j < aval[i-1])
658 		needsort = i;
659 	}
660 	if (!needsort)
661 	    return;
662 
663 	while (needsort < num) {
664 	    for (j = needsort - 1; j >= 0; j--) {
665 		if (aval[j] > aval[j+1]) {
666 		    char *hp;
667 
668 		    i = aval[j];
669 		    aval[j] = aval[j+1];
670 		    aval[j+1] = i;
671 
672 		    hp = ap[j];
673 		    ap[j] = ap[j+1];
674 		    ap[j+1] = hp;
675 
676 		} else
677 		    break;
678 	    }
679 	    needsort++;
680 	}
681 }
682 #endif
683 
684 void
685 _sethostdnsent(stayopen)
686 	int stayopen;
687 {
688 	res_state statp;
689 
690 	statp = __res_state();
691 	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
692 		return;
693 	if (stayopen)
694 		statp->options |= RES_STAYOPEN | RES_USEVC;
695 }
696 
697 void
698 _endhostdnsent()
699 {
700 	res_state statp;
701 
702 	statp = __res_state();
703 	statp->options &= ~(RES_STAYOPEN | RES_USEVC);
704 	res_nclose(statp);
705 }
706