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