xref: /freebsd/lib/libc/net/gethostbyht.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1 /*-
2  * Copyright (c) 1985, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  * -
33  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34  *
35  * Permission to use, copy, modify, and distribute this software for any
36  * purpose with or without fee is hereby granted, provided that the above
37  * copyright notice and this permission notice appear in all copies, and that
38  * the name of Digital Equipment Corporation not be used in advertising or
39  * publicity pertaining to distribution of the document or software without
40  * specific, written prior permission.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
45  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49  * SOFTWARE.
50  * -
51  * --Copyright--
52  */
53 
54 #if defined(LIBC_SCCS) && !defined(lint)
55 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
56 static char rcsid[] = "$Id: gethostnamadr.c,v 1.1 1994/05/27 04:57:16 rgrimes Exp $";
57 #endif /* LIBC_SCCS and not lint */
58 
59 #include <sys/param.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <netdb.h>
64 #include <stdio.h>
65 #include <ctype.h>
66 #include <errno.h>
67 #include <string.h>
68 
69 #define	MAXALIASES	35
70 #define	MAXADDRS	35
71 
72 static struct hostent host;
73 static char *host_aliases[MAXALIASES];
74 static char hostbuf[BUFSIZ+1];
75 static FILE *hostf = NULL;
76 static char hostaddr[MAXADDRS];
77 static char *host_addrs[2];
78 static int stayopen = 0;
79 
80 void
81 _sethosthtent(f)
82 	int f;
83 {
84 	if (hostf == NULL)
85 		hostf = fopen(_PATH_HOSTS, "r" );
86 	else
87 		rewind(hostf);
88 	stayopen |= f;
89 }
90 
91 void
92 _endhosthtent()
93 {
94 	if (hostf && !stayopen) {
95 		(void) fclose(hostf);
96 		hostf = NULL;
97 	}
98 }
99 
100 struct hostent *
101 gethostent()
102 {
103 	char *p;
104 	register char *cp, **q;
105 
106 	if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
107 		return (NULL);
108 again:
109 	if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
110 		return (NULL);
111 	if (*p == '#')
112 		goto again;
113 	cp = strpbrk(p, "#\n");
114 	if (cp == NULL)
115 		goto again;
116 	*cp = '\0';
117 	cp = strpbrk(p, " \t");
118 	if (cp == NULL)
119 		goto again;
120 	*cp++ = '\0';
121 	/* THIS STUFF IS INTERNET SPECIFIC */
122 	host.h_addr_list = host_addrs;
123 	host.h_addr = hostaddr;
124 	*((u_int32_t *)host.h_addr) = inet_addr(p);
125 	host.h_length = sizeof (u_int32_t);
126 	host.h_addrtype = AF_INET;
127 	while (*cp == ' ' || *cp == '\t')
128 		cp++;
129 	host.h_name = cp;
130 	q = host.h_aliases = host_aliases;
131 	cp = strpbrk(cp, " \t");
132 	if (cp != NULL)
133 		*cp++ = '\0';
134 	while (cp && *cp) {
135 		if (*cp == ' ' || *cp == '\t') {
136 			cp++;
137 			continue;
138 		}
139 		if (q < &host_aliases[MAXALIASES - 1])
140 			*q++ = cp;
141 		cp = strpbrk(cp, " \t");
142 		if (cp != NULL)
143 			*cp++ = '\0';
144 	}
145 	*q = NULL;
146 	return (&host);
147 }
148 
149 struct hostent *
150 _gethostbyhtname(name)
151 	char *name;
152 {
153 	register struct hostent *p;
154 	register char **cp;
155 
156 	sethostent(0);
157 	while ((p = gethostent())) {
158 		if (strcasecmp(p->h_name, name) == 0)
159 			break;
160 		for (cp = p->h_aliases; *cp != 0; cp++)
161 			if (strcasecmp(*cp, name) == 0)
162 				goto found;
163 	}
164 found:
165 	endhostent();
166 	return (p);
167 }
168 
169 struct hostent *
170 _gethostbyhtaddr(addr, len, type)
171 	const char *addr;
172 	int len, type;
173 {
174 	register struct hostent *p;
175 
176 	sethostent(0);
177 	while ((p = gethostent()))
178 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
179 			break;
180 	endhostent();
181 	return (p);
182 }
183