1*30d43af9Schristos /* $NetBSD: res_init.c,v 1.32 2021/09/30 12:35:55 christos Exp $ */
22b237084Schristos
32b237084Schristos /*
42b237084Schristos * Copyright (c) 1985, 1989, 1993
52b237084Schristos * The Regents of the University of California. All rights reserved.
62b237084Schristos *
72b237084Schristos * Redistribution and use in source and binary forms, with or without
82b237084Schristos * modification, are permitted provided that the following conditions
92b237084Schristos * are met:
102b237084Schristos * 1. Redistributions of source code must retain the above copyright
112b237084Schristos * notice, this list of conditions and the following disclaimer.
122b237084Schristos * 2. Redistributions in binary form must reproduce the above copyright
132b237084Schristos * notice, this list of conditions and the following disclaimer in the
142b237084Schristos * documentation and/or other materials provided with the distribution.
154d416d88Schristos * 3. Neither the name of the University nor the names of its contributors
162b237084Schristos * may be used to endorse or promote products derived from this software
172b237084Schristos * without specific prior written permission.
182b237084Schristos *
192b237084Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202b237084Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212b237084Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222b237084Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232b237084Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242b237084Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252b237084Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262b237084Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272b237084Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282b237084Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292b237084Schristos * SUCH DAMAGE.
302b237084Schristos */
312b237084Schristos
322b237084Schristos /*
332b237084Schristos * Portions Copyright (c) 1993 by Digital Equipment Corporation.
342b237084Schristos *
352b237084Schristos * Permission to use, copy, modify, and distribute this software for any
362b237084Schristos * purpose with or without fee is hereby granted, provided that the above
372b237084Schristos * copyright notice and this permission notice appear in all copies, and that
382b237084Schristos * the name of Digital Equipment Corporation not be used in advertising or
392b237084Schristos * publicity pertaining to distribution of the document or software without
402b237084Schristos * specific, written prior permission.
412b237084Schristos *
422b237084Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
432b237084Schristos * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
442b237084Schristos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
452b237084Schristos * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
462b237084Schristos * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
472b237084Schristos * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
482b237084Schristos * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
492b237084Schristos * SOFTWARE.
502b237084Schristos */
512b237084Schristos
522b237084Schristos /*
532b237084Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
542b237084Schristos * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
552b237084Schristos *
562b237084Schristos * Permission to use, copy, modify, and distribute this software for any
572b237084Schristos * purpose with or without fee is hereby granted, provided that the above
582b237084Schristos * copyright notice and this permission notice appear in all copies.
592b237084Schristos *
602b237084Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
612b237084Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
622b237084Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
632b237084Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
642b237084Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
652b237084Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
662b237084Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
672b237084Schristos */
682b237084Schristos
69ca31adbdSchristos #include <sys/cdefs.h>
702b237084Schristos #if defined(LIBC_SCCS) && !defined(lint)
71ca31adbdSchristos #ifdef notdef
722b237084Schristos static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
7359a755a4Schristos static const char rcsid[] = "Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp";
74ca31adbdSchristos #else
75*30d43af9Schristos __RCSID("$NetBSD: res_init.c,v 1.32 2021/09/30 12:35:55 christos Exp $");
76ca31adbdSchristos #endif
772b237084Schristos #endif /* LIBC_SCCS and not lint */
782b237084Schristos
792b237084Schristos #include "port_before.h"
802b237084Schristos
816f3786f4Schristos #include "namespace.h"
822b237084Schristos #include <sys/types.h>
832b237084Schristos #include <sys/param.h>
842b237084Schristos #include <sys/socket.h>
85249296c4Schristos #include <sys/stat.h>
862b237084Schristos #include <sys/time.h>
87249296c4Schristos #include <sys/event.h>
882b237084Schristos
892b237084Schristos #include <netinet/in.h>
902b237084Schristos #include <arpa/inet.h>
912b237084Schristos #include <arpa/nameser.h>
922b237084Schristos
932b237084Schristos #include <ctype.h>
942b237084Schristos #include <stdio.h>
952b237084Schristos #include <stdlib.h>
962b237084Schristos #include <string.h>
972b237084Schristos #include <unistd.h>
98249296c4Schristos #include <fcntl.h>
992b237084Schristos #include <netdb.h>
1002b237084Schristos
10159a755a4Schristos #define HAVE_MD5
10259a755a4Schristos #include <md5.h>
10359a755a4Schristos
10459a755a4Schristos #ifndef HAVE_MD5
10559a755a4Schristos # include "../dst/md5.h"
10659a755a4Schristos #else
10759a755a4Schristos # ifdef SOLARIS2
10859a755a4Schristos # include <sys/md5.h>
10959a755a4Schristos # endif
11059a755a4Schristos #endif
11159a755a4Schristos #ifndef _MD5_H_
11259a755a4Schristos # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
11359a755a4Schristos #endif
11459a755a4Schristos
1152b237084Schristos #include "port_after.h"
1162b237084Schristos
1175ce4c264Schristos #if 0
1186f3786f4Schristos #ifdef __weak_alias
1196f3786f4Schristos __weak_alias(res_ninit,_res_ninit)
1206f3786f4Schristos __weak_alias(res_randomid,__res_randomid)
1216f3786f4Schristos __weak_alias(res_nclose,_res_nclose)
1226f3786f4Schristos __weak_alias(res_ndestroy,_res_ndestroy)
1236f3786f4Schristos __weak_alias(res_get_nibblesuffix,__res_get_nibblesuffix)
1246f3786f4Schristos __weak_alias(res_get_nibblesuffix2,__res_get_nibblesuffix2)
1256f3786f4Schristos __weak_alias(res_getservers,__res_getservers)
1266f3786f4Schristos __weak_alias(res_setservers,__res_setservers)
1276f3786f4Schristos #endif
1285ce4c264Schristos #endif
1296f3786f4Schristos
1306f3786f4Schristos
1312b237084Schristos /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
1322b237084Schristos #include <resolv.h>
1332b237084Schristos
1342b237084Schristos #include "res_private.h"
1352b237084Schristos
1367ce5e09dSchristos #define RESOLVSORT
137d73eb73dSchristos /*% Options. Should all be left alone. */
13801267133Schristos #ifndef DEBUG
1392b237084Schristos #define DEBUG
14001267133Schristos #endif
1412b237084Schristos
142d73eb73dSchristos #ifdef SOLARIS2
143d73eb73dSchristos #include <sys/systeminfo.h>
144d73eb73dSchristos #endif
145d73eb73dSchristos
146504f8671Smatt static void res_setoptions(res_state, const char *, const char *);
1472b237084Schristos
1483873655bSchristos #ifdef RESOLVSORT
1492b237084Schristos static const char sort_mask[] = "/&";
1502b237084Schristos #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
151504f8671Smatt static uint32_t net_mask(struct in_addr);
1523873655bSchristos #endif
1532b237084Schristos
154d73eb73dSchristos #if !defined(isascii) /*%< XXX - could be a function */
1552b237084Schristos # define isascii(c) (!(c & 0200))
1562b237084Schristos #endif
1572b237084Schristos
158249296c4Schristos static struct timespec __res_conf_time;
159249296c4Schristos static const struct timespec ts = { 0, 0 };
160249296c4Schristos
161173d9091Schristos const char *__res_conf_name = _PATH_RESCONF;
162173d9091Schristos
1632b237084Schristos /*
1642b237084Schristos * Resolver state default settings.
1652b237084Schristos */
1662b237084Schristos
167d73eb73dSchristos /*%
1682b237084Schristos * Set up default settings. If the configuration file exist, the values
1692b237084Schristos * there will have precedence. Otherwise, the server address is set to
1702b237084Schristos * INADDR_ANY and the default domain name comes from the gethostname().
1712b237084Schristos *
1722b237084Schristos * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
1732b237084Schristos * rather than INADDR_ANY ("0.0.0.0") as the default name server address
1742b237084Schristos * since it was noted that INADDR_ANY actually meant ``the first interface
1752b237084Schristos * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
1762b237084Schristos * it had to be "up" in order for you to reach your own name server. It
1772b237084Schristos * was later decided that since the recommended practice is to always
1782b237084Schristos * install local static routes through 127.0.0.1 for all your network
1792b237084Schristos * interfaces, that we could solve this problem without a code change.
1802b237084Schristos *
1812b237084Schristos * The configuration file should always be used, since it is the only way
1822b237084Schristos * to specify a default domain. If you are running a server on your local
1832b237084Schristos * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
1842b237084Schristos * in the configuration file.
1852b237084Schristos *
1862b237084Schristos * Return 0 if completes successfully, -1 on error
1872b237084Schristos */
1882b237084Schristos int
res_ninit(res_state statp)1892b237084Schristos res_ninit(res_state statp) {
1902b237084Schristos return (__res_vinit(statp, 0));
1912b237084Schristos }
1922b237084Schristos
193*30d43af9Schristos static int
__res_kqinit(res_state statp)194*30d43af9Schristos __res_kqinit(res_state statp)
195*30d43af9Schristos {
196*30d43af9Schristos struct kevent kc;
197*30d43af9Schristos struct __res_state_ext *ext = statp->_u._ext.ext;
198*30d43af9Schristos
199*30d43af9Schristos ext->kq = kqueue1(O_CLOEXEC);
200*30d43af9Schristos ext->kqpid = getpid();
201*30d43af9Schristos EV_SET(&kc, ext->resfd, EVFILT_VNODE,
202*30d43af9Schristos EV_ADD|EV_ENABLE|EV_CLEAR, NOTE_DELETE|NOTE_WRITE| NOTE_EXTEND|
203*30d43af9Schristos NOTE_ATTRIB|NOTE_LINK|NOTE_RENAME|NOTE_REVOKE, 0, 0);
204*30d43af9Schristos (void)kevent(ext->kq, &kc, 1, NULL, 0, &ts);
205*30d43af9Schristos return ext->kq;
206*30d43af9Schristos }
207*30d43af9Schristos
208d73eb73dSchristos /*% This function has to be reachable by res_data.c but not publically. */
2092b237084Schristos int
__res_vinit(res_state statp,int preinit)2102b237084Schristos __res_vinit(res_state statp, int preinit) {
2112b237084Schristos register FILE *fp;
2122b237084Schristos register char *cp, **pp;
2132b237084Schristos register int n;
2142b237084Schristos char buf[BUFSIZ];
215d73eb73dSchristos int nserv = 0; /*%< number of nameserver records read from file */
2162b237084Schristos int haveenv = 0;
2172b237084Schristos int havesearch = 0;
2183873655bSchristos #ifdef RESOLVSORT
2192b237084Schristos int nsort = 0;
2202b237084Schristos char *net;
2213873655bSchristos #endif
2222b237084Schristos int dots;
2232b237084Schristos union res_sockaddr_union u[2];
2243873655bSchristos int maxns = MAXNS;
2253873655bSchristos
2263873655bSchristos RES_SET_H_ERRNO(statp, 0);
2273873655bSchristos
228249296c4Schristos if ((statp->options & RES_INIT) != 0U)
229249296c4Schristos res_ndestroy(statp);
230249296c4Schristos
2312b237084Schristos if (!preinit) {
2322b237084Schristos statp->retrans = RES_TIMEOUT;
2332b237084Schristos statp->retry = RES_DFLRETRY;
2342b237084Schristos statp->options = RES_DEFAULT;
235249296c4Schristos }
2362cca8c55Schristos statp->_rnd = malloc(16);
23759a755a4Schristos res_rndinit(statp);
23859a755a4Schristos statp->id = res_nrandomid(statp);
239233898bdSchristos
2402b237084Schristos memset(u, 0, sizeof(u));
2412b237084Schristos #ifdef USELOOPBACK
2422b237084Schristos u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
2432b237084Schristos #else
2442b237084Schristos u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
2452b237084Schristos #endif
2462b237084Schristos u[nserv].sin.sin_family = AF_INET;
2472b237084Schristos u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
2482b237084Schristos #ifdef HAVE_SA_LEN
2492b237084Schristos u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
2502b237084Schristos #endif
2512b237084Schristos nserv++;
2522b237084Schristos #ifdef HAS_INET6_STRUCTS
2532b237084Schristos #ifdef USELOOPBACK
2542b237084Schristos u[nserv].sin6.sin6_addr = in6addr_loopback;
2552b237084Schristos #else
2562b237084Schristos u[nserv].sin6.sin6_addr = in6addr_any;
2572b237084Schristos #endif
2582b237084Schristos u[nserv].sin6.sin6_family = AF_INET6;
2592b237084Schristos u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
2602b237084Schristos #ifdef HAVE_SA_LEN
2612b237084Schristos u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
2622b237084Schristos #endif
2632b237084Schristos nserv++;
2642b237084Schristos #endif
2652b237084Schristos statp->nscount = 0;
2662b237084Schristos statp->ndots = 1;
2672b237084Schristos statp->pfcode = 0;
2682b237084Schristos statp->_vcsock = -1;
2692b237084Schristos statp->_flags = 0;
2702b237084Schristos statp->qhook = NULL;
2712b237084Schristos statp->rhook = NULL;
2722b237084Schristos statp->_u._ext.nscount = 0;
2732b237084Schristos statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
2742b237084Schristos if (statp->_u._ext.ext != NULL) {
2752b237084Schristos memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
2762b237084Schristos statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
2772b237084Schristos strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
2782b237084Schristos strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
2793873655bSchristos } else {
2803873655bSchristos /*
2813873655bSchristos * Historically res_init() rarely, if at all, failed.
2823873655bSchristos * Examples and applications exist which do not check
2833873655bSchristos * our return code. Furthermore several applications
2843873655bSchristos * simply call us to get the systems domainname. So
285d860f590Swiz * rather than immediately fail here we store the
2863873655bSchristos * failure, which is returned later, in h_errno. And
2873873655bSchristos * prevent the collection of 'nameserver' information
2883873655bSchristos * by setting maxns to 0. Thus applications that fail
2893873655bSchristos * to check our return code wont be able to make
2903873655bSchristos * queries anyhow.
2913873655bSchristos */
2923873655bSchristos RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
2933873655bSchristos maxns = 0;
2943873655bSchristos }
2953873655bSchristos #ifdef RESOLVSORT
2962b237084Schristos statp->nsort = 0;
2973873655bSchristos #endif
2982b237084Schristos res_setservers(statp, u, nserv);
2992b237084Schristos
300d73eb73dSchristos #ifdef SOLARIS2
301d73eb73dSchristos /*
302d73eb73dSchristos * The old libresolv derived the defaultdomain from NIS/NIS+.
303d73eb73dSchristos * We want to keep this behaviour
304d73eb73dSchristos */
305d73eb73dSchristos {
306d73eb73dSchristos char buf[sizeof(statp->defdname)], *cp;
307d73eb73dSchristos int ret;
308d73eb73dSchristos
309d73eb73dSchristos if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
310d73eb73dSchristos (unsigned int)ret <= sizeof(buf)) {
311d73eb73dSchristos if (buf[0] == '+')
312d73eb73dSchristos buf[0] = '.';
313d73eb73dSchristos cp = strchr(buf, '.');
314d73eb73dSchristos cp = (cp == NULL) ? buf : (cp + 1);
3153873655bSchristos (void)strlcpy(statp->defdname, cp,
3163873655bSchristos sizeof(statp->defdname));
317d73eb73dSchristos }
318d73eb73dSchristos }
319d73eb73dSchristos #endif /* SOLARIS2 */
320d73eb73dSchristos
3212b237084Schristos /* Allow user to override the local domain definition */
3222b237084Schristos if ((cp = getenv("LOCALDOMAIN")) != NULL) {
3232b237084Schristos (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
3242b237084Schristos statp->defdname[sizeof(statp->defdname) - 1] = '\0';
3252b237084Schristos haveenv++;
3262b237084Schristos
3272b237084Schristos /*
3282b237084Schristos * Set search list to be blank-separated strings
3292b237084Schristos * from rest of env value. Permits users of LOCALDOMAIN
3302b237084Schristos * to still have a search list, and anyone to set the
3312b237084Schristos * one that they want to use as an individual (even more
3322b237084Schristos * important now that the rfc1535 stuff restricts searches)
3332b237084Schristos */
3342b237084Schristos cp = statp->defdname;
3352b237084Schristos pp = statp->dnsrch;
3362b237084Schristos *pp++ = cp;
3372b237084Schristos for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
338d73eb73dSchristos if (*cp == '\n') /*%< silly backwards compat */
3392b237084Schristos break;
3402b237084Schristos else if (*cp == ' ' || *cp == '\t') {
3412b237084Schristos *cp = 0;
3422b237084Schristos n = 1;
3432b237084Schristos } else if (n) {
3442b237084Schristos *pp++ = cp;
3452b237084Schristos n = 0;
3462b237084Schristos havesearch = 1;
3472b237084Schristos }
3482b237084Schristos }
3492b237084Schristos /* null terminate last domain if there are excess */
3502b237084Schristos while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
3512b237084Schristos cp++;
3522b237084Schristos *cp = '\0';
3532b237084Schristos *pp++ = 0;
3542b237084Schristos }
3552b237084Schristos
3562b237084Schristos #define MATCH(line, name) \
3572b237084Schristos (!strncmp(line, name, sizeof(name) - 1) && \
3582b237084Schristos (line[sizeof(name) - 1] == ' ' || \
3592b237084Schristos line[sizeof(name) - 1] == '\t'))
3602b237084Schristos
3612b237084Schristos nserv = 0;
362173d9091Schristos if ((fp = fopen(__res_conf_name, "re")) != NULL) {
363249296c4Schristos struct stat st;
364249296c4Schristos
3652b237084Schristos /* read the config file */
366c5e820caSchristos while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
3672b237084Schristos /* skip comments */
3682b237084Schristos if (*buf == ';' || *buf == '#')
3692b237084Schristos continue;
3702b237084Schristos /* read default domain name */
3712b237084Schristos if (MATCH(buf, "domain")) {
372d73eb73dSchristos if (haveenv) /*%< skip if have from environ */
3732b237084Schristos continue;
3742b237084Schristos cp = buf + sizeof("domain") - 1;
3752b237084Schristos while (*cp == ' ' || *cp == '\t')
3762b237084Schristos cp++;
3772b237084Schristos if ((*cp == '\0') || (*cp == '\n'))
3782b237084Schristos continue;
3792b237084Schristos strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
3802b237084Schristos statp->defdname[sizeof(statp->defdname) - 1] = '\0';
3812b237084Schristos if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
3822b237084Schristos *cp = '\0';
3832b237084Schristos havesearch = 0;
3842b237084Schristos continue;
3852b237084Schristos }
3862b237084Schristos /* set search list */
3872b237084Schristos if (MATCH(buf, "search")) {
388d73eb73dSchristos if (haveenv) /*%< skip if have from environ */
3892b237084Schristos continue;
3902b237084Schristos cp = buf + sizeof("search") - 1;
3912b237084Schristos while (*cp == ' ' || *cp == '\t')
3922b237084Schristos cp++;
3932b237084Schristos if ((*cp == '\0') || (*cp == '\n'))
3942b237084Schristos continue;
3952b237084Schristos strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
3962b237084Schristos statp->defdname[sizeof(statp->defdname) - 1] = '\0';
3972b237084Schristos if ((cp = strchr(statp->defdname, '\n')) != NULL)
3982b237084Schristos *cp = '\0';
3992b237084Schristos /*
4002b237084Schristos * Set search list to be blank-separated strings
4012b237084Schristos * on rest of line.
4022b237084Schristos */
4032b237084Schristos cp = statp->defdname;
4042b237084Schristos pp = statp->dnsrch;
4052b237084Schristos *pp++ = cp;
4062b237084Schristos for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
4072b237084Schristos if (*cp == ' ' || *cp == '\t') {
4082b237084Schristos *cp = 0;
4092b237084Schristos n = 1;
4102b237084Schristos } else if (n) {
4112b237084Schristos *pp++ = cp;
4122b237084Schristos n = 0;
4132b237084Schristos }
4142b237084Schristos }
4152b237084Schristos /* null terminate last domain if there are excess */
4162b237084Schristos while (*cp != '\0' && *cp != ' ' && *cp != '\t')
4172b237084Schristos cp++;
4182b237084Schristos *cp = '\0';
4192b237084Schristos *pp++ = 0;
4202b237084Schristos havesearch = 1;
4212b237084Schristos continue;
4222b237084Schristos }
4232b237084Schristos /* read nameservers to query */
4243873655bSchristos if (MATCH(buf, "nameserver") && nserv < maxns) {
4252b237084Schristos struct addrinfo hints, *ai;
4262b237084Schristos char sbuf[NI_MAXSERV];
4272b237084Schristos const size_t minsiz =
4282b237084Schristos sizeof(statp->_u._ext.ext->nsaddrs[0]);
4292b237084Schristos
4302b237084Schristos cp = buf + sizeof("nameserver") - 1;
4312b237084Schristos while (*cp == ' ' || *cp == '\t')
4322b237084Schristos cp++;
4332b237084Schristos cp[strcspn(cp, ";# \t\n")] = '\0';
4342b237084Schristos if ((*cp != '\0') && (*cp != '\n')) {
4352b237084Schristos memset(&hints, 0, sizeof(hints));
4362b237084Schristos hints.ai_family = PF_UNSPEC;
4372b237084Schristos hints.ai_socktype = SOCK_DGRAM; /*dummy*/
4382b237084Schristos hints.ai_flags = AI_NUMERICHOST;
4392b237084Schristos sprintf(sbuf, "%u", NAMESERVER_PORT);
4402b237084Schristos if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
4412b237084Schristos ai->ai_addrlen <= minsiz) {
4422b237084Schristos if (statp->_u._ext.ext != NULL) {
4432b237084Schristos memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
4442b237084Schristos ai->ai_addr, ai->ai_addrlen);
4452b237084Schristos }
4462b237084Schristos if (ai->ai_addrlen <=
4472b237084Schristos sizeof(statp->nsaddr_list[nserv])) {
4482b237084Schristos memcpy(&statp->nsaddr_list[nserv],
4492b237084Schristos ai->ai_addr, ai->ai_addrlen);
4502b237084Schristos } else
4512b237084Schristos statp->nsaddr_list[nserv].sin_family = 0;
4522b237084Schristos freeaddrinfo(ai);
4532b237084Schristos nserv++;
4542b237084Schristos }
4552b237084Schristos }
4562b237084Schristos continue;
4572b237084Schristos }
4583873655bSchristos #ifdef RESOLVSORT
4592b237084Schristos if (MATCH(buf, "sortlist")) {
4602b237084Schristos struct in_addr a;
4612b237084Schristos
4622b237084Schristos cp = buf + sizeof("sortlist") - 1;
4632b237084Schristos while (nsort < MAXRESOLVSORT) {
4642b237084Schristos while (*cp == ' ' || *cp == '\t')
4652b237084Schristos cp++;
4662b237084Schristos if (*cp == '\0' || *cp == '\n' || *cp == ';')
4672b237084Schristos break;
4682b237084Schristos net = cp;
4692b237084Schristos while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
4702b237084Schristos isascii(*cp) && !isspace((unsigned char)*cp))
4712b237084Schristos cp++;
4722b237084Schristos n = *cp;
4732b237084Schristos *cp = 0;
4742b237084Schristos if (inet_aton(net, &a)) {
4752b237084Schristos statp->sort_list[nsort].addr = a;
4762b237084Schristos if (ISSORTMASK(n)) {
4772b237084Schristos *cp++ = n;
4782b237084Schristos net = cp;
4792b237084Schristos while (*cp && *cp != ';' &&
4802b237084Schristos isascii(*cp) &&
4812b237084Schristos !isspace((unsigned char)*cp))
4822b237084Schristos cp++;
4832b237084Schristos n = *cp;
4842b237084Schristos *cp = 0;
4852b237084Schristos if (inet_aton(net, &a)) {
4862b237084Schristos statp->sort_list[nsort].mask = a.s_addr;
4872b237084Schristos } else {
4882b237084Schristos statp->sort_list[nsort].mask =
4892b237084Schristos net_mask(statp->sort_list[nsort].addr);
4902b237084Schristos }
4912b237084Schristos } else {
4922b237084Schristos statp->sort_list[nsort].mask =
4932b237084Schristos net_mask(statp->sort_list[nsort].addr);
4942b237084Schristos }
4952b237084Schristos nsort++;
4962b237084Schristos }
4972b237084Schristos *cp = n;
4982b237084Schristos }
4992b237084Schristos continue;
5002b237084Schristos }
5013873655bSchristos #endif
5022b237084Schristos if (MATCH(buf, "options")) {
5032b237084Schristos res_setoptions(statp, buf + sizeof("options") - 1, "conf");
5042b237084Schristos continue;
5052b237084Schristos }
5062b237084Schristos }
5072b237084Schristos if (nserv > 0)
5082b237084Schristos statp->nscount = nserv;
5093873655bSchristos #ifdef RESOLVSORT
5102b237084Schristos statp->nsort = nsort;
5113873655bSchristos #endif
5126ad75611Schristos statp->_u._ext.ext->resfd = fcntl(fileno(fp), F_DUPFD_CLOEXEC, 0);
5132b237084Schristos (void) fclose(fp);
514249296c4Schristos if (fstat(statp->_u._ext.ext->resfd, &st) != -1)
515249296c4Schristos __res_conf_time = statp->_u._ext.ext->res_conf_time =
516249296c4Schristos st.st_mtimespec;
517*30d43af9Schristos __res_kqinit(statp);
518249296c4Schristos } else {
519249296c4Schristos statp->_u._ext.ext->kq = -1;
520249296c4Schristos statp->_u._ext.ext->resfd = -1;
5212b237084Schristos }
5222b237084Schristos /*
5232b237084Schristos * Last chance to get a nameserver. This should not normally
5242b237084Schristos * be necessary
5252b237084Schristos */
5262b237084Schristos #ifdef NO_RESOLV_CONF
5272b237084Schristos if(nserv == 0)
5282b237084Schristos nserv = get_nameservers(statp);
5292b237084Schristos #endif
5302b237084Schristos
5312b237084Schristos if (statp->defdname[0] == 0 &&
5322b237084Schristos gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
5332b237084Schristos (cp = strchr(buf, '.')) != NULL)
5342b237084Schristos strcpy(statp->defdname, cp + 1);
5352b237084Schristos
5362b237084Schristos /* find components of local domain that might be searched */
5372b237084Schristos if (havesearch == 0) {
5382b237084Schristos pp = statp->dnsrch;
5392b237084Schristos *pp++ = statp->defdname;
5402b237084Schristos *pp = NULL;
5412b237084Schristos
5422b237084Schristos dots = 0;
5432b237084Schristos for (cp = statp->defdname; *cp; cp++)
5442b237084Schristos dots += (*cp == '.');
5452b237084Schristos
5462b237084Schristos cp = statp->defdname;
5472b237084Schristos while (pp < statp->dnsrch + MAXDFLSRCH) {
5482b237084Schristos if (dots < LOCALDOMAINPARTS)
5492b237084Schristos break;
550d73eb73dSchristos cp = strchr(cp, '.') + 1; /*%< we know there is one */
5512b237084Schristos *pp++ = cp;
5522b237084Schristos dots--;
5532b237084Schristos }
5542b237084Schristos *pp = NULL;
5552b237084Schristos #ifdef DEBUG
5562b237084Schristos if (statp->options & RES_DEBUG) {
5572b237084Schristos printf(";; res_init()... default dnsrch list:\n");
5582b237084Schristos for (pp = statp->dnsrch; *pp; pp++)
5592b237084Schristos printf(";;\t%s\n", *pp);
5602b237084Schristos printf(";;\t..END..\n");
5612b237084Schristos }
5622b237084Schristos #endif
5632b237084Schristos }
5642b237084Schristos
5652b237084Schristos if ((cp = getenv("RES_OPTIONS")) != NULL)
5662b237084Schristos res_setoptions(statp, cp, "env");
5672b237084Schristos statp->options |= RES_INIT;
5683873655bSchristos return (statp->res_h_errno);
5692b237084Schristos }
5702b237084Schristos
571e942b6ecSchristos int
res_check(res_state statp,struct timespec * mtime)572e942b6ecSchristos res_check(res_state statp, struct timespec *mtime)
573249296c4Schristos {
574249296c4Schristos /*
575249296c4Schristos * If the times are equal, then we check if there
576249296c4Schristos * was a kevent related to resolv.conf and reload.
577249296c4Schristos * If the times are not equal, then we don't bother
578249296c4Schristos * to check the kevent, because another thread already
579249296c4Schristos * did, loaded and changed the time.
580249296c4Schristos */
581249296c4Schristos if (timespeccmp(&statp->_u._ext.ext->res_conf_time,
582249296c4Schristos &__res_conf_time, ==)) {
583249296c4Schristos struct kevent ke;
584249296c4Schristos if (statp->_u._ext.ext->kq == -1)
585e942b6ecSchristos goto out;
586*30d43af9Schristos if (statp->_u._ext.ext->kqpid != getpid() &&
587*30d43af9Schristos __res_kqinit(statp) == -1)
588*30d43af9Schristos goto out;
589249296c4Schristos
590249296c4Schristos switch (kevent(statp->_u._ext.ext->kq, NULL, 0, &ke, 1, &ts)) {
591249296c4Schristos case 0:
592249296c4Schristos case -1:
593e942b6ecSchristos out:
594e942b6ecSchristos if (mtime)
595e942b6ecSchristos *mtime = __res_conf_time;
596e942b6ecSchristos return 0;
597249296c4Schristos default:
598249296c4Schristos break;
599249296c4Schristos }
600249296c4Schristos }
601249296c4Schristos (void)__res_vinit(statp, 0);
602e942b6ecSchristos if (mtime)
603e942b6ecSchristos *mtime = __res_conf_time;
604e942b6ecSchristos return 1;
605249296c4Schristos }
606249296c4Schristos
6072b237084Schristos static void
res_setoptions(res_state statp,const char * options,const char * source)6082b237084Schristos res_setoptions(res_state statp, const char *options, const char *source)
6092b237084Schristos {
6102b237084Schristos const char *cp = options;
6112b237084Schristos int i;
612c5e820caSchristos size_t j;
6132b237084Schristos struct __res_state_ext *ext = statp->_u._ext.ext;
6142b237084Schristos
6152b237084Schristos #ifdef DEBUG
6162b237084Schristos if (statp->options & RES_DEBUG)
6172b237084Schristos printf(";; res_setoptions(\"%s\", \"%s\")...\n",
6182b237084Schristos options, source);
6192b237084Schristos #endif
6202b237084Schristos while (*cp) {
6212b237084Schristos /* skip leading and inner runs of spaces */
6222b237084Schristos while (*cp == ' ' || *cp == '\t')
6232b237084Schristos cp++;
6242b237084Schristos /* search for and process individual options */
6252b237084Schristos if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
6262b237084Schristos i = atoi(cp + sizeof("ndots:") - 1);
6272b237084Schristos if (i <= RES_MAXNDOTS)
6282b237084Schristos statp->ndots = i;
6292b237084Schristos else
6302b237084Schristos statp->ndots = RES_MAXNDOTS;
6312b237084Schristos #ifdef DEBUG
6322b237084Schristos if (statp->options & RES_DEBUG)
6332b237084Schristos printf(";;\tndots=%d\n", statp->ndots);
6342b237084Schristos #endif
6352b237084Schristos } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
6362b237084Schristos i = atoi(cp + sizeof("timeout:") - 1);
6372b237084Schristos if (i <= RES_MAXRETRANS)
6382b237084Schristos statp->retrans = i;
6392b237084Schristos else
6402b237084Schristos statp->retrans = RES_MAXRETRANS;
6412b237084Schristos #ifdef DEBUG
6422b237084Schristos if (statp->options & RES_DEBUG)
6432b237084Schristos printf(";;\ttimeout=%d\n", statp->retrans);
6442b237084Schristos #endif
645d73eb73dSchristos #ifdef SOLARIS2
646d73eb73dSchristos } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
647d73eb73dSchristos /*
648d73eb73dSchristos * For backward compatibility, 'retrans' is
649d73eb73dSchristos * supported as an alias for 'timeout', though
650d73eb73dSchristos * without an imposed maximum.
651d73eb73dSchristos */
652d73eb73dSchristos statp->retrans = atoi(cp + sizeof("retrans:") - 1);
653d73eb73dSchristos } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
654d73eb73dSchristos /*
655d73eb73dSchristos * For backward compatibility, 'retry' is
656d73eb73dSchristos * supported as an alias for 'attempts', though
657d73eb73dSchristos * without an imposed maximum.
658d73eb73dSchristos */
659d73eb73dSchristos statp->retry = atoi(cp + sizeof("retry:") - 1);
660d73eb73dSchristos #endif /* SOLARIS2 */
6612b237084Schristos } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
6622b237084Schristos i = atoi(cp + sizeof("attempts:") - 1);
6632b237084Schristos if (i <= RES_MAXRETRY)
6642b237084Schristos statp->retry = i;
6652b237084Schristos else
6662b237084Schristos statp->retry = RES_MAXRETRY;
6672b237084Schristos #ifdef DEBUG
6682b237084Schristos if (statp->options & RES_DEBUG)
6692b237084Schristos printf(";;\tattempts=%d\n", statp->retry);
6702b237084Schristos #endif
6712b237084Schristos } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
6722b237084Schristos #ifdef DEBUG
6732b237084Schristos if (!(statp->options & RES_DEBUG)) {
6742b237084Schristos printf(";; res_setoptions(\"%s\", \"%s\")..\n",
6752b237084Schristos options, source);
6762b237084Schristos statp->options |= RES_DEBUG;
6772b237084Schristos }
6782b237084Schristos printf(";;\tdebug\n");
6792b237084Schristos #endif
6802b237084Schristos } else if (!strncmp(cp, "no_tld_query",
6812b237084Schristos sizeof("no_tld_query") - 1) ||
6822b237084Schristos !strncmp(cp, "no-tld-query",
6832b237084Schristos sizeof("no-tld-query") - 1)) {
6842b237084Schristos statp->options |= RES_NOTLDQUERY;
6852b237084Schristos } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
6862b237084Schristos statp->options |= RES_USE_INET6;
6872b237084Schristos } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
6882b237084Schristos statp->options |= RES_ROTATE;
6892b237084Schristos } else if (!strncmp(cp, "no-check-names",
6902b237084Schristos sizeof("no-check-names") - 1)) {
6912b237084Schristos statp->options |= RES_NOCHECKNAME;
6922a7b0a49Schristos } else if (!strncmp(cp, "check-names",
6932a7b0a49Schristos sizeof("check-names") - 1)) {
6942a7b0a49Schristos statp->options &= ~RES_NOCHECKNAME;
6952b237084Schristos }
6962b237084Schristos #ifdef RES_USE_EDNS0
6972b237084Schristos else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
6982b237084Schristos statp->options |= RES_USE_EDNS0;
6992b237084Schristos }
7002b237084Schristos #endif
7012b237084Schristos else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
7022b237084Schristos statp->options |= RES_USE_DNAME;
7032b237084Schristos }
7042b237084Schristos else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
7052b237084Schristos if (ext == NULL)
7062b237084Schristos goto skip;
7072b237084Schristos cp += sizeof("nibble:") - 1;
708c5e820caSchristos j = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
709c5e820caSchristos strncpy(ext->nsuffix, cp, j);
710c5e820caSchristos ext->nsuffix[j] = '\0';
7112b237084Schristos }
7122b237084Schristos else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
7132b237084Schristos if (ext == NULL)
7142b237084Schristos goto skip;
7152b237084Schristos cp += sizeof("nibble2:") - 1;
716c5e820caSchristos j = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
717c5e820caSchristos strncpy(ext->nsuffix2, cp, j);
718c5e820caSchristos ext->nsuffix2[j] = '\0';
7192b237084Schristos }
7202b237084Schristos else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
7212b237084Schristos cp += sizeof("v6revmode:") - 1;
7222b237084Schristos /* "nibble" and "bitstring" used to be valid */
7232b237084Schristos if (!strncmp(cp, "single", sizeof("single") - 1)) {
7242b237084Schristos statp->options |= RES_NO_NIBBLE2;
7252b237084Schristos } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
7262b237084Schristos statp->options &=
7272b237084Schristos ~RES_NO_NIBBLE2;
7282b237084Schristos }
7292b237084Schristos }
7302b237084Schristos else {
7312b237084Schristos /* XXX - print a warning here? */
7322b237084Schristos }
7332b237084Schristos skip:
7342b237084Schristos /* skip to next run of spaces */
7352b237084Schristos while (*cp && *cp != ' ' && *cp != '\t')
7362b237084Schristos cp++;
7372b237084Schristos }
7382b237084Schristos }
7392b237084Schristos
7403873655bSchristos #ifdef RESOLVSORT
7412b237084Schristos /* XXX - should really support CIDR which means explicit masks always. */
742504f8671Smatt static uint32_t
net_mask(struct in_addr in)743504f8671Smatt net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
7442b237084Schristos {
745504f8671Smatt register uint32_t i = ntohl(in.s_addr);
7462b237084Schristos
7472b237084Schristos if (IN_CLASSA(i))
7482b237084Schristos return (htonl(IN_CLASSA_NET));
7492b237084Schristos else if (IN_CLASSB(i))
7502b237084Schristos return (htonl(IN_CLASSB_NET));
7512b237084Schristos return (htonl(IN_CLASSC_NET));
7522b237084Schristos }
7533873655bSchristos #endif
7542b237084Schristos
7552cca8c55Schristos static u_char srnd[16];
7562cca8c55Schristos
75759a755a4Schristos void
res_rndinit(res_state statp)75859a755a4Schristos res_rndinit(res_state statp)
75959a755a4Schristos {
7602b237084Schristos struct timeval now;
761504f8671Smatt uint32_t u32;
762504f8671Smatt uint16_t u16;
7632cca8c55Schristos u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
7642b237084Schristos
7652b237084Schristos gettimeofday(&now, NULL);
766504f8671Smatt u32 = (uint32_t)now.tv_sec;
7672cca8c55Schristos memcpy(rnd, &u32, 4);
76859a755a4Schristos u32 = now.tv_usec;
7692cca8c55Schristos memcpy(rnd + 4, &u32, 4);
770504f8671Smatt u32 += (uint32_t)now.tv_sec;
7712cca8c55Schristos memcpy(rnd + 8, &u32, 4);
77259a755a4Schristos u16 = getpid();
7732cca8c55Schristos memcpy(rnd + 12, &u16, 2);
77459a755a4Schristos }
77559a755a4Schristos
77659a755a4Schristos u_int
res_nrandomid(res_state statp)777504f8671Smatt res_nrandomid(res_state statp)
778504f8671Smatt {
77959a755a4Schristos struct timeval now;
780504f8671Smatt uint16_t u16;
78159a755a4Schristos MD5_CTX ctx;
7822cca8c55Schristos u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
78359a755a4Schristos
78459a755a4Schristos gettimeofday(&now, NULL);
785504f8671Smatt u16 = (uint16_t) (now.tv_sec ^ now.tv_usec);
7862cca8c55Schristos memcpy(rnd + 14, &u16, 2);
78759a755a4Schristos #ifndef HAVE_MD5
78859a755a4Schristos MD5_Init(&ctx);
7892cca8c55Schristos MD5_Update(&ctx, rnd, 16);
7902cca8c55Schristos MD5_Final(rnd, &ctx);
79159a755a4Schristos #else
79259a755a4Schristos MD5Init(&ctx);
7932cca8c55Schristos MD5Update(&ctx, rnd, 16);
7942cca8c55Schristos MD5Final(rnd, &ctx);
79559a755a4Schristos #endif
7962cca8c55Schristos memcpy(&u16, rnd + 14, 2);
79759a755a4Schristos return ((u_int) u16);
7982b237084Schristos }
7992b237084Schristos
800d73eb73dSchristos /*%
8012b237084Schristos * This routine is for closing the socket if a virtual circuit is used and
8022b237084Schristos * the program wants to close it. This provides support for endhostent()
8032b237084Schristos * which expects to close the socket.
8042b237084Schristos *
8052b237084Schristos * This routine is not expected to be user visible.
8062b237084Schristos */
8072b237084Schristos void
res_nclose(res_state statp)808504f8671Smatt res_nclose(res_state statp)
809504f8671Smatt {
8102b237084Schristos int ns;
8112b237084Schristos
8122b237084Schristos if (statp->_vcsock >= 0) {
8132b237084Schristos (void) close(statp->_vcsock);
8142b237084Schristos statp->_vcsock = -1;
8152b237084Schristos statp->_flags &= ~(RES_F_VC | RES_F_CONN);
8162b237084Schristos }
8172b237084Schristos for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
8182b237084Schristos if (statp->_u._ext.nssocks[ns] != -1) {
8192b237084Schristos (void) close(statp->_u._ext.nssocks[ns]);
8202b237084Schristos statp->_u._ext.nssocks[ns] = -1;
8212b237084Schristos }
8222b237084Schristos }
8232b237084Schristos }
8242b237084Schristos
8252b237084Schristos void
res_ndestroy(res_state statp)826504f8671Smatt res_ndestroy(res_state statp)
827504f8671Smatt {
828*30d43af9Schristos struct __res_state_ext *ext = statp->_u._ext.ext;
8292b237084Schristos res_nclose(statp);
830*30d43af9Schristos if (ext != NULL) {
831*30d43af9Schristos if (ext->kq != -1 && ext->kqpid == getpid())
832*30d43af9Schristos (void)close(ext->kq);
833*30d43af9Schristos if (ext->resfd != -1)
834*30d43af9Schristos (void)close(ext->resfd);
835*30d43af9Schristos free(ext);
8362b237084Schristos statp->_u._ext.ext = NULL;
8372b237084Schristos }
8382cca8c55Schristos if (statp->_rnd != NULL) {
8392cca8c55Schristos free(statp->_rnd);
8402cca8c55Schristos statp->_rnd = NULL;
8412cca8c55Schristos }
8422cca8c55Schristos statp->options &= ~RES_INIT;
8432cca8c55Schristos }
8442b237084Schristos
8452b237084Schristos const char *
res_get_nibblesuffix(res_state statp)846504f8671Smatt res_get_nibblesuffix(res_state statp)
847504f8671Smatt {
8482b237084Schristos if (statp->_u._ext.ext)
8492b237084Schristos return (statp->_u._ext.ext->nsuffix);
8502b237084Schristos return ("ip6.arpa");
8512b237084Schristos }
8522b237084Schristos
8532b237084Schristos const char *
res_get_nibblesuffix2(res_state statp)854504f8671Smatt res_get_nibblesuffix2(res_state statp)
855504f8671Smatt {
8562b237084Schristos if (statp->_u._ext.ext)
8572b237084Schristos return (statp->_u._ext.ext->nsuffix2);
8582b237084Schristos return ("ip6.int");
8592b237084Schristos }
8602b237084Schristos
8612b237084Schristos void
res_setservers(res_state statp,const union res_sockaddr_union * set,int cnt)862504f8671Smatt res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt)
863504f8671Smatt {
8642b237084Schristos int i, nserv;
8652b237084Schristos size_t size;
8662b237084Schristos
8672b237084Schristos /* close open servers */
8682b237084Schristos res_nclose(statp);
8692b237084Schristos
8702b237084Schristos /* cause rtt times to be forgotten */
8712b237084Schristos statp->_u._ext.nscount = 0;
8722b237084Schristos
8732b237084Schristos nserv = 0;
8742b237084Schristos for (i = 0; i < cnt && nserv < MAXNS; i++) {
8752b237084Schristos switch (set->sin.sin_family) {
8762b237084Schristos case AF_INET:
8772b237084Schristos size = sizeof(set->sin);
8782b237084Schristos if (statp->_u._ext.ext)
8792b237084Schristos memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
8802b237084Schristos &set->sin, size);
8812b237084Schristos if (size <= sizeof(statp->nsaddr_list[nserv]))
8822b237084Schristos memcpy(&statp->nsaddr_list[nserv],
8832b237084Schristos &set->sin, size);
8842b237084Schristos else
8852b237084Schristos statp->nsaddr_list[nserv].sin_family = 0;
8862b237084Schristos nserv++;
8872b237084Schristos break;
8882b237084Schristos
8892b237084Schristos #ifdef HAS_INET6_STRUCTS
8902b237084Schristos case AF_INET6:
8912b237084Schristos size = sizeof(set->sin6);
8922b237084Schristos if (statp->_u._ext.ext)
8932b237084Schristos memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
8942b237084Schristos &set->sin6, size);
8952b237084Schristos if (size <= sizeof(statp->nsaddr_list[nserv]))
8962b237084Schristos memcpy(&statp->nsaddr_list[nserv],
8972b237084Schristos &set->sin6, size);
8982b237084Schristos else
8992b237084Schristos statp->nsaddr_list[nserv].sin_family = 0;
9002b237084Schristos nserv++;
9012b237084Schristos break;
9022b237084Schristos #endif
9032b237084Schristos
9042b237084Schristos default:
9052b237084Schristos break;
9062b237084Schristos }
9072b237084Schristos set++;
9082b237084Schristos }
9092b237084Schristos statp->nscount = nserv;
9102b237084Schristos
9112b237084Schristos }
9122b237084Schristos
9132b237084Schristos int
res_getservers(res_state statp,union res_sockaddr_union * set,int cnt)914504f8671Smatt res_getservers(res_state statp, union res_sockaddr_union *set, int cnt)
915504f8671Smatt {
9162b237084Schristos int i;
9172b237084Schristos size_t size;
918504f8671Smatt uint16_t family;
9192b237084Schristos
9202b237084Schristos for (i = 0; i < statp->nscount && i < cnt; i++) {
9212b237084Schristos if (statp->_u._ext.ext)
9222b237084Schristos family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
9232b237084Schristos else
9242b237084Schristos family = statp->nsaddr_list[i].sin_family;
9252b237084Schristos
9262b237084Schristos switch (family) {
9272b237084Schristos case AF_INET:
9282b237084Schristos size = sizeof(set->sin);
9292b237084Schristos if (statp->_u._ext.ext)
9302b237084Schristos memcpy(&set->sin,
9312b237084Schristos &statp->_u._ext.ext->nsaddrs[i],
9322b237084Schristos size);
9332b237084Schristos else
9342b237084Schristos memcpy(&set->sin, &statp->nsaddr_list[i],
9352b237084Schristos size);
9362b237084Schristos break;
9372b237084Schristos
9382b237084Schristos #ifdef HAS_INET6_STRUCTS
9392b237084Schristos case AF_INET6:
9402b237084Schristos size = sizeof(set->sin6);
9412b237084Schristos if (statp->_u._ext.ext)
9422b237084Schristos memcpy(&set->sin6,
9432b237084Schristos &statp->_u._ext.ext->nsaddrs[i],
9442b237084Schristos size);
9452b237084Schristos else
9462b237084Schristos memcpy(&set->sin6, &statp->nsaddr_list[i],
9472b237084Schristos size);
9482b237084Schristos break;
9492b237084Schristos #endif
9502b237084Schristos
9512b237084Schristos default:
9522b237084Schristos set->sin.sin_family = 0;
9532b237084Schristos break;
9542b237084Schristos }
9552b237084Schristos set++;
9562b237084Schristos }
9572b237084Schristos return (statp->nscount);
9582b237084Schristos }
959d73eb73dSchristos
960d73eb73dSchristos /*! \file */
961