gethost.c revision 1.6 1 /* $NetBSD: gethost.c,v 1.6 2001/04/28 14:56:42 kleink 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/cdefs.h>
62
63 #if defined(__weak_alias)
64 /* From namespace.h: */
65 #define gethostbyaddr _gethostbyaddr
66 #define gethostbyname _gethostbyname
67 #define gethostname _gethostname
68
69 __weak_alias(gethostbyaddr,_gethostbyaddr);
70 __weak_alias(gethostbyname,_gethostbyname);
71 __weak_alias(gethostbyname2,gethostbyname);
72 #endif
73
74 #include <sys/param.h>
75 #include <sys/socket.h>
76 #include <netinet/in.h>
77 #include <arpa/inet.h>
78 #include <arpa/nameser.h>
79 #include <netdb.h>
80 #include <resolv.h>
81 #include <stdio.h>
82 #include <ctype.h>
83 #include <errno.h>
84 #include <string.h>
85
86 #define MAXALIASES 35
87 #define MAXADDRS 35
88
89 static char *h_addr_ptrs[MAXADDRS + 1];
90
91 static struct hostent host;
92 static char *host_aliases[MAXALIASES];
93 static char hostbuf[BUFSIZ+1];
94 static struct in_addr host_addr;
95 static FILE *hostf = NULL;
96 static int stayopen = 0;
97
98 void _sethtent __P((int));
99 void _endhtent __P((void));
100 struct hostent *_gethtent __P((void));
101 struct hostent *_gethtbyname __P((const char *));
102 struct hostent *_gethtbyaddr __P((const char *, int, int));
103
104
105 #if PACKETSZ > 1024
106 #define MAXPACKET PACKETSZ
107 #else
108 #define MAXPACKET 1024
109 #endif
110
111 extern int h_errno;
112
113 struct hostent *
114 gethostbyname(name)
115 const char *name;
116 {
117 register const char *cp;
118
119 /*
120 * disallow names consisting only of digits/dots, unless
121 * they end in a dot.
122 */
123 if (isdigit(name[0]))
124 for (cp = name;; ++cp) {
125 if (!*cp) {
126 if (*--cp == '.')
127 break;
128 /*
129 * All-numeric, no dot at the end.
130 * Fake up a hostent as if we'd actually
131 * done a lookup.
132 */
133 if (!inet_aton(name, &host_addr)) {
134 h_errno = HOST_NOT_FOUND;
135 return((struct hostent *) NULL);
136 }
137 host.h_name = (char *)name;
138 host.h_aliases = host_aliases;
139 host_aliases[0] = NULL;
140 host.h_addrtype = AF_INET;
141 host.h_length = sizeof(u_int32_t);
142 h_addr_ptrs[0] = (char *)&host_addr;
143 h_addr_ptrs[1] = NULL;
144 host.h_addr_list = h_addr_ptrs;
145 return (&host);
146 }
147 if (!isdigit(*cp) && *cp != '.')
148 break;
149 }
150
151 /* XXX - Force host table lookup. */
152 return (_gethtbyname(name));
153 }
154
155 struct hostent *
156 gethostbyaddr(addr, len, type)
157 const char *addr;
158 socklen_t len;
159 int type;
160 {
161 char qbuf[MAXDNAME];
162
163 if (type != AF_INET)
164 return ((struct hostent *) NULL);
165 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
166 ((unsigned)addr[3] & 0xff),
167 ((unsigned)addr[2] & 0xff),
168 ((unsigned)addr[1] & 0xff),
169 ((unsigned)addr[0] & 0xff));
170
171 /* XXX - Force host table lookup. */
172 return (_gethtbyaddr(addr, len, type));
173 }
174
175 void
176 _sethtent(f)
177 int f;
178 {
179 if (hostf == NULL)
180 hostf = fopen(_PATH_HOSTS, "r" );
181 else
182 rewind(hostf);
183 stayopen = f;
184 }
185
186 void
187 _endhtent()
188 {
189 if (hostf && !stayopen) {
190 (void) fclose(hostf);
191 hostf = NULL;
192 }
193 }
194
195 struct hostent *
196 _gethtent()
197 {
198 char *p;
199 register char *cp, **q;
200
201 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
202 return (NULL);
203 again:
204 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
205 return (NULL);
206 if (*p == '#')
207 goto again;
208 cp = strpbrk(p, "#\n");
209 if (cp == NULL)
210 goto again;
211 *cp = '\0';
212 cp = strpbrk(p, " \t");
213 if (cp == NULL)
214 goto again;
215 *cp++ = '\0';
216 /* THIS STUFF IS INTERNET SPECIFIC */
217 h_addr_ptrs[0] = (char *)&host_addr;
218 h_addr_ptrs[1] = NULL;
219 (void) inet_aton(p, &host_addr);
220 host.h_addr_list = h_addr_ptrs;
221 host.h_length = sizeof(u_int32_t);
222 host.h_addrtype = AF_INET;
223 while (*cp == ' ' || *cp == '\t')
224 cp++;
225 host.h_name = cp;
226 q = host.h_aliases = host_aliases;
227 cp = strpbrk(cp, " \t");
228 if (cp != NULL)
229 *cp++ = '\0';
230 while (cp && *cp) {
231 if (*cp == ' ' || *cp == '\t') {
232 cp++;
233 continue;
234 }
235 if (q < &host_aliases[MAXALIASES - 1])
236 *q++ = cp;
237 cp = strpbrk(cp, " \t");
238 if (cp != NULL)
239 *cp++ = '\0';
240 }
241 *q = NULL;
242 return (&host);
243 }
244
245 struct hostent *
246 _gethtbyname(name)
247 const char *name;
248 {
249 register struct hostent *p;
250 register char **cp;
251
252 _sethtent(0);
253 while ((p = _gethtent()) != NULL) {
254 if (strcasecmp(p->h_name, name) == 0)
255 break;
256 for (cp = p->h_aliases; *cp != 0; cp++)
257 if (strcasecmp(*cp, name) == 0)
258 goto found;
259 }
260 found:
261 _endhtent();
262 if (p==NULL)
263 h_errno = HOST_NOT_FOUND;
264 return (p);
265 }
266
267 struct hostent *
268 _gethtbyaddr(addr, len, type)
269 const char *addr;
270 int len, type;
271 {
272 register struct hostent *p;
273
274 _sethtent(0);
275 while ((p = _gethtent()) != NULL)
276 if (p->h_addrtype == type && !memcmp(p->h_addr, addr, len))
277 break;
278 _endhtent();
279 if (p==NULL)
280 h_errno = HOST_NOT_FOUND;
281 return (p);
282 }
283
284