gethost.c revision 1.4 1 /* $NetBSD: gethost.c,v 1.4 1999/03/13 19:08:44 sommerfe Exp $ */
2
3 /*-
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 * -
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36 *
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
43 *
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 * -
53 * --Copyright--
54 */
55
56 /*
57 * Copied from: lib/libc/net/gethostnamadr.c
58 * and then gutted, leaving only /etc/hosts support.
59 */
60
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
66 #include <netdb.h>
67 #include <resolv.h>
68 #include <stdio.h>
69 #include <ctype.h>
70 #include <errno.h>
71 #include <string.h>
72
73 #define MAXALIASES 35
74 #define MAXADDRS 35
75
76 static char *h_addr_ptrs[MAXADDRS + 1];
77
78 static struct hostent host;
79 static char *host_aliases[MAXALIASES];
80 static char hostbuf[BUFSIZ+1];
81 static struct in_addr host_addr;
82 static FILE *hostf = NULL;
83 static int stayopen = 0;
84
85 void _sethtent __P((int));
86 void _endhtent __P((void));
87 struct hostent *_gethtent __P((void));
88 struct hostent *_gethtbyname __P((const char *));
89 struct hostent *_gethtbyaddr __P((const char *, int, int));
90
91
92 #if PACKETSZ > 1024
93 #define MAXPACKET PACKETSZ
94 #else
95 #define MAXPACKET 1024
96 #endif
97
98 extern int h_errno;
99
100 struct hostent *
101 gethostbyname(name)
102 const char *name;
103 {
104 register const char *cp;
105
106 /*
107 * disallow names consisting only of digits/dots, unless
108 * they end in a dot.
109 */
110 if (isdigit(name[0]))
111 for (cp = name;; ++cp) {
112 if (!*cp) {
113 if (*--cp == '.')
114 break;
115 /*
116 * All-numeric, no dot at the end.
117 * Fake up a hostent as if we'd actually
118 * done a lookup.
119 */
120 if (!inet_aton(name, &host_addr)) {
121 h_errno = HOST_NOT_FOUND;
122 return((struct hostent *) NULL);
123 }
124 host.h_name = (char *)name;
125 host.h_aliases = host_aliases;
126 host_aliases[0] = NULL;
127 host.h_addrtype = AF_INET;
128 host.h_length = sizeof(u_int32_t);
129 h_addr_ptrs[0] = (char *)&host_addr;
130 h_addr_ptrs[1] = NULL;
131 host.h_addr_list = h_addr_ptrs;
132 return (&host);
133 }
134 if (!isdigit(*cp) && *cp != '.')
135 break;
136 }
137
138 /* XXX - Force host table lookup. */
139 return (_gethtbyname(name));
140 }
141
142 struct hostent *
143 gethostbyaddr(addr, len, type)
144 const char *addr;
145 int len, type;
146 {
147 char qbuf[MAXDNAME];
148
149 if (type != AF_INET)
150 return ((struct hostent *) NULL);
151 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
152 ((unsigned)addr[3] & 0xff),
153 ((unsigned)addr[2] & 0xff),
154 ((unsigned)addr[1] & 0xff),
155 ((unsigned)addr[0] & 0xff));
156
157 /* XXX - Force host table lookup. */
158 return (_gethtbyaddr(addr, len, type));
159 }
160
161 void
162 _sethtent(f)
163 int f;
164 {
165 if (hostf == NULL)
166 hostf = fopen(_PATH_HOSTS, "r" );
167 else
168 rewind(hostf);
169 stayopen = f;
170 }
171
172 void
173 _endhtent()
174 {
175 if (hostf && !stayopen) {
176 (void) fclose(hostf);
177 hostf = NULL;
178 }
179 }
180
181 struct hostent *
182 _gethtent()
183 {
184 char *p;
185 register char *cp, **q;
186
187 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
188 return (NULL);
189 again:
190 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
191 return (NULL);
192 if (*p == '#')
193 goto again;
194 cp = strpbrk(p, "#\n");
195 if (cp == NULL)
196 goto again;
197 *cp = '\0';
198 cp = strpbrk(p, " \t");
199 if (cp == NULL)
200 goto again;
201 *cp++ = '\0';
202 /* THIS STUFF IS INTERNET SPECIFIC */
203 h_addr_ptrs[0] = (char *)&host_addr;
204 h_addr_ptrs[1] = NULL;
205 (void) inet_aton(p, &host_addr);
206 host.h_addr_list = h_addr_ptrs;
207 host.h_length = sizeof(u_int32_t);
208 host.h_addrtype = AF_INET;
209 while (*cp == ' ' || *cp == '\t')
210 cp++;
211 host.h_name = cp;
212 q = host.h_aliases = host_aliases;
213 cp = strpbrk(cp, " \t");
214 if (cp != NULL)
215 *cp++ = '\0';
216 while (cp && *cp) {
217 if (*cp == ' ' || *cp == '\t') {
218 cp++;
219 continue;
220 }
221 if (q < &host_aliases[MAXALIASES - 1])
222 *q++ = cp;
223 cp = strpbrk(cp, " \t");
224 if (cp != NULL)
225 *cp++ = '\0';
226 }
227 *q = NULL;
228 return (&host);
229 }
230
231 struct hostent *
232 _gethtbyname(name)
233 const char *name;
234 {
235 register struct hostent *p;
236 register char **cp;
237
238 _sethtent(0);
239 while ((p = _gethtent()) != NULL) {
240 if (strcasecmp(p->h_name, name) == 0)
241 break;
242 for (cp = p->h_aliases; *cp != 0; cp++)
243 if (strcasecmp(*cp, name) == 0)
244 goto found;
245 }
246 found:
247 _endhtent();
248 if (p==NULL)
249 h_errno = HOST_NOT_FOUND;
250 return (p);
251 }
252
253 struct hostent *
254 _gethtbyaddr(addr, len, type)
255 const char *addr;
256 int len, type;
257 {
258 register struct hostent *p;
259
260 _sethtent(0);
261 while ((p = _gethtent()) != NULL)
262 if (p->h_addrtype == type && !memcmp(p->h_addr, addr, len))
263 break;
264 _endhtent();
265 if (p==NULL)
266 h_errno = HOST_NOT_FOUND;
267 return (p);
268 }
269
270