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