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