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