Libinfo-129.tar.gz
[apple/libinfo.git] / dns.subproj / res_init.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * ++Copyright++ 1985, 1989, 1993
26 * -
27 * Copyright (c) 1985, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 * -
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
66 *
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
74 * SOFTWARE.
75 * -
76 * --Copyright--
77 */
78
79 #if defined(LIBC_SCCS) && !defined(lint)
80 static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
81 static char rcsid[] = "$Id: res_init.c,v 1.5 2002/03/26 20:12:06 ajn Exp $";
82 #endif /* LIBC_SCCS and not lint */
83
84 #include <sys/param.h>
85 #include <sys/socket.h>
86 #include <sys/time.h>
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
89 #include <arpa/nameser.h>
90
91 #include <stdio.h>
92 #include <ctype.h>
93 #include <resolv.h>
94 #if defined(BSD) && (BSD >= 199103)
95 # include <unistd.h>
96 # include <stdlib.h>
97 # include <string.h>
98 #else
99 # include "portability.h"
100 #endif
101
102 /*-------------------------------------- info about "sortlist" --------------
103 * Marc Majka 1994/04/16
104 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
105 *
106 * NetInfo resolver configuration directory support.
107 *
108 * Allow a NetInfo directory to be created in the hierarchy which
109 * contains the same information as the resolver configuration file.
110 *
111 * - The local domain name is stored as the value of the "domain" property.
112 * - The Internet address(es) of the name server(s) are stored as values
113 * of the "nameserver" property.
114 * - The name server addresses are stored as values of the "nameserver"
115 * property.
116 * - The search list for host-name lookup is stored as values of the
117 * "search" property.
118 * - The sortlist comprised of IP address netmask pairs are stored as
119 * values of the "sortlist" property. The IP address and optional netmask
120 * should be seperated by a slash (/) or ampersand (&) character.
121 * - Internal resolver variables can be set from the value of the "options"
122 * property.
123 */
124 #if defined(__APPLE__) && defined(CONFIGURE_RESOLVER_FROM_NETINFO)
125 # include <netinfo/ni.h>
126 # define NI_PATH_RESCONF "/locations/resolver"
127 # define NI_TIMEOUT 10
128 static int netinfo_res_init __P((int *haveenv, int *havesearch));
129 #endif
130
131 #if defined(USE_OPTIONS_H)
132 # include "options.h"
133 #endif
134
135 static void res_setoptions __P((char *, char *));
136
137 #ifdef RESOLVSORT
138 static const char sort_mask[] = "/&";
139 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
140 static u_int32_t net_mask __P((struct in_addr));
141 #endif
142
143 #if !defined(isascii) /* XXX - could be a function */
144 # define isascii(c) (!(c & 0200))
145 #endif
146
147 /*
148 * Resolver state default settings.
149 */
150
151 #if defined(__APPLE__)
152 extern
153 #endif /* NeXT */
154 struct __res_state _res;
155
156 /*
157 * Set up default settings. If the configuration file exist, the values
158 * there will have precedence. Otherwise, the server address is set to
159 * INADDR_ANY and the default domain name comes from the gethostname().
160 *
161 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
162 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
163 * since it was noted that INADDR_ANY actually meant ``the first interface
164 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
165 * it had to be "up" in order for you to reach your own name server. It
166 * was later decided that since the recommended practice is to always
167 * install local static routes through 127.0.0.1 for all your network
168 * interfaces, that we could solve this problem without a code change.
169 *
170 * The configuration file should always be used, since it is the only way
171 * to specify a default domain. If you are running a server on your local
172 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
173 * in the configuration file.
174 *
175 * Return 0 if completes successfully, -1 on error
176 */
177 int
178 res_init()
179 {
180 register FILE *fp;
181 register char *cp, **pp;
182 register int n;
183 char buf[BUFSIZ];
184 int nserv = 0; /* number of nameserver records read from file */
185 int haveenv = 0;
186 int havesearch = 0;
187 #ifdef RESOLVSORT
188 int nsort = 0;
189 char *net;
190 #endif
191 #ifndef RFC1535
192 int dots;
193 #endif
194
195 /*
196 * These three fields used to be statically initialized. This made
197 * it hard to use this code in a shared library. It is necessary,
198 * now that we're doing dynamic initialization here, that we preserve
199 * the old semantics: if an application modifies one of these three
200 * fields of _res before res_init() is called, res_init() will not
201 * alter them. Of course, if an application is setting them to
202 * _zero_ before calling res_init(), hoping to override what used
203 * to be the static default, we can't detect it and unexpected results
204 * will follow. Zero for any of these fields would make no sense,
205 * so one can safely assume that the applications were already getting
206 * unexpected results.
207 *
208 * _res.options is tricky since some apps were known to diddle the bits
209 * before res_init() was first called. We can't replicate that semantic
210 * with dynamic initialization (they may have turned bits off that are
211 * set in RES_DEFAULT). Our solution is to declare such applications
212 * "broken". They could fool us by setting RES_INIT but none do (yet).
213 */
214 if (!_res.retrans)
215 _res.retrans = RES_TIMEOUT;
216 if (!_res.retry)
217 _res.retry = 4;
218 if (!(_res.options & RES_INIT))
219 _res.options = RES_DEFAULT;
220
221 /*
222 * This one used to initialize implicitly to zero, so unless the app
223 * has set it to something in particular, we can randomize it now.
224 */
225 if (!_res.id)
226 _res.id = res_randomid();
227
228 #ifdef USELOOPBACK
229 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
230 #else
231 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
232 #endif
233 _res.nsaddr.sin_family = AF_INET;
234 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
235 _res.nscount = 1;
236 _res.ndots = 1;
237 _res.pfcode = 0;
238
239 /* Allow user to override the local domain definition */
240 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
241 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
242 haveenv++;
243
244 /*
245 * Set search list to be blank-separated strings
246 * from rest of env value. Permits users of LOCALDOMAIN
247 * to still have a search list, and anyone to set the
248 * one that they want to use as an individual (even more
249 * important now that the rfc1535 stuff restricts searches)
250 */
251 cp = _res.defdname;
252 pp = _res.dnsrch;
253 *pp++ = cp;
254 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
255 if (*cp == '\n') /* silly backwards compat */
256 break;
257 else if (*cp == ' ' || *cp == '\t') {
258 *cp = 0;
259 n = 1;
260 } else if (n) {
261 *pp++ = cp;
262 n = 0;
263 havesearch = 1;
264 }
265 }
266 /* null terminate last domain if there are excess */
267 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
268 cp++;
269 *cp = '\0';
270 *pp++ = 0;
271 }
272
273 #define MATCH(line, name) \
274 (!strncmp(line, name, sizeof(name) - 1) && \
275 (line[sizeof(name) - 1] == ' ' || \
276 line[sizeof(name) - 1] == '\t'))
277
278 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
279 /* read the config file */
280 while (fgets(buf, sizeof(buf), fp) != NULL) {
281 /* skip comments */
282 if (*buf == ';' || *buf == '#')
283 continue;
284 /* read default domain name */
285 if (MATCH(buf, "domain")) {
286 if (haveenv) /* skip if have from environ */
287 continue;
288 cp = buf + sizeof("domain") - 1;
289 while (*cp == ' ' || *cp == '\t')
290 cp++;
291 if ((*cp == '\0') || (*cp == '\n'))
292 continue;
293 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
294 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
295 *cp = '\0';
296 havesearch = 0;
297 continue;
298 }
299 /* set search list */
300 if (MATCH(buf, "search")) {
301 if (haveenv) /* skip if have from environ */
302 continue;
303 cp = buf + sizeof("search") - 1;
304 while (*cp == ' ' || *cp == '\t')
305 cp++;
306 if ((*cp == '\0') || (*cp == '\n'))
307 continue;
308 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
309 if ((cp = strchr(_res.defdname, '\n')) != NULL)
310 *cp = '\0';
311 /*
312 * Set search list to be blank-separated strings
313 * on rest of line.
314 */
315 cp = _res.defdname;
316 pp = _res.dnsrch;
317 *pp++ = cp;
318 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
319 if (*cp == ' ' || *cp == '\t') {
320 *cp = 0;
321 n = 1;
322 } else if (n) {
323 *pp++ = cp;
324 n = 0;
325 }
326 }
327 /* null terminate last domain if there are excess */
328 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
329 cp++;
330 *cp = '\0';
331 *pp++ = 0;
332 havesearch = 1;
333 continue;
334 }
335 /* read nameservers to query */
336 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
337 struct in_addr a;
338
339 cp = buf + sizeof("nameserver") - 1;
340 while (*cp == ' ' || *cp == '\t')
341 cp++;
342 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
343 _res.nsaddr_list[nserv].sin_addr = a;
344 _res.nsaddr_list[nserv].sin_family = AF_INET;
345 _res.nsaddr_list[nserv].sin_port =
346 htons(NAMESERVER_PORT);
347 nserv++;
348 }
349 continue;
350 }
351 #ifdef RESOLVSORT
352 if (MATCH(buf, "sortlist")) {
353 struct in_addr a;
354
355 cp = buf + sizeof("sortlist") - 1;
356 while (nsort < MAXRESOLVSORT) {
357 while (*cp == ' ' || *cp == '\t')
358 cp++;
359 if (*cp == '\0' || *cp == '\n' || *cp == ';')
360 break;
361 net = cp;
362 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
363 isascii(*cp) && !isspace(*cp))
364 cp++;
365 n = *cp;
366 *cp = 0;
367 if (inet_aton(net, &a)) {
368 _res.sort_list[nsort].addr = a;
369 if (ISSORTMASK(n)) {
370 *cp++ = n;
371 net = cp;
372 while (*cp && *cp != ';' &&
373 isascii(*cp) && !isspace(*cp))
374 cp++;
375 n = *cp;
376 *cp = 0;
377 if (inet_aton(net, &a)) {
378 _res.sort_list[nsort].mask = a.s_addr;
379 } else {
380 _res.sort_list[nsort].mask =
381 net_mask(_res.sort_list[nsort].addr);
382 }
383 } else {
384 _res.sort_list[nsort].mask =
385 net_mask(_res.sort_list[nsort].addr);
386 }
387 nsort++;
388 }
389 *cp = n;
390 }
391 continue;
392 }
393 #endif
394 if (MATCH(buf, "options")) {
395 res_setoptions(buf + sizeof("options") - 1, "conf");
396 continue;
397 }
398 }
399 if (nserv > 1)
400 _res.nscount = nserv;
401 #ifdef RESOLVSORT
402 _res.nsort = nsort;
403 #endif
404 (void) fclose(fp);
405 }
406 #if defined(__APPLE__) && defined(CONFIGURE_RESOLVER_FROM_NETINFO)
407 else netinfo_res_init(&haveenv, &havesearch);
408 #endif
409
410 if (_res.defdname[0] == 0 &&
411 gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
412 (cp = strchr(buf, '.')) != NULL)
413 strcpy(_res.defdname, cp + 1);
414
415 /* find components of local domain that might be searched */
416 if (havesearch == 0) {
417 pp = _res.dnsrch;
418 *pp++ = _res.defdname;
419 *pp = NULL;
420
421 #ifndef RFC1535
422 dots = 0;
423 for (cp = _res.defdname; *cp; cp++)
424 dots += (*cp == '.');
425
426 cp = _res.defdname;
427 while (pp < _res.dnsrch + MAXDFLSRCH) {
428 if (dots < LOCALDOMAINPARTS)
429 break;
430 cp = strchr(cp, '.') + 1; /* we know there is one */
431 *pp++ = cp;
432 dots--;
433 }
434 *pp = NULL;
435 #ifdef DEBUG
436 if (_res.options & RES_DEBUG) {
437 printf(";; res_init()... default dnsrch list:\n");
438 for (pp = _res.dnsrch; *pp; pp++)
439 printf(";;\t%s\n", *pp);
440 printf(";;\t..END..\n");
441 }
442 #endif /* DEBUG */
443 #endif /* !RFC1535 */
444 }
445
446 if ((cp = getenv("RES_OPTIONS")) != NULL)
447 res_setoptions(cp, "env");
448 _res.options |= RES_INIT;
449 return (0);
450 }
451
452 static void
453 res_setoptions(options, source)
454 char *options, *source;
455 {
456 char *cp = options;
457 int i;
458
459 #ifdef DEBUG
460 if (_res.options & RES_DEBUG)
461 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
462 options, source);
463 #endif
464 while (*cp) {
465 /* skip leading and inner runs of spaces */
466 while (*cp == ' ' || *cp == '\t')
467 cp++;
468 /* search for and process individual options */
469 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
470 i = atoi(cp + sizeof("ndots:") - 1);
471 if (i <= RES_MAXNDOTS)
472 _res.ndots = i;
473 else
474 _res.ndots = RES_MAXNDOTS;
475 #ifdef DEBUG
476 if (_res.options & RES_DEBUG)
477 printf(";;\tndots=%d\n", _res.ndots);
478 #endif
479 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
480 #ifdef DEBUG
481 if (!(_res.options & RES_DEBUG)) {
482 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
483 options, source);
484 _res.options |= RES_DEBUG;
485 }
486 printf(";;\tdebug\n");
487 #endif
488 } else {
489 /* XXX - print a warning here? */
490 }
491 /* skip to next run of spaces */
492 while (*cp && *cp != ' ' && *cp != '\t')
493 cp++;
494 }
495 }
496
497 #ifdef RESOLVSORT
498 /* XXX - should really support CIDR which means explicit masks always. */
499 static u_int32_t
500 net_mask(in) /* XXX - should really use system's version of this */
501 struct in_addr in;
502 {
503 register u_int32_t i = ntohl(in.s_addr);
504
505 if (IN_CLASSA(i))
506 return (htonl(IN_CLASSA_NET));
507 else if (IN_CLASSB(i))
508 return (htonl(IN_CLASSB_NET));
509 return (htonl(IN_CLASSC_NET));
510 }
511 #endif
512
513 #if defined(__APPLE__) && defined(CONFIGURE_RESOLVER_FROM_NETINFO)
514 static int
515 netinfo_res_init(haveenv, havesearch)
516 int *haveenv;
517 int *havesearch;
518 {
519 register int n;
520 void *domain, *parent;
521 ni_id dir;
522 ni_status status;
523 ni_namelist nl;
524 int nserv = 0;
525 #ifdef RESOLVSORT
526 int nsort = 0;
527 #endif
528
529 status = ni_open(NULL, ".", &domain);
530 if (status == NI_OK) {
531 ni_setreadtimeout(domain, NI_TIMEOUT);
532 ni_setabort(domain, 1);
533
534 /* climb the NetInfo hierarchy to find a resolver directory */
535 while (status == NI_OK) {
536 status = ni_pathsearch(domain, &dir, NI_PATH_RESCONF);
537 if (status == NI_OK) {
538 /* found a resolver directory */
539
540 if (*haveenv == 0) {
541 /* get the default domain name */
542 status = ni_lookupprop(domain, &dir, "domain", &nl);
543 if (status == NI_OK && nl.ni_namelist_len > 0) {
544 (void)strncpy(_res.defdname,
545 nl.ni_namelist_val[0],
546 sizeof(_res.defdname) - 1);
547 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
548 ni_namelist_free(&nl);
549 *havesearch = 0;
550 }
551
552 /* get search list */
553 status = ni_lookupprop(domain, &dir, "search", &nl);
554 if (status == NI_OK && nl.ni_namelist_len > 0) {
555 (void)strncpy(_res.defdname,
556 nl.ni_namelist_val[0],
557 sizeof(_res.defdname) - 1);
558 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
559 /* copy */
560 for (n = 0;
561 n < nl.ni_namelist_len && n < MAXDNSRCH;
562 n++) {
563 /* duplicate up to MAXDNSRCH servers */
564 char *cp = nl.ni_namelist_val[n];
565 _res.dnsrch[n] =
566 strcpy((char *)malloc(strlen(cp) + 1), cp);
567 }
568 ni_namelist_free(&nl);
569 *havesearch = 1;
570 }
571 }
572
573 /* get list of nameservers */
574 status = ni_lookupprop(domain, &dir, "nameserver", &nl);
575 if (status == NI_OK && nl.ni_namelist_len > 0) {
576 /* copy up to MAXNS servers */
577 for (n = 0;
578 n < nl.ni_namelist_len && nserv < MAXNS;
579 n++) {
580 struct in_addr a;
581
582 if (inet_aton(nl.ni_namelist_val[n], &a)) {
583 _res.nsaddr_list[nserv].sin_addr = a;
584 _res.nsaddr_list[nserv].sin_family = AF_INET;
585 _res.nsaddr_list[nserv].sin_port =
586 htons(NAMESERVER_PORT);
587 nserv++;
588 }
589 }
590 ni_namelist_free(&nl);
591 }
592
593 if (nserv > 1)
594 _res.nscount = nserv;
595
596 #ifdef RESOLVSORT
597 /* get sort order */
598 status = ni_lookupprop(domain, &dir, "sortlist", &nl);
599 if (status == NI_OK && nl.ni_namelist_len > 0) {
600
601 /* copy up to MAXRESOLVSORT address/netmask pairs */
602 for (n = 0;
603 n < nl.ni_namelist_len && nsort < MAXRESOLVSORT;
604 n++) {
605 char ch = '\0';
606 char *cp;
607 const char *sp;
608 struct in_addr a;
609
610 cp = NULL;
611 for (sp = sort_mask; *sp; sp++) {
612 char *cp1;
613 cp1 = strchr(nl.ni_namelist_val[n], *sp);
614 if (cp && cp1)
615 cp = (cp < cp1)? cp : cp1;
616 else if (cp1)
617 cp = cp1;
618 }
619 if (cp != NULL) {
620 ch = *cp;
621 *cp = '\0';
622 break;
623 }
624 if (inet_aton(nl.ni_namelist_val[n], &a)) {
625 _res.sort_list[nsort].addr = a;
626 if (*cp && ISSORTMASK(ch)) {
627 *cp++ = ch;
628 if (inet_aton(cp, &a)) {
629 _res.sort_list[nsort].mask = a.s_addr;
630 } else {
631 _res.sort_list[nsort].mask =
632 net_mask(_res.sort_list[nsort].addr);
633 }
634 } else {
635 _res.sort_list[nsort].mask =
636 net_mask(_res.sort_list[nsort].addr);
637 }
638 nsort++;
639 }
640 }
641 ni_namelist_free(&nl);
642 }
643
644 _res.nsort = nsort;
645 #endif
646
647 /* get resolver options */
648 status = ni_lookupprop(domain, &dir, "options", &nl);
649 if (status == NI_OK && nl.ni_namelist_len > 0) {
650 res_setoptions(nl.ni_namelist_val[0], "conf");
651 ni_namelist_free(&nl);
652 }
653
654 ni_free(domain);
655 return(1); /* using DNS configuration from NetInfo */
656 }
657
658 status = ni_open(domain, "..", &parent);
659 ni_free(domain);
660 if (status == NI_OK)
661 domain = parent;
662 }
663 }
664 return(0); /* if not using DNS configuration from NetInfo */
665 }
666 #endif /* __APPLE__ */
667
668 u_int16_t
669 res_randomid()
670 {
671 struct timeval now;
672
673 gettimeofday(&now, NULL);
674 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
675 }