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