OSDN Git Service

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