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