1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1985, 1989, 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 | * 4. 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. |
---|
18 | * |
---|
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 |
---|
29 | * SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | /* |
---|
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
---|
34 | * |
---|
35 | * Permission to use, copy, modify, and distribute this software for any |
---|
36 | * purpose with or without fee is hereby granted, provided that the above |
---|
37 | * copyright notice and this permission notice appear in all copies, and that |
---|
38 | * the name of Digital Equipment Corporation not be used in advertising or |
---|
39 | * publicity pertaining to distribution of the document or software without |
---|
40 | * specific, written prior permission. |
---|
41 | * |
---|
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
---|
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
---|
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
---|
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
---|
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
---|
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
---|
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
---|
49 | * SOFTWARE. |
---|
50 | */ |
---|
51 | |
---|
52 | /* |
---|
53 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
---|
54 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
---|
55 | * |
---|
56 | * Permission to use, copy, modify, and distribute this software for any |
---|
57 | * purpose with or without fee is hereby granted, provided that the above |
---|
58 | * copyright notice and this permission notice appear in all copies. |
---|
59 | * |
---|
60 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
---|
61 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
62 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
---|
63 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
64 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
65 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
---|
66 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
67 | */ |
---|
68 | |
---|
69 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
70 | static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; |
---|
71 | static const char rcsid[] = "$Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp $"; |
---|
72 | #endif /* LIBC_SCCS and not lint */ |
---|
73 | #include <sys/cdefs.h> |
---|
74 | __FBSDID("$FreeBSD$"); |
---|
75 | |
---|
76 | #include "port_before.h" |
---|
77 | |
---|
78 | #include "namespace.h" |
---|
79 | |
---|
80 | #include <rtems/bsd/sys/types.h> |
---|
81 | #include <rtems/bsd/sys/param.h> |
---|
82 | #include <sys/socket.h> |
---|
83 | #include <rtems/bsd/sys/time.h> |
---|
84 | |
---|
85 | #include <netinet/in.h> |
---|
86 | #include <arpa/inet.h> |
---|
87 | #include <arpa/nameser.h> |
---|
88 | |
---|
89 | #include <ctype.h> |
---|
90 | #include <stdio.h> |
---|
91 | #include <stdlib.h> |
---|
92 | #include <string.h> |
---|
93 | #include <unistd.h> |
---|
94 | #include <netdb.h> |
---|
95 | |
---|
96 | #ifndef HAVE_MD5 |
---|
97 | # include "../dst/md5.h" |
---|
98 | #else |
---|
99 | # ifdef SOLARIS2 |
---|
100 | # include <sys/md5.h> |
---|
101 | # elif _LIBC |
---|
102 | # include <md5.h> |
---|
103 | # endif |
---|
104 | #endif |
---|
105 | #ifndef _MD5_H_ |
---|
106 | # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */ |
---|
107 | #endif |
---|
108 | |
---|
109 | #include "un-namespace.h" |
---|
110 | |
---|
111 | #include "port_after.h" |
---|
112 | |
---|
113 | /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ |
---|
114 | #include <resolv.h> |
---|
115 | |
---|
116 | #include "res_private.h" |
---|
117 | |
---|
118 | /*% Options. Should all be left alone. */ |
---|
119 | #define RESOLVSORT |
---|
120 | #define DEBUG |
---|
121 | |
---|
122 | #ifdef SOLARIS2 |
---|
123 | #include <sys/systeminfo.h> |
---|
124 | #endif |
---|
125 | |
---|
126 | static void res_setoptions(res_state, const char *, const char *); |
---|
127 | |
---|
128 | #ifdef RESOLVSORT |
---|
129 | static const char sort_mask[] = "/&"; |
---|
130 | #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) |
---|
131 | static u_int32_t net_mask(struct in_addr); |
---|
132 | #endif |
---|
133 | |
---|
134 | #if !defined(isascii) /*%< XXX - could be a function */ |
---|
135 | # define isascii(c) (!(c & 0200)) |
---|
136 | #endif |
---|
137 | |
---|
138 | /* |
---|
139 | * Resolver state default settings. |
---|
140 | */ |
---|
141 | |
---|
142 | /*% |
---|
143 | * Set up default settings. If the configuration file exist, the values |
---|
144 | * there will have precedence. Otherwise, the server address is set to |
---|
145 | * INADDR_ANY and the default domain name comes from the gethostname(). |
---|
146 | * |
---|
147 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 |
---|
148 | * rather than INADDR_ANY ("0.0.0.0") as the default name server address |
---|
149 | * since it was noted that INADDR_ANY actually meant ``the first interface |
---|
150 | * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, |
---|
151 | * it had to be "up" in order for you to reach your own name server. It |
---|
152 | * was later decided that since the recommended practice is to always |
---|
153 | * install local static routes through 127.0.0.1 for all your network |
---|
154 | * interfaces, that we could solve this problem without a code change. |
---|
155 | * |
---|
156 | * The configuration file should always be used, since it is the only way |
---|
157 | * to specify a default domain. If you are running a server on your local |
---|
158 | * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" |
---|
159 | * in the configuration file. |
---|
160 | * |
---|
161 | * Return 0 if completes successfully, -1 on error |
---|
162 | */ |
---|
163 | int |
---|
164 | res_ninit(res_state statp) { |
---|
165 | extern int __res_vinit(res_state, int); |
---|
166 | |
---|
167 | return (__res_vinit(statp, 0)); |
---|
168 | } |
---|
169 | |
---|
170 | /*% This function has to be reachable by res_data.c but not publically. */ |
---|
171 | int |
---|
172 | __res_vinit(res_state statp, int preinit) { |
---|
173 | FILE *fp; |
---|
174 | char *cp, **pp; |
---|
175 | int n; |
---|
176 | char buf[BUFSIZ]; |
---|
177 | int nserv = 0; /*%< number of nameserver records read from file */ |
---|
178 | int haveenv = 0; |
---|
179 | int havesearch = 0; |
---|
180 | #ifdef RESOLVSORT |
---|
181 | int nsort = 0; |
---|
182 | char *net; |
---|
183 | #endif |
---|
184 | int dots; |
---|
185 | union res_sockaddr_union u[2]; |
---|
186 | int maxns = MAXNS; |
---|
187 | |
---|
188 | RES_SET_H_ERRNO(statp, 0); |
---|
189 | if (statp->_u._ext.ext != NULL) |
---|
190 | res_ndestroy(statp); |
---|
191 | |
---|
192 | if (!preinit) { |
---|
193 | statp->retrans = RES_TIMEOUT; |
---|
194 | statp->retry = RES_DFLRETRY; |
---|
195 | statp->options = RES_DEFAULT; |
---|
196 | } |
---|
197 | |
---|
198 | statp->_rnd = malloc(16); |
---|
199 | res_rndinit(statp); |
---|
200 | statp->id = res_nrandomid(statp); |
---|
201 | |
---|
202 | memset(u, 0, sizeof(u)); |
---|
203 | #ifdef USELOOPBACK |
---|
204 | u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); |
---|
205 | #else |
---|
206 | u[nserv].sin.sin_addr.s_addr = INADDR_ANY; |
---|
207 | #endif |
---|
208 | u[nserv].sin.sin_family = AF_INET; |
---|
209 | u[nserv].sin.sin_port = htons(NAMESERVER_PORT); |
---|
210 | #ifdef HAVE_SA_LEN |
---|
211 | u[nserv].sin.sin_len = sizeof(struct sockaddr_in); |
---|
212 | #endif |
---|
213 | nserv++; |
---|
214 | #ifdef HAS_INET6_STRUCTS |
---|
215 | #ifdef USELOOPBACK |
---|
216 | u[nserv].sin6.sin6_addr = in6addr_loopback; |
---|
217 | #else |
---|
218 | u[nserv].sin6.sin6_addr = in6addr_any; |
---|
219 | #endif |
---|
220 | u[nserv].sin6.sin6_family = AF_INET6; |
---|
221 | u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); |
---|
222 | #ifdef HAVE_SA_LEN |
---|
223 | u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); |
---|
224 | #endif |
---|
225 | nserv++; |
---|
226 | #endif |
---|
227 | statp->nscount = 0; |
---|
228 | statp->ndots = 1; |
---|
229 | statp->pfcode = 0; |
---|
230 | statp->_vcsock = -1; |
---|
231 | statp->_flags = 0; |
---|
232 | statp->qhook = NULL; |
---|
233 | statp->rhook = NULL; |
---|
234 | statp->_u._ext.nscount = 0; |
---|
235 | statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); |
---|
236 | if (statp->_u._ext.ext != NULL) { |
---|
237 | memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); |
---|
238 | statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; |
---|
239 | strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); |
---|
240 | strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); |
---|
241 | } else { |
---|
242 | /* |
---|
243 | * Historically res_init() rarely, if at all, failed. |
---|
244 | * Examples and applications exist which do not check |
---|
245 | * our return code. Furthermore several applications |
---|
246 | * simply call us to get the systems domainname. So |
---|
247 | * rather then immediately fail here we store the |
---|
248 | * failure, which is returned later, in h_errno. And |
---|
249 | * prevent the collection of 'nameserver' information |
---|
250 | * by setting maxns to 0. Thus applications that fail |
---|
251 | * to check our return code wont be able to make |
---|
252 | * queries anyhow. |
---|
253 | */ |
---|
254 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
255 | maxns = 0; |
---|
256 | } |
---|
257 | #ifdef RESOLVSORT |
---|
258 | statp->nsort = 0; |
---|
259 | #endif |
---|
260 | res_setservers(statp, u, nserv); |
---|
261 | |
---|
262 | #ifdef SOLARIS2 |
---|
263 | /* |
---|
264 | * The old libresolv derived the defaultdomain from NIS/NIS+. |
---|
265 | * We want to keep this behaviour |
---|
266 | */ |
---|
267 | { |
---|
268 | char buf[sizeof(statp->defdname)], *cp; |
---|
269 | int ret; |
---|
270 | |
---|
271 | if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 && |
---|
272 | (unsigned int)ret <= sizeof(buf)) { |
---|
273 | if (buf[0] == '+') |
---|
274 | buf[0] = '.'; |
---|
275 | cp = strchr(buf, '.'); |
---|
276 | cp = (cp == NULL) ? buf : (cp + 1); |
---|
277 | strncpy(statp->defdname, cp, |
---|
278 | sizeof(statp->defdname) - 1); |
---|
279 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
280 | } |
---|
281 | } |
---|
282 | #endif /* SOLARIS2 */ |
---|
283 | |
---|
284 | /* Allow user to override the local domain definition */ |
---|
285 | if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { |
---|
286 | (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
---|
287 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
288 | haveenv++; |
---|
289 | |
---|
290 | /* |
---|
291 | * Set search list to be blank-separated strings |
---|
292 | * from rest of env value. Permits users of LOCALDOMAIN |
---|
293 | * to still have a search list, and anyone to set the |
---|
294 | * one that they want to use as an individual (even more |
---|
295 | * important now that the rfc1535 stuff restricts searches) |
---|
296 | */ |
---|
297 | cp = statp->defdname; |
---|
298 | pp = statp->dnsrch; |
---|
299 | *pp++ = cp; |
---|
300 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
---|
301 | if (*cp == '\n') /*%< silly backwards compat */ |
---|
302 | break; |
---|
303 | else if (*cp == ' ' || *cp == '\t') { |
---|
304 | *cp = 0; |
---|
305 | n = 1; |
---|
306 | } else if (n) { |
---|
307 | *pp++ = cp; |
---|
308 | n = 0; |
---|
309 | havesearch = 1; |
---|
310 | } |
---|
311 | } |
---|
312 | /* null terminate last domain if there are excess */ |
---|
313 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') |
---|
314 | cp++; |
---|
315 | *cp = '\0'; |
---|
316 | *pp++ = 0; |
---|
317 | } |
---|
318 | |
---|
319 | #define MATCH(line, name) \ |
---|
320 | (!strncmp(line, name, sizeof(name) - 1) && \ |
---|
321 | (line[sizeof(name) - 1] == ' ' || \ |
---|
322 | line[sizeof(name) - 1] == '\t')) |
---|
323 | |
---|
324 | nserv = 0; |
---|
325 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { |
---|
326 | /* read the config file */ |
---|
327 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
---|
328 | /* skip comments */ |
---|
329 | if (*buf == ';' || *buf == '#') |
---|
330 | continue; |
---|
331 | /* read default domain name */ |
---|
332 | if (MATCH(buf, "domain")) { |
---|
333 | if (haveenv) /*%< skip if have from environ */ |
---|
334 | continue; |
---|
335 | cp = buf + sizeof("domain") - 1; |
---|
336 | while (*cp == ' ' || *cp == '\t') |
---|
337 | cp++; |
---|
338 | if ((*cp == '\0') || (*cp == '\n')) |
---|
339 | continue; |
---|
340 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
---|
341 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
342 | if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) |
---|
343 | *cp = '\0'; |
---|
344 | havesearch = 0; |
---|
345 | continue; |
---|
346 | } |
---|
347 | /* set search list */ |
---|
348 | if (MATCH(buf, "search")) { |
---|
349 | if (haveenv) /*%< skip if have from environ */ |
---|
350 | continue; |
---|
351 | cp = buf + sizeof("search") - 1; |
---|
352 | while (*cp == ' ' || *cp == '\t') |
---|
353 | cp++; |
---|
354 | if ((*cp == '\0') || (*cp == '\n')) |
---|
355 | continue; |
---|
356 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
---|
357 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
358 | if ((cp = strchr(statp->defdname, '\n')) != NULL) |
---|
359 | *cp = '\0'; |
---|
360 | /* |
---|
361 | * Set search list to be blank-separated strings |
---|
362 | * on rest of line. |
---|
363 | */ |
---|
364 | cp = statp->defdname; |
---|
365 | pp = statp->dnsrch; |
---|
366 | *pp++ = cp; |
---|
367 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
---|
368 | if (*cp == ' ' || *cp == '\t') { |
---|
369 | *cp = 0; |
---|
370 | n = 1; |
---|
371 | } else if (n) { |
---|
372 | *pp++ = cp; |
---|
373 | n = 0; |
---|
374 | } |
---|
375 | } |
---|
376 | /* null terminate last domain if there are excess */ |
---|
377 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
---|
378 | cp++; |
---|
379 | *cp = '\0'; |
---|
380 | *pp++ = 0; |
---|
381 | havesearch = 1; |
---|
382 | continue; |
---|
383 | } |
---|
384 | /* read nameservers to query */ |
---|
385 | if (MATCH(buf, "nameserver") && nserv < maxns) { |
---|
386 | struct addrinfo hints, *ai; |
---|
387 | char sbuf[NI_MAXSERV]; |
---|
388 | const size_t minsiz = |
---|
389 | sizeof(statp->_u._ext.ext->nsaddrs[0]); |
---|
390 | |
---|
391 | cp = buf + sizeof("nameserver") - 1; |
---|
392 | while (*cp == ' ' || *cp == '\t') |
---|
393 | cp++; |
---|
394 | cp[strcspn(cp, ";# \t\n")] = '\0'; |
---|
395 | if ((*cp != '\0') && (*cp != '\n')) { |
---|
396 | memset(&hints, 0, sizeof(hints)); |
---|
397 | hints.ai_family = PF_UNSPEC; |
---|
398 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
---|
399 | hints.ai_flags = AI_NUMERICHOST; |
---|
400 | sprintf(sbuf, "%u", NAMESERVER_PORT); |
---|
401 | if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && |
---|
402 | ai->ai_addrlen <= minsiz) { |
---|
403 | if (statp->_u._ext.ext != NULL) { |
---|
404 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], |
---|
405 | ai->ai_addr, ai->ai_addrlen); |
---|
406 | } |
---|
407 | if (ai->ai_addrlen <= |
---|
408 | sizeof(statp->nsaddr_list[nserv])) { |
---|
409 | memcpy(&statp->nsaddr_list[nserv], |
---|
410 | ai->ai_addr, ai->ai_addrlen); |
---|
411 | } else |
---|
412 | statp->nsaddr_list[nserv].sin_family = 0; |
---|
413 | freeaddrinfo(ai); |
---|
414 | nserv++; |
---|
415 | } |
---|
416 | } |
---|
417 | continue; |
---|
418 | } |
---|
419 | #ifdef RESOLVSORT |
---|
420 | if (MATCH(buf, "sortlist")) { |
---|
421 | struct in_addr a; |
---|
422 | struct in6_addr a6; |
---|
423 | int m, i; |
---|
424 | u_char *u; |
---|
425 | struct __res_state_ext *ext = statp->_u._ext.ext; |
---|
426 | |
---|
427 | cp = buf + sizeof("sortlist") - 1; |
---|
428 | while (nsort < MAXRESOLVSORT) { |
---|
429 | while (*cp == ' ' || *cp == '\t') |
---|
430 | cp++; |
---|
431 | if (*cp == '\0' || *cp == '\n' || *cp == ';') |
---|
432 | break; |
---|
433 | net = cp; |
---|
434 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && |
---|
435 | isascii(*cp) && !isspace((unsigned char)*cp)) |
---|
436 | cp++; |
---|
437 | n = *cp; |
---|
438 | *cp = 0; |
---|
439 | if (inet_aton(net, &a)) { |
---|
440 | statp->sort_list[nsort].addr = a; |
---|
441 | if (ISSORTMASK(n)) { |
---|
442 | *cp++ = n; |
---|
443 | net = cp; |
---|
444 | while (*cp && *cp != ';' && |
---|
445 | isascii(*cp) && |
---|
446 | !isspace((unsigned char)*cp)) |
---|
447 | cp++; |
---|
448 | n = *cp; |
---|
449 | *cp = 0; |
---|
450 | if (inet_aton(net, &a)) { |
---|
451 | statp->sort_list[nsort].mask = a.s_addr; |
---|
452 | } else { |
---|
453 | statp->sort_list[nsort].mask = |
---|
454 | net_mask(statp->sort_list[nsort].addr); |
---|
455 | } |
---|
456 | } else { |
---|
457 | statp->sort_list[nsort].mask = |
---|
458 | net_mask(statp->sort_list[nsort].addr); |
---|
459 | } |
---|
460 | ext->sort_list[nsort].af = AF_INET; |
---|
461 | ext->sort_list[nsort].addr.ina = |
---|
462 | statp->sort_list[nsort].addr; |
---|
463 | ext->sort_list[nsort].mask.ina.s_addr = |
---|
464 | statp->sort_list[nsort].mask; |
---|
465 | nsort++; |
---|
466 | } |
---|
467 | else if (inet_pton(AF_INET6, net, &a6) == 1) { |
---|
468 | |
---|
469 | ext->sort_list[nsort].af = AF_INET6; |
---|
470 | ext->sort_list[nsort].addr.in6a = a6; |
---|
471 | u = (u_char *)&ext->sort_list[nsort].mask.in6a; |
---|
472 | *cp++ = n; |
---|
473 | net = cp; |
---|
474 | while (*cp && *cp != ';' && |
---|
475 | isascii(*cp) && !isspace(*cp)) |
---|
476 | cp++; |
---|
477 | m = n; |
---|
478 | n = *cp; |
---|
479 | *cp = 0; |
---|
480 | switch (m) { |
---|
481 | case '/': |
---|
482 | m = atoi(net); |
---|
483 | break; |
---|
484 | case '&': |
---|
485 | if (inet_pton(AF_INET6, net, u) == 1) { |
---|
486 | m = -1; |
---|
487 | break; |
---|
488 | } |
---|
489 | /*FALLTHROUGH*/ |
---|
490 | default: |
---|
491 | m = sizeof(struct in6_addr) * CHAR_BIT; |
---|
492 | break; |
---|
493 | } |
---|
494 | if (m >= 0) { |
---|
495 | for (i = 0; i < sizeof(struct in6_addr); i++) { |
---|
496 | if (m <= 0) { |
---|
497 | *u = 0; |
---|
498 | } else { |
---|
499 | m -= CHAR_BIT; |
---|
500 | *u = (u_char)~0; |
---|
501 | if (m < 0) |
---|
502 | *u <<= -m; |
---|
503 | } |
---|
504 | u++; |
---|
505 | } |
---|
506 | } |
---|
507 | statp->sort_list[nsort].addr.s_addr = |
---|
508 | (u_int32_t)0xffffffff; |
---|
509 | statp->sort_list[nsort].mask = |
---|
510 | (u_int32_t)0xffffffff; |
---|
511 | nsort++; |
---|
512 | } |
---|
513 | *cp = n; |
---|
514 | } |
---|
515 | continue; |
---|
516 | } |
---|
517 | #endif |
---|
518 | if (MATCH(buf, "options")) { |
---|
519 | res_setoptions(statp, buf + sizeof("options") - 1, "conf"); |
---|
520 | continue; |
---|
521 | } |
---|
522 | } |
---|
523 | if (nserv > 0) |
---|
524 | statp->nscount = nserv; |
---|
525 | #ifdef RESOLVSORT |
---|
526 | statp->nsort = nsort; |
---|
527 | #endif |
---|
528 | (void) fclose(fp); |
---|
529 | } |
---|
530 | /* |
---|
531 | * Last chance to get a nameserver. This should not normally |
---|
532 | * be necessary |
---|
533 | */ |
---|
534 | #ifdef NO_RESOLV_CONF |
---|
535 | if(nserv == 0) |
---|
536 | nserv = get_nameservers(statp); |
---|
537 | #endif |
---|
538 | |
---|
539 | if (statp->defdname[0] == 0 && |
---|
540 | gethostname(buf, sizeof(statp->defdname) - 1) == 0 && |
---|
541 | (cp = strchr(buf, '.')) != NULL) |
---|
542 | strcpy(statp->defdname, cp + 1); |
---|
543 | |
---|
544 | /* find components of local domain that might be searched */ |
---|
545 | if (havesearch == 0) { |
---|
546 | pp = statp->dnsrch; |
---|
547 | *pp++ = statp->defdname; |
---|
548 | *pp = NULL; |
---|
549 | |
---|
550 | dots = 0; |
---|
551 | for (cp = statp->defdname; *cp; cp++) |
---|
552 | dots += (*cp == '.'); |
---|
553 | |
---|
554 | cp = statp->defdname; |
---|
555 | while (pp < statp->dnsrch + MAXDFLSRCH) { |
---|
556 | if (dots < LOCALDOMAINPARTS) |
---|
557 | break; |
---|
558 | cp = strchr(cp, '.') + 1; /*%< we know there is one */ |
---|
559 | *pp++ = cp; |
---|
560 | dots--; |
---|
561 | } |
---|
562 | *pp = NULL; |
---|
563 | #ifdef DEBUG |
---|
564 | if (statp->options & RES_DEBUG) { |
---|
565 | printf(";; res_init()... default dnsrch list:\n"); |
---|
566 | for (pp = statp->dnsrch; *pp; pp++) |
---|
567 | printf(";;\t%s\n", *pp); |
---|
568 | printf(";;\t..END..\n"); |
---|
569 | } |
---|
570 | #endif |
---|
571 | } |
---|
572 | |
---|
573 | if (issetugid()) |
---|
574 | statp->options |= RES_NOALIASES; |
---|
575 | else if ((cp = getenv("RES_OPTIONS")) != NULL) |
---|
576 | res_setoptions(statp, cp, "env"); |
---|
577 | statp->options |= RES_INIT; |
---|
578 | return (statp->res_h_errno); |
---|
579 | } |
---|
580 | |
---|
581 | static void |
---|
582 | res_setoptions(res_state statp, const char *options, const char *source) |
---|
583 | { |
---|
584 | const char *cp = options; |
---|
585 | int i; |
---|
586 | #ifndef _LIBC |
---|
587 | struct __res_state_ext *ext = statp->_u._ext.ext; |
---|
588 | #endif |
---|
589 | |
---|
590 | #ifdef DEBUG |
---|
591 | if (statp->options & RES_DEBUG) |
---|
592 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", |
---|
593 | options, source); |
---|
594 | #endif |
---|
595 | while (*cp) { |
---|
596 | /* skip leading and inner runs of spaces */ |
---|
597 | while (*cp == ' ' || *cp == '\t') |
---|
598 | cp++; |
---|
599 | /* search for and process individual options */ |
---|
600 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { |
---|
601 | i = atoi(cp + sizeof("ndots:") - 1); |
---|
602 | if (i <= RES_MAXNDOTS) |
---|
603 | statp->ndots = i; |
---|
604 | else |
---|
605 | statp->ndots = RES_MAXNDOTS; |
---|
606 | #ifdef DEBUG |
---|
607 | if (statp->options & RES_DEBUG) |
---|
608 | printf(";;\tndots=%d\n", statp->ndots); |
---|
609 | #endif |
---|
610 | } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { |
---|
611 | i = atoi(cp + sizeof("timeout:") - 1); |
---|
612 | if (i <= RES_MAXRETRANS) |
---|
613 | statp->retrans = i; |
---|
614 | else |
---|
615 | statp->retrans = RES_MAXRETRANS; |
---|
616 | #ifdef DEBUG |
---|
617 | if (statp->options & RES_DEBUG) |
---|
618 | printf(";;\ttimeout=%d\n", statp->retrans); |
---|
619 | #endif |
---|
620 | #ifdef SOLARIS2 |
---|
621 | } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) { |
---|
622 | /* |
---|
623 | * For backward compatibility, 'retrans' is |
---|
624 | * supported as an alias for 'timeout', though |
---|
625 | * without an imposed maximum. |
---|
626 | */ |
---|
627 | statp->retrans = atoi(cp + sizeof("retrans:") - 1); |
---|
628 | } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){ |
---|
629 | /* |
---|
630 | * For backward compatibility, 'retry' is |
---|
631 | * supported as an alias for 'attempts', though |
---|
632 | * without an imposed maximum. |
---|
633 | */ |
---|
634 | statp->retry = atoi(cp + sizeof("retry:") - 1); |
---|
635 | #endif /* SOLARIS2 */ |
---|
636 | } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ |
---|
637 | i = atoi(cp + sizeof("attempts:") - 1); |
---|
638 | if (i <= RES_MAXRETRY) |
---|
639 | statp->retry = i; |
---|
640 | else |
---|
641 | statp->retry = RES_MAXRETRY; |
---|
642 | #ifdef DEBUG |
---|
643 | if (statp->options & RES_DEBUG) |
---|
644 | printf(";;\tattempts=%d\n", statp->retry); |
---|
645 | #endif |
---|
646 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { |
---|
647 | #ifdef DEBUG |
---|
648 | if (!(statp->options & RES_DEBUG)) { |
---|
649 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", |
---|
650 | options, source); |
---|
651 | statp->options |= RES_DEBUG; |
---|
652 | } |
---|
653 | printf(";;\tdebug\n"); |
---|
654 | #endif |
---|
655 | } else if (!strncmp(cp, "no_tld_query", |
---|
656 | sizeof("no_tld_query") - 1) || |
---|
657 | !strncmp(cp, "no-tld-query", |
---|
658 | sizeof("no-tld-query") - 1)) { |
---|
659 | statp->options |= RES_NOTLDQUERY; |
---|
660 | } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { |
---|
661 | statp->options |= RES_USE_INET6; |
---|
662 | } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { |
---|
663 | statp->options |= RES_INSECURE1; |
---|
664 | } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { |
---|
665 | statp->options |= RES_INSECURE2; |
---|
666 | } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { |
---|
667 | statp->options |= RES_ROTATE; |
---|
668 | } else if (!strncmp(cp, "no-check-names", |
---|
669 | sizeof("no-check-names") - 1)) { |
---|
670 | statp->options |= RES_NOCHECKNAME; |
---|
671 | } |
---|
672 | #ifdef RES_USE_EDNS0 |
---|
673 | else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { |
---|
674 | statp->options |= RES_USE_EDNS0; |
---|
675 | } |
---|
676 | #endif |
---|
677 | #ifndef _LIBC |
---|
678 | else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { |
---|
679 | statp->options |= RES_USE_DNAME; |
---|
680 | } |
---|
681 | else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { |
---|
682 | if (ext == NULL) |
---|
683 | goto skip; |
---|
684 | cp += sizeof("nibble:") - 1; |
---|
685 | i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); |
---|
686 | strncpy(ext->nsuffix, cp, i); |
---|
687 | ext->nsuffix[i] = '\0'; |
---|
688 | } |
---|
689 | else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { |
---|
690 | if (ext == NULL) |
---|
691 | goto skip; |
---|
692 | cp += sizeof("nibble2:") - 1; |
---|
693 | i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); |
---|
694 | strncpy(ext->nsuffix2, cp, i); |
---|
695 | ext->nsuffix2[i] = '\0'; |
---|
696 | } |
---|
697 | else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { |
---|
698 | cp += sizeof("v6revmode:") - 1; |
---|
699 | /* "nibble" and "bitstring" used to be valid */ |
---|
700 | if (!strncmp(cp, "single", sizeof("single") - 1)) { |
---|
701 | statp->options |= RES_NO_NIBBLE2; |
---|
702 | } else if (!strncmp(cp, "both", sizeof("both") - 1)) { |
---|
703 | statp->options &= |
---|
704 | ~RES_NO_NIBBLE2; |
---|
705 | } |
---|
706 | } |
---|
707 | #endif |
---|
708 | else { |
---|
709 | /* XXX - print a warning here? */ |
---|
710 | } |
---|
711 | #ifndef _LIBC |
---|
712 | skip: |
---|
713 | #endif |
---|
714 | /* skip to next run of spaces */ |
---|
715 | while (*cp && *cp != ' ' && *cp != '\t') |
---|
716 | cp++; |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | #ifdef RESOLVSORT |
---|
721 | /* XXX - should really support CIDR which means explicit masks always. */ |
---|
722 | static u_int32_t |
---|
723 | net_mask(in) /*!< XXX - should really use system's version of this */ |
---|
724 | struct in_addr in; |
---|
725 | { |
---|
726 | u_int32_t i = ntohl(in.s_addr); |
---|
727 | |
---|
728 | if (IN_CLASSA(i)) |
---|
729 | return (htonl(IN_CLASSA_NET)); |
---|
730 | else if (IN_CLASSB(i)) |
---|
731 | return (htonl(IN_CLASSB_NET)); |
---|
732 | return (htonl(IN_CLASSC_NET)); |
---|
733 | } |
---|
734 | #endif |
---|
735 | |
---|
736 | static u_char srnd[16]; |
---|
737 | |
---|
738 | void |
---|
739 | res_rndinit(res_state statp) |
---|
740 | { |
---|
741 | struct timeval now; |
---|
742 | u_int32_t u32; |
---|
743 | u_int16_t u16; |
---|
744 | u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd; |
---|
745 | |
---|
746 | gettimeofday(&now, NULL); |
---|
747 | u32 = now.tv_sec; |
---|
748 | memcpy(rnd, &u32, 4); |
---|
749 | u32 = now.tv_usec; |
---|
750 | memcpy(rnd + 4, &u32, 4); |
---|
751 | u32 += now.tv_sec; |
---|
752 | memcpy(rnd + 8, &u32, 4); |
---|
753 | u16 = getpid(); |
---|
754 | memcpy(rnd + 12, &u16, 2); |
---|
755 | } |
---|
756 | |
---|
757 | u_int |
---|
758 | res_nrandomid(res_state statp) { |
---|
759 | struct timeval now; |
---|
760 | u_int16_t u16; |
---|
761 | MD5_CTX ctx; |
---|
762 | u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd; |
---|
763 | |
---|
764 | gettimeofday(&now, NULL); |
---|
765 | u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec); |
---|
766 | memcpy(rnd + 14, &u16, 2); |
---|
767 | #ifndef HAVE_MD5 |
---|
768 | MD5_Init(&ctx); |
---|
769 | MD5_Update(&ctx, rnd, 16); |
---|
770 | MD5_Final(rnd, &ctx); |
---|
771 | #else |
---|
772 | MD5Init(&ctx); |
---|
773 | MD5Update(&ctx, rnd, 16); |
---|
774 | MD5Final(rnd, &ctx); |
---|
775 | #endif |
---|
776 | memcpy(&u16, rnd + 14, 2); |
---|
777 | return ((u_int) u16); |
---|
778 | } |
---|
779 | |
---|
780 | /*% |
---|
781 | * This routine is for closing the socket if a virtual circuit is used and |
---|
782 | * the program wants to close it. This provides support for endhostent() |
---|
783 | * which expects to close the socket. |
---|
784 | * |
---|
785 | * This routine is not expected to be user visible. |
---|
786 | */ |
---|
787 | void |
---|
788 | res_nclose(res_state statp) { |
---|
789 | int ns; |
---|
790 | |
---|
791 | if (statp->_vcsock >= 0) { |
---|
792 | (void) _close(statp->_vcsock); |
---|
793 | statp->_vcsock = -1; |
---|
794 | statp->_flags &= ~(RES_F_VC | RES_F_CONN); |
---|
795 | } |
---|
796 | for (ns = 0; ns < statp->_u._ext.nscount; ns++) { |
---|
797 | if (statp->_u._ext.nssocks[ns] != -1) { |
---|
798 | (void) _close(statp->_u._ext.nssocks[ns]); |
---|
799 | statp->_u._ext.nssocks[ns] = -1; |
---|
800 | } |
---|
801 | } |
---|
802 | } |
---|
803 | |
---|
804 | void |
---|
805 | res_ndestroy(res_state statp) { |
---|
806 | res_nclose(statp); |
---|
807 | if (statp->_u._ext.ext != NULL) { |
---|
808 | free(statp->_u._ext.ext); |
---|
809 | statp->_u._ext.ext = NULL; |
---|
810 | } |
---|
811 | if (statp->_rnd != NULL) { |
---|
812 | free(statp->_rnd); |
---|
813 | statp->_rnd = NULL; |
---|
814 | } |
---|
815 | statp->options &= ~RES_INIT; |
---|
816 | } |
---|
817 | |
---|
818 | #ifndef _LIBC |
---|
819 | const char * |
---|
820 | res_get_nibblesuffix(res_state statp) { |
---|
821 | if (statp->_u._ext.ext) |
---|
822 | return (statp->_u._ext.ext->nsuffix); |
---|
823 | return ("ip6.arpa"); |
---|
824 | } |
---|
825 | |
---|
826 | const char * |
---|
827 | res_get_nibblesuffix2(res_state statp) { |
---|
828 | if (statp->_u._ext.ext) |
---|
829 | return (statp->_u._ext.ext->nsuffix2); |
---|
830 | return ("ip6.int"); |
---|
831 | } |
---|
832 | #endif |
---|
833 | |
---|
834 | void |
---|
835 | res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { |
---|
836 | int i, nserv; |
---|
837 | size_t size; |
---|
838 | |
---|
839 | /* close open servers */ |
---|
840 | res_nclose(statp); |
---|
841 | |
---|
842 | /* cause rtt times to be forgotten */ |
---|
843 | statp->_u._ext.nscount = 0; |
---|
844 | |
---|
845 | nserv = 0; |
---|
846 | for (i = 0; i < cnt && nserv < MAXNS; i++) { |
---|
847 | switch (set->sin.sin_family) { |
---|
848 | case AF_INET: |
---|
849 | size = sizeof(set->sin); |
---|
850 | if (statp->_u._ext.ext) |
---|
851 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], |
---|
852 | &set->sin, size); |
---|
853 | if (size <= sizeof(statp->nsaddr_list[nserv])) |
---|
854 | memcpy(&statp->nsaddr_list[nserv], |
---|
855 | &set->sin, size); |
---|
856 | else |
---|
857 | statp->nsaddr_list[nserv].sin_family = 0; |
---|
858 | nserv++; |
---|
859 | break; |
---|
860 | |
---|
861 | #ifdef HAS_INET6_STRUCTS |
---|
862 | case AF_INET6: |
---|
863 | size = sizeof(set->sin6); |
---|
864 | if (statp->_u._ext.ext) |
---|
865 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], |
---|
866 | &set->sin6, size); |
---|
867 | if (size <= sizeof(statp->nsaddr_list[nserv])) |
---|
868 | memcpy(&statp->nsaddr_list[nserv], |
---|
869 | &set->sin6, size); |
---|
870 | else |
---|
871 | statp->nsaddr_list[nserv].sin_family = 0; |
---|
872 | nserv++; |
---|
873 | break; |
---|
874 | #endif |
---|
875 | |
---|
876 | default: |
---|
877 | break; |
---|
878 | } |
---|
879 | set++; |
---|
880 | } |
---|
881 | statp->nscount = nserv; |
---|
882 | |
---|
883 | } |
---|
884 | |
---|
885 | int |
---|
886 | res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { |
---|
887 | int i; |
---|
888 | size_t size; |
---|
889 | u_int16_t family; |
---|
890 | |
---|
891 | for (i = 0; i < statp->nscount && i < cnt; i++) { |
---|
892 | if (statp->_u._ext.ext) |
---|
893 | family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; |
---|
894 | else |
---|
895 | family = statp->nsaddr_list[i].sin_family; |
---|
896 | |
---|
897 | switch (family) { |
---|
898 | case AF_INET: |
---|
899 | size = sizeof(set->sin); |
---|
900 | if (statp->_u._ext.ext) |
---|
901 | memcpy(&set->sin, |
---|
902 | &statp->_u._ext.ext->nsaddrs[i], |
---|
903 | size); |
---|
904 | else |
---|
905 | memcpy(&set->sin, &statp->nsaddr_list[i], |
---|
906 | size); |
---|
907 | break; |
---|
908 | |
---|
909 | #ifdef HAS_INET6_STRUCTS |
---|
910 | case AF_INET6: |
---|
911 | size = sizeof(set->sin6); |
---|
912 | if (statp->_u._ext.ext) |
---|
913 | memcpy(&set->sin6, |
---|
914 | &statp->_u._ext.ext->nsaddrs[i], |
---|
915 | size); |
---|
916 | else |
---|
917 | memcpy(&set->sin6, &statp->nsaddr_list[i], |
---|
918 | size); |
---|
919 | break; |
---|
920 | #endif |
---|
921 | |
---|
922 | default: |
---|
923 | set->sin.sin_family = 0; |
---|
924 | break; |
---|
925 | } |
---|
926 | set++; |
---|
927 | } |
---|
928 | return (statp->nscount); |
---|
929 | } |
---|
930 | |
---|
931 | /*! \file */ |
---|