xref: /csrg-svn/lib/libc/net/gethostnamadr.c (revision 31780)
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.20 (Berkeley) 07/05/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 	register char *cp, **domain;
235 	int n;
236 	struct hostent *hp, *gethostdomain();
237 	char *hostalias();
238 	extern struct hostent *_gethtbyname();
239 
240 	if (!(_res.options & RES_INIT) && res_init() == -1)
241 		return (NULL);
242 	errno = 0;
243 	for (cp = name, n = 0; *cp; cp++)
244 		if (*cp == '.')
245 			n++;
246 	if ((n && cp[-1] == '.') || (_res.options & RES_DEFNAMES) == 0) {
247 		cp[-1] = 0;
248 		hp = gethostdomain(name, (char *)NULL);
249 		cp[-1] = '.';
250 		return (hp);
251 	}
252 	if (n == 0 && (cp = hostalias(name)))
253 		return (gethostdomain(cp, (char *)NULL));
254 	for (domain = _res.dnsrch; *domain; domain++) {
255 		hp = gethostdomain(name, *domain);
256 		if (hp)
257 			return (hp);
258 		if (errno == ECONNREFUSED)
259 			return (_gethtbyname(name));
260 		if (h_errno != HOST_NOT_FOUND ||
261 		    (_res.options & RES_DNSRCH) == 0)
262 			return (NULL);
263 		h_errno = 0;
264 	}
265 	return (gethostdomain(name, (char *)NULL));
266 }
267 
268 static struct hostent *
269 gethostdomain(name, domain)
270 	char *name, *domain;
271 {
272 	querybuf buf;
273 	char nbuf[2*MAXDNAME+2];
274 	int n;
275 
276 	sprintf(nbuf, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain);
277 	n = res_mkquery(QUERY, nbuf, C_IN, T_A, (char *)NULL, 0, NULL,
278 		(char *)&buf, sizeof(buf));
279 	if (n < 0) {
280 #ifdef DEBUG
281 		if (_res.options & RES_DEBUG)
282 			printf("res_mkquery failed\n");
283 #endif
284 		return (NULL);
285 	}
286 	return (getanswer((char *)&buf, n, 0));
287 }
288 
289 struct hostent *
290 gethostbyaddr(addr, len, type)
291 	char *addr;
292 	int len, type;
293 {
294 	int n;
295 	querybuf buf;
296 	register struct hostent *hp;
297 	char qbuf[MAXDNAME];
298 	extern struct hostent *_gethtbyaddr();
299 
300 	if (type != AF_INET)
301 		return (NULL);
302 	(void)sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa",
303 		((unsigned)addr[3] & 0xff),
304 		((unsigned)addr[2] & 0xff),
305 		((unsigned)addr[1] & 0xff),
306 		((unsigned)addr[0] & 0xff));
307 	n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, (char *)NULL, 0, NULL,
308 		(char *)&buf, sizeof(buf));
309 	if (n < 0) {
310 #ifdef DEBUG
311 		if (_res.options & RES_DEBUG)
312 			printf("res_mkquery failed\n");
313 #endif
314 		return (NULL);
315 	}
316 	hp = getanswer((char *)&buf, n, 1);
317 	if (hp == NULL && errno == ECONNREFUSED)
318 		hp = _gethtbyaddr(addr, len, type);
319 	if (hp == NULL)
320 		return(NULL);
321 	hp->h_addrtype = type;
322 	hp->h_length = len;
323 	h_addr_ptrs[0] = (char *)&host_addr;
324 	h_addr_ptrs[1] = (char *)0;
325 	host_addr = *(struct in_addr *)addr;
326 	return(hp);
327 }
328 
329 char *
330 hostalias(name)
331 	register char *name;
332 {
333 	register char *C1, *C2;
334 	FILE *fp;
335 	char *file, *getenv(), *strcpy(), *strncpy();
336 	char buf[BUFSIZ];
337 	static char abuf[MAXDNAME];
338 
339 	file = getenv("HOSTALIASES");
340 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
341 		return (NULL);
342 	buf[sizeof(buf) - 1] = '\0';
343 	while (fgets(buf, sizeof(buf), fp)) {
344 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
345 		if (!*C1 || *C1 == '\n')
346 			break;
347 		if (!strncmp(buf, name, C1 - buf)) {
348 			while (isspace(*++C1));
349 			if (!*C1)
350 				break;
351 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
352 			abuf[sizeof(abuf) - 1] = *C2 = '\0';
353 			(void)strncpy(abuf, C1, sizeof(abuf) - 1);
354 			fclose(fp);
355 			return (abuf);
356 		}
357 	}
358 	fclose(fp);
359 	return (NULL);
360 }
361 
362 _sethtent(f)
363 	int f;
364 {
365 	if (hostf == NULL)
366 		hostf = fopen(HOSTDB, "r" );
367 	else
368 		rewind(hostf);
369 	stayopen |= f;
370 }
371 
372 _endhtent()
373 {
374 	if (hostf && !stayopen) {
375 		(void) fclose(hostf);
376 		hostf = NULL;
377 	}
378 }
379 
380 struct hostent *
381 _gethtent()
382 {
383 	char *p;
384 	register char *cp, **q;
385 
386 	if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
387 		return (NULL);
388 again:
389 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
390 		return (NULL);
391 	if (*p == '#')
392 		goto again;
393 	cp = any(p, "#\n");
394 	if (cp == NULL)
395 		goto again;
396 	*cp = '\0';
397 	cp = any(p, " \t");
398 	if (cp == NULL)
399 		goto again;
400 	*cp++ = '\0';
401 	/* THIS STUFF IS INTERNET SPECIFIC */
402 #if	BSD >= 43
403 	host.h_addr_list = host_addrs;
404 #endif
405 	host.h_addr = hostaddr;
406 	*((u_long *)host.h_addr) = inet_addr(p);
407 	host.h_length = sizeof (u_long);
408 	host.h_addrtype = AF_INET;
409 	while (*cp == ' ' || *cp == '\t')
410 		cp++;
411 	host.h_name = cp;
412 	q = host.h_aliases = host_aliases;
413 	cp = any(cp, " \t");
414 	if (cp != NULL)
415 		*cp++ = '\0';
416 	while (cp && *cp) {
417 		if (*cp == ' ' || *cp == '\t') {
418 			cp++;
419 			continue;
420 		}
421 		if (q < &host_aliases[MAXALIASES - 1])
422 			*q++ = cp;
423 		cp = any(cp, " \t");
424 		if (cp != NULL)
425 			*cp++ = '\0';
426 	}
427 	*q = NULL;
428 	return (&host);
429 }
430 
431 static char *
432 any(cp, match)
433 	register char *cp;
434 	char *match;
435 {
436 	register char *mp, c;
437 
438 	while (c = *cp) {
439 		for (mp = match; *mp; mp++)
440 			if (*mp == c)
441 				return (cp);
442 		cp++;
443 	}
444 	return ((char *)0);
445 }
446 
447 struct hostent *
448 _gethtbyname(name)
449 	char *name;
450 {
451 	register struct hostent *p;
452 	register char **cp;
453 	char lowname[128];
454 	register char *lp = lowname;
455 
456 	while (*name)
457 		if (isupper(*name))
458 			*lp++ = tolower(*name++);
459 		else
460 			*lp++ = *name++;
461 	*lp = '\0';
462 
463 	_sethtent(0);
464 	while (p = _gethtent()) {
465 		if (strcmp(p->h_name, lowname) == 0)
466 			break;
467 		for (cp = p->h_aliases; *cp != 0; cp++)
468 			if (strcmp(*cp, lowname) == 0)
469 				goto found;
470 	}
471 found:
472 	_endhtent();
473 	return (p);
474 }
475 
476 struct hostent *
477 _gethtbyaddr(addr, len, type)
478 	char *addr;
479 	int len, type;
480 {
481 	register struct hostent *p;
482 
483 	_sethtent(0);
484 	while (p = _gethtent())
485 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
486 			break;
487 	_endhtent();
488 	return (p);
489 }
490