gethost.c revision 1.1 1 /* $NetBSD: gethost.c,v 1.1 1995/10/08 23:08:48 gwr 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 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <arpa/nameser.h>
62 #include <netdb.h>
63 #include <resolv.h>
64 #include <stdio.h>
65 #include <ctype.h>
66 #include <errno.h>
67 #include <string.h>
68
69
70 #define MAXALIASES 35
71 #define MAXADDRS 35
72
73 static char *h_addr_ptrs[MAXADDRS + 1];
74
75
76 static struct hostent host;
77 static char *host_aliases[MAXALIASES];
78 static char hostbuf[BUFSIZ+1];
79 static struct in_addr host_addr;
80 static FILE *hostf = NULL;
81 static int stayopen = 0;
82
83 #if PACKETSZ > 1024
84 #define MAXPACKET PACKETSZ
85 #else
86 #define MAXPACKET 1024
87 #endif
88
89 extern int h_errno;
90
91
92 struct hostent *
93 gethostbyname(name)
94 const char *name;
95 {
96 register const char *cp;
97 int n, i;
98 extern struct hostent *_gethtbyname();
99 register struct hostent *hp;
100 char lookups[MAXDNSLUS];
101
102 /*
103 * disallow names consisting only of digits/dots, unless
104 * they end in a dot.
105 */
106 if (isdigit(name[0]))
107 for (cp = name;; ++cp) {
108 if (!*cp) {
109 if (*--cp == '.')
110 break;
111 /*
112 * All-numeric, no dot at the end.
113 * Fake up a hostent as if we'd actually
114 * done a lookup.
115 */
116 if (!inet_aton(name, &host_addr)) {
117 h_errno = HOST_NOT_FOUND;
118 return((struct hostent *) NULL);
119 }
120 host.h_name = (char *)name;
121 host.h_aliases = host_aliases;
122 host_aliases[0] = NULL;
123 host.h_addrtype = AF_INET;
124 host.h_length = sizeof(u_int32_t);
125 h_addr_ptrs[0] = (char *)&host_addr;
126 h_addr_ptrs[1] = NULL;
127 host.h_addr_list = h_addr_ptrs;
128 return (&host);
129 }
130 if (!isdigit(*cp) && *cp != '.')
131 break;
132 }
133
134 /* XXX - Force host table lookup. */
135 return (_gethtbyname(name));
136 }
137
138 struct hostent *
139 gethostbyaddr(addr, len, type)
140 const char *addr;
141 int len, type;
142 {
143 int n, i;
144 register struct hostent *hp;
145 char qbuf[MAXDNAME];
146 extern struct hostent *_gethtbyaddr();
147 char lookups[MAXDNSLUS];
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 char *name;
234 {
235 register struct hostent *p;
236 register char **cp;
237
238 _sethtent(0);
239 while (p = _gethtent()) {
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())
262 if (p->h_addrtype == type && !bcmp(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