1 /* $NetBSD: gethost.c,v 1.7 2001/06/15 17:26:51 tsutsui Exp $ */
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * Copied from: lib/libc/net/gethostnamadr.c
54 * and then gutted, leaving only /etc/hosts support.
57 #include <sys/cdefs.h>
60 #define gethostbyaddr _gethostbyaddr
61 #define gethostbyname _gethostbyname
64 #include <sys/param.h>
65 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
77 __weak_alias(gethostbyaddr
,_gethostbyaddr
);
78 __weak_alias(gethostbyname
,_gethostbyname
);
84 static char *h_addr_ptrs
[MAXADDRS
+ 1];
86 static struct hostent host
;
87 static char *host_aliases
[MAXALIASES
];
88 static char hostbuf
[BUFSIZ
+1];
89 static struct in_addr host_addr
;
90 static FILE *hostf
= NULL
;
91 static int stayopen
= 0;
93 void _sethtent
__P((int));
94 void _endhtent
__P((void));
95 struct hostent
*_gethtent
__P((void));
96 struct hostent
*_gethtbyname
__P((const char *));
97 struct hostent
*_gethtbyaddr
__P((const char *, int, int));
101 #define MAXPACKET PACKETSZ
103 #define MAXPACKET 1024
112 register const char *cp
;
115 * disallow names consisting only of digits/dots, unless
118 if (isdigit(name
[0]))
119 for (cp
= name
;; ++cp
) {
124 * All-numeric, no dot at the end.
125 * Fake up a hostent as if we'd actually
128 if (!inet_aton(name
, &host_addr
)) {
129 h_errno
= HOST_NOT_FOUND
;
130 return((struct hostent
*) NULL
);
132 host
.h_name
= (char *)name
;
133 host
.h_aliases
= host_aliases
;
134 host_aliases
[0] = NULL
;
135 host
.h_addrtype
= AF_INET
;
136 host
.h_length
= sizeof(u_int32_t
);
137 h_addr_ptrs
[0] = (char *)&host_addr
;
138 h_addr_ptrs
[1] = NULL
;
139 host
.h_addr_list
= h_addr_ptrs
;
142 if (!isdigit(*cp
) && *cp
!= '.')
146 /* XXX - Force host table lookup. */
147 return (_gethtbyname(name
));
151 gethostbyaddr(addr
, len
, type
)
159 return ((struct hostent
*) NULL
);
160 (void)sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
161 ((unsigned)addr
[3] & 0xff),
162 ((unsigned)addr
[2] & 0xff),
163 ((unsigned)addr
[1] & 0xff),
164 ((unsigned)addr
[0] & 0xff));
166 /* XXX - Force host table lookup. */
167 return (_gethtbyaddr(addr
, len
, type
));
175 hostf
= fopen(_PATH_HOSTS
, "r" );
184 if (hostf
&& !stayopen
) {
185 (void) fclose(hostf
);
194 register char *cp
, **q
;
196 if (hostf
== NULL
&& (hostf
= fopen(_PATH_HOSTS
, "r" )) == NULL
)
199 if ((p
= fgets(hostbuf
, BUFSIZ
, hostf
)) == NULL
)
203 cp
= strpbrk(p
, "#\n");
207 cp
= strpbrk(p
, " \t");
211 /* THIS STUFF IS INTERNET SPECIFIC */
212 h_addr_ptrs
[0] = (char *)&host_addr
;
213 h_addr_ptrs
[1] = NULL
;
214 (void) inet_aton(p
, &host_addr
);
215 host
.h_addr_list
= h_addr_ptrs
;
216 host
.h_length
= sizeof(u_int32_t
);
217 host
.h_addrtype
= AF_INET
;
218 while (*cp
== ' ' || *cp
== '\t')
221 q
= host
.h_aliases
= host_aliases
;
222 cp
= strpbrk(cp
, " \t");
226 if (*cp
== ' ' || *cp
== '\t') {
230 if (q
< &host_aliases
[MAXALIASES
- 1])
232 cp
= strpbrk(cp
, " \t");
244 register struct hostent
*p
;
248 while ((p
= _gethtent()) != NULL
) {
249 if (strcasecmp(p
->h_name
, name
) == 0)
251 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
252 if (strcasecmp(*cp
, name
) == 0)
258 h_errno
= HOST_NOT_FOUND
;
263 _gethtbyaddr(addr
, len
, type
)
267 register struct hostent
*p
;
270 while ((p
= _gethtent()) != NULL
)
271 if (p
->h_addrtype
== type
&& !memcmp(p
->h_addr
, addr
, len
))
275 h_errno
= HOST_NOT_FOUND
;