lcl_ho.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 jym /* $NetBSD: lcl_ho.c,v 1.1.1.1.2.2 2009/05/13 18:52:11 jym Exp $ */
2 1.1.1.1.2.2 jym
3 1.1.1.1.2.2 jym /*
4 1.1.1.1.2.2 jym * Copyright (c) 1985, 1988, 1993
5 1.1.1.1.2.2 jym * The Regents of the University of California. All rights reserved.
6 1.1.1.1.2.2 jym *
7 1.1.1.1.2.2 jym * Redistribution and use in source and binary forms, with or without
8 1.1.1.1.2.2 jym * modification, are permitted provided that the following conditions
9 1.1.1.1.2.2 jym * are met:
10 1.1.1.1.2.2 jym * 1. Redistributions of source code must retain the above copyright
11 1.1.1.1.2.2 jym * notice, this list of conditions and the following disclaimer.
12 1.1.1.1.2.2 jym * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.1.1.2.2 jym * notice, this list of conditions and the following disclaimer in the
14 1.1.1.1.2.2 jym * documentation and/or other materials provided with the distribution.
15 1.1.1.1.2.2 jym * 3. All advertising materials mentioning features or use of this software
16 1.1.1.1.2.2 jym * must display the following acknowledgement:
17 1.1.1.1.2.2 jym * This product includes software developed by the University of
18 1.1.1.1.2.2 jym * California, Berkeley and its contributors.
19 1.1.1.1.2.2 jym * 4. Neither the name of the University nor the names of its contributors
20 1.1.1.1.2.2 jym * may be used to endorse or promote products derived from this software
21 1.1.1.1.2.2 jym * without specific prior written permission.
22 1.1.1.1.2.2 jym *
23 1.1.1.1.2.2 jym * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1.1.1.2.2 jym * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1.1.1.2.2 jym * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1.1.1.2.2 jym * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1.1.1.2.2 jym * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1.1.1.2.2 jym * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1.1.1.2.2 jym * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1.1.1.2.2 jym * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1.1.1.2.2 jym * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1.1.1.2.2 jym * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1.1.1.2.2 jym * SUCH DAMAGE.
34 1.1.1.1.2.2 jym */
35 1.1.1.1.2.2 jym
36 1.1.1.1.2.2 jym /*
37 1.1.1.1.2.2 jym * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
38 1.1.1.1.2.2 jym * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
39 1.1.1.1.2.2 jym *
40 1.1.1.1.2.2 jym * Permission to use, copy, modify, and distribute this software for any
41 1.1.1.1.2.2 jym * purpose with or without fee is hereby granted, provided that the above
42 1.1.1.1.2.2 jym * copyright notice and this permission notice appear in all copies.
43 1.1.1.1.2.2 jym *
44 1.1.1.1.2.2 jym * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
45 1.1.1.1.2.2 jym * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
46 1.1.1.1.2.2 jym * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
47 1.1.1.1.2.2 jym * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
48 1.1.1.1.2.2 jym * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
49 1.1.1.1.2.2 jym * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
50 1.1.1.1.2.2 jym * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 1.1.1.1.2.2 jym */
52 1.1.1.1.2.2 jym
53 1.1.1.1.2.2 jym /* from gethostnamadr.c 8.1 (Berkeley) 6/4/93 */
54 1.1.1.1.2.2 jym /* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */
55 1.1.1.1.2.2 jym
56 1.1.1.1.2.2 jym #if defined(LIBC_SCCS) && !defined(lint)
57 1.1.1.1.2.2 jym static const char rcsid[] = "Id: lcl_ho.c,v 1.5 2006/03/09 23:57:56 marka Exp";
58 1.1.1.1.2.2 jym #endif /* LIBC_SCCS and not lint */
59 1.1.1.1.2.2 jym
60 1.1.1.1.2.2 jym /* Imports. */
61 1.1.1.1.2.2 jym
62 1.1.1.1.2.2 jym #include "port_before.h"
63 1.1.1.1.2.2 jym
64 1.1.1.1.2.2 jym #include <sys/types.h>
65 1.1.1.1.2.2 jym #include <sys/param.h>
66 1.1.1.1.2.2 jym #include <sys/socket.h>
67 1.1.1.1.2.2 jym
68 1.1.1.1.2.2 jym #include <netinet/in.h>
69 1.1.1.1.2.2 jym #include <arpa/inet.h>
70 1.1.1.1.2.2 jym #include <arpa/nameser.h>
71 1.1.1.1.2.2 jym
72 1.1.1.1.2.2 jym #include <ctype.h>
73 1.1.1.1.2.2 jym #include <errno.h>
74 1.1.1.1.2.2 jym #include <fcntl.h>
75 1.1.1.1.2.2 jym #include <netdb.h>
76 1.1.1.1.2.2 jym #include <resolv.h>
77 1.1.1.1.2.2 jym #include <stdio.h>
78 1.1.1.1.2.2 jym #include <stdlib.h>
79 1.1.1.1.2.2 jym #include <string.h>
80 1.1.1.1.2.2 jym
81 1.1.1.1.2.2 jym #include <irs.h>
82 1.1.1.1.2.2 jym #include <isc/memcluster.h>
83 1.1.1.1.2.2 jym
84 1.1.1.1.2.2 jym #include "port_after.h"
85 1.1.1.1.2.2 jym
86 1.1.1.1.2.2 jym #include "irs_p.h"
87 1.1.1.1.2.2 jym #include "dns_p.h"
88 1.1.1.1.2.2 jym #include "lcl_p.h"
89 1.1.1.1.2.2 jym
90 1.1.1.1.2.2 jym #ifdef SPRINTF_CHAR
91 1.1.1.1.2.2 jym # define SPRINTF(x) strlen(sprintf/**/x)
92 1.1.1.1.2.2 jym #else
93 1.1.1.1.2.2 jym # define SPRINTF(x) sprintf x
94 1.1.1.1.2.2 jym #endif
95 1.1.1.1.2.2 jym
96 1.1.1.1.2.2 jym /* Definitions. */
97 1.1.1.1.2.2 jym
98 1.1.1.1.2.2 jym #define MAXALIASES 35
99 1.1.1.1.2.2 jym #define MAXADDRS 35
100 1.1.1.1.2.2 jym #define Max(a,b) ((a) > (b) ? (a) : (b))
101 1.1.1.1.2.2 jym
102 1.1.1.1.2.2 jym #if PACKETSZ > 1024
103 1.1.1.1.2.2 jym #define MAXPACKET PACKETSZ
104 1.1.1.1.2.2 jym #else
105 1.1.1.1.2.2 jym #define MAXPACKET 1024
106 1.1.1.1.2.2 jym #endif
107 1.1.1.1.2.2 jym
108 1.1.1.1.2.2 jym struct pvt {
109 1.1.1.1.2.2 jym FILE * fp;
110 1.1.1.1.2.2 jym struct hostent host;
111 1.1.1.1.2.2 jym char * h_addr_ptrs[MAXADDRS + 1];
112 1.1.1.1.2.2 jym char * host_aliases[MAXALIASES];
113 1.1.1.1.2.2 jym char hostbuf[8*1024];
114 1.1.1.1.2.2 jym u_char host_addr[16]; /*%< IPv4 or IPv6 */
115 1.1.1.1.2.2 jym struct __res_state *res;
116 1.1.1.1.2.2 jym void (*free_res)(void *);
117 1.1.1.1.2.2 jym };
118 1.1.1.1.2.2 jym
119 1.1.1.1.2.2 jym typedef union {
120 1.1.1.1.2.2 jym int32_t al;
121 1.1.1.1.2.2 jym char ac;
122 1.1.1.1.2.2 jym } align;
123 1.1.1.1.2.2 jym
124 1.1.1.1.2.2 jym static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
125 1.1.1.1.2.2 jym static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
126 1.1.1.1.2.2 jym
127 1.1.1.1.2.2 jym /* Forward. */
128 1.1.1.1.2.2 jym
129 1.1.1.1.2.2 jym static void ho_close(struct irs_ho *this);
130 1.1.1.1.2.2 jym static struct hostent * ho_byname(struct irs_ho *this, const char *name);
131 1.1.1.1.2.2 jym static struct hostent * ho_byname2(struct irs_ho *this, const char *name,
132 1.1.1.1.2.2 jym int af);
133 1.1.1.1.2.2 jym static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
134 1.1.1.1.2.2 jym int len, int af);
135 1.1.1.1.2.2 jym static struct hostent * ho_next(struct irs_ho *this);
136 1.1.1.1.2.2 jym static void ho_rewind(struct irs_ho *this);
137 1.1.1.1.2.2 jym static void ho_minimize(struct irs_ho *this);
138 1.1.1.1.2.2 jym static struct __res_state * ho_res_get(struct irs_ho *this);
139 1.1.1.1.2.2 jym static void ho_res_set(struct irs_ho *this,
140 1.1.1.1.2.2 jym struct __res_state *res,
141 1.1.1.1.2.2 jym void (*free_res)(void *));
142 1.1.1.1.2.2 jym static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
143 1.1.1.1.2.2 jym const struct addrinfo *pai);
144 1.1.1.1.2.2 jym
145 1.1.1.1.2.2 jym static size_t ns_namelen(const char *);
146 1.1.1.1.2.2 jym static int init(struct irs_ho *this);
147 1.1.1.1.2.2 jym
148 1.1.1.1.2.2 jym /* Portability. */
149 1.1.1.1.2.2 jym
150 1.1.1.1.2.2 jym #ifndef SEEK_SET
151 1.1.1.1.2.2 jym # define SEEK_SET 0
152 1.1.1.1.2.2 jym #endif
153 1.1.1.1.2.2 jym
154 1.1.1.1.2.2 jym /* Public. */
155 1.1.1.1.2.2 jym
156 1.1.1.1.2.2 jym struct irs_ho *
157 1.1.1.1.2.2 jym irs_lcl_ho(struct irs_acc *this) {
158 1.1.1.1.2.2 jym struct irs_ho *ho;
159 1.1.1.1.2.2 jym struct pvt *pvt;
160 1.1.1.1.2.2 jym
161 1.1.1.1.2.2 jym UNUSED(this);
162 1.1.1.1.2.2 jym
163 1.1.1.1.2.2 jym if (!(pvt = memget(sizeof *pvt))) {
164 1.1.1.1.2.2 jym errno = ENOMEM;
165 1.1.1.1.2.2 jym return (NULL);
166 1.1.1.1.2.2 jym }
167 1.1.1.1.2.2 jym memset(pvt, 0, sizeof *pvt);
168 1.1.1.1.2.2 jym if (!(ho = memget(sizeof *ho))) {
169 1.1.1.1.2.2 jym memput(pvt, sizeof *pvt);
170 1.1.1.1.2.2 jym errno = ENOMEM;
171 1.1.1.1.2.2 jym return (NULL);
172 1.1.1.1.2.2 jym }
173 1.1.1.1.2.2 jym memset(ho, 0x5e, sizeof *ho);
174 1.1.1.1.2.2 jym ho->private = pvt;
175 1.1.1.1.2.2 jym ho->close = ho_close;
176 1.1.1.1.2.2 jym ho->byname = ho_byname;
177 1.1.1.1.2.2 jym ho->byname2 = ho_byname2;
178 1.1.1.1.2.2 jym ho->byaddr = ho_byaddr;
179 1.1.1.1.2.2 jym ho->next = ho_next;
180 1.1.1.1.2.2 jym ho->rewind = ho_rewind;
181 1.1.1.1.2.2 jym ho->minimize = ho_minimize;
182 1.1.1.1.2.2 jym ho->res_get = ho_res_get;
183 1.1.1.1.2.2 jym ho->res_set = ho_res_set;
184 1.1.1.1.2.2 jym ho->addrinfo = ho_addrinfo;
185 1.1.1.1.2.2 jym return (ho);
186 1.1.1.1.2.2 jym }
187 1.1.1.1.2.2 jym
188 1.1.1.1.2.2 jym /* Methods. */
189 1.1.1.1.2.2 jym
190 1.1.1.1.2.2 jym static void
191 1.1.1.1.2.2 jym ho_close(struct irs_ho *this) {
192 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
193 1.1.1.1.2.2 jym
194 1.1.1.1.2.2 jym ho_minimize(this);
195 1.1.1.1.2.2 jym if (pvt->fp)
196 1.1.1.1.2.2 jym (void) fclose(pvt->fp);
197 1.1.1.1.2.2 jym if (pvt->res && pvt->free_res)
198 1.1.1.1.2.2 jym (*pvt->free_res)(pvt->res);
199 1.1.1.1.2.2 jym memput(pvt, sizeof *pvt);
200 1.1.1.1.2.2 jym memput(this, sizeof *this);
201 1.1.1.1.2.2 jym }
202 1.1.1.1.2.2 jym
203 1.1.1.1.2.2 jym static struct hostent *
204 1.1.1.1.2.2 jym ho_byname(struct irs_ho *this, const char *name) {
205 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
206 1.1.1.1.2.2 jym struct hostent *hp;
207 1.1.1.1.2.2 jym
208 1.1.1.1.2.2 jym if (init(this) == -1)
209 1.1.1.1.2.2 jym return (NULL);
210 1.1.1.1.2.2 jym
211 1.1.1.1.2.2 jym if (pvt->res->options & RES_USE_INET6) {
212 1.1.1.1.2.2 jym hp = ho_byname2(this, name, AF_INET6);
213 1.1.1.1.2.2 jym if (hp)
214 1.1.1.1.2.2 jym return (hp);
215 1.1.1.1.2.2 jym }
216 1.1.1.1.2.2 jym return (ho_byname2(this, name, AF_INET));
217 1.1.1.1.2.2 jym }
218 1.1.1.1.2.2 jym
219 1.1.1.1.2.2 jym static struct hostent *
220 1.1.1.1.2.2 jym ho_byname2(struct irs_ho *this, const char *name, int af) {
221 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
222 1.1.1.1.2.2 jym struct hostent *hp;
223 1.1.1.1.2.2 jym char **hap;
224 1.1.1.1.2.2 jym size_t n;
225 1.1.1.1.2.2 jym
226 1.1.1.1.2.2 jym if (init(this) == -1)
227 1.1.1.1.2.2 jym return (NULL);
228 1.1.1.1.2.2 jym
229 1.1.1.1.2.2 jym ho_rewind(this);
230 1.1.1.1.2.2 jym n = ns_namelen(name);
231 1.1.1.1.2.2 jym while ((hp = ho_next(this)) != NULL) {
232 1.1.1.1.2.2 jym size_t nn;
233 1.1.1.1.2.2 jym
234 1.1.1.1.2.2 jym if (hp->h_addrtype != af)
235 1.1.1.1.2.2 jym continue;
236 1.1.1.1.2.2 jym nn = ns_namelen(hp->h_name);
237 1.1.1.1.2.2 jym if (strncasecmp(hp->h_name, name, Max(n, nn)) == 0)
238 1.1.1.1.2.2 jym goto found;
239 1.1.1.1.2.2 jym for (hap = hp->h_aliases; *hap; hap++) {
240 1.1.1.1.2.2 jym nn = ns_namelen(*hap);
241 1.1.1.1.2.2 jym if (strncasecmp(*hap, name, Max(n, nn)) == 0)
242 1.1.1.1.2.2 jym goto found;
243 1.1.1.1.2.2 jym }
244 1.1.1.1.2.2 jym }
245 1.1.1.1.2.2 jym found:
246 1.1.1.1.2.2 jym if (!hp) {
247 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
248 1.1.1.1.2.2 jym return (NULL);
249 1.1.1.1.2.2 jym }
250 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
251 1.1.1.1.2.2 jym return (hp);
252 1.1.1.1.2.2 jym }
253 1.1.1.1.2.2 jym
254 1.1.1.1.2.2 jym static struct hostent *
255 1.1.1.1.2.2 jym ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
256 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
257 1.1.1.1.2.2 jym const u_char *uaddr = addr;
258 1.1.1.1.2.2 jym struct hostent *hp;
259 1.1.1.1.2.2 jym int size;
260 1.1.1.1.2.2 jym
261 1.1.1.1.2.2 jym if (init(this) == -1)
262 1.1.1.1.2.2 jym return (NULL);
263 1.1.1.1.2.2 jym
264 1.1.1.1.2.2 jym if (af == AF_INET6 && len == IN6ADDRSZ &&
265 1.1.1.1.2.2 jym (!memcmp(uaddr, mapped, sizeof mapped) ||
266 1.1.1.1.2.2 jym !memcmp(uaddr, tunnelled, sizeof tunnelled))) {
267 1.1.1.1.2.2 jym /* Unmap. */
268 1.1.1.1.2.2 jym addr = (const u_char *)addr + sizeof mapped;
269 1.1.1.1.2.2 jym uaddr += sizeof mapped;
270 1.1.1.1.2.2 jym af = AF_INET;
271 1.1.1.1.2.2 jym len = INADDRSZ;
272 1.1.1.1.2.2 jym }
273 1.1.1.1.2.2 jym switch (af) {
274 1.1.1.1.2.2 jym case AF_INET:
275 1.1.1.1.2.2 jym size = INADDRSZ;
276 1.1.1.1.2.2 jym break;
277 1.1.1.1.2.2 jym case AF_INET6:
278 1.1.1.1.2.2 jym size = IN6ADDRSZ;
279 1.1.1.1.2.2 jym break;
280 1.1.1.1.2.2 jym default:
281 1.1.1.1.2.2 jym errno = EAFNOSUPPORT;
282 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
283 1.1.1.1.2.2 jym return (NULL);
284 1.1.1.1.2.2 jym }
285 1.1.1.1.2.2 jym if (size > len) {
286 1.1.1.1.2.2 jym errno = EINVAL;
287 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
288 1.1.1.1.2.2 jym return (NULL);
289 1.1.1.1.2.2 jym }
290 1.1.1.1.2.2 jym
291 1.1.1.1.2.2 jym /*
292 1.1.1.1.2.2 jym * Do the search.
293 1.1.1.1.2.2 jym */
294 1.1.1.1.2.2 jym ho_rewind(this);
295 1.1.1.1.2.2 jym while ((hp = ho_next(this)) != NULL) {
296 1.1.1.1.2.2 jym char **hap;
297 1.1.1.1.2.2 jym
298 1.1.1.1.2.2 jym for (hap = hp->h_addr_list; *hap; hap++) {
299 1.1.1.1.2.2 jym const u_char *taddr = (const u_char *)*hap;
300 1.1.1.1.2.2 jym int taf = hp->h_addrtype;
301 1.1.1.1.2.2 jym int tlen = hp->h_length;
302 1.1.1.1.2.2 jym
303 1.1.1.1.2.2 jym if (taf == AF_INET6 && tlen == IN6ADDRSZ &&
304 1.1.1.1.2.2 jym (!memcmp(taddr, mapped, sizeof mapped) ||
305 1.1.1.1.2.2 jym !memcmp(taddr, tunnelled, sizeof tunnelled))) {
306 1.1.1.1.2.2 jym /* Unmap. */
307 1.1.1.1.2.2 jym taddr += sizeof mapped;
308 1.1.1.1.2.2 jym taf = AF_INET;
309 1.1.1.1.2.2 jym tlen = INADDRSZ;
310 1.1.1.1.2.2 jym }
311 1.1.1.1.2.2 jym if (taf == af && tlen == len &&
312 1.1.1.1.2.2 jym !memcmp(taddr, uaddr, tlen))
313 1.1.1.1.2.2 jym goto found;
314 1.1.1.1.2.2 jym }
315 1.1.1.1.2.2 jym }
316 1.1.1.1.2.2 jym found:
317 1.1.1.1.2.2 jym if (!hp) {
318 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
319 1.1.1.1.2.2 jym return (NULL);
320 1.1.1.1.2.2 jym }
321 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
322 1.1.1.1.2.2 jym return (hp);
323 1.1.1.1.2.2 jym }
324 1.1.1.1.2.2 jym
325 1.1.1.1.2.2 jym static struct hostent *
326 1.1.1.1.2.2 jym ho_next(struct irs_ho *this) {
327 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
328 1.1.1.1.2.2 jym char *cp, **q, *p;
329 1.1.1.1.2.2 jym char *bufp, *ndbuf, *dbuf = NULL;
330 1.1.1.1.2.2 jym int c, af, len, bufsiz, offset;
331 1.1.1.1.2.2 jym
332 1.1.1.1.2.2 jym if (init(this) == -1)
333 1.1.1.1.2.2 jym return (NULL);
334 1.1.1.1.2.2 jym
335 1.1.1.1.2.2 jym if (!pvt->fp)
336 1.1.1.1.2.2 jym ho_rewind(this);
337 1.1.1.1.2.2 jym if (!pvt->fp) {
338 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
339 1.1.1.1.2.2 jym return (NULL);
340 1.1.1.1.2.2 jym }
341 1.1.1.1.2.2 jym bufp = pvt->hostbuf;
342 1.1.1.1.2.2 jym bufsiz = sizeof pvt->hostbuf;
343 1.1.1.1.2.2 jym offset = 0;
344 1.1.1.1.2.2 jym again:
345 1.1.1.1.2.2 jym if (!(p = fgets(bufp + offset, bufsiz - offset, pvt->fp))) {
346 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
347 1.1.1.1.2.2 jym if (dbuf)
348 1.1.1.1.2.2 jym free(dbuf);
349 1.1.1.1.2.2 jym return (NULL);
350 1.1.1.1.2.2 jym }
351 1.1.1.1.2.2 jym if (!strchr(p, '\n') && !feof(pvt->fp)) {
352 1.1.1.1.2.2 jym #define GROWBUF 1024
353 1.1.1.1.2.2 jym /* allocate space for longer line */
354 1.1.1.1.2.2 jym if (dbuf == NULL) {
355 1.1.1.1.2.2 jym if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL)
356 1.1.1.1.2.2 jym strcpy(ndbuf, bufp);
357 1.1.1.1.2.2 jym } else
358 1.1.1.1.2.2 jym ndbuf = realloc(dbuf, bufsiz + GROWBUF);
359 1.1.1.1.2.2 jym if (ndbuf) {
360 1.1.1.1.2.2 jym dbuf = ndbuf;
361 1.1.1.1.2.2 jym bufp = dbuf;
362 1.1.1.1.2.2 jym bufsiz += GROWBUF;
363 1.1.1.1.2.2 jym offset = strlen(dbuf);
364 1.1.1.1.2.2 jym } else {
365 1.1.1.1.2.2 jym /* allocation failed; skip this long line */
366 1.1.1.1.2.2 jym while ((c = getc(pvt->fp)) != EOF)
367 1.1.1.1.2.2 jym if (c == '\n')
368 1.1.1.1.2.2 jym break;
369 1.1.1.1.2.2 jym if (c != EOF)
370 1.1.1.1.2.2 jym ungetc(c, pvt->fp);
371 1.1.1.1.2.2 jym }
372 1.1.1.1.2.2 jym goto again;
373 1.1.1.1.2.2 jym }
374 1.1.1.1.2.2 jym
375 1.1.1.1.2.2 jym p -= offset;
376 1.1.1.1.2.2 jym offset = 0;
377 1.1.1.1.2.2 jym
378 1.1.1.1.2.2 jym if (*p == '#')
379 1.1.1.1.2.2 jym goto again;
380 1.1.1.1.2.2 jym if ((cp = strpbrk(p, "#\n")) != NULL)
381 1.1.1.1.2.2 jym *cp = '\0';
382 1.1.1.1.2.2 jym if (!(cp = strpbrk(p, " \t")))
383 1.1.1.1.2.2 jym goto again;
384 1.1.1.1.2.2 jym *cp++ = '\0';
385 1.1.1.1.2.2 jym if (inet_pton(AF_INET6, p, pvt->host_addr) > 0) {
386 1.1.1.1.2.2 jym af = AF_INET6;
387 1.1.1.1.2.2 jym len = IN6ADDRSZ;
388 1.1.1.1.2.2 jym } else if (inet_aton(p, (struct in_addr *)pvt->host_addr) > 0) {
389 1.1.1.1.2.2 jym if (pvt->res->options & RES_USE_INET6) {
390 1.1.1.1.2.2 jym map_v4v6_address((char*)pvt->host_addr,
391 1.1.1.1.2.2 jym (char*)pvt->host_addr);
392 1.1.1.1.2.2 jym af = AF_INET6;
393 1.1.1.1.2.2 jym len = IN6ADDRSZ;
394 1.1.1.1.2.2 jym } else {
395 1.1.1.1.2.2 jym af = AF_INET;
396 1.1.1.1.2.2 jym len = INADDRSZ;
397 1.1.1.1.2.2 jym }
398 1.1.1.1.2.2 jym } else {
399 1.1.1.1.2.2 jym goto again;
400 1.1.1.1.2.2 jym }
401 1.1.1.1.2.2 jym pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
402 1.1.1.1.2.2 jym pvt->h_addr_ptrs[1] = NULL;
403 1.1.1.1.2.2 jym pvt->host.h_addr_list = pvt->h_addr_ptrs;
404 1.1.1.1.2.2 jym pvt->host.h_length = len;
405 1.1.1.1.2.2 jym pvt->host.h_addrtype = af;
406 1.1.1.1.2.2 jym while (*cp == ' ' || *cp == '\t')
407 1.1.1.1.2.2 jym cp++;
408 1.1.1.1.2.2 jym pvt->host.h_name = cp;
409 1.1.1.1.2.2 jym q = pvt->host.h_aliases = pvt->host_aliases;
410 1.1.1.1.2.2 jym if ((cp = strpbrk(cp, " \t")) != NULL)
411 1.1.1.1.2.2 jym *cp++ = '\0';
412 1.1.1.1.2.2 jym while (cp && *cp) {
413 1.1.1.1.2.2 jym if (*cp == ' ' || *cp == '\t') {
414 1.1.1.1.2.2 jym cp++;
415 1.1.1.1.2.2 jym continue;
416 1.1.1.1.2.2 jym }
417 1.1.1.1.2.2 jym if (q < &pvt->host_aliases[MAXALIASES - 1])
418 1.1.1.1.2.2 jym *q++ = cp;
419 1.1.1.1.2.2 jym if ((cp = strpbrk(cp, " \t")) != NULL)
420 1.1.1.1.2.2 jym *cp++ = '\0';
421 1.1.1.1.2.2 jym }
422 1.1.1.1.2.2 jym *q = NULL;
423 1.1.1.1.2.2 jym if (dbuf)
424 1.1.1.1.2.2 jym free(dbuf);
425 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
426 1.1.1.1.2.2 jym return (&pvt->host);
427 1.1.1.1.2.2 jym }
428 1.1.1.1.2.2 jym
429 1.1.1.1.2.2 jym static void
430 1.1.1.1.2.2 jym ho_rewind(struct irs_ho *this) {
431 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
432 1.1.1.1.2.2 jym
433 1.1.1.1.2.2 jym if (pvt->fp) {
434 1.1.1.1.2.2 jym if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
435 1.1.1.1.2.2 jym return;
436 1.1.1.1.2.2 jym (void)fclose(pvt->fp);
437 1.1.1.1.2.2 jym }
438 1.1.1.1.2.2 jym if (!(pvt->fp = fopen(_PATH_HOSTS, "r")))
439 1.1.1.1.2.2 jym return;
440 1.1.1.1.2.2 jym if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
441 1.1.1.1.2.2 jym (void)fclose(pvt->fp);
442 1.1.1.1.2.2 jym pvt->fp = NULL;
443 1.1.1.1.2.2 jym }
444 1.1.1.1.2.2 jym }
445 1.1.1.1.2.2 jym
446 1.1.1.1.2.2 jym static void
447 1.1.1.1.2.2 jym ho_minimize(struct irs_ho *this) {
448 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
449 1.1.1.1.2.2 jym
450 1.1.1.1.2.2 jym if (pvt->fp != NULL) {
451 1.1.1.1.2.2 jym (void)fclose(pvt->fp);
452 1.1.1.1.2.2 jym pvt->fp = NULL;
453 1.1.1.1.2.2 jym }
454 1.1.1.1.2.2 jym if (pvt->res)
455 1.1.1.1.2.2 jym res_nclose(pvt->res);
456 1.1.1.1.2.2 jym }
457 1.1.1.1.2.2 jym
458 1.1.1.1.2.2 jym static struct __res_state *
459 1.1.1.1.2.2 jym ho_res_get(struct irs_ho *this) {
460 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
461 1.1.1.1.2.2 jym
462 1.1.1.1.2.2 jym if (!pvt->res) {
463 1.1.1.1.2.2 jym struct __res_state *res;
464 1.1.1.1.2.2 jym res = (struct __res_state *)malloc(sizeof *res);
465 1.1.1.1.2.2 jym if (!res) {
466 1.1.1.1.2.2 jym errno = ENOMEM;
467 1.1.1.1.2.2 jym return (NULL);
468 1.1.1.1.2.2 jym }
469 1.1.1.1.2.2 jym memset(res, 0, sizeof *res);
470 1.1.1.1.2.2 jym ho_res_set(this, res, free);
471 1.1.1.1.2.2 jym }
472 1.1.1.1.2.2 jym
473 1.1.1.1.2.2 jym return (pvt->res);
474 1.1.1.1.2.2 jym }
475 1.1.1.1.2.2 jym
476 1.1.1.1.2.2 jym static void
477 1.1.1.1.2.2 jym ho_res_set(struct irs_ho *this, struct __res_state *res,
478 1.1.1.1.2.2 jym void (*free_res)(void *)) {
479 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
480 1.1.1.1.2.2 jym
481 1.1.1.1.2.2 jym if (pvt->res && pvt->free_res) {
482 1.1.1.1.2.2 jym res_nclose(pvt->res);
483 1.1.1.1.2.2 jym (*pvt->free_res)(pvt->res);
484 1.1.1.1.2.2 jym }
485 1.1.1.1.2.2 jym
486 1.1.1.1.2.2 jym pvt->res = res;
487 1.1.1.1.2.2 jym pvt->free_res = free_res;
488 1.1.1.1.2.2 jym }
489 1.1.1.1.2.2 jym
490 1.1.1.1.2.2 jym struct lcl_res_target {
491 1.1.1.1.2.2 jym struct lcl_res_target *next;
492 1.1.1.1.2.2 jym int family;
493 1.1.1.1.2.2 jym };
494 1.1.1.1.2.2 jym
495 1.1.1.1.2.2 jym /* XXX */
496 1.1.1.1.2.2 jym extern struct addrinfo *hostent2addrinfo __P((struct hostent *,
497 1.1.1.1.2.2 jym const struct addrinfo *pai));
498 1.1.1.1.2.2 jym
499 1.1.1.1.2.2 jym static struct addrinfo *
500 1.1.1.1.2.2 jym ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
501 1.1.1.1.2.2 jym {
502 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
503 1.1.1.1.2.2 jym struct hostent *hp;
504 1.1.1.1.2.2 jym struct lcl_res_target q, q2, *p;
505 1.1.1.1.2.2 jym struct addrinfo sentinel, *cur;
506 1.1.1.1.2.2 jym
507 1.1.1.1.2.2 jym memset(&q, 0, sizeof(q2));
508 1.1.1.1.2.2 jym memset(&q2, 0, sizeof(q2));
509 1.1.1.1.2.2 jym memset(&sentinel, 0, sizeof(sentinel));
510 1.1.1.1.2.2 jym cur = &sentinel;
511 1.1.1.1.2.2 jym
512 1.1.1.1.2.2 jym switch(pai->ai_family) {
513 1.1.1.1.2.2 jym case AF_UNSPEC: /*%< INET6 then INET4 */
514 1.1.1.1.2.2 jym q.family = AF_INET6;
515 1.1.1.1.2.2 jym q.next = &q2;
516 1.1.1.1.2.2 jym q2.family = AF_INET;
517 1.1.1.1.2.2 jym break;
518 1.1.1.1.2.2 jym case AF_INET6:
519 1.1.1.1.2.2 jym q.family = AF_INET6;
520 1.1.1.1.2.2 jym break;
521 1.1.1.1.2.2 jym case AF_INET:
522 1.1.1.1.2.2 jym q.family = AF_INET;
523 1.1.1.1.2.2 jym break;
524 1.1.1.1.2.2 jym default:
525 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< ??? */
526 1.1.1.1.2.2 jym return(NULL);
527 1.1.1.1.2.2 jym }
528 1.1.1.1.2.2 jym
529 1.1.1.1.2.2 jym for (p = &q; p; p = p->next) {
530 1.1.1.1.2.2 jym struct addrinfo *ai;
531 1.1.1.1.2.2 jym
532 1.1.1.1.2.2 jym hp = (*this->byname2)(this, name, p->family);
533 1.1.1.1.2.2 jym if (hp == NULL) {
534 1.1.1.1.2.2 jym /* byname2 should've set an appropriate error */
535 1.1.1.1.2.2 jym continue;
536 1.1.1.1.2.2 jym }
537 1.1.1.1.2.2 jym if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
538 1.1.1.1.2.2 jym (hp->h_addr_list[0] == NULL)) {
539 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
540 1.1.1.1.2.2 jym continue;
541 1.1.1.1.2.2 jym }
542 1.1.1.1.2.2 jym
543 1.1.1.1.2.2 jym ai = hostent2addrinfo(hp, pai);
544 1.1.1.1.2.2 jym if (ai) {
545 1.1.1.1.2.2 jym cur->ai_next = ai;
546 1.1.1.1.2.2 jym while (cur->ai_next)
547 1.1.1.1.2.2 jym cur = cur->ai_next;
548 1.1.1.1.2.2 jym }
549 1.1.1.1.2.2 jym }
550 1.1.1.1.2.2 jym
551 1.1.1.1.2.2 jym if (sentinel.ai_next == NULL)
552 1.1.1.1.2.2 jym RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
553 1.1.1.1.2.2 jym
554 1.1.1.1.2.2 jym return(sentinel.ai_next);
555 1.1.1.1.2.2 jym }
556 1.1.1.1.2.2 jym
557 1.1.1.1.2.2 jym /* Private. */
558 1.1.1.1.2.2 jym
559 1.1.1.1.2.2 jym static size_t
560 1.1.1.1.2.2 jym ns_namelen(const char *s) {
561 1.1.1.1.2.2 jym int i;
562 1.1.1.1.2.2 jym
563 1.1.1.1.2.2 jym for (i = strlen(s); i > 0 && s[i-1] == '.'; i--)
564 1.1.1.1.2.2 jym (void)NULL;
565 1.1.1.1.2.2 jym return ((size_t) i);
566 1.1.1.1.2.2 jym }
567 1.1.1.1.2.2 jym
568 1.1.1.1.2.2 jym static int
569 1.1.1.1.2.2 jym init(struct irs_ho *this) {
570 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
571 1.1.1.1.2.2 jym
572 1.1.1.1.2.2 jym if (!pvt->res && !ho_res_get(this))
573 1.1.1.1.2.2 jym return (-1);
574 1.1.1.1.2.2 jym if (((pvt->res->options & RES_INIT) == 0U) &&
575 1.1.1.1.2.2 jym res_ninit(pvt->res) == -1)
576 1.1.1.1.2.2 jym return (-1);
577 1.1.1.1.2.2 jym return (0);
578 1.1.1.1.2.2 jym }
579 1.1.1.1.2.2 jym
580 1.1.1.1.2.2 jym /*! \file */
581