xref: /csrg-svn/lib/libc/net/gethostnamadr.c (revision 30468)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)gethostnamadr.c	6.17 (Berkeley) 02/12/87";
9 #endif LIBC_SCCS and not lint
10 
11 #include <sys/param.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <ctype.h>
15 #include <netdb.h>
16 #include <stdio.h>
17 #include <errno.h>
18 #include <arpa/inet.h>
19 #include <arpa/nameser.h>
20 #include <resolv.h>
21 
22 #define	MAXALIASES	35
23 #define MAXADDRS	35
24 
25 static char *h_addr_ptrs[MAXADDRS + 1];
26 
27 static struct hostent host;
28 static char *host_aliases[MAXALIASES];
29 static char hostbuf[BUFSIZ+1];
30 static struct in_addr host_addr;
31 static char HOSTDB[] = "/etc/hosts";
32 static FILE *hostf = NULL;
33 static char line[BUFSIZ+1];
34 static char hostaddr[MAXADDRS];
35 static char *host_addrs[2];
36 static int stayopen = 0;
37 static char *any();
38 
39 typedef union {
40     HEADER qb1;
41     char qb2[PACKETSZ];
42 } querybuf;
43 
44 static union {
45     long al;
46     char ac;
47 } align;
48 
49 
50 int h_errno;
51 extern errno;
52 
53 static struct hostent *
54 getanswer(msg, msglen, iquery)
55 	char *msg;
56 	int msglen, iquery;
57 {
58 	register HEADER *hp;
59 	register char *cp;
60 	register int n;
61 	querybuf answer;
62 	char *eom, *bp, **ap;
63 	int type, class, buflen, ancount, qdcount;
64 	int haveanswer, getclass = C_ANY;
65 	char **hap;
66 
67 	n = res_send(msg, msglen, (char *)&answer, sizeof(answer));
68 	if (n < 0) {
69 #ifdef DEBUG
70 		int terrno;
71 		terrno = errno;
72 		if (_res.options & RES_DEBUG)
73 			printf("res_send failed\n");
74 		errno = terrno;
75 #endif
76 		h_errno = TRY_AGAIN;
77 		return (NULL);
78 	}
79 	eom = (char *)&answer + n;
80 	/*
81 	 * find first satisfactory answer
82 	 */
83 	hp = (HEADER *) &answer;
84 	ancount = ntohs(hp->ancount);
85 	qdcount = ntohs(hp->qdcount);
86 	if (hp->rcode != NOERROR || ancount == 0) {
87 #ifdef DEBUG
88 		if (_res.options & RES_DEBUG)
89 			printf("rcode = %d, ancount=%d\n", hp->rcode, ancount);
90 #endif
91 		switch (hp->rcode) {
92 			case NXDOMAIN:
93 				/* Check if it's an authoritive answer */
94 				if (hp->aa)
95 					h_errno = HOST_NOT_FOUND;
96 				else
97 					h_errno = TRY_AGAIN;
98 				break;
99 			case SERVFAIL:
100 				h_errno = TRY_AGAIN;
101 				break;
102 			case NOERROR:
103 				h_errno = NO_ADDRESS;
104 				break;
105 			case FORMERR:
106 			case NOTIMP:
107 			case REFUSED:
108 				h_errno = NO_RECOVERY;
109 		}
110 		return (NULL);
111 	}
112 	bp = hostbuf;
113 	buflen = sizeof(hostbuf);
114 	cp = (char *)&answer + sizeof(HEADER);
115 	if (qdcount) {
116 		if (iquery) {
117 			if ((n = dn_expand((char *)&answer, eom,
118 			     cp, bp, buflen)) < 0) {
119 				h_errno = NO_RECOVERY;
120 				return (NULL);
121 			}
122 			cp += n + QFIXEDSZ;
123 			host.h_name = bp;
124 			n = strlen(bp) + 1;
125 			bp += n;
126 			buflen -= n;
127 		} else
128 			cp += dn_skip(cp) + QFIXEDSZ;
129 		while (--qdcount > 0)
130 			cp += dn_skip(cp) + QFIXEDSZ;
131 	} else if (iquery) {
132 		if (hp->aa)
133 			h_errno = HOST_NOT_FOUND;
134 		else
135 			h_errno = TRY_AGAIN;
136 		return (NULL);
137 	}
138 	ap = host_aliases;
139 	host.h_aliases = host_aliases;
140 	hap = h_addr_ptrs;
141 #if BSD >= 43
142 	host.h_addr_list = h_addr_ptrs;
143 #else
144 	host.h_addr = h_addr_ptrs[0];
145 #endif
146 	haveanswer = 0;
147 	while (--ancount >= 0 && cp < eom) {
148 		if ((n = dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0)
149 			break;
150 		cp += n;
151 		type = _getshort(cp);
152  		cp += sizeof(u_short);
153 		class = _getshort(cp);
154  		cp += sizeof(u_short) + sizeof(u_long);
155 		n = _getshort(cp);
156 		cp += sizeof(u_short);
157 		if (type == T_CNAME) {
158 			cp += n;
159 			if (ap >= &host_aliases[MAXALIASES-1])
160 				continue;
161 			*ap++ = bp;
162 			n = strlen(bp) + 1;
163 			bp += n;
164 			buflen -= n;
165 			continue;
166 		}
167 		if (type == T_PTR) {
168 			if ((n = dn_expand((char *)&answer, eom,
169 			    cp, bp, buflen)) < 0) {
170 				cp += n;
171 				continue;
172 			}
173 			cp += n;
174 			host.h_name = bp;
175 			return(&host);
176 		}
177 		if (type != T_A)  {
178 #ifdef DEBUG
179 			if (_res.options & RES_DEBUG)
180 				printf("unexpected answer type %d, size %d\n",
181 					type, n);
182 #endif
183 			cp += n;
184 			continue;
185 		}
186 		if (haveanswer) {
187 			if (n != host.h_length) {
188 				cp += n;
189 				continue;
190 			}
191 			if (class != getclass) {
192 				cp += n;
193 				continue;
194 			}
195 		} else {
196 			host.h_length = n;
197 			getclass = class;
198 			host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
199 			if (!iquery) {
200 				host.h_name = bp;
201 				bp += strlen(bp) + 1;
202 			}
203 		}
204 
205 		bp += (sizeof(align) - ((u_long)bp % sizeof(align))) &~
206 		    sizeof(align);
207 
208 		if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
209 #ifdef DEBUG
210 			if (_res.options & RES_DEBUG)
211 				printf("size (%d) too big\n", n);
212 #endif
213 			break;
214 		}
215 		bcopy(cp, *hap++ = bp, n);
216 		bp +=n;
217 		cp += n;
218 		haveanswer++;
219 	}
220 	if (haveanswer) {
221 		*ap = NULL;
222 		*hap = NULL;
223 		return (&host);
224 	} else {
225 		h_errno = TRY_AGAIN;
226 		return (NULL);
227 	}
228 }
229 
230 struct hostent *
231 gethostbyname(name)
232 	char *name;
233 {
234 	int n;
235 	querybuf buf;
236 	register struct hostent *hp;
237 	extern struct hostent *_gethtbyname();
238 
239 	errno = 0;
240 	n = res_mkquery(QUERY, name, C_IN, T_A, (char *)NULL, 0, NULL,
241 		(char *)&buf, sizeof(buf));
242 	if (n < 0) {
243 #ifdef DEBUG
244 		if (_res.options & RES_DEBUG)
245 			printf("res_mkquery failed\n");
246 #endif
247 		return (NULL);
248 	}
249 	hp = getanswer((char *)&buf, n, 0);
250 	if (hp == NULL && errno == ECONNREFUSED)
251 		hp = _gethtbyname(name);
252 	return(hp);
253 }
254 
255 struct hostent *
256 gethostbyaddr(addr, len, type)
257 	char *addr;
258 	int len, type;
259 {
260 	int n;
261 	querybuf buf;
262 	register struct hostent *hp;
263 	char qbuf[MAXDNAME];
264 	extern struct hostent *_gethtbyaddr();
265 
266 	if (type != AF_INET)
267 		return (NULL);
268 	(void)sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa",
269 		((unsigned)addr[3] & 0xff),
270 		((unsigned)addr[2] & 0xff),
271 		((unsigned)addr[1] & 0xff),
272 		((unsigned)addr[0] & 0xff));
273 	n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, (char *)NULL, 0, NULL,
274 		(char *)&buf, sizeof(buf));
275 	if (n < 0) {
276 #ifdef DEBUG
277 		if (_res.options & RES_DEBUG)
278 			printf("res_mkquery failed\n");
279 #endif
280 		return (NULL);
281 	}
282 	hp = getanswer((char *)&buf, n, 1);
283 	if (hp == NULL && errno == ECONNREFUSED)
284 		hp = _gethtbyaddr(addr, len, type);
285 	if (hp == NULL)
286 		return(NULL);
287 	hp->h_addrtype = type;
288 	hp->h_length = len;
289 	h_addr_ptrs[0] = (char *)&host_addr;
290 	h_addr_ptrs[1] = (char *)0;
291 	host_addr = *(struct in_addr *)addr;
292 	return(hp);
293 }
294 
295 
296 _sethtent(f)
297 	int f;
298 {
299 	if (hostf == NULL)
300 		hostf = fopen(HOSTDB, "r" );
301 	else
302 		rewind(hostf);
303 	stayopen |= f;
304 }
305 
306 _endhtent()
307 {
308 	if (hostf && !stayopen) {
309 		(void) fclose(hostf);
310 		hostf = NULL;
311 	}
312 }
313 
314 struct hostent *
315 _gethtent()
316 {
317 	char *p;
318 	register char *cp, **q;
319 
320 	if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
321 		return (NULL);
322 again:
323 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
324 		return (NULL);
325 	if (*p == '#')
326 		goto again;
327 	cp = any(p, "#\n");
328 	if (cp == NULL)
329 		goto again;
330 	*cp = '\0';
331 	cp = any(p, " \t");
332 	if (cp == NULL)
333 		goto again;
334 	*cp++ = '\0';
335 	/* THIS STUFF IS INTERNET SPECIFIC */
336 #if	BSD >= 43
337 	host.h_addr_list = host_addrs;
338 #endif
339 	host.h_addr = hostaddr;
340 	*((u_long *)host.h_addr) = inet_addr(p);
341 	host.h_length = sizeof (u_long);
342 	host.h_addrtype = AF_INET;
343 	while (*cp == ' ' || *cp == '\t')
344 		cp++;
345 	host.h_name = cp;
346 	q = host.h_aliases = host_aliases;
347 	cp = any(cp, " \t");
348 	if (cp != NULL)
349 		*cp++ = '\0';
350 	while (cp && *cp) {
351 		if (*cp == ' ' || *cp == '\t') {
352 			cp++;
353 			continue;
354 		}
355 		if (q < &host_aliases[MAXALIASES - 1])
356 			*q++ = cp;
357 		cp = any(cp, " \t");
358 		if (cp != NULL)
359 			*cp++ = '\0';
360 	}
361 	*q = NULL;
362 	return (&host);
363 }
364 
365 static char *
366 any(cp, match)
367 	register char *cp;
368 	char *match;
369 {
370 	register char *mp, c;
371 
372 	while (c = *cp) {
373 		for (mp = match; *mp; mp++)
374 			if (*mp == c)
375 				return (cp);
376 		cp++;
377 	}
378 	return ((char *)0);
379 }
380 
381 struct hostent *
382 _gethtbyname(name)
383 	char *name;
384 {
385 	register struct hostent *p;
386 	register char **cp;
387 	char lowname[128];
388 	register char *lp = lowname;
389 
390 	while (*name)
391 		if (isupper(*name))
392 			*lp++ = tolower(*name++);
393 		else
394 			*lp++ = *name++;
395 	*lp = '\0';
396 
397 	_sethtent(0);
398 	while (p = _gethtent()) {
399 		if (strcmp(p->h_name, lowname) == 0)
400 			break;
401 		for (cp = p->h_aliases; *cp != 0; cp++)
402 			if (strcmp(*cp, lowname) == 0)
403 				goto found;
404 	}
405 found:
406 	_endhtent();
407 	return (p);
408 }
409 
410 struct hostent *
411 _gethtbyaddr(addr, len, type)
412 	char *addr;
413 	int len, type;
414 {
415 	register struct hostent *p;
416 
417 	_sethtent(0);
418 	while (p = _gethtent())
419 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
420 			break;
421 	_endhtent();
422 	return (p);
423 }
424