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