xref: /csrg-svn/lib/libc/net/gethostnamadr.c (revision 29568)
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.13 (Berkeley) 07/16/86";
9 #endif LIBC_SCCS and not lint
10 
11 #include <sys/types.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 	host.h_addr_list = h_addr_ptrs;
142 	haveanswer = 0;
143 	while (--ancount >= 0 && cp < eom) {
144 		if ((n = dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0)
145 			break;
146 		cp += n;
147 		type = getshort(cp);
148  		cp += sizeof(u_short);
149 		class = getshort(cp);
150  		cp += sizeof(u_short) + sizeof(u_long);
151 		n = getshort(cp);
152 		cp += sizeof(u_short);
153 		if (type == T_CNAME) {
154 			cp += n;
155 			if (ap >= &host_aliases[MAXALIASES-1])
156 				continue;
157 			*ap++ = bp;
158 			n = strlen(bp) + 1;
159 			bp += n;
160 			buflen -= n;
161 			continue;
162 		}
163 		if (type == T_PTR) {
164 			if ((n = dn_expand((char *)&answer, eom,
165 			    cp, bp, buflen)) < 0) {
166 				cp += n;
167 				continue;
168 			}
169 			cp += n;
170 			host.h_name = bp;
171 			return(&host);
172 		}
173 		if (type != T_A)  {
174 #ifdef DEBUG
175 			if (_res.options & RES_DEBUG)
176 				printf("unexpected answer type %d, size %d\n",
177 					type, n);
178 #endif
179 			cp += n;
180 			continue;
181 		}
182 		if (haveanswer) {
183 			if (n != host.h_length) {
184 				cp += n;
185 				continue;
186 			}
187 			if (class != getclass) {
188 				cp += n;
189 				continue;
190 			}
191 		} else {
192 			host.h_length = n;
193 			getclass = class;
194 			host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
195 			if (!iquery) {
196 				host.h_name = bp;
197 				bp += strlen(bp) + 1;
198 			}
199 		}
200 
201 		bp += (sizeof(align) - ((u_long)bp % sizeof(align))) &~
202 		    sizeof(align);
203 
204 		if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
205 #ifdef DEBUG
206 			if (_res.options & RES_DEBUG)
207 				printf("size (%d) too big\n", n);
208 #endif
209 			break;
210 		}
211 		bcopy(cp, *hap++ = bp, n);
212 		bp +=n;
213 		cp += n;
214 		haveanswer++;
215 	}
216 	if (haveanswer) {
217 		*ap = NULL;
218 		*hap = NULL;
219 		return (&host);
220 	} else {
221 		h_errno = TRY_AGAIN;
222 		return (NULL);
223 	}
224 }
225 
226 struct hostent *
227 gethostbyname(name)
228 	char *name;
229 {
230 	int n;
231 	querybuf buf;
232 	register struct hostent *hp;
233 	extern struct hostent *_gethtbyname();
234 
235 	n = res_mkquery(QUERY, name, C_IN, T_A, (char *)NULL, 0, NULL,
236 		(char *)&buf, sizeof(buf));
237 	if (n < 0) {
238 #ifdef DEBUG
239 		if (_res.options & RES_DEBUG)
240 			printf("res_mkquery failed\n");
241 #endif
242 		return (NULL);
243 	}
244 	hp = getanswer((char *)&buf, n, 0);
245 	if (hp == NULL && errno == ECONNREFUSED)
246 		hp = _gethtbyname(name);
247 	return(hp);
248 }
249 
250 struct hostent *
251 gethostbyaddr(addr, len, type)
252 	char *addr;
253 	int len, type;
254 {
255 	int n;
256 	querybuf buf;
257 	register struct hostent *hp;
258 	char qbuf[MAXDNAME];
259 	extern struct hostent *_gethtbyaddr();
260 
261 	if (type != AF_INET)
262 		return (NULL);
263 	(void)sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa",
264 		((unsigned)addr[3] & 0xff),
265 		((unsigned)addr[2] & 0xff),
266 		((unsigned)addr[1] & 0xff),
267 		((unsigned)addr[0] & 0xff));
268 	n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, (char *)NULL, 0, NULL,
269 		(char *)&buf, sizeof(buf));
270 	if (n < 0) {
271 #ifdef DEBUG
272 		if (_res.options & RES_DEBUG)
273 			printf("res_mkquery failed\n");
274 #endif
275 		return (NULL);
276 	}
277 	hp = getanswer((char *)&buf, n, 1);
278 	if (hp == NULL && errno == ECONNREFUSED)
279 		hp = _gethtbyaddr(addr, len, type);
280 	if (hp == NULL)
281 		return(NULL);
282 	hp->h_addrtype = type;
283 	hp->h_length = len;
284 	h_addr_ptrs[0] = (char *)&host_addr;
285 	h_addr_ptrs[1] = (char *)0;
286 	host_addr = *(struct in_addr *)addr;
287 	return(hp);
288 }
289 
290 
291 _sethtent(f)
292 	int f;
293 {
294 	if (hostf == NULL)
295 		hostf = fopen(HOSTDB, "r" );
296 	else
297 		rewind(hostf);
298 	stayopen |= f;
299 }
300 
301 _endhtent()
302 {
303 	if (hostf && !stayopen) {
304 		(void) fclose(hostf);
305 		hostf = NULL;
306 	}
307 }
308 
309 struct hostent *
310 _gethtent()
311 {
312 	char *p;
313 	register char *cp, **q;
314 
315 	if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
316 		return (NULL);
317 again:
318 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
319 		return (NULL);
320 	if (*p == '#')
321 		goto again;
322 	cp = any(p, "#\n");
323 	if (cp == NULL)
324 		goto again;
325 	*cp = '\0';
326 	cp = any(p, " \t");
327 	if (cp == NULL)
328 		goto again;
329 	*cp++ = '\0';
330 	/* THIS STUFF IS INTERNET SPECIFIC */
331 	host.h_addr_list = host_addrs;
332 	host.h_addr = hostaddr;
333 	*((u_long *)host.h_addr) = inet_addr(p);
334 	host.h_length = sizeof (u_long);
335 	host.h_addrtype = AF_INET;
336 	while (*cp == ' ' || *cp == '\t')
337 		cp++;
338 	host.h_name = cp;
339 	q = host.h_aliases = host_aliases;
340 	cp = any(cp, " \t");
341 	if (cp != NULL)
342 		*cp++ = '\0';
343 	while (cp && *cp) {
344 		if (*cp == ' ' || *cp == '\t') {
345 			cp++;
346 			continue;
347 		}
348 		if (q < &host_aliases[MAXALIASES - 1])
349 			*q++ = cp;
350 		cp = any(cp, " \t");
351 		if (cp != NULL)
352 			*cp++ = '\0';
353 	}
354 	*q = NULL;
355 	return (&host);
356 }
357 
358 static char *
359 any(cp, match)
360 	register char *cp;
361 	char *match;
362 {
363 	register char *mp, c;
364 
365 	while (c = *cp) {
366 		for (mp = match; *mp; mp++)
367 			if (*mp == c)
368 				return (cp);
369 		cp++;
370 	}
371 	return ((char *)0);
372 }
373 
374 struct hostent *
375 _gethtbyname(name)
376 	char *name;
377 {
378 	register struct hostent *p;
379 	register char **cp;
380 	char lowname[128];
381 	register char *lp = lowname;
382 
383 	while (*name)
384 		if (isupper(*name))
385 			*lp++ = tolower(*name++);
386 		else
387 			*lp++ = *name++;
388 	*lp = '\0';
389 
390 	_sethtent(0);
391 	while (p = _gethtent()) {
392 		if (strcmp(p->h_name, lowname) == 0)
393 			break;
394 		for (cp = p->h_aliases; *cp != 0; cp++)
395 			if (strcmp(*cp, lowname) == 0)
396 				goto found;
397 	}
398 found:
399 	_endhtent();
400 	return (p);
401 }
402 
403 struct hostent *
404 _gethtbyaddr(addr, len, type)
405 	char *addr;
406 	int len, type;
407 {
408 	register struct hostent *p;
409 
410 	_sethtent(0);
411 	while (p = _gethtent())
412 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
413 			break;
414 	_endhtent();
415 	return (p);
416 }
417