xref: /csrg-svn/lib/libc/net/gethostnamadr.c (revision 42625)
121374Sdist /*
233734Sbostic  * Copyright (c) 1985, 1988 Regents of the University of California.
333734Sbostic  * All rights reserved.
433734Sbostic  *
5*42625Sbostic  * %sccs.include.redist.c%
621374Sdist  */
715662Sralph 
826630Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42625Sbostic static char sccsid[] = "@(#)gethostnamadr.c	6.41 (Berkeley) 06/01/90";
1033734Sbostic #endif /* LIBC_SCCS and not lint */
1121374Sdist 
1230468Skjd #include <sys/param.h>
1318505Sralph #include <sys/socket.h>
1418505Sralph #include <netinet/in.h>
1526887Skjd #include <ctype.h>
1618505Sralph #include <netdb.h>
1715662Sralph #include <stdio.h>
1826887Skjd #include <errno.h>
1927034Skjd #include <arpa/inet.h>
2024508Sbloom #include <arpa/nameser.h>
2126894Skjd #include <resolv.h>
2215662Sralph 
2315662Sralph #define	MAXALIASES	35
2433735Sbostic #define	MAXADDRS	35
2515662Sralph 
2624687Sbloom static char *h_addr_ptrs[MAXADDRS + 1];
2724687Sbloom 
2824687Sbloom static struct hostent host;
2915662Sralph static char *host_aliases[MAXALIASES];
3015912Sralph static char hostbuf[BUFSIZ+1];
3125153Sbloom static struct in_addr host_addr;
3226887Skjd static FILE *hostf = NULL;
3326887Skjd static char hostaddr[MAXADDRS];
3426887Skjd static char *host_addrs[2];
3526887Skjd static int stayopen = 0;
3637108Sbostic char *strpbrk();
3715662Sralph 
3832648Skarels #if PACKETSZ > 1024
3933735Sbostic #define	MAXPACKET	PACKETSZ
4032648Skarels #else
4133735Sbostic #define	MAXPACKET	1024
4232648Skarels #endif
4332648Skarels 
4425302Skjd typedef union {
4533735Sbostic     HEADER hdr;
4633735Sbostic     u_char buf[MAXPACKET];
4725302Skjd } querybuf;
4824509Sbloom 
4935765Skarels typedef union {
5025302Skjd     long al;
5125302Skjd     char ac;
5225302Skjd } align;
5325302Skjd 
5425302Skjd 
5525484Skjd int h_errno;
5626887Skjd extern errno;
5725484Skjd 
5815662Sralph static struct hostent *
5933735Sbostic getanswer(answer, anslen, iquery)
6033735Sbostic 	querybuf *answer;
6133735Sbostic 	int anslen;
6233735Sbostic 	int iquery;
6315662Sralph {
6418505Sralph 	register HEADER *hp;
6533735Sbostic 	register u_char *cp;
6618505Sralph 	register int n;
6733735Sbostic 	u_char *eom;
6833735Sbostic 	char *bp, **ap;
6927034Skjd 	int type, class, buflen, ancount, qdcount;
7027034Skjd 	int haveanswer, getclass = C_ANY;
7124687Sbloom 	char **hap;
7215662Sralph 
7333735Sbostic 	eom = answer->buf + anslen;
7418505Sralph 	/*
7518505Sralph 	 * find first satisfactory answer
7618505Sralph 	 */
7733735Sbostic 	hp = &answer->hdr;
7818505Sralph 	ancount = ntohs(hp->ancount);
7925153Sbloom 	qdcount = ntohs(hp->qdcount);
8018505Sralph 	bp = hostbuf;
8118505Sralph 	buflen = sizeof(hostbuf);
8233735Sbostic 	cp = answer->buf + sizeof(HEADER);
8325153Sbloom 	if (qdcount) {
8418505Sralph 		if (iquery) {
8533735Sbostic 			if ((n = dn_expand((char *)answer->buf, eom,
8626066Skjd 			     cp, bp, buflen)) < 0) {
8725484Skjd 				h_errno = NO_RECOVERY;
8833500Skarels 				return ((struct hostent *) NULL);
8925484Skjd 			}
9018505Sralph 			cp += n + QFIXEDSZ;
9118505Sralph 			host.h_name = bp;
9218505Sralph 			n = strlen(bp) + 1;
9318505Sralph 			bp += n;
9418505Sralph 			buflen -= n;
9518505Sralph 		} else
9632648Skarels 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
9725153Sbloom 		while (--qdcount > 0)
9832648Skarels 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
9925484Skjd 	} else if (iquery) {
10025484Skjd 		if (hp->aa)
10125484Skjd 			h_errno = HOST_NOT_FOUND;
10225484Skjd 		else
10325484Skjd 			h_errno = TRY_AGAIN;
10433500Skarels 		return ((struct hostent *) NULL);
10525484Skjd 	}
10624687Sbloom 	ap = host_aliases;
10739862Skarels 	*ap = NULL;
10824687Sbloom 	host.h_aliases = host_aliases;
10924687Sbloom 	hap = h_addr_ptrs;
11039862Skarels 	*hap = NULL;
11133600Skarels #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
11224687Sbloom 	host.h_addr_list = h_addr_ptrs;
11330460Skjd #endif
11424687Sbloom 	haveanswer = 0;
11518505Sralph 	while (--ancount >= 0 && cp < eom) {
11633735Sbostic 		if ((n = dn_expand((char *)answer->buf, eom, cp, bp, buflen)) < 0)
11724687Sbloom 			break;
11818505Sralph 		cp += n;
11930440Skjd 		type = _getshort(cp);
12018505Sralph  		cp += sizeof(u_short);
12130440Skjd 		class = _getshort(cp);
12218505Sralph  		cp += sizeof(u_short) + sizeof(u_long);
12330440Skjd 		n = _getshort(cp);
12418505Sralph 		cp += sizeof(u_short);
12518505Sralph 		if (type == T_CNAME) {
12618505Sralph 			cp += n;
12718505Sralph 			if (ap >= &host_aliases[MAXALIASES-1])
12818505Sralph 				continue;
12918505Sralph 			*ap++ = bp;
13018505Sralph 			n = strlen(bp) + 1;
13118505Sralph 			bp += n;
13218505Sralph 			buflen -= n;
13318505Sralph 			continue;
13418505Sralph 		}
13533735Sbostic 		if (iquery && type == T_PTR) {
13633735Sbostic 			if ((n = dn_expand((char *)answer->buf, eom,
13727034Skjd 			    cp, bp, buflen)) < 0) {
13825153Sbloom 				cp += n;
13925153Sbloom 				continue;
14025153Sbloom 			}
14125153Sbloom 			cp += n;
14225153Sbloom 			host.h_name = bp;
14325153Sbloom 			return(&host);
14425153Sbloom 		}
14533735Sbostic 		if (iquery || type != T_A)  {
14624733Sbloom #ifdef DEBUG
14718505Sralph 			if (_res.options & RES_DEBUG)
14818505Sralph 				printf("unexpected answer type %d, size %d\n",
14918505Sralph 					type, n);
15024733Sbloom #endif
15124687Sbloom 			cp += n;
15218505Sralph 			continue;
15318505Sralph 		}
15424687Sbloom 		if (haveanswer) {
15524687Sbloom 			if (n != host.h_length) {
15624687Sbloom 				cp += n;
15724687Sbloom 				continue;
15824687Sbloom 			}
15924687Sbloom 			if (class != getclass) {
16024687Sbloom 				cp += n;
16124687Sbloom 				continue;
16224687Sbloom 			}
16324687Sbloom 		} else {
16424687Sbloom 			host.h_length = n;
16524687Sbloom 			getclass = class;
16625386Skjd 			host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
16724687Sbloom 			if (!iquery) {
16824687Sbloom 				host.h_name = bp;
16924687Sbloom 				bp += strlen(bp) + 1;
17024687Sbloom 			}
17118505Sralph 		}
17225302Skjd 
17331902Skarels 		bp += sizeof(align) - ((u_long)bp % sizeof(align));
17425302Skjd 
17518505Sralph 		if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
17624733Sbloom #ifdef DEBUG
17718505Sralph 			if (_res.options & RES_DEBUG)
17818505Sralph 				printf("size (%d) too big\n", n);
17924733Sbloom #endif
18024687Sbloom 			break;
18118505Sralph 		}
18224687Sbloom 		bcopy(cp, *hap++ = bp, n);
18324687Sbloom 		bp +=n;
18424687Sbloom 		cp += n;
18524687Sbloom 		haveanswer++;
18624687Sbloom 	}
18724687Sbloom 	if (haveanswer) {
18824687Sbloom 		*ap = NULL;
18933600Skarels #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
19024687Sbloom 		*hap = NULL;
19131902Skarels #else
19231902Skarels 		host.h_addr = h_addr_ptrs[0];
19331902Skarels #endif
19418505Sralph 		return (&host);
19525484Skjd 	} else {
19625484Skjd 		h_errno = TRY_AGAIN;
19733500Skarels 		return ((struct hostent *) NULL);
19825484Skjd 	}
19915662Sralph }
20015662Sralph 
20115662Sralph struct hostent *
20218505Sralph gethostbyname(name)
20318505Sralph 	char *name;
20415662Sralph {
20533735Sbostic 	querybuf buf;
20633735Sbostic 	register char *cp;
20718505Sralph 	int n;
20826887Skjd 	extern struct hostent *_gethtbyname();
20915662Sralph 
21032976Sbostic 	/*
21132976Sbostic 	 * disallow names consisting only of digits/dots, unless
21232976Sbostic 	 * they end in a dot.
21332976Sbostic 	 */
21432976Sbostic 	if (isdigit(name[0]))
21532976Sbostic 		for (cp = name;; ++cp) {
21632976Sbostic 			if (!*cp) {
21732976Sbostic 				if (*--cp == '.')
21832976Sbostic 					break;
21939710Sbloom 				/*
22039710Sbloom 				 * All-numeric, no dot at the end.
22139710Sbloom 				 * Fake up a hostent as if we'd actually
22239710Sbloom 				 * done a lookup.  What if someone types
22339710Sbloom 				 * 255.255.255.255?  The test below will
22439710Sbloom 				 * succeed spuriously... ???
22539710Sbloom 				 */
22639710Sbloom 				if ((host_addr.s_addr = inet_addr(name)) == -1) {
22739710Sbloom 					h_errno = HOST_NOT_FOUND;
22839710Sbloom 					return((struct hostent *) NULL);
22939710Sbloom 				}
23039710Sbloom 				host.h_name = name;
23139710Sbloom 				host.h_aliases = host_aliases;
23239710Sbloom 				host_aliases[0] = NULL;
23339710Sbloom 				host.h_addrtype = AF_INET;
23439710Sbloom 				host.h_length = sizeof(u_long);
23539710Sbloom 				h_addr_ptrs[0] = (char *)&host_addr;
23639710Sbloom 				h_addr_ptrs[1] = (char *)0;
23739710Sbloom #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
23839710Sbloom 				host.h_addr_list = h_addr_ptrs;
23939710Sbloom #else
24039710Sbloom 				host.h_addr = h_addr_ptrs[0];
24139710Sbloom #endif
24239710Sbloom 				return (&host);
24332976Sbostic 			}
24432976Sbostic 			if (!isdigit(*cp) && *cp != '.')
24532976Sbostic 				break;
24632976Sbostic 		}
24731111Skarels 
24833735Sbostic 	if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
24924733Sbloom #ifdef DEBUG
25018505Sralph 		if (_res.options & RES_DEBUG)
25133735Sbostic 			printf("res_search failed\n");
25224733Sbloom #endif
25333735Sbostic 		if (errno == ECONNREFUSED)
25433735Sbostic 			return (_gethtbyname(name));
25533735Sbostic 		else
25633735Sbostic 			return ((struct hostent *) NULL);
25717761Sserge 	}
25833735Sbostic 	return (getanswer(&buf, n, 0));
25915662Sralph }
26015662Sralph 
26115662Sralph struct hostent *
26218505Sralph gethostbyaddr(addr, len, type)
26315662Sralph 	char *addr;
26418505Sralph 	int len, type;
26515662Sralph {
26618505Sralph 	int n;
26725302Skjd 	querybuf buf;
26825153Sbloom 	register struct hostent *hp;
26925153Sbloom 	char qbuf[MAXDNAME];
27026887Skjd 	extern struct hostent *_gethtbyaddr();
27126887Skjd 
27218505Sralph 	if (type != AF_INET)
27333500Skarels 		return ((struct hostent *) NULL);
27435395Sbostic 	(void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
27525484Skjd 		((unsigned)addr[3] & 0xff),
27625484Skjd 		((unsigned)addr[2] & 0xff),
27725484Skjd 		((unsigned)addr[1] & 0xff),
27825484Skjd 		((unsigned)addr[0] & 0xff));
27933735Sbostic 	n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf));
28018505Sralph 	if (n < 0) {
28124733Sbloom #ifdef DEBUG
28218505Sralph 		if (_res.options & RES_DEBUG)
28333735Sbostic 			printf("res_query failed\n");
28424733Sbloom #endif
28533735Sbostic 		if (errno == ECONNREFUSED)
28633990Skarels 			return (_gethtbyaddr(addr, len, type));
28733500Skarels 		return ((struct hostent *) NULL);
28817761Sserge 	}
28933735Sbostic 	hp = getanswer(&buf, n, 1);
29026887Skjd 	if (hp == NULL)
29133500Skarels 		return ((struct hostent *) NULL);
29225153Sbloom 	hp->h_addrtype = type;
29325153Sbloom 	hp->h_length = len;
29425282Sbloom 	h_addr_ptrs[0] = (char *)&host_addr;
29525153Sbloom 	h_addr_ptrs[1] = (char *)0;
29625153Sbloom 	host_addr = *(struct in_addr *)addr;
29739710Sbloom #if BSD < 43 && !defined(h_addr)	/* new-style hostent structure */
29839710Sbloom 	hp->h_addr = h_addr_ptrs[0];
29939710Sbloom #endif
30025153Sbloom 	return(hp);
30115662Sralph }
30225302Skjd 
30326887Skjd _sethtent(f)
30426887Skjd 	int f;
30526887Skjd {
30626887Skjd 	if (hostf == NULL)
30742266Sbostic 		hostf = fopen(_PATH_HOSTS, "r" );
30826887Skjd 	else
30926887Skjd 		rewind(hostf);
31026887Skjd 	stayopen |= f;
31126887Skjd }
31226887Skjd 
31326887Skjd _endhtent()
31426887Skjd {
31526887Skjd 	if (hostf && !stayopen) {
31627034Skjd 		(void) fclose(hostf);
31726887Skjd 		hostf = NULL;
31826887Skjd 	}
31926887Skjd }
32026887Skjd 
32126887Skjd struct hostent *
32226887Skjd _gethtent()
32326887Skjd {
32426887Skjd 	char *p;
32526887Skjd 	register char *cp, **q;
32626887Skjd 
32742266Sbostic 	if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
32826887Skjd 		return (NULL);
32926887Skjd again:
33033735Sbostic 	if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
33126887Skjd 		return (NULL);
33226887Skjd 	if (*p == '#')
33326887Skjd 		goto again;
33437108Sbostic 	cp = strpbrk(p, "#\n");
33526887Skjd 	if (cp == NULL)
33626887Skjd 		goto again;
33726887Skjd 	*cp = '\0';
33837108Sbostic 	cp = strpbrk(p, " \t");
33926887Skjd 	if (cp == NULL)
34026887Skjd 		goto again;
34126887Skjd 	*cp++ = '\0';
34226887Skjd 	/* THIS STUFF IS INTERNET SPECIFIC */
34333600Skarels #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
34426887Skjd 	host.h_addr_list = host_addrs;
34530460Skjd #endif
34626887Skjd 	host.h_addr = hostaddr;
34726887Skjd 	*((u_long *)host.h_addr) = inet_addr(p);
34826887Skjd 	host.h_length = sizeof (u_long);
34926887Skjd 	host.h_addrtype = AF_INET;
35026887Skjd 	while (*cp == ' ' || *cp == '\t')
35126887Skjd 		cp++;
35226887Skjd 	host.h_name = cp;
35326887Skjd 	q = host.h_aliases = host_aliases;
35437108Sbostic 	cp = strpbrk(cp, " \t");
35526887Skjd 	if (cp != NULL)
35626887Skjd 		*cp++ = '\0';
35726887Skjd 	while (cp && *cp) {
35826887Skjd 		if (*cp == ' ' || *cp == '\t') {
35926887Skjd 			cp++;
36026887Skjd 			continue;
36126887Skjd 		}
36226887Skjd 		if (q < &host_aliases[MAXALIASES - 1])
36326887Skjd 			*q++ = cp;
36437108Sbostic 		cp = strpbrk(cp, " \t");
36526887Skjd 		if (cp != NULL)
36626887Skjd 			*cp++ = '\0';
36726887Skjd 	}
36826887Skjd 	*q = NULL;
36926887Skjd 	return (&host);
37026887Skjd }
37126887Skjd 
37226887Skjd struct hostent *
37326887Skjd _gethtbyname(name)
37426887Skjd 	char *name;
37526887Skjd {
37626887Skjd 	register struct hostent *p;
37726887Skjd 	register char **cp;
37828307Skarels 
37926887Skjd 	_sethtent(0);
38026887Skjd 	while (p = _gethtent()) {
38131960Sbostic 		if (strcasecmp(p->h_name, name) == 0)
38226887Skjd 			break;
38326887Skjd 		for (cp = p->h_aliases; *cp != 0; cp++)
38431960Sbostic 			if (strcasecmp(*cp, name) == 0)
38526887Skjd 				goto found;
38626887Skjd 	}
38726887Skjd found:
38826887Skjd 	_endhtent();
38926887Skjd 	return (p);
39026887Skjd }
39126887Skjd 
39226887Skjd struct hostent *
39326887Skjd _gethtbyaddr(addr, len, type)
39426887Skjd 	char *addr;
39526887Skjd 	int len, type;
39626887Skjd {
39726887Skjd 	register struct hostent *p;
39826887Skjd 
39926887Skjd 	_sethtent(0);
40026887Skjd 	while (p = _gethtent())
40126909Skjd 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
40226887Skjd 			break;
40326887Skjd 	_endhtent();
40426887Skjd 	return (p);
40526887Skjd }
406