xref: /csrg-svn/lib/libc/net/gethostnamadr.c (revision 32648)
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.26 (Berkeley) 11/19/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 #if PACKETSZ > 1024
40 #define MAXPACKET	PACKETSZ
41 #else
42 #define MAXPACKET	1024
43 #endif
44 
45 typedef union {
46     HEADER qb1;
47     char qb2[MAXPACKET];
48 } querybuf;
49 
50 static union {
51     long al;
52     char ac;
53 } align;
54 
55 
56 int h_errno;
57 extern errno;
58 
59 static struct hostent *
60 getanswer(msg, msglen, iquery)
61 	char *msg;
62 	int msglen, iquery;
63 {
64 	register HEADER *hp;
65 	register char *cp;
66 	register int n;
67 	querybuf answer;
68 	char *eom, *bp, **ap;
69 	int type, class, buflen, ancount, qdcount;
70 	int haveanswer, getclass = C_ANY;
71 	char **hap;
72 
73 	n = res_send(msg, msglen, (char *)&answer, sizeof(answer));
74 	if (n < 0) {
75 #ifdef DEBUG
76 		int terrno;
77 		terrno = errno;
78 		if (_res.options & RES_DEBUG)
79 			printf("res_send failed\n");
80 		errno = terrno;
81 #endif
82 		h_errno = TRY_AGAIN;
83 		return (NULL);
84 	}
85 	eom = (char *)&answer + n;
86 	/*
87 	 * find first satisfactory answer
88 	 */
89 	hp = (HEADER *) &answer;
90 	ancount = ntohs(hp->ancount);
91 	qdcount = ntohs(hp->qdcount);
92 	if (hp->rcode != NOERROR || ancount == 0) {
93 #ifdef DEBUG
94 		if (_res.options & RES_DEBUG)
95 			printf("rcode = %d, ancount=%d\n", hp->rcode, ancount);
96 #endif
97 		switch (hp->rcode) {
98 			case NXDOMAIN:
99 				/* Check if it's an authoritive answer */
100 				if (hp->aa)
101 					h_errno = HOST_NOT_FOUND;
102 				else
103 					h_errno = TRY_AGAIN;
104 				break;
105 			case SERVFAIL:
106 				h_errno = TRY_AGAIN;
107 				break;
108 			case NOERROR:
109 				if (hp->aa)
110 					h_errno = NO_ADDRESS;
111 				else
112 					h_errno = TRY_AGAIN;
113 				break;
114 			case FORMERR:
115 			case NOTIMP:
116 			case REFUSED:
117 				h_errno = NO_RECOVERY;
118 		}
119 		return (NULL);
120 	}
121 	bp = hostbuf;
122 	buflen = sizeof(hostbuf);
123 	cp = (char *)&answer + sizeof(HEADER);
124 	if (qdcount) {
125 		if (iquery) {
126 			if ((n = dn_expand((char *)&answer, eom,
127 			     cp, bp, buflen)) < 0) {
128 				h_errno = NO_RECOVERY;
129 				return (NULL);
130 			}
131 			cp += n + QFIXEDSZ;
132 			host.h_name = bp;
133 			n = strlen(bp) + 1;
134 			bp += n;
135 			buflen -= n;
136 		} else
137 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
138 		while (--qdcount > 0)
139 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
140 	} else if (iquery) {
141 		if (hp->aa)
142 			h_errno = HOST_NOT_FOUND;
143 		else
144 			h_errno = TRY_AGAIN;
145 		return (NULL);
146 	}
147 	ap = host_aliases;
148 	host.h_aliases = host_aliases;
149 	hap = h_addr_ptrs;
150 #if BSD >= 43
151 	host.h_addr_list = h_addr_ptrs;
152 #endif
153 	haveanswer = 0;
154 	while (--ancount >= 0 && cp < eom) {
155 		if ((n = dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0)
156 			break;
157 		cp += n;
158 		type = _getshort(cp);
159  		cp += sizeof(u_short);
160 		class = _getshort(cp);
161  		cp += sizeof(u_short) + sizeof(u_long);
162 		n = _getshort(cp);
163 		cp += sizeof(u_short);
164 		if (type == T_CNAME) {
165 			cp += n;
166 			if (ap >= &host_aliases[MAXALIASES-1])
167 				continue;
168 			*ap++ = bp;
169 			n = strlen(bp) + 1;
170 			bp += n;
171 			buflen -= n;
172 			continue;
173 		}
174 		if (type == T_PTR) {
175 			if ((n = dn_expand((char *)&answer, eom,
176 			    cp, bp, buflen)) < 0) {
177 				cp += n;
178 				continue;
179 			}
180 			cp += n;
181 			host.h_name = bp;
182 			return(&host);
183 		}
184 		if (type != T_A)  {
185 #ifdef DEBUG
186 			if (_res.options & RES_DEBUG)
187 				printf("unexpected answer type %d, size %d\n",
188 					type, n);
189 #endif
190 			cp += n;
191 			continue;
192 		}
193 		if (haveanswer) {
194 			if (n != host.h_length) {
195 				cp += n;
196 				continue;
197 			}
198 			if (class != getclass) {
199 				cp += n;
200 				continue;
201 			}
202 		} else {
203 			host.h_length = n;
204 			getclass = class;
205 			host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
206 			if (!iquery) {
207 				host.h_name = bp;
208 				bp += strlen(bp) + 1;
209 			}
210 		}
211 
212 		bp += sizeof(align) - ((u_long)bp % sizeof(align));
213 
214 		if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
215 #ifdef DEBUG
216 			if (_res.options & RES_DEBUG)
217 				printf("size (%d) too big\n", n);
218 #endif
219 			break;
220 		}
221 		bcopy(cp, *hap++ = bp, n);
222 		bp +=n;
223 		cp += n;
224 		haveanswer++;
225 	}
226 	if (haveanswer) {
227 		*ap = NULL;
228 #if BSD >= 43
229 		*hap = NULL;
230 #else
231 		host.h_addr = h_addr_ptrs[0];
232 #endif
233 		return (&host);
234 	} else {
235 		h_errno = TRY_AGAIN;
236 		return (NULL);
237 	}
238 }
239 
240 struct hostent *
241 gethostbyname(name)
242 	char *name;
243 {
244 	register char *cp, **domain;
245 	int n;
246 	struct hostent *hp, *gethostdomain();
247 	char *hostalias();
248 	extern struct hostent *_gethtbyname();
249 
250 	if (!(_res.options & RES_INIT) && res_init() == -1)
251 		return (NULL);
252 	if (isdigit(name[0])) {
253 		h_errno = HOST_NOT_FOUND;
254 		return (NULL);
255 
256 	}
257 	errno = 0;
258 	for (cp = name, n = 0; *cp; cp++)
259 		if (*cp == '.')
260 			n++;
261 	if ((n && *--cp == '.') || (_res.options & RES_DEFNAMES) == 0) {
262 		int defflag = _res.options & RES_DEFNAMES;
263 
264 		_res.options &= ~RES_DEFNAMES;			/* XXX */
265 		if (n && *cp == '.')
266 			*cp = 0;
267 		hp = gethostdomain(name, (char *)NULL);
268 		if (n && *cp == 0)
269 			*cp = '.';
270 		if (defflag)
271 			_res.options |= RES_DEFNAMES;
272 		return (hp);
273 	}
274 	if (n == 0 && (cp = hostalias(name)))
275 		return (gethostdomain(cp, (char *)NULL));
276 	for (domain = _res.dnsrch; *domain; domain++) {
277 		hp = gethostdomain(name, *domain);
278 		if (hp)
279 			return (hp);
280 		/*
281 		 * If no server present, use host table.
282 		 * If host isn't found in this domain,
283 		 * keep trying higher domains in the search list
284 		 * (if that's enabled).
285 		 * On a NO_ADDRESS error, keep trying,
286 		 * or a wildcard MX entry could keep us from finding
287 		 * host entries higher in the domain.
288 		 */
289 		if (errno == ECONNREFUSED)
290 			return (_gethtbyname(name));
291 		if ((h_errno != HOST_NOT_FOUND &&
292 		    h_errno != NO_ADDRESS) ||
293 		    (_res.options & RES_DNSRCH) == 0)
294 			return (NULL);
295 		h_errno = 0;
296 	}
297 	return (gethostdomain(name, (char *)NULL));
298 }
299 
300 static struct hostent *
301 gethostdomain(name, domain)
302 	char *name, *domain;
303 {
304 	querybuf buf;
305 	char nbuf[2*MAXDNAME+2];
306 	int n;
307 
308 	if (domain == NULL)
309 		(void)sprintf(nbuf, "%.*s", MAXDNAME, name);
310 	else
311 		(void)sprintf(nbuf, "%.*s.%.*s",
312 		    MAXDNAME, name, MAXDNAME, domain);
313 	n = res_mkquery(QUERY, nbuf, C_IN, T_A, (char *)NULL, 0, NULL,
314 		(char *)&buf, sizeof(buf));
315 	if (n < 0) {
316 #ifdef DEBUG
317 		if (_res.options & RES_DEBUG)
318 			printf("res_mkquery failed\n");
319 #endif
320 		return (NULL);
321 	}
322 	return (getanswer((char *)&buf, n, 0));
323 }
324 
325 struct hostent *
326 gethostbyaddr(addr, len, type)
327 	char *addr;
328 	int len, type;
329 {
330 	int n;
331 	querybuf buf;
332 	register struct hostent *hp;
333 	char qbuf[MAXDNAME];
334 	extern struct hostent *_gethtbyaddr();
335 
336 	if (type != AF_INET)
337 		return (NULL);
338 	(void)sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa",
339 		((unsigned)addr[3] & 0xff),
340 		((unsigned)addr[2] & 0xff),
341 		((unsigned)addr[1] & 0xff),
342 		((unsigned)addr[0] & 0xff));
343 	n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, (char *)NULL, 0, NULL,
344 		(char *)&buf, sizeof(buf));
345 	if (n < 0) {
346 #ifdef DEBUG
347 		if (_res.options & RES_DEBUG)
348 			printf("res_mkquery failed\n");
349 #endif
350 		return (NULL);
351 	}
352 	hp = getanswer((char *)&buf, n, 1);
353 	if (hp == NULL && errno == ECONNREFUSED)
354 		hp = _gethtbyaddr(addr, len, type);
355 	if (hp == NULL)
356 		return(NULL);
357 	hp->h_addrtype = type;
358 	hp->h_length = len;
359 	h_addr_ptrs[0] = (char *)&host_addr;
360 	h_addr_ptrs[1] = (char *)0;
361 	host_addr = *(struct in_addr *)addr;
362 	return(hp);
363 }
364 
365 char *
366 hostalias(name)
367 	register char *name;
368 {
369 	register char *C1, *C2;
370 	FILE *fp;
371 	char *file, *getenv(), *strcpy(), *strncpy();
372 	char buf[BUFSIZ];
373 	static char abuf[MAXDNAME];
374 
375 	file = getenv("HOSTALIASES");
376 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
377 		return (NULL);
378 	buf[sizeof(buf) - 1] = '\0';
379 	while (fgets(buf, sizeof(buf), fp)) {
380 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
381 		if (!*C1)
382 			break;
383 		*C1 = '\0';
384 		if (!strcasecmp(buf, name)) {
385 			while (isspace(*++C1));
386 			if (!*C1)
387 				break;
388 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
389 			abuf[sizeof(abuf) - 1] = *C2 = '\0';
390 			(void)strncpy(abuf, C1, sizeof(abuf) - 1);
391 			fclose(fp);
392 			return (abuf);
393 		}
394 	}
395 	fclose(fp);
396 	return (NULL);
397 }
398 
399 _sethtent(f)
400 	int f;
401 {
402 	if (hostf == NULL)
403 		hostf = fopen(HOSTDB, "r" );
404 	else
405 		rewind(hostf);
406 	stayopen |= f;
407 }
408 
409 _endhtent()
410 {
411 	if (hostf && !stayopen) {
412 		(void) fclose(hostf);
413 		hostf = NULL;
414 	}
415 }
416 
417 struct hostent *
418 _gethtent()
419 {
420 	char *p;
421 	register char *cp, **q;
422 
423 	if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
424 		return (NULL);
425 again:
426 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
427 		return (NULL);
428 	if (*p == '#')
429 		goto again;
430 	cp = any(p, "#\n");
431 	if (cp == NULL)
432 		goto again;
433 	*cp = '\0';
434 	cp = any(p, " \t");
435 	if (cp == NULL)
436 		goto again;
437 	*cp++ = '\0';
438 	/* THIS STUFF IS INTERNET SPECIFIC */
439 #if	BSD >= 43
440 	host.h_addr_list = host_addrs;
441 #endif
442 	host.h_addr = hostaddr;
443 	*((u_long *)host.h_addr) = inet_addr(p);
444 	host.h_length = sizeof (u_long);
445 	host.h_addrtype = AF_INET;
446 	while (*cp == ' ' || *cp == '\t')
447 		cp++;
448 	host.h_name = cp;
449 	q = host.h_aliases = host_aliases;
450 	cp = any(cp, " \t");
451 	if (cp != NULL)
452 		*cp++ = '\0';
453 	while (cp && *cp) {
454 		if (*cp == ' ' || *cp == '\t') {
455 			cp++;
456 			continue;
457 		}
458 		if (q < &host_aliases[MAXALIASES - 1])
459 			*q++ = cp;
460 		cp = any(cp, " \t");
461 		if (cp != NULL)
462 			*cp++ = '\0';
463 	}
464 	*q = NULL;
465 	return (&host);
466 }
467 
468 static char *
469 any(cp, match)
470 	register char *cp;
471 	char *match;
472 {
473 	register char *mp, c;
474 
475 	while (c = *cp) {
476 		for (mp = match; *mp; mp++)
477 			if (*mp == c)
478 				return (cp);
479 		cp++;
480 	}
481 	return ((char *)0);
482 }
483 
484 struct hostent *
485 _gethtbyname(name)
486 	char *name;
487 {
488 	register struct hostent *p;
489 	register char **cp;
490 
491 	_sethtent(0);
492 	while (p = _gethtent()) {
493 		if (strcasecmp(p->h_name, name) == 0)
494 			break;
495 		for (cp = p->h_aliases; *cp != 0; cp++)
496 			if (strcasecmp(*cp, name) == 0)
497 				goto found;
498 	}
499 found:
500 	_endhtent();
501 	return (p);
502 }
503 
504 struct hostent *
505 _gethtbyaddr(addr, len, type)
506 	char *addr;
507 	int len, type;
508 {
509 	register struct hostent *p;
510 
511 	_sethtent(0);
512 	while (p = _gethtent())
513 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
514 			break;
515 	_endhtent();
516 	return (p);
517 }
518