gethostbydns.c revision 103350
1228753Smm/*
2228753Smm * ++Copyright++ 1985, 1988, 1993
3228753Smm * -
4228753Smm * Copyright (c) 1985, 1988, 1993
5228753Smm *	The Regents of the University of California.  All rights reserved.
6228753Smm *
7228753Smm * Redistribution and use in source and binary forms, with or without
8228753Smm * modification, are permitted provided that the following conditions
9228753Smm * are met:
10228753Smm * 1. Redistributions of source code must retain the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer.
12228753Smm * 2. Redistributions in binary form must reproduce the above copyright
13228753Smm *    notice, this list of conditions and the following disclaimer in the
14228753Smm *    documentation and/or other materials provided with the distribution.
15228753Smm * 3. All advertising materials mentioning features or use of this software
16228753Smm *    must display the following acknowledgement:
17228753Smm *	This product includes software developed by the University of
18228753Smm *	California, Berkeley and its contributors.
19228753Smm * 4. Neither the name of the University nor the names of its contributors
20228753Smm *    may be used to endorse or promote products derived from this software
21228753Smm *    without specific prior written permission.
22228753Smm *
23228753Smm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24228753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25228753Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26228753Smm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27228753Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28228753Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29228753Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30228753Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31228753Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32228753Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33228753Smm * SUCH DAMAGE.
34228753Smm * -
35228753Smm * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36228753Smm *
37228753Smm * Permission to use, copy, modify, and distribute this software for any
38229592Smm * purpose with or without fee is hereby granted, provided that the above
39228753Smm * copyright notice and this permission notice appear in all copies, and that
40228753Smm * the name of Digital Equipment Corporation not be used in advertising or
41228753Smm * publicity pertaining to distribution of the document or software without
42228753Smm * specific, written prior permission.
43228753Smm *
44228753Smm * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45228753Smm * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46228753Smm * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
47228753Smm * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48228753Smm * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49228753Smm * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50228753Smm * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51228753Smm * SOFTWARE.
52228753Smm * -
53228753Smm * --Copyright--
54228753Smm */
55228753Smm
56228753Smm#if defined(LIBC_SCCS) && !defined(lint)
57228753Smmstatic char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
58228753Smmstatic char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
59228753Smm#endif /* LIBC_SCCS and not lint */
60228753Smm#include <sys/cdefs.h>
61228753Smm__FBSDID("$FreeBSD: head/lib/libc/net/gethostbydns.c 103350 2002-09-15 16:51:09Z nectar $");
62228753Smm
63229592Smm#include <sys/types.h>
64228753Smm#include <sys/param.h>
65228753Smm#include <sys/socket.h>
66228753Smm#include <netinet/in.h>
67228753Smm#include <arpa/inet.h>
68228753Smm#include <arpa/nameser.h>
69228753Smm
70228753Smm#include <stdio.h>
71228753Smm#include <unistd.h>
72228753Smm#include <string.h>
73228753Smm#include <netdb.h>
74228753Smm#include <resolv.h>
75228753Smm#include <ctype.h>
76228753Smm#include <errno.h>
77228753Smm#include <syslog.h>
78228753Smm#include <stdarg.h>
79228753Smm#include <nsswitch.h>
80228753Smm
81228753Smm#include "res_config.h"
82228753Smm
83228753Smm#define SPRINTF(x) ((size_t)sprintf x)
84228753Smm
85228753Smm#define	MAXALIASES	35
86228753Smm#define	MAXADDRS	35
87228753Smm
88228753Smmstatic const char AskedForGot[] =
89228753Smm		"gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
90228753Smm
91228753Smmstatic char *h_addr_ptrs[MAXADDRS + 1];
92228753Smm
93228753Smmstatic struct hostent host;
94228753Smmstatic char *host_aliases[MAXALIASES];
95228753Smmstatic char hostbuf[8*1024];
96228753Smmstatic u_char host_addr[16];	/* IPv4 or IPv6 */
97228753Smm
98228753Smm#ifdef RESOLVSORT
99228753Smmstatic void addrsort(char **, int);
100228753Smm#endif
101228753Smm
102228753Smm#ifdef DEBUG
103228753Smmstatic void dprintf(char *, int) __printflike(1, 0);
104228753Smm#endif
105228753Smm
106228753Smm#if PACKETSZ > 1024
107228753Smm#define	MAXPACKET	PACKETSZ
108228753Smm#else
109228753Smm#define	MAXPACKET	1024
110228753Smm#endif
111228753Smm
112228753Smmtypedef union {
113228753Smm    HEADER hdr;
114228753Smm    u_char buf[MAXPACKET];
115228753Smm} querybuf;
116228753Smm
117228753Smmtypedef union {
118228753Smm    int32_t al;
119228753Smm    char ac;
120228753Smm} align;
121228753Smm
122228753Smmextern int h_errno;
123228753Smmint _dns_ttl_;
124228753Smm
125228753Smm#ifdef DEBUG
126228753Smmstatic void
127228753Smmdprintf(msg, num)
128228753Smm	char *msg;
129228753Smm	int num;
130228753Smm{
131228753Smm	if (_res.options & RES_DEBUG) {
132228753Smm		int save = errno;
133228753Smm
134228753Smm		printf(msg, num);
135228753Smm		errno = save;
136228753Smm	}
137228753Smm}
138228753Smm#else
139228753Smm# define dprintf(msg, num) /*nada*/
140228753Smm#endif
141228753Smm
142228753Smm#define BOUNDED_INCR(x) \
143228753Smm	do { \
144228753Smm		cp += x; \
145228753Smm		if (cp > eom) { \
146228753Smm			h_errno = NO_RECOVERY; \
147228753Smm			return (NULL); \
148228753Smm		} \
149228753Smm	} while (0)
150228753Smm
151228753Smm#define BOUNDS_CHECK(ptr, count) \
152228753Smm	do { \
153228753Smm		if ((ptr) + (count) > eom) { \
154228753Smm			h_errno = NO_RECOVERY; \
155228753Smm			return (NULL); \
156228753Smm		} \
157228753Smm	} while (0)
158228753Smm
159228753Smmstatic struct hostent *
160228753Smmgethostanswer(answer, anslen, qname, qtype)
161228753Smm	const querybuf *answer;
162228753Smm	int anslen;
163228753Smm	const char *qname;
164228753Smm	int qtype;
165228753Smm{
166228753Smm	const HEADER *hp;
167228753Smm	const u_char *cp;
168228753Smm	int n;
169228753Smm	const u_char *eom, *erdata;
170228753Smm	char *bp, *ep, **ap, **hap;
171228753Smm	int type, class, ancount, qdcount;
172228753Smm	int haveanswer, had_error;
173228753Smm	int toobig = 0;
174228753Smm	char tbuf[MAXDNAME];
175228753Smm	const char *tname;
176228753Smm	int (*name_ok)(const char *);
177228753Smm
178228753Smm	tname = qname;
179228753Smm	host.h_name = NULL;
180228753Smm	eom = answer->buf + anslen;
181228753Smm	switch (qtype) {
182228753Smm	case T_A:
183228753Smm	case T_AAAA:
184228753Smm		name_ok = res_hnok;
185228753Smm		break;
186228753Smm	case T_PTR:
187228753Smm		name_ok = res_dnok;
188228753Smm		break;
189228753Smm	default:
190228753Smm		h_errno = NO_RECOVERY;
191228753Smm		return (NULL);	/* XXX should be abort(); */
192228753Smm	}
193228753Smm	/*
194228753Smm	 * find first satisfactory answer
195228753Smm	 */
196228753Smm	hp = &answer->hdr;
197228753Smm	ancount = ntohs(hp->ancount);
198228753Smm	qdcount = ntohs(hp->qdcount);
199228753Smm	bp = hostbuf;
200228753Smm	ep = hostbuf + sizeof hostbuf;
201228753Smm	cp = answer->buf;
202228753Smm	BOUNDED_INCR(HFIXEDSZ);
203228753Smm	if (qdcount != 1) {
204228753Smm		h_errno = NO_RECOVERY;
205228753Smm		return (NULL);
206228753Smm	}
207228753Smm	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
208228753Smm	if ((n < 0) || !(*name_ok)(bp)) {
209228753Smm		h_errno = NO_RECOVERY;
210228753Smm		return (NULL);
211228753Smm	}
212228753Smm	BOUNDED_INCR(n + QFIXEDSZ);
213228753Smm	if (qtype == T_A || qtype == T_AAAA) {
214228753Smm		/* res_send() has already verified that the query name is the
215228753Smm		 * same as the one we sent; this just gets the expanded name
216228753Smm		 * (i.e., with the succeeding search-domain tacked on).
217228753Smm		 */
218228753Smm		n = strlen(bp) + 1;		/* for the \0 */
219228753Smm		if (n >= MAXHOSTNAMELEN) {
220228753Smm			h_errno = NO_RECOVERY;
221228753Smm			return (NULL);
222228753Smm		}
223228753Smm		host.h_name = bp;
224228753Smm		bp += n;
225228753Smm		/* The qname can be abbreviated, but h_name is now absolute. */
226228753Smm		qname = host.h_name;
227228753Smm	}
228228753Smm	ap = host_aliases;
229228753Smm	*ap = NULL;
230228753Smm	host.h_aliases = host_aliases;
231228753Smm	hap = h_addr_ptrs;
232228753Smm	*hap = NULL;
233228753Smm	host.h_addr_list = h_addr_ptrs;
234228753Smm	haveanswer = 0;
235228753Smm	had_error = 0;
236228753Smm	_dns_ttl_ = -1;
237228753Smm	while (ancount-- > 0 && cp < eom && !had_error) {
238228753Smm		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
239228753Smm		if ((n < 0) || !(*name_ok)(bp)) {
240228753Smm			had_error++;
241228753Smm			continue;
242228753Smm		}
243228753Smm		cp += n;			/* name */
244228753Smm		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
245228753Smm		type = _getshort(cp);
246228753Smm 		cp += INT16SZ;			/* type */
247228753Smm		class = _getshort(cp);
248228753Smm 		cp += INT16SZ;			/* class */
249228753Smm		if (qtype == T_A  && type == T_A)
250228753Smm			_dns_ttl_ = _getlong(cp);
251228753Smm		cp += INT32SZ;			/* TTL */
252228753Smm		n = _getshort(cp);
253228753Smm		cp += INT16SZ;			/* len */
254228753Smm		BOUNDS_CHECK(cp, n);
255228753Smm		erdata = cp + n;
256		if (class != C_IN) {
257			/* XXX - debug? syslog? */
258			cp += n;
259			continue;		/* XXX - had_error++ ? */
260		}
261		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
262			if (ap >= &host_aliases[MAXALIASES-1])
263				continue;
264			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
265			if ((n < 0) || !(*name_ok)(tbuf)) {
266				had_error++;
267				continue;
268			}
269			cp += n;
270			if (cp != erdata) {
271				h_errno = NO_RECOVERY;
272				return (NULL);
273			}
274			/* Store alias. */
275			*ap++ = bp;
276			n = strlen(bp) + 1;	/* for the \0 */
277			if (n >= MAXHOSTNAMELEN) {
278				had_error++;
279				continue;
280			}
281			bp += n;
282			/* Get canonical name. */
283			n = strlen(tbuf) + 1;	/* for the \0 */
284			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
285				had_error++;
286				continue;
287			}
288			strcpy(bp, tbuf);
289			host.h_name = bp;
290			bp += n;
291			continue;
292		}
293		if (qtype == T_PTR && type == T_CNAME) {
294			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
295			if (n < 0 || !res_dnok(tbuf)) {
296				had_error++;
297				continue;
298			}
299			cp += n;
300			if (cp != erdata) {
301				h_errno = NO_RECOVERY;
302				return (NULL);
303			}
304			/* Get canonical name. */
305			n = strlen(tbuf) + 1;	/* for the \0 */
306			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
307				had_error++;
308				continue;
309			}
310			strcpy(bp, tbuf);
311			tname = bp;
312			bp += n;
313			continue;
314		}
315		if (type != qtype) {
316			if (type != T_SIG)
317				syslog(LOG_NOTICE|LOG_AUTH,
318	"gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
319				       qname, p_class(C_IN), p_type(qtype),
320				       p_type(type));
321			cp += n;
322			continue;		/* XXX - had_error++ ? */
323		}
324		switch (type) {
325		case T_PTR:
326			if (strcasecmp(tname, bp) != 0) {
327				syslog(LOG_NOTICE|LOG_AUTH,
328				       AskedForGot, qname, bp);
329				cp += n;
330				continue;	/* XXX - had_error++ ? */
331			}
332			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
333			if ((n < 0) || !res_hnok(bp)) {
334				had_error++;
335				break;
336			}
337#if MULTI_PTRS_ARE_ALIASES
338			cp += n;
339			if (cp != erdata) {
340				h_errno = NO_RECOVERY;
341				return (NULL);
342			}
343			if (!haveanswer)
344				host.h_name = bp;
345			else if (ap < &host_aliases[MAXALIASES-1])
346				*ap++ = bp;
347			else
348				n = -1;
349			if (n != -1) {
350				n = strlen(bp) + 1;	/* for the \0 */
351				if (n >= MAXHOSTNAMELEN) {
352					had_error++;
353					break;
354				}
355				bp += n;
356			}
357			break;
358#else
359			host.h_name = bp;
360			if (_res.options & RES_USE_INET6) {
361				n = strlen(bp) + 1;	/* for the \0 */
362				if (n >= MAXHOSTNAMELEN) {
363					had_error++;
364					break;
365				}
366				bp += n;
367				_map_v4v6_hostent(&host, &bp, &ep);
368			}
369			h_errno = NETDB_SUCCESS;
370			return (&host);
371#endif
372		case T_A:
373		case T_AAAA:
374			if (strcasecmp(host.h_name, bp) != 0) {
375				syslog(LOG_NOTICE|LOG_AUTH,
376				       AskedForGot, host.h_name, bp);
377				cp += n;
378				continue;	/* XXX - had_error++ ? */
379			}
380			if (n != host.h_length) {
381				cp += n;
382				continue;
383			}
384			if (!haveanswer) {
385				int nn;
386
387				host.h_name = bp;
388				nn = strlen(bp) + 1;	/* for the \0 */
389				bp += nn;
390			}
391
392			bp += sizeof(align) - ((u_long)bp % sizeof(align));
393
394			if (bp + n >= ep) {
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			cp += n;
409			if (cp != erdata) {
410				h_errno = NO_RECOVERY;
411				return (NULL);
412			}
413			break;
414		default:
415			dprintf("Impossible condition (type=%d)\n", type);
416			h_errno = NO_RECOVERY;
417			return (NULL);
418			/* BIND has abort() here, too risky on bad data */
419		}
420		if (!had_error)
421			haveanswer++;
422	}
423	if (haveanswer) {
424		*ap = NULL;
425		*hap = NULL;
426# if defined(RESOLVSORT)
427		/*
428		 * Note: we sort even if host can take only one address
429		 * in its return structures - should give it the "best"
430		 * address in that case, not some random one
431		 */
432		if (_res.nsort && haveanswer > 1 && qtype == T_A)
433			addrsort(h_addr_ptrs, haveanswer);
434# endif /*RESOLVSORT*/
435		if (!host.h_name) {
436			n = strlen(qname) + 1;	/* for the \0 */
437			if (n > ep - bp || n >= MAXHOSTNAMELEN)
438				goto no_recovery;
439			strcpy(bp, qname);
440			host.h_name = bp;
441			bp += n;
442		}
443		if (_res.options & RES_USE_INET6)
444			_map_v4v6_hostent(&host, &bp, &ep);
445		h_errno = NETDB_SUCCESS;
446		return (&host);
447	}
448 no_recovery:
449	h_errno = NO_RECOVERY;
450	return (NULL);
451}
452
453struct hostent *
454__dns_getanswer(answer, anslen, qname, qtype)
455	const char *answer;
456	int anslen;
457	const char *qname;
458	int qtype;
459{
460	switch(qtype) {
461	case T_AAAA:
462		host.h_addrtype = AF_INET6;
463		host.h_length = IN6ADDRSZ;
464		break;
465	case T_A:
466	default:
467		host.h_addrtype = AF_INET;
468		host.h_length = INADDRSZ;
469		break;
470	}
471
472	return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
473}
474
475int
476_dns_gethostbyname(void *rval, void *cb_data, va_list ap)
477{
478	const char *name;
479	int af;
480	querybuf buf;
481	const char *cp;
482	char *bp, *ep;
483	int n, size, type, len;
484
485	name = va_arg(ap, const char *);
486	af = va_arg(ap, int);
487	*(struct hostent **)rval = NULL;
488
489	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
490		h_errno = NETDB_INTERNAL;
491		return NS_UNAVAIL;
492	}
493
494	switch (af) {
495	case AF_INET:
496		size = INADDRSZ;
497		type = T_A;
498		break;
499	case AF_INET6:
500		size = IN6ADDRSZ;
501		type = T_AAAA;
502		break;
503	default:
504		h_errno = NETDB_INTERNAL;
505		errno = EAFNOSUPPORT;
506		return NS_UNAVAIL;
507	}
508
509	host.h_addrtype = af;
510	host.h_length = size;
511
512	/*
513	 * if there aren't any dots, it could be a user-level alias.
514	 * this is also done in res_query() since we are not the only
515	 * function that looks up host names.
516	 */
517	if (!strchr(name, '.') && (cp = __hostalias(name)))
518		name = cp;
519
520	/*
521	 * disallow names consisting only of digits/dots, unless
522	 * they end in a dot.
523	 */
524	if (isdigit((unsigned char)name[0]))
525		for (cp = name;; ++cp) {
526			if (!*cp) {
527				if (*--cp == '.')
528					break;
529				/*
530				 * All-numeric, no dot at the end.
531				 * Fake up a hostent as if we'd actually
532				 * done a lookup.
533				 */
534				if (inet_pton(af, name, host_addr) <= 0) {
535					h_errno = HOST_NOT_FOUND;
536					return NS_NOTFOUND;
537				}
538				strncpy(hostbuf, name, MAXDNAME);
539				hostbuf[MAXDNAME] = '\0';
540				bp = hostbuf + MAXDNAME;
541				ep = hostbuf + sizeof hostbuf;
542				host.h_name = hostbuf;
543				host.h_aliases = host_aliases;
544				host_aliases[0] = NULL;
545				h_addr_ptrs[0] = (char *)host_addr;
546				h_addr_ptrs[1] = NULL;
547				host.h_addr_list = h_addr_ptrs;
548				if (_res.options & RES_USE_INET6)
549					_map_v4v6_hostent(&host, &bp, &ep);
550				h_errno = NETDB_SUCCESS;
551				*(struct hostent **)rval = &host;
552				return NS_SUCCESS;
553			}
554			if (!isdigit((unsigned char)*cp) && *cp != '.')
555				break;
556		}
557	if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
558	    name[0] == ':')
559		for (cp = name;; ++cp) {
560			if (!*cp) {
561				if (*--cp == '.')
562					break;
563				/*
564				 * All-IPv6-legal, no dot at the end.
565				 * Fake up a hostent as if we'd actually
566				 * done a lookup.
567				 */
568				if (inet_pton(af, name, host_addr) <= 0) {
569					h_errno = HOST_NOT_FOUND;
570					return NS_NOTFOUND;
571				}
572				strncpy(hostbuf, name, MAXDNAME);
573				hostbuf[MAXDNAME] = '\0';
574				bp = hostbuf + MAXDNAME;
575				len = sizeof hostbuf - MAXDNAME;
576				host.h_name = hostbuf;
577				host.h_aliases = host_aliases;
578				host_aliases[0] = NULL;
579				h_addr_ptrs[0] = (char *)host_addr;
580				h_addr_ptrs[1] = NULL;
581				host.h_addr_list = h_addr_ptrs;
582				h_errno = NETDB_SUCCESS;
583				*(struct hostent **)rval = &host;
584				return NS_SUCCESS;
585			}
586			if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
587				break;
588		}
589
590	n = res_search(name, C_IN, type, buf.buf, sizeof(buf.buf));
591	if (n < 0) {
592		dprintf("res_search failed (%d)\n", n);
593		return (NULL);
594	} else if (n > sizeof(buf.buf)) {
595		dprintf("static buffer is too small (%d)\n", n);
596		return (NULL);
597	}
598	*(struct hostent **)rval = gethostanswer(&buf, n, name, type);
599	return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
600}
601
602int
603_dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
604{
605	const char *addr;	/* XXX should have been def'd as u_char! */
606	int len, af;
607	const u_char *uaddr;
608	static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
609	static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
610	int n, size;
611	querybuf buf;
612	struct hostent *hp;
613	char qbuf[MAXDNAME+1], *qp;
614#ifdef SUNSECURITY
615	struct hostent *rhp;
616	char **haddr;
617	u_long old_options;
618	char hname2[MAXDNAME+1];
619#endif /*SUNSECURITY*/
620
621	addr = va_arg(ap, const char *);
622	uaddr = (const u_char *)addr;
623	len = va_arg(ap, int);
624	af = va_arg(ap, int);
625
626	*(struct hostent **)rval = NULL;
627
628	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
629		h_errno = NETDB_INTERNAL;
630		return NS_UNAVAIL;
631	}
632	if (af == AF_INET6 && len == IN6ADDRSZ &&
633	    (!bcmp(uaddr, mapped, sizeof mapped) ||
634	     !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
635		/* Unmap. */
636		addr += sizeof mapped;
637		uaddr += sizeof mapped;
638		af = AF_INET;
639		len = INADDRSZ;
640	}
641	switch (af) {
642	case AF_INET:
643		size = INADDRSZ;
644		break;
645	case AF_INET6:
646		size = IN6ADDRSZ;
647		break;
648	default:
649		errno = EAFNOSUPPORT;
650		h_errno = NETDB_INTERNAL;
651		return NS_UNAVAIL;
652	}
653	if (size != len) {
654		errno = EINVAL;
655		h_errno = NETDB_INTERNAL;
656		return NS_UNAVAIL;
657	}
658	switch (af) {
659	case AF_INET:
660		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
661			       (uaddr[3] & 0xff),
662			       (uaddr[2] & 0xff),
663			       (uaddr[1] & 0xff),
664			       (uaddr[0] & 0xff));
665		break;
666	case AF_INET6:
667		qp = qbuf;
668		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
669			qp += SPRINTF((qp, "%x.%x.",
670				       uaddr[n] & 0xf,
671				       (uaddr[n] >> 4) & 0xf));
672		}
673		strcpy(qp, "ip6.int");
674		break;
675	default:
676		abort();
677	}
678	n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
679	if (n < 0) {
680		dprintf("res_query failed (%d)\n", n);
681		return NS_UNAVAIL;
682	}
683	if (n > sizeof buf.buf) {
684		dprintf("static buffer is too small (%d)\n", n);
685		return NS_UNAVAIL;
686	}
687	if (!(hp = gethostanswer(&buf, n, qbuf, T_PTR)))
688		return NS_NOTFOUND;   /* h_errno was set by gethostanswer() */
689#ifdef SUNSECURITY
690	if (af == AF_INET) {
691	    /*
692	     * turn off search as the name should be absolute,
693	     * 'localhost' should be matched by defnames
694	     */
695	    strncpy(hname2, hp->h_name, MAXDNAME);
696	    hname2[MAXDNAME] = '\0';
697	    old_options = _res.options;
698	    _res.options &= ~RES_DNSRCH;
699	    _res.options |= RES_DEFNAMES;
700	    if (!(rhp = gethostbyname(hname2))) {
701		syslog(LOG_NOTICE|LOG_AUTH,
702		       "gethostbyaddr: No A record for %s (verifying [%s])",
703		       hname2, inet_ntoa(*((struct in_addr *)addr)));
704		_res.options = old_options;
705		h_errno = HOST_NOT_FOUND;
706		return NS_NOTFOUND;
707	    }
708	    _res.options = old_options;
709	    for (haddr = rhp->h_addr_list; *haddr; haddr++)
710		if (!memcmp(*haddr, addr, INADDRSZ))
711			break;
712	    if (!*haddr) {
713		syslog(LOG_NOTICE|LOG_AUTH,
714		       "gethostbyaddr: A record of %s != PTR record [%s]",
715		       hname2, inet_ntoa(*((struct in_addr *)addr)));
716		h_errno = HOST_NOT_FOUND;
717		return NS_NOTFOUND;
718	    }
719	}
720#endif /*SUNSECURITY*/
721	hp->h_addrtype = af;
722	hp->h_length = len;
723	bcopy(addr, host_addr, len);
724	h_addr_ptrs[0] = (char *)host_addr;
725	h_addr_ptrs[1] = NULL;
726	if (af == AF_INET && (_res.options & RES_USE_INET6)) {
727		_map_v4v6_address((char*)host_addr, (char*)host_addr);
728		hp->h_addrtype = AF_INET6;
729		hp->h_length = IN6ADDRSZ;
730	}
731	h_errno = NETDB_SUCCESS;
732	*(struct hostent **)rval = hp;
733	return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
734}
735
736#ifdef RESOLVSORT
737static void
738addrsort(ap, num)
739	char **ap;
740	int num;
741{
742	int i, j;
743	char **p;
744	short aval[MAXADDRS];
745	int needsort = 0;
746
747	p = ap;
748	for (i = 0; i < num; i++, p++) {
749	    for (j = 0 ; (unsigned)j < _res.nsort; j++)
750		if (_res.sort_list[j].addr.s_addr ==
751		    (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
752			break;
753	    aval[i] = j;
754	    if (needsort == 0 && i > 0 && j < aval[i-1])
755		needsort = i;
756	}
757	if (!needsort)
758	    return;
759
760	while (needsort < num) {
761	    for (j = needsort - 1; j >= 0; j--) {
762		if (aval[j] > aval[j+1]) {
763		    char *hp;
764
765		    i = aval[j];
766		    aval[j] = aval[j+1];
767		    aval[j+1] = i;
768
769		    hp = ap[j];
770		    ap[j] = ap[j+1];
771		    ap[j+1] = hp;
772
773		} else
774		    break;
775	    }
776	    needsort++;
777	}
778}
779#endif
780void
781_sethostdnsent(stayopen)
782	int stayopen;
783{
784	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
785		return;
786	if (stayopen)
787		_res.options |= RES_STAYOPEN | RES_USEVC;
788}
789
790void
791_endhostdnsent()
792{
793	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
794	res_close();
795}
796