1 /*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Portions copyright (c) 1999, 2000
6 * Intel Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 *
23 * This product includes software developed by the University of
24 * California, Berkeley, Intel Corporation, and its contributors.
25 *
26 * 4. Neither the name of University, Intel Corporation, or their respective
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
43 *
44 * Permission to use, copy, modify, and distribute this software for any
45 * purpose with or without fee is hereby granted, provided that the above
46 * copyright notice and this permission notice appear in all copies, and that
47 * the name of Digital Equipment Corporation not be used in advertising or
48 * publicity pertaining to distribution of the document or software without
49 * specific, written prior permission.
50 *
51 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
52 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
54 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
55 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
56 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
57 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
58 * SOFTWARE.
59 * -
60 * --Copyright--
61 */
62
63 #if defined(LIBC_SCCS) && !defined(lint)
64 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
65 static char rcsid[] = "$Id: gethostbyht.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";
66 #endif /* LIBC_SCCS and not lint */
67
68 #include <sys/param.h>
69 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
72 #include <netdb.h>
73 #include <stdio.h>
74 #include <ctype.h>
75 #include <errno.h>
76 #include <string.h>
77 #include <arpa/nameser.h> /* XXX */
78 #include <resolv.h> /* XXX */
79 #include "Socklib_internals.h"
80
81 #define MAXALIASES 35
82
83 static struct hostent host;
84 static char *host_aliases[MAXALIASES];
85 static char hostbuf[BUFSIZ+1];
86 static FILE *hostf = NULL;
87 static u_char host_addr[16]; /* IPv4 or IPv6 */
88 static char *h_addr_ptrs[2];
89 static int stayopen = 0;
90
91 void
_sethosthtent(int f)92 _sethosthtent(int f)
93 {
94 if (!hostf)
95 hostf = fopen(_PATH_HOSTS, "r" );
96 else
97 rewind(hostf);
98 stayopen = f;
99 }
100
101 void
_endhosthtent()102 _endhosthtent()
103 {
104 if (hostf && !stayopen) {
105 (void) fclose(hostf);
106 hostf = NULL;
107 }
108 }
109
110 struct hostent *
gethostent()111 gethostent()
112 {
113 char *p;
114 register char *cp, **q;
115 int af, len;
116
117 if (!hostf && ( NULL == (hostf = fopen(_PATH_HOSTS, "r" )))) {
118 h_errno = NETDB_INTERNAL;
119 return (NULL);
120 }
121 again:
122 if ( NULL == (p = fgets(hostbuf, sizeof hostbuf, hostf))) {
123 h_errno = HOST_NOT_FOUND;
124 return (NULL);
125 }
126 if (*p == '#')
127 goto again;
128 if ( NULL == (cp = strpbrk(p, "#\n")))
129 goto again;
130 *cp = '\0';
131 if ( NULL == (cp = strpbrk(p, " \t")))
132 goto again;
133 *cp++ = '\0';
134 if (inet_pton(AF_INET6, p, host_addr) > 0) {
135 af = AF_INET6;
136 len = IN6ADDRSZ;
137 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
138 if (_res.options & RES_USE_INET6) {
139 _map_v4v6_address((char*)host_addr, (char*)host_addr);
140 af = AF_INET6;
141 len = IN6ADDRSZ;
142 } else {
143 af = AF_INET;
144 len = INADDRSZ;
145 }
146 } else {
147 goto again;
148 }
149 h_addr_ptrs[0] = (char *)host_addr;
150 h_addr_ptrs[1] = NULL;
151 host.h_addr_list = h_addr_ptrs;
152 host.h_length = len;
153 host.h_addrtype = af;
154 while (*cp == ' ' || *cp == '\t')
155 cp++;
156 host.h_name = cp;
157 q = host.h_aliases = host_aliases;
158 if ((cp = strpbrk(cp, " \t\r")) != NULL)
159 *cp++ = '\0';
160 while (cp && *cp) {
161 if (*cp == ' ' || *cp == '\t') {
162 cp++;
163 continue;
164 }
165 if (q < &host_aliases[MAXALIASES - 1])
166 *q++ = cp;
167 if ((cp = strpbrk(cp, " \t\r")) != NULL)
168 *cp++ = '\0';
169 }
170 *q = NULL;
171 h_errno = NETDB_SUCCESS;
172 return (&host);
173 }
174
175 struct hostent *
_gethostbyhtname(const char * name,int af)176 _gethostbyhtname(const char *name, int af)
177 {
178 register struct hostent *p;
179 register char **cp;
180
181 sethostent(0);
182 while ((p = gethostent()) != NULL) {
183 if (p->h_addrtype != af)
184 continue;
185 if (strcasecmp(p->h_name, name) == 0)
186 break;
187 for (cp = p->h_aliases; *cp != 0; cp++)
188 if (strcasecmp(*cp, name) == 0)
189 goto found;
190 }
191 found:
192 endhostent();
193 return (p);
194 }
195
196 struct hostent *
_gethostbyhtaddr(const char * addr,int len,int af)197 _gethostbyhtaddr(const char *addr, int len, int af)
198 {
199 register struct hostent *p;
200
201 sethostent(0);
202 while ((p = gethostent()) != NULL)
203 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
204 break;
205 endhostent();
206 return (p);
207 }
208