1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 1985, 1988 Regents of the University of California.
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
6*0Sstevel@tonic-gate  * provided that this notice is preserved and that due credit is given
7*0Sstevel@tonic-gate  * to the University of California at Berkeley. The name of the University
8*0Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
9*0Sstevel@tonic-gate  * software without specific prior written permission. This software
10*0Sstevel@tonic-gate  * is provided ``as is'' without express or implied warranty.
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" /* SunOS 1.11; UCB 6.32 */
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #include "synonyms.h"
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate #include <sys/param.h>
19*0Sstevel@tonic-gate #include <sys/socket.h>
20*0Sstevel@tonic-gate #include <netinet/in.h>
21*0Sstevel@tonic-gate #include <ctype.h>
22*0Sstevel@tonic-gate #include <netdb.h>
23*0Sstevel@tonic-gate #include <stdio.h>
24*0Sstevel@tonic-gate #include <errno.h>
25*0Sstevel@tonic-gate #include <arpa/inet.h>
26*0Sstevel@tonic-gate #include <arpa/nameser.h>
27*0Sstevel@tonic-gate #include <resolv.h>
28*0Sstevel@tonic-gate #include <syslog.h>
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * When the name service switch calls libresolv, it doesn't want fallback
32*0Sstevel@tonic-gate  * to /etc/hosts, so we provide a method to turn it off.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate static int	no_hosts_fallback = 0;
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate void
37*0Sstevel@tonic-gate __res_set_no_hosts_fallback(void) {
38*0Sstevel@tonic-gate 	no_hosts_fallback = 1;
39*0Sstevel@tonic-gate }
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static int
42*0Sstevel@tonic-gate __res_no_hosts_fallback(void) {
43*0Sstevel@tonic-gate 	return(no_hosts_fallback);
44*0Sstevel@tonic-gate }
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate static char *h_addr_ptrs[MAXADDRS + 1];
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static struct hostent host;
49*0Sstevel@tonic-gate static char *host_aliases[MAXALIASES];
50*0Sstevel@tonic-gate static char hostbuf[BUFSIZ+1];
51*0Sstevel@tonic-gate static struct in_addr host_addr;
52*0Sstevel@tonic-gate static char HOSTDB[] = "/etc/hosts";
53*0Sstevel@tonic-gate static FILE *hostf = NULL;
54*0Sstevel@tonic-gate static char hostaddr[MAXADDRS];
55*0Sstevel@tonic-gate static char *host_addrs[2];
56*0Sstevel@tonic-gate static int stayopen = 0;
57*0Sstevel@tonic-gate static char *any();
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #if PACKETSZ > 1024
60*0Sstevel@tonic-gate #define	MAXPACKET	PACKETSZ
61*0Sstevel@tonic-gate #else
62*0Sstevel@tonic-gate #define	MAXPACKET	1024
63*0Sstevel@tonic-gate #endif
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate typedef union {
66*0Sstevel@tonic-gate 	HEADER hdr;
67*0Sstevel@tonic-gate 	u_char buf[MAXPACKET];
68*0Sstevel@tonic-gate } querybuf;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static union {
71*0Sstevel@tonic-gate 	long al;
72*0Sstevel@tonic-gate 	char ac;
73*0Sstevel@tonic-gate } align;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate int h_errno;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static struct hostent *
79*0Sstevel@tonic-gate getanswer(answer, anslen, iquery)
80*0Sstevel@tonic-gate 	querybuf *answer;
81*0Sstevel@tonic-gate 	int anslen;
82*0Sstevel@tonic-gate 	int iquery;
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	register HEADER *hp;
85*0Sstevel@tonic-gate 	register u_char *cp;
86*0Sstevel@tonic-gate 	register int n;
87*0Sstevel@tonic-gate 	u_char *eom;
88*0Sstevel@tonic-gate 	char *bp, **ap;
89*0Sstevel@tonic-gate 	int type, class, buflen, ancount, qdcount;
90*0Sstevel@tonic-gate 	int haveanswer, getclass = C_ANY;
91*0Sstevel@tonic-gate 	char **hap;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	eom = answer->buf + anslen;
94*0Sstevel@tonic-gate 	/*
95*0Sstevel@tonic-gate 	 * find first satisfactory answer
96*0Sstevel@tonic-gate 	 */
97*0Sstevel@tonic-gate 	hp = &answer->hdr;
98*0Sstevel@tonic-gate 	ancount = ntohs(hp->ancount);
99*0Sstevel@tonic-gate 	qdcount = ntohs(hp->qdcount);
100*0Sstevel@tonic-gate 	bp = hostbuf;
101*0Sstevel@tonic-gate 	buflen = sizeof (hostbuf);
102*0Sstevel@tonic-gate 	cp = answer->buf + sizeof (HEADER);
103*0Sstevel@tonic-gate 	if (qdcount) {
104*0Sstevel@tonic-gate 		if (iquery) {
105*0Sstevel@tonic-gate 			if ((n = dn_expand((char *)answer->buf, eom,
106*0Sstevel@tonic-gate 						cp, bp, buflen)) < 0) {
107*0Sstevel@tonic-gate 				h_errno = NO_RECOVERY;
108*0Sstevel@tonic-gate 				return ((struct hostent *) NULL);
109*0Sstevel@tonic-gate 			}
110*0Sstevel@tonic-gate 			cp += n + QFIXEDSZ;
111*0Sstevel@tonic-gate 			host.h_name = bp;
112*0Sstevel@tonic-gate 			n = strlen(bp) + 1;
113*0Sstevel@tonic-gate 			bp += n;
114*0Sstevel@tonic-gate 			buflen -= n;
115*0Sstevel@tonic-gate 		} else
116*0Sstevel@tonic-gate 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
117*0Sstevel@tonic-gate 		while (--qdcount > 0)
118*0Sstevel@tonic-gate 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
119*0Sstevel@tonic-gate 	} else if (iquery) {
120*0Sstevel@tonic-gate 		if (hp->aa)
121*0Sstevel@tonic-gate 			h_errno = HOST_NOT_FOUND;
122*0Sstevel@tonic-gate 		else
123*0Sstevel@tonic-gate 			h_errno = TRY_AGAIN;
124*0Sstevel@tonic-gate 		return ((struct hostent *) NULL);
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate 	ap = host_aliases;
127*0Sstevel@tonic-gate 	host.h_aliases = host_aliases;
128*0Sstevel@tonic-gate 	hap = h_addr_ptrs;
129*0Sstevel@tonic-gate #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
130*0Sstevel@tonic-gate 	host.h_addr_list = h_addr_ptrs;
131*0Sstevel@tonic-gate #endif
132*0Sstevel@tonic-gate 	haveanswer = 0;
133*0Sstevel@tonic-gate 	while (--ancount >= 0 && cp < eom && haveanswer < MAXADDRS) {
134*0Sstevel@tonic-gate 		if ((n = dn_expand((char *)answer->buf, eom,
135*0Sstevel@tonic-gate 						cp, bp, buflen)) < 0)
136*0Sstevel@tonic-gate 			break;
137*0Sstevel@tonic-gate 		cp += n;
138*0Sstevel@tonic-gate 		type = _getshort(cp);
139*0Sstevel@tonic-gate 		cp += sizeof (u_short);
140*0Sstevel@tonic-gate 		class = _getshort(cp);
141*0Sstevel@tonic-gate 		cp += sizeof (u_short) + sizeof (u_long);
142*0Sstevel@tonic-gate 		n = _getshort(cp);
143*0Sstevel@tonic-gate 		cp += sizeof (u_short);
144*0Sstevel@tonic-gate 		if (type == T_CNAME) {
145*0Sstevel@tonic-gate 			cp += n;
146*0Sstevel@tonic-gate 			if (ap >= &host_aliases[MAXALIASES-1])
147*0Sstevel@tonic-gate 				continue;
148*0Sstevel@tonic-gate 			*ap++ = bp;
149*0Sstevel@tonic-gate 			n = strlen(bp) + 1;
150*0Sstevel@tonic-gate 			bp += n;
151*0Sstevel@tonic-gate 			buflen -= n;
152*0Sstevel@tonic-gate 			continue;
153*0Sstevel@tonic-gate 		}
154*0Sstevel@tonic-gate 		if (iquery && type == T_PTR) {
155*0Sstevel@tonic-gate 			if ((n = dn_expand((char *)answer->buf, eom,
156*0Sstevel@tonic-gate 					cp, bp, buflen)) < 0) {
157*0Sstevel@tonic-gate 				cp += n;
158*0Sstevel@tonic-gate 				continue;
159*0Sstevel@tonic-gate 			}
160*0Sstevel@tonic-gate 			cp += n;
161*0Sstevel@tonic-gate 			host.h_name = bp;
162*0Sstevel@tonic-gate 			return (&host);
163*0Sstevel@tonic-gate 		}
164*0Sstevel@tonic-gate 		if (iquery || type != T_A) {
165*0Sstevel@tonic-gate #ifdef DEBUG
166*0Sstevel@tonic-gate 			if (_res.options & RES_DEBUG)
167*0Sstevel@tonic-gate 				printf("unexpected answer type %d, size %d\n",
168*0Sstevel@tonic-gate 					type, n);
169*0Sstevel@tonic-gate #endif
170*0Sstevel@tonic-gate 			cp += n;
171*0Sstevel@tonic-gate 			continue;
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 		if (haveanswer) {
174*0Sstevel@tonic-gate 			if (n != host.h_length) {
175*0Sstevel@tonic-gate 				cp += n;
176*0Sstevel@tonic-gate 				continue;
177*0Sstevel@tonic-gate 			}
178*0Sstevel@tonic-gate 			if (class != getclass) {
179*0Sstevel@tonic-gate 				cp += n;
180*0Sstevel@tonic-gate 				continue;
181*0Sstevel@tonic-gate 			}
182*0Sstevel@tonic-gate 		} else {
183*0Sstevel@tonic-gate 			host.h_length = n;
184*0Sstevel@tonic-gate 			getclass = class;
185*0Sstevel@tonic-gate 			host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
186*0Sstevel@tonic-gate 			if (!iquery) {
187*0Sstevel@tonic-gate 				host.h_name = bp;
188*0Sstevel@tonic-gate 				bp += strlen(bp) + 1;
189*0Sstevel@tonic-gate 			}
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		bp += sizeof (align) - ((u_long)bp % sizeof (align));
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 		if (bp + n >= &hostbuf[sizeof (hostbuf)]) {
195*0Sstevel@tonic-gate #ifdef DEBUG
196*0Sstevel@tonic-gate 			if (_res.options & RES_DEBUG)
197*0Sstevel@tonic-gate 				printf("size (%d) too big\n", n);
198*0Sstevel@tonic-gate #endif
199*0Sstevel@tonic-gate 			break;
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate #ifdef SYSV
202*0Sstevel@tonic-gate 		memcpy((void *)(*hap++ = bp), (void *)cp, n);
203*0Sstevel@tonic-gate #else
204*0Sstevel@tonic-gate 		bcopy(cp, *hap++ = bp, n);
205*0Sstevel@tonic-gate #endif
206*0Sstevel@tonic-gate 		bp += n;
207*0Sstevel@tonic-gate 		cp += n;
208*0Sstevel@tonic-gate 		haveanswer++;
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 	if (haveanswer) {
211*0Sstevel@tonic-gate 		*ap = NULL;
212*0Sstevel@tonic-gate #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
213*0Sstevel@tonic-gate 		*hap = NULL;
214*0Sstevel@tonic-gate #else
215*0Sstevel@tonic-gate 		host.h_addr = h_addr_ptrs[0];
216*0Sstevel@tonic-gate #endif
217*0Sstevel@tonic-gate 		return (&host);
218*0Sstevel@tonic-gate 	} else {
219*0Sstevel@tonic-gate 		h_errno = TRY_AGAIN;
220*0Sstevel@tonic-gate 		return ((struct hostent *) NULL);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate struct hostent *
225*0Sstevel@tonic-gate res_gethostbyname(name)
226*0Sstevel@tonic-gate 	char *name;
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	querybuf buf;
229*0Sstevel@tonic-gate 	register char *cp;
230*0Sstevel@tonic-gate 	int n;
231*0Sstevel@tonic-gate 	struct hostent *hp, *gethostdomain();
232*0Sstevel@tonic-gate 	static struct hostent *_gethtbyname();
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	/*
235*0Sstevel@tonic-gate 	 * disallow names consisting only of digits/dots, unless
236*0Sstevel@tonic-gate 	 * they end in a dot.
237*0Sstevel@tonic-gate 	 */
238*0Sstevel@tonic-gate 	if (isdigit(name[0]))
239*0Sstevel@tonic-gate 		for (cp = name; /*EMPTY*/; ++cp) {
240*0Sstevel@tonic-gate 			if (!*cp) {
241*0Sstevel@tonic-gate 				if (*--cp == '.')
242*0Sstevel@tonic-gate 					break;
243*0Sstevel@tonic-gate 				h_errno = HOST_NOT_FOUND;
244*0Sstevel@tonic-gate 				return ((struct hostent *) NULL);
245*0Sstevel@tonic-gate 			}
246*0Sstevel@tonic-gate 			if (!isdigit(*cp) && *cp != '.')
247*0Sstevel@tonic-gate 				break;
248*0Sstevel@tonic-gate 		}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof (buf))) < 0) {
251*0Sstevel@tonic-gate #ifdef DEBUG
252*0Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
253*0Sstevel@tonic-gate 			printf("res_search failed\n");
254*0Sstevel@tonic-gate #endif
255*0Sstevel@tonic-gate 		if (errno == ECONNREFUSED)
256*0Sstevel@tonic-gate 			return (_gethtbyname(name));
257*0Sstevel@tonic-gate 		else
258*0Sstevel@tonic-gate 			return ((struct hostent *) NULL);
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 	return (getanswer(&buf, n, 0));
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate static struct hostent *
264*0Sstevel@tonic-gate _getrhbyaddr(addr, len, type)
265*0Sstevel@tonic-gate 	char *addr;
266*0Sstevel@tonic-gate 	int len, type;
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate 	int n;
269*0Sstevel@tonic-gate 	querybuf buf;
270*0Sstevel@tonic-gate 	register struct hostent *hp;
271*0Sstevel@tonic-gate 	char qbuf[MAXDNAME];
272*0Sstevel@tonic-gate 	static struct hostent *_gethtbyaddr();
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	if (type != AF_INET)
275*0Sstevel@tonic-gate 		return ((struct hostent *) NULL);
276*0Sstevel@tonic-gate 	(void) sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa",
277*0Sstevel@tonic-gate 		((unsigned)addr[3] & 0xff),
278*0Sstevel@tonic-gate 		((unsigned)addr[2] & 0xff),
279*0Sstevel@tonic-gate 		((unsigned)addr[1] & 0xff),
280*0Sstevel@tonic-gate 		((unsigned)addr[0] & 0xff));
281*0Sstevel@tonic-gate 	n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof (buf));
282*0Sstevel@tonic-gate 	if (n < 0) {
283*0Sstevel@tonic-gate #ifdef DEBUG
284*0Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
285*0Sstevel@tonic-gate 			printf("res_query failed\n");
286*0Sstevel@tonic-gate #endif
287*0Sstevel@tonic-gate 		if (errno == ECONNREFUSED)
288*0Sstevel@tonic-gate 			return (_gethtbyaddr(addr, len, type));
289*0Sstevel@tonic-gate 		return ((struct hostent *) NULL);
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 	hp = getanswer(&buf, n, 1);
292*0Sstevel@tonic-gate 	if (hp == NULL)
293*0Sstevel@tonic-gate 		return ((struct hostent *) NULL);
294*0Sstevel@tonic-gate 	hp->h_addrtype = type;
295*0Sstevel@tonic-gate 	hp->h_length = len;
296*0Sstevel@tonic-gate 	h_addr_ptrs[0] = (char *)&host_addr;
297*0Sstevel@tonic-gate 	h_addr_ptrs[1] = (char *)0;
298*0Sstevel@tonic-gate 	host_addr = *(struct in_addr *)addr;
299*0Sstevel@tonic-gate 	return (hp);
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate /*
303*0Sstevel@tonic-gate  * First we get what the PTR record says, but do an extra call
304*0Sstevel@tonic-gate  * to gethostbyname() to make sure that someone is not trying to
305*0Sstevel@tonic-gate  * spoof us.  Hopefully this is not done that often, so good
306*0Sstevel@tonic-gate  * performance is not really an issue.
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate struct hostent *
309*0Sstevel@tonic-gate res_gethostbyaddr(addr, len, type)
310*0Sstevel@tonic-gate 	char	*addr;
311*0Sstevel@tonic-gate 	int	len;
312*0Sstevel@tonic-gate 	int	type;
313*0Sstevel@tonic-gate {
314*0Sstevel@tonic-gate 	char		**a, hbuf[MAXHOSTNAMELEN];
315*0Sstevel@tonic-gate 	struct hostent	*hp, *hp2;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	if ((hp = _getrhbyaddr(addr, len, type)) == (struct hostent *)NULL)
318*0Sstevel@tonic-gate 		return ((struct hostent *)NULL);
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	/* hang on to what we got as an answer */
321*0Sstevel@tonic-gate 	(void) strcpy(hbuf, hp->h_name);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	/* check to make sure by doing a forward query */
324*0Sstevel@tonic-gate 	if ((hp2 = res_gethostbyname(hbuf)) != (struct hostent *)NULL)
325*0Sstevel@tonic-gate 		for (a = hp2->h_addr_list; *a; a++)
326*0Sstevel@tonic-gate #ifdef SYSV
327*0Sstevel@tonic-gate 			if (memcmp(*a, addr, hp2->h_length) == 0)
328*0Sstevel@tonic-gate #else
329*0Sstevel@tonic-gate 			if (bcmp(*a, addr, hp2->h_length) == 0)
330*0Sstevel@tonic-gate #endif
331*0Sstevel@tonic-gate 				return (hp2);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	/*
334*0Sstevel@tonic-gate 	 * we've been spoofed, make sure to log it.
335*0Sstevel@tonic-gate 	 * XXX - syslog needs a security priority level.
336*0Sstevel@tonic-gate 	 */
337*0Sstevel@tonic-gate 	syslog(LOG_NOTICE, "gethostbyaddr: %s != %s", hbuf,
338*0Sstevel@tonic-gate 		inet_ntoa(*(struct in_addr *)addr));
339*0Sstevel@tonic-gate 	return ((struct hostent *)NULL);
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate static
343*0Sstevel@tonic-gate _sethtent(f)
344*0Sstevel@tonic-gate 	int f;
345*0Sstevel@tonic-gate {
346*0Sstevel@tonic-gate 	if (__res_no_hosts_fallback()) return(0);
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	if (hostf == NULL)
349*0Sstevel@tonic-gate 		hostf = fopen(HOSTDB, "r");
350*0Sstevel@tonic-gate 	else
351*0Sstevel@tonic-gate 		rewind(hostf);
352*0Sstevel@tonic-gate 	stayopen |= f;
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate static
356*0Sstevel@tonic-gate _endhtent()
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	if (__res_no_hosts_fallback()) return(0);
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 	if (hostf && !stayopen) {
361*0Sstevel@tonic-gate 		(void) fclose(hostf);
362*0Sstevel@tonic-gate 		hostf = NULL;
363*0Sstevel@tonic-gate 	}
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate static struct hostent *
367*0Sstevel@tonic-gate _gethtent()
368*0Sstevel@tonic-gate {
369*0Sstevel@tonic-gate 	char *p;
370*0Sstevel@tonic-gate 	register char *cp, **q;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if (__res_no_hosts_fallback()) return(NULL);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	if (hostf == NULL && (hostf = fopen(HOSTDB, "r")) == NULL)
375*0Sstevel@tonic-gate 		return (NULL);
376*0Sstevel@tonic-gate again:
377*0Sstevel@tonic-gate 	if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
378*0Sstevel@tonic-gate 		return (NULL);
379*0Sstevel@tonic-gate 	if (*p == '#')
380*0Sstevel@tonic-gate 		goto again;
381*0Sstevel@tonic-gate 	cp = any(p, "#\n");
382*0Sstevel@tonic-gate 	if (cp == NULL)
383*0Sstevel@tonic-gate 		goto again;
384*0Sstevel@tonic-gate 	*cp = '\0';
385*0Sstevel@tonic-gate 	cp = any(p, " \t");
386*0Sstevel@tonic-gate 	if (cp == NULL)
387*0Sstevel@tonic-gate 		goto again;
388*0Sstevel@tonic-gate 	*cp++ = '\0';
389*0Sstevel@tonic-gate 	/* THIS STUFF IS INTERNET SPECIFIC */
390*0Sstevel@tonic-gate #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
391*0Sstevel@tonic-gate 	host.h_addr_list = host_addrs;
392*0Sstevel@tonic-gate #endif
393*0Sstevel@tonic-gate 	host.h_addr = hostaddr;
394*0Sstevel@tonic-gate 	*((u_long *)host.h_addr) = inet_addr(p);
395*0Sstevel@tonic-gate 	host.h_length = sizeof (u_long);
396*0Sstevel@tonic-gate 	host.h_addrtype = AF_INET;
397*0Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
398*0Sstevel@tonic-gate 		cp++;
399*0Sstevel@tonic-gate 	host.h_name = cp;
400*0Sstevel@tonic-gate 	q = host.h_aliases = host_aliases;
401*0Sstevel@tonic-gate 	cp = any(cp, " \t");
402*0Sstevel@tonic-gate 	if (cp != NULL)
403*0Sstevel@tonic-gate 		*cp++ = '\0';
404*0Sstevel@tonic-gate 	while (cp && *cp) {
405*0Sstevel@tonic-gate 		if (*cp == ' ' || *cp == '\t') {
406*0Sstevel@tonic-gate 			cp++;
407*0Sstevel@tonic-gate 			continue;
408*0Sstevel@tonic-gate 		}
409*0Sstevel@tonic-gate 		if (q < &host_aliases[MAXALIASES - 1])
410*0Sstevel@tonic-gate 			*q++ = cp;
411*0Sstevel@tonic-gate 		cp = any(cp, " \t");
412*0Sstevel@tonic-gate 		if (cp != NULL)
413*0Sstevel@tonic-gate 			*cp++ = '\0';
414*0Sstevel@tonic-gate 	}
415*0Sstevel@tonic-gate 	*q = NULL;
416*0Sstevel@tonic-gate 	return (&host);
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate static char *
420*0Sstevel@tonic-gate any(cp, match)
421*0Sstevel@tonic-gate 	register char *cp;
422*0Sstevel@tonic-gate 	char *match;
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate 	register char *mp, c;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	while (c = *cp) {
427*0Sstevel@tonic-gate 		for (mp = match; *mp; mp++)
428*0Sstevel@tonic-gate 			if (*mp == c)
429*0Sstevel@tonic-gate 				return (cp);
430*0Sstevel@tonic-gate 		cp++;
431*0Sstevel@tonic-gate 	}
432*0Sstevel@tonic-gate 	return ((char *)0);
433*0Sstevel@tonic-gate }
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate static struct hostent *
436*0Sstevel@tonic-gate _gethtbyname(name)
437*0Sstevel@tonic-gate 	char *name;
438*0Sstevel@tonic-gate {
439*0Sstevel@tonic-gate 	register struct hostent *p;
440*0Sstevel@tonic-gate 	register char **cp;
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	_sethtent(0);
443*0Sstevel@tonic-gate 	while (p = _gethtent()) {
444*0Sstevel@tonic-gate 		if (strcasecmp(p->h_name, name) == 0)
445*0Sstevel@tonic-gate 			break;
446*0Sstevel@tonic-gate 		for (cp = p->h_aliases; *cp != 0; cp++)
447*0Sstevel@tonic-gate 			if (strcasecmp(*cp, name) == 0)
448*0Sstevel@tonic-gate 				goto found;
449*0Sstevel@tonic-gate 	}
450*0Sstevel@tonic-gate found:
451*0Sstevel@tonic-gate 	_endhtent();
452*0Sstevel@tonic-gate 	return (p);
453*0Sstevel@tonic-gate }
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate static struct hostent *
456*0Sstevel@tonic-gate _gethtbyaddr(addr, len, type)
457*0Sstevel@tonic-gate 	char *addr;
458*0Sstevel@tonic-gate 	int len, type;
459*0Sstevel@tonic-gate {
460*0Sstevel@tonic-gate 	register struct hostent *p;
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	_sethtent(0);
463*0Sstevel@tonic-gate 	while (p = _gethtent())
464*0Sstevel@tonic-gate #ifdef SYSV
465*0Sstevel@tonic-gate 		if (p->h_addrtype == type && !memcmp(p->h_addr, addr, len))
466*0Sstevel@tonic-gate #else
467*0Sstevel@tonic-gate 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
468*0Sstevel@tonic-gate #endif
469*0Sstevel@tonic-gate 			break;
470*0Sstevel@tonic-gate 	_endhtent();
471*0Sstevel@tonic-gate 	return (p);
472*0Sstevel@tonic-gate }
473