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