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