10Sstevel@tonic-gate /*
2*12094SStacey.Marshall@Sun.COM * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
30Sstevel@tonic-gate */
40Sstevel@tonic-gate
511038SRao.Shoaib@Sun.COM
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 1985, 1989, 1993
80Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
110Sstevel@tonic-gate * modification, are permitted provided that the following conditions
120Sstevel@tonic-gate * are met:
130Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
170Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
180Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
190Sstevel@tonic-gate * must display the following acknowledgement:
200Sstevel@tonic-gate * This product includes software developed by the University of
210Sstevel@tonic-gate * California, Berkeley and its contributors.
220Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
230Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
240Sstevel@tonic-gate * without specific prior written permission.
250Sstevel@tonic-gate *
260Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
270Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
280Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
290Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
300Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
310Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
320Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
330Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
340Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
350Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
360Sstevel@tonic-gate * SUCH DAMAGE.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation.
410Sstevel@tonic-gate *
420Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
430Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
440Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that
450Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or
460Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without
470Sstevel@tonic-gate * specific, written prior permission.
480Sstevel@tonic-gate *
490Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
500Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
510Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
520Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
530Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
540Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
550Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
560Sstevel@tonic-gate * SOFTWARE.
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate /*
6011038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
610Sstevel@tonic-gate * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
620Sstevel@tonic-gate *
630Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
640Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
650Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
660Sstevel@tonic-gate *
6711038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
6811038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
6911038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
7011038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
7111038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
7211038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
7311038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
740Sstevel@tonic-gate */
750Sstevel@tonic-gate
760Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
770Sstevel@tonic-gate static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
7811038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp $";
790Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
800Sstevel@tonic-gate
810Sstevel@tonic-gate #include "port_before.h"
820Sstevel@tonic-gate
830Sstevel@tonic-gate #include <sys/types.h>
840Sstevel@tonic-gate #include <sys/param.h>
850Sstevel@tonic-gate #include <sys/socket.h>
860Sstevel@tonic-gate #include <sys/time.h>
870Sstevel@tonic-gate
880Sstevel@tonic-gate #include <netinet/in.h>
890Sstevel@tonic-gate #include <arpa/inet.h>
900Sstevel@tonic-gate #include <arpa/nameser.h>
910Sstevel@tonic-gate
920Sstevel@tonic-gate #include <ctype.h>
930Sstevel@tonic-gate #include <stdio.h>
940Sstevel@tonic-gate #include <stdlib.h>
950Sstevel@tonic-gate #include <string.h>
960Sstevel@tonic-gate #include <unistd.h>
970Sstevel@tonic-gate #include <netdb.h>
980Sstevel@tonic-gate
9911038SRao.Shoaib@Sun.COM #ifndef HAVE_MD5
10011038SRao.Shoaib@Sun.COM # include "../dst/md5.h"
10111038SRao.Shoaib@Sun.COM #else
10211038SRao.Shoaib@Sun.COM # ifdef SOLARIS2
10311038SRao.Shoaib@Sun.COM # include <sys/md5.h>
10411038SRao.Shoaib@Sun.COM # endif
10511038SRao.Shoaib@Sun.COM #endif
10611038SRao.Shoaib@Sun.COM #ifndef _MD5_H_
10711038SRao.Shoaib@Sun.COM # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
10811038SRao.Shoaib@Sun.COM #endif
10911038SRao.Shoaib@Sun.COM
11011038SRao.Shoaib@Sun.COM
1110Sstevel@tonic-gate #include "port_after.h"
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
1140Sstevel@tonic-gate #include <resolv.h>
1150Sstevel@tonic-gate
11611038SRao.Shoaib@Sun.COM /* ISC purposely put port_after.h before <resolv.h> to force in6 stuff
11711038SRao.Shoaib@Sun.COM * (above) so we explicitly include port_resolv.h here */
11811038SRao.Shoaib@Sun.COM #include "port_resolv.h"
11911038SRao.Shoaib@Sun.COM
1200Sstevel@tonic-gate #include "res_private.h"
1210Sstevel@tonic-gate
12211038SRao.Shoaib@Sun.COM /*% Options. Should all be left alone. */
1230Sstevel@tonic-gate #define RESOLVSORT
1240Sstevel@tonic-gate #define DEBUG
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate #ifdef SUNW_INITCHKIF
1270Sstevel@tonic-gate #include <net/if.h>
1280Sstevel@tonic-gate #include <netinet/if_ether.h>
1290Sstevel@tonic-gate #include <sys/sockio.h>
1300Sstevel@tonic-gate #define MAXIFS 8192
1310Sstevel@tonic-gate #endif /* SUNW_INITCHKIF */
1320Sstevel@tonic-gate
13311038SRao.Shoaib@Sun.COM #ifdef SOLARIS2
1340Sstevel@tonic-gate #include <sys/systeminfo.h>
13511038SRao.Shoaib@Sun.COM #endif
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate static void res_setoptions __P((res_state, const char *, const char *));
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate #ifdef RESOLVSORT
1400Sstevel@tonic-gate static const char sort_mask[] = "/&";
1410Sstevel@tonic-gate #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
1420Sstevel@tonic-gate static u_int32_t net_mask __P((struct in_addr));
1430Sstevel@tonic-gate #endif
1440Sstevel@tonic-gate
14511038SRao.Shoaib@Sun.COM #if !defined(isascii) /*%< XXX - could be a function */
1460Sstevel@tonic-gate # define isascii(c) (!(c & 0200))
1470Sstevel@tonic-gate #endif
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * Resolver state default settings.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate
15311038SRao.Shoaib@Sun.COM /*%
1540Sstevel@tonic-gate * Set up default settings. If the configuration file exist, the values
1550Sstevel@tonic-gate * there will have precedence. Otherwise, the server address is set to
1560Sstevel@tonic-gate * INADDR_ANY and the default domain name comes from the gethostname().
1570Sstevel@tonic-gate *
1580Sstevel@tonic-gate * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
1590Sstevel@tonic-gate * rather than INADDR_ANY ("0.0.0.0") as the default name server address
1600Sstevel@tonic-gate * since it was noted that INADDR_ANY actually meant ``the first interface
1610Sstevel@tonic-gate * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
1620Sstevel@tonic-gate * it had to be "up" in order for you to reach your own name server. It
1630Sstevel@tonic-gate * was later decided that since the recommended practice is to always
1640Sstevel@tonic-gate * install local static routes through 127.0.0.1 for all your network
1650Sstevel@tonic-gate * interfaces, that we could solve this problem without a code change.
1660Sstevel@tonic-gate *
1670Sstevel@tonic-gate * The configuration file should always be used, since it is the only way
1680Sstevel@tonic-gate * to specify a default domain. If you are running a server on your local
1690Sstevel@tonic-gate * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
1700Sstevel@tonic-gate * in the configuration file.
1710Sstevel@tonic-gate *
1720Sstevel@tonic-gate * Return 0 if completes successfully, -1 on error
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate int
res_ninit(res_state statp)1750Sstevel@tonic-gate res_ninit(res_state statp) {
1760Sstevel@tonic-gate extern int __res_vinit(res_state, int);
1770Sstevel@tonic-gate return (__res_vinit(statp, 0));
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
18011038SRao.Shoaib@Sun.COM /*% This function has to be reachable by res_data.c but not publically. */
1810Sstevel@tonic-gate int
__res_vinit(res_state statp,int preinit)1820Sstevel@tonic-gate __res_vinit(res_state statp, int preinit) {
1830Sstevel@tonic-gate register FILE *fp;
1840Sstevel@tonic-gate register char *cp, **pp;
1850Sstevel@tonic-gate register int n;
1860Sstevel@tonic-gate char buf[BUFSIZ];
18711038SRao.Shoaib@Sun.COM int nserv = 0; /*%< number of nameserver records read from file */
1880Sstevel@tonic-gate int haveenv = 0;
1890Sstevel@tonic-gate int havesearch = 0;
1900Sstevel@tonic-gate #ifdef RESOLVSORT
1910Sstevel@tonic-gate int nsort = 0;
1920Sstevel@tonic-gate char *net;
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate int dots;
1950Sstevel@tonic-gate union res_sockaddr_union u[2];
19611038SRao.Shoaib@Sun.COM int maxns = MAXNS;
1970Sstevel@tonic-gate
19811038SRao.Shoaib@Sun.COM RES_SET_H_ERRNO(statp, 0);
1993360Ssm26363 if (statp->_u._ext.ext != NULL)
2003360Ssm26363 res_ndestroy(statp);
2013360Ssm26363
2020Sstevel@tonic-gate if (!preinit) {
2030Sstevel@tonic-gate statp->retrans = RES_TIMEOUT;
2040Sstevel@tonic-gate statp->retry = RES_DFLRETRY;
2050Sstevel@tonic-gate statp->options = RES_DEFAULT;
20611038SRao.Shoaib@Sun.COM res_rndinit(statp);
20711038SRao.Shoaib@Sun.COM statp->id = res_nrandomid(statp);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate memset(u, 0, sizeof(u));
2110Sstevel@tonic-gate #ifdef USELOOPBACK
2120Sstevel@tonic-gate u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
2130Sstevel@tonic-gate #else
2140Sstevel@tonic-gate u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
2150Sstevel@tonic-gate #endif
2160Sstevel@tonic-gate u[nserv].sin.sin_family = AF_INET;
2170Sstevel@tonic-gate u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
2180Sstevel@tonic-gate #ifdef HAVE_SA_LEN
2190Sstevel@tonic-gate u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
2200Sstevel@tonic-gate #endif
2210Sstevel@tonic-gate nserv++;
2220Sstevel@tonic-gate #ifdef HAS_INET6_STRUCTS
2230Sstevel@tonic-gate #ifdef USELOOPBACK
2240Sstevel@tonic-gate u[nserv].sin6.sin6_addr = in6addr_loopback;
2250Sstevel@tonic-gate #else
2260Sstevel@tonic-gate u[nserv].sin6.sin6_addr = in6addr_any;
2270Sstevel@tonic-gate #endif
2280Sstevel@tonic-gate u[nserv].sin6.sin6_family = AF_INET6;
2290Sstevel@tonic-gate u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
2300Sstevel@tonic-gate #ifdef HAVE_SA_LEN
2310Sstevel@tonic-gate u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
2320Sstevel@tonic-gate #endif
2330Sstevel@tonic-gate nserv++;
2340Sstevel@tonic-gate #endif
2350Sstevel@tonic-gate statp->nscount = 0;
2360Sstevel@tonic-gate statp->ndots = 1;
2370Sstevel@tonic-gate statp->pfcode = 0;
2380Sstevel@tonic-gate statp->_vcsock = -1;
2390Sstevel@tonic-gate statp->_flags = 0;
2400Sstevel@tonic-gate statp->qhook = NULL;
2410Sstevel@tonic-gate statp->rhook = NULL;
2420Sstevel@tonic-gate statp->_u._ext.nscount = 0;
2430Sstevel@tonic-gate statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
2440Sstevel@tonic-gate if (statp->_u._ext.ext != NULL) {
2450Sstevel@tonic-gate memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
2460Sstevel@tonic-gate statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
2470Sstevel@tonic-gate strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
2480Sstevel@tonic-gate strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
24911038SRao.Shoaib@Sun.COM } else {
25011038SRao.Shoaib@Sun.COM /*
25111038SRao.Shoaib@Sun.COM * Historically res_init() rarely, if at all, failed.
25211038SRao.Shoaib@Sun.COM * Examples and applications exist which do not check
25311038SRao.Shoaib@Sun.COM * our return code. Furthermore several applications
25411038SRao.Shoaib@Sun.COM * simply call us to get the systems domainname. So
25511038SRao.Shoaib@Sun.COM * rather then immediately fail here we store the
25611038SRao.Shoaib@Sun.COM * failure, which is returned later, in h_errno. And
25711038SRao.Shoaib@Sun.COM * prevent the collection of 'nameserver' information
25811038SRao.Shoaib@Sun.COM * by setting maxns to 0. Thus applications that fail
25911038SRao.Shoaib@Sun.COM * to check our return code wont be able to make
26011038SRao.Shoaib@Sun.COM * queries anyhow.
26111038SRao.Shoaib@Sun.COM */
26211038SRao.Shoaib@Sun.COM RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
26311038SRao.Shoaib@Sun.COM maxns = 0;
26411038SRao.Shoaib@Sun.COM }
2650Sstevel@tonic-gate #ifdef RESOLVSORT
2660Sstevel@tonic-gate statp->nsort = 0;
2670Sstevel@tonic-gate #endif
2680Sstevel@tonic-gate res_setservers(statp, u, nserv);
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate #ifdef SUNW_INITCHKIF
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate * Short circuit res_init() if no non-loopback interfaces are up. This is
2730Sstevel@tonic-gate * done to avoid boot delays if "dns" comes before "files" in nsswitch.conf.
2740Sstevel@tonic-gate * An additional fix has been added to this code, to count all external
2750Sstevel@tonic-gate * interfaces, which includes the IPv6 interfaces. If no external interfaces
2760Sstevel@tonic-gate * are found, an additional check is carried out to determine if any deprecated
2770Sstevel@tonic-gate * interfaces are up.
2780Sstevel@tonic-gate */
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate int s;
2810Sstevel@tonic-gate struct lifnum lifn;
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2840Sstevel@tonic-gate perror("res_init: socket");
2853360Ssm26363 goto freedata;
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC;
2880Sstevel@tonic-gate lifn.lifn_flags = LIFC_EXTERNAL_SOURCE;
2890Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
2900Sstevel@tonic-gate close(s);
2913360Ssm26363 goto freedata;
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate if (lifn.lifn_count == 0) {
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate * Check if there are any deprecated interfaces up
2960Sstevel@tonic-gate */
2970Sstevel@tonic-gate struct lifconf lifc;
2980Sstevel@tonic-gate uchar_t *buf;
2990Sstevel@tonic-gate int buflen, i, int_up = 0;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate lifn.lifn_flags = 0;
3020Sstevel@tonic-gate if ((ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) ||
3030Sstevel@tonic-gate (lifn.lifn_count < 1)) {
3040Sstevel@tonic-gate close(s);
3053360Ssm26363 goto freedata;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate buflen = lifn.lifn_count * sizeof (struct lifreq);
3090Sstevel@tonic-gate buf = (uchar_t *)malloc(buflen);
3100Sstevel@tonic-gate if (buf == NULL) {
3110Sstevel@tonic-gate close(s);
3123360Ssm26363 goto freedata;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC;
3160Sstevel@tonic-gate lifc.lifc_flags = 0;
3170Sstevel@tonic-gate lifc.lifc_len = buflen;
3180Sstevel@tonic-gate lifc.lifc_lifcu.lifcu_buf = (caddr_t)buf;
3190Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
3200Sstevel@tonic-gate close(s);
3210Sstevel@tonic-gate free(buf);
3223360Ssm26363 goto freedata;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate for (i = 0; i < lifn.lifn_count; ++i) {
3260Sstevel@tonic-gate struct lifreq *lreqp, lreq;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate lreqp = (struct lifreq *)&lifc.lifc_req[i];
3290Sstevel@tonic-gate strlcpy(lreq.lifr_name, lreqp->lifr_name,
3300Sstevel@tonic-gate sizeof (lreq.lifr_name));
3310Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, &lreq) < 0) {
3320Sstevel@tonic-gate close(s);
3330Sstevel@tonic-gate free(buf);
3343360Ssm26363 goto freedata;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate if ((lreq.lifr_flags & IFF_UP) &&
3370Sstevel@tonic-gate !(lreq.lifr_flags & IFF_NOLOCAL) &&
3380Sstevel@tonic-gate !(lreq.lifr_flags & IFF_NOXMIT) &&
3390Sstevel@tonic-gate !(lreq.lifr_flags & IFF_LOOPBACK)) {
3400Sstevel@tonic-gate int_up = 1;
3410Sstevel@tonic-gate break;
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate free(buf);
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate if (!int_up) {
3470Sstevel@tonic-gate close(s);
3483360Ssm26363 goto freedata;
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate close(s);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate #endif /* SUNW_INITCHKIF */
3540Sstevel@tonic-gate
35511038SRao.Shoaib@Sun.COM #ifdef SOLARIS2
35611038SRao.Shoaib@Sun.COM /*
35711038SRao.Shoaib@Sun.COM * The old libresolv derived the defaultdomain from NIS/NIS+.
35811038SRao.Shoaib@Sun.COM * We want to keep this behaviour
35911038SRao.Shoaib@Sun.COM */
36011038SRao.Shoaib@Sun.COM {
36111038SRao.Shoaib@Sun.COM char buf[sizeof(statp->defdname)], *cp;
36211038SRao.Shoaib@Sun.COM int ret;
36311038SRao.Shoaib@Sun.COM
36411038SRao.Shoaib@Sun.COM if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
36511038SRao.Shoaib@Sun.COM (unsigned int)ret <= sizeof(buf)) {
36611038SRao.Shoaib@Sun.COM if (buf[0] == '+')
36711038SRao.Shoaib@Sun.COM buf[0] = '.';
36811038SRao.Shoaib@Sun.COM cp = strchr(buf, '.');
36911038SRao.Shoaib@Sun.COM cp = (cp == NULL) ? buf : (cp + 1);
37011038SRao.Shoaib@Sun.COM strncpy(statp->defdname, cp,
37111038SRao.Shoaib@Sun.COM sizeof(statp->defdname) - 1);
37211038SRao.Shoaib@Sun.COM statp->defdname[sizeof(statp->defdname) - 1] = '\0';
37311038SRao.Shoaib@Sun.COM }
37411038SRao.Shoaib@Sun.COM }
37511038SRao.Shoaib@Sun.COM #endif /* SOLARIS2 */
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /* Allow user to override the local domain definition */
3780Sstevel@tonic-gate if ((cp = getenv("LOCALDOMAIN")) != NULL) {
3790Sstevel@tonic-gate (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
3800Sstevel@tonic-gate statp->defdname[sizeof(statp->defdname) - 1] = '\0';
3810Sstevel@tonic-gate haveenv++;
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate * Set search list to be blank-separated strings
3850Sstevel@tonic-gate * from rest of env value. Permits users of LOCALDOMAIN
3860Sstevel@tonic-gate * to still have a search list, and anyone to set the
3870Sstevel@tonic-gate * one that they want to use as an individual (even more
3880Sstevel@tonic-gate * important now that the rfc1535 stuff restricts searches)
3890Sstevel@tonic-gate */
3900Sstevel@tonic-gate cp = statp->defdname;
3910Sstevel@tonic-gate pp = statp->dnsrch;
3920Sstevel@tonic-gate *pp++ = cp;
3930Sstevel@tonic-gate for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
39411038SRao.Shoaib@Sun.COM if (*cp == '\n') /*%< silly backwards compat */
3950Sstevel@tonic-gate break;
3960Sstevel@tonic-gate else if (*cp == ' ' || *cp == '\t') {
3970Sstevel@tonic-gate *cp = 0;
3980Sstevel@tonic-gate n = 1;
3990Sstevel@tonic-gate } else if (n) {
4000Sstevel@tonic-gate *pp++ = cp;
4010Sstevel@tonic-gate n = 0;
4020Sstevel@tonic-gate havesearch = 1;
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate /* null terminate last domain if there are excess */
4060Sstevel@tonic-gate while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
4070Sstevel@tonic-gate cp++;
4080Sstevel@tonic-gate *cp = '\0';
4090Sstevel@tonic-gate *pp++ = 0;
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate #define MATCH(line, name) \
4130Sstevel@tonic-gate (!strncmp(line, name, sizeof(name) - 1) && \
4140Sstevel@tonic-gate (line[sizeof(name) - 1] == ' ' || \
4150Sstevel@tonic-gate line[sizeof(name) - 1] == '\t'))
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate nserv = 0;
4180Sstevel@tonic-gate if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
4190Sstevel@tonic-gate /* read the config file */
4200Sstevel@tonic-gate while (fgets(buf, sizeof(buf), fp) != NULL) {
4210Sstevel@tonic-gate /* skip comments */
4220Sstevel@tonic-gate if (*buf == ';' || *buf == '#')
4230Sstevel@tonic-gate continue;
4240Sstevel@tonic-gate /* read default domain name */
4250Sstevel@tonic-gate if (MATCH(buf, "domain")) {
42611038SRao.Shoaib@Sun.COM if (haveenv) /*%< skip if have from environ */
4270Sstevel@tonic-gate continue;
4280Sstevel@tonic-gate cp = buf + sizeof("domain") - 1;
4290Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
4300Sstevel@tonic-gate cp++;
4310Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n'))
4320Sstevel@tonic-gate continue;
4330Sstevel@tonic-gate strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
4340Sstevel@tonic-gate statp->defdname[sizeof(statp->defdname) - 1] = '\0';
4350Sstevel@tonic-gate if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
4360Sstevel@tonic-gate *cp = '\0';
4370Sstevel@tonic-gate havesearch = 0;
4380Sstevel@tonic-gate continue;
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate /* set search list */
4410Sstevel@tonic-gate if (MATCH(buf, "search")) {
44211038SRao.Shoaib@Sun.COM if (haveenv) /*%< skip if have from environ */
4430Sstevel@tonic-gate continue;
4440Sstevel@tonic-gate cp = buf + sizeof("search") - 1;
4450Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
4460Sstevel@tonic-gate cp++;
4470Sstevel@tonic-gate if ((*cp == '\0') || (*cp == '\n'))
4480Sstevel@tonic-gate continue;
4490Sstevel@tonic-gate strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
4500Sstevel@tonic-gate statp->defdname[sizeof(statp->defdname) - 1] = '\0';
4510Sstevel@tonic-gate if ((cp = strchr(statp->defdname, '\n')) != NULL)
4520Sstevel@tonic-gate *cp = '\0';
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate * Set search list to be blank-separated strings
4550Sstevel@tonic-gate * on rest of line.
4560Sstevel@tonic-gate */
4570Sstevel@tonic-gate cp = statp->defdname;
4580Sstevel@tonic-gate pp = statp->dnsrch;
4590Sstevel@tonic-gate *pp++ = cp;
4600Sstevel@tonic-gate for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
4610Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') {
4620Sstevel@tonic-gate *cp = 0;
4630Sstevel@tonic-gate n = 1;
4640Sstevel@tonic-gate } else if (n) {
4650Sstevel@tonic-gate *pp++ = cp;
4660Sstevel@tonic-gate n = 0;
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate /* null terminate last domain if there are excess */
4700Sstevel@tonic-gate while (*cp != '\0' && *cp != ' ' && *cp != '\t')
4710Sstevel@tonic-gate cp++;
4720Sstevel@tonic-gate *cp = '\0';
4730Sstevel@tonic-gate *pp++ = 0;
4740Sstevel@tonic-gate havesearch = 1;
4750Sstevel@tonic-gate continue;
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate /* read nameservers to query */
47811038SRao.Shoaib@Sun.COM if (MATCH(buf, "nameserver") && nserv < maxns) {
4790Sstevel@tonic-gate struct addrinfo hints, *ai;
4800Sstevel@tonic-gate char sbuf[NI_MAXSERV];
4810Sstevel@tonic-gate const size_t minsiz =
4820Sstevel@tonic-gate sizeof(statp->_u._ext.ext->nsaddrs[0]);
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate cp = buf + sizeof("nameserver") - 1;
4850Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
4860Sstevel@tonic-gate cp++;
4870Sstevel@tonic-gate cp[strcspn(cp, ";# \t\n")] = '\0';
4880Sstevel@tonic-gate if ((*cp != '\0') && (*cp != '\n')) {
4890Sstevel@tonic-gate memset(&hints, 0, sizeof(hints));
4900Sstevel@tonic-gate hints.ai_family = PF_UNSPEC;
4910Sstevel@tonic-gate hints.ai_socktype = SOCK_DGRAM; /*dummy*/
4920Sstevel@tonic-gate hints.ai_flags = AI_NUMERICHOST;
4930Sstevel@tonic-gate sprintf(sbuf, "%u", NAMESERVER_PORT);
4940Sstevel@tonic-gate if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
4950Sstevel@tonic-gate ai->ai_addrlen <= minsiz) {
4960Sstevel@tonic-gate if (statp->_u._ext.ext != NULL) {
4970Sstevel@tonic-gate memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
4980Sstevel@tonic-gate ai->ai_addr, ai->ai_addrlen);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate if (ai->ai_addrlen <=
5010Sstevel@tonic-gate sizeof(statp->nsaddr_list[nserv])) {
5020Sstevel@tonic-gate memcpy(&statp->nsaddr_list[nserv],
5030Sstevel@tonic-gate ai->ai_addr, ai->ai_addrlen);
5040Sstevel@tonic-gate } else
5050Sstevel@tonic-gate statp->nsaddr_list[nserv].sin_family = 0;
5060Sstevel@tonic-gate freeaddrinfo(ai);
5070Sstevel@tonic-gate nserv++;
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate continue;
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate #ifdef RESOLVSORT
5130Sstevel@tonic-gate if (MATCH(buf, "sortlist")) {
5140Sstevel@tonic-gate struct in_addr a;
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate cp = buf + sizeof("sortlist") - 1;
5170Sstevel@tonic-gate while (nsort < MAXRESOLVSORT) {
5180Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
5190Sstevel@tonic-gate cp++;
5200Sstevel@tonic-gate if (*cp == '\0' || *cp == '\n' || *cp == ';')
5210Sstevel@tonic-gate break;
5220Sstevel@tonic-gate net = cp;
5230Sstevel@tonic-gate while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
5240Sstevel@tonic-gate isascii(*cp) && !isspace((unsigned char)*cp))
5250Sstevel@tonic-gate cp++;
5260Sstevel@tonic-gate n = *cp;
5270Sstevel@tonic-gate *cp = 0;
5280Sstevel@tonic-gate if (inet_aton(net, &a)) {
5290Sstevel@tonic-gate statp->sort_list[nsort].addr = a;
5300Sstevel@tonic-gate if (ISSORTMASK(n)) {
5310Sstevel@tonic-gate *cp++ = n;
5320Sstevel@tonic-gate net = cp;
5330Sstevel@tonic-gate while (*cp && *cp != ';' &&
5340Sstevel@tonic-gate isascii(*cp) &&
5350Sstevel@tonic-gate !isspace((unsigned char)*cp))
5360Sstevel@tonic-gate cp++;
5370Sstevel@tonic-gate n = *cp;
5380Sstevel@tonic-gate *cp = 0;
5390Sstevel@tonic-gate if (inet_aton(net, &a)) {
5400Sstevel@tonic-gate statp->sort_list[nsort].mask = a.s_addr;
5410Sstevel@tonic-gate } else {
5420Sstevel@tonic-gate statp->sort_list[nsort].mask =
5430Sstevel@tonic-gate net_mask(statp->sort_list[nsort].addr);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate } else {
5460Sstevel@tonic-gate statp->sort_list[nsort].mask =
5470Sstevel@tonic-gate net_mask(statp->sort_list[nsort].addr);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate nsort++;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate *cp = n;
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate continue;
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate #endif
5560Sstevel@tonic-gate if (MATCH(buf, "options")) {
5570Sstevel@tonic-gate res_setoptions(statp, buf + sizeof("options") - 1, "conf");
5580Sstevel@tonic-gate continue;
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate if (nserv > 0)
5620Sstevel@tonic-gate statp->nscount = nserv;
5630Sstevel@tonic-gate #ifdef RESOLVSORT
5640Sstevel@tonic-gate statp->nsort = nsort;
5650Sstevel@tonic-gate #endif
56611038SRao.Shoaib@Sun.COM (void) fclose(fp);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate * Last chance to get a nameserver. This should not normally
5700Sstevel@tonic-gate * be necessary
5710Sstevel@tonic-gate */
5720Sstevel@tonic-gate #ifdef NO_RESOLV_CONF
5730Sstevel@tonic-gate if(nserv == 0)
5740Sstevel@tonic-gate nserv = get_nameservers(statp);
5750Sstevel@tonic-gate #endif
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate if (statp->defdname[0] == 0 &&
5780Sstevel@tonic-gate gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
5790Sstevel@tonic-gate (cp = strchr(buf, '.')) != NULL)
5800Sstevel@tonic-gate strcpy(statp->defdname, cp + 1);
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate /* find components of local domain that might be searched */
5830Sstevel@tonic-gate if (havesearch == 0) {
5840Sstevel@tonic-gate pp = statp->dnsrch;
5850Sstevel@tonic-gate *pp++ = statp->defdname;
5860Sstevel@tonic-gate *pp = NULL;
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate dots = 0;
5890Sstevel@tonic-gate for (cp = statp->defdname; *cp; cp++)
5900Sstevel@tonic-gate dots += (*cp == '.');
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate cp = statp->defdname;
5930Sstevel@tonic-gate while (pp < statp->dnsrch + MAXDFLSRCH) {
5940Sstevel@tonic-gate if (dots < LOCALDOMAINPARTS)
5950Sstevel@tonic-gate break;
59611038SRao.Shoaib@Sun.COM cp = strchr(cp, '.') + 1; /*%< we know there is one */
5970Sstevel@tonic-gate *pp++ = cp;
5980Sstevel@tonic-gate dots--;
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate *pp = NULL;
6010Sstevel@tonic-gate #ifdef DEBUG
6020Sstevel@tonic-gate if (statp->options & RES_DEBUG) {
6030Sstevel@tonic-gate printf(";; res_init()... default dnsrch list:\n");
6040Sstevel@tonic-gate for (pp = statp->dnsrch; *pp; pp++)
6050Sstevel@tonic-gate printf(";;\t%s\n", *pp);
6060Sstevel@tonic-gate printf(";;\t..END..\n");
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate #endif
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate if ((cp = getenv("RES_OPTIONS")) != NULL)
6120Sstevel@tonic-gate res_setoptions(statp, cp, "env");
6130Sstevel@tonic-gate statp->options |= RES_INIT;
61411038SRao.Shoaib@Sun.COM return (statp->res_h_errno);
6153360Ssm26363 #ifdef SUNW_INITCHKIF
6163360Ssm26363 freedata:
617*12094SStacey.Marshall@Sun.COM RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
6183360Ssm26363 if (statp->_u._ext.ext != NULL) {
6193360Ssm26363 free(statp->_u._ext.ext);
6203360Ssm26363 statp->_u._ext.ext = NULL;
6213360Ssm26363 }
6223360Ssm26363 return (-1);
6233360Ssm26363 #endif /* SUNW_INITCHKIF */
62411038SRao.Shoaib@Sun.COM
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate static void
res_setoptions(res_state statp,const char * options,const char * source)6280Sstevel@tonic-gate res_setoptions(res_state statp, const char *options, const char *source)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate const char *cp = options;
6310Sstevel@tonic-gate int i;
6320Sstevel@tonic-gate struct __res_state_ext *ext = statp->_u._ext.ext;
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate #ifdef DEBUG
6350Sstevel@tonic-gate if (statp->options & RES_DEBUG)
6360Sstevel@tonic-gate printf(";; res_setoptions(\"%s\", \"%s\")...\n",
6370Sstevel@tonic-gate options, source);
6380Sstevel@tonic-gate #endif
6390Sstevel@tonic-gate while (*cp) {
6400Sstevel@tonic-gate /* skip leading and inner runs of spaces */
6410Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
6420Sstevel@tonic-gate cp++;
6430Sstevel@tonic-gate /* search for and process individual options */
6440Sstevel@tonic-gate if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
6450Sstevel@tonic-gate i = atoi(cp + sizeof("ndots:") - 1);
6460Sstevel@tonic-gate if (i <= RES_MAXNDOTS)
6470Sstevel@tonic-gate statp->ndots = i;
6480Sstevel@tonic-gate else
6490Sstevel@tonic-gate statp->ndots = RES_MAXNDOTS;
6500Sstevel@tonic-gate #ifdef DEBUG
6510Sstevel@tonic-gate if (statp->options & RES_DEBUG)
6520Sstevel@tonic-gate printf(";;\tndots=%d\n", statp->ndots);
6530Sstevel@tonic-gate #endif
6540Sstevel@tonic-gate } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
6550Sstevel@tonic-gate i = atoi(cp + sizeof("timeout:") - 1);
6560Sstevel@tonic-gate if (i <= RES_MAXRETRANS)
6570Sstevel@tonic-gate statp->retrans = i;
6580Sstevel@tonic-gate else
6590Sstevel@tonic-gate statp->retrans = RES_MAXRETRANS;
66011038SRao.Shoaib@Sun.COM #ifdef DEBUG
66111038SRao.Shoaib@Sun.COM if (statp->options & RES_DEBUG)
66211038SRao.Shoaib@Sun.COM printf(";;\ttimeout=%d\n", statp->retrans);
66311038SRao.Shoaib@Sun.COM #endif
66411038SRao.Shoaib@Sun.COM #ifdef SOLARIS2
6650Sstevel@tonic-gate } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
6660Sstevel@tonic-gate /*
66711038SRao.Shoaib@Sun.COM * For backward compatibility, 'retrans' is
66811038SRao.Shoaib@Sun.COM * supported as an alias for 'timeout', though
66911038SRao.Shoaib@Sun.COM * without an imposed maximum.
67011038SRao.Shoaib@Sun.COM */
6710Sstevel@tonic-gate statp->retrans = atoi(cp + sizeof("retrans:") - 1);
6720Sstevel@tonic-gate } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate * For backward compatibility, 'retry' is
6750Sstevel@tonic-gate * supported as an alias for 'attempts', though
6760Sstevel@tonic-gate * without an imposed maximum.
6770Sstevel@tonic-gate */
6780Sstevel@tonic-gate statp->retry = atoi(cp + sizeof("retry:") - 1);
67911038SRao.Shoaib@Sun.COM #endif /* SOLARIS2 */
68011038SRao.Shoaib@Sun.COM } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
68111038SRao.Shoaib@Sun.COM i = atoi(cp + sizeof("attempts:") - 1);
68211038SRao.Shoaib@Sun.COM if (i <= RES_MAXRETRY)
68311038SRao.Shoaib@Sun.COM statp->retry = i;
68411038SRao.Shoaib@Sun.COM else
68511038SRao.Shoaib@Sun.COM statp->retry = RES_MAXRETRY;
68611038SRao.Shoaib@Sun.COM #ifdef DEBUG
68711038SRao.Shoaib@Sun.COM if (statp->options & RES_DEBUG)
68811038SRao.Shoaib@Sun.COM printf(";;\tattempts=%d\n", statp->retry);
68911038SRao.Shoaib@Sun.COM #endif
6900Sstevel@tonic-gate } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
6910Sstevel@tonic-gate #ifdef DEBUG
6920Sstevel@tonic-gate if (!(statp->options & RES_DEBUG)) {
6930Sstevel@tonic-gate printf(";; res_setoptions(\"%s\", \"%s\")..\n",
6940Sstevel@tonic-gate options, source);
6950Sstevel@tonic-gate statp->options |= RES_DEBUG;
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate printf(";;\tdebug\n");
6980Sstevel@tonic-gate #endif
6990Sstevel@tonic-gate } else if (!strncmp(cp, "no_tld_query",
7000Sstevel@tonic-gate sizeof("no_tld_query") - 1) ||
7010Sstevel@tonic-gate !strncmp(cp, "no-tld-query",
7020Sstevel@tonic-gate sizeof("no-tld-query") - 1)) {
7030Sstevel@tonic-gate statp->options |= RES_NOTLDQUERY;
7040Sstevel@tonic-gate } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
7050Sstevel@tonic-gate statp->options |= RES_USE_INET6;
7060Sstevel@tonic-gate } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
7070Sstevel@tonic-gate statp->options |= RES_ROTATE;
7080Sstevel@tonic-gate } else if (!strncmp(cp, "no-check-names",
7090Sstevel@tonic-gate sizeof("no-check-names") - 1)) {
7100Sstevel@tonic-gate statp->options |= RES_NOCHECKNAME;
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate #ifdef RES_USE_EDNS0
7130Sstevel@tonic-gate else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
7140Sstevel@tonic-gate statp->options |= RES_USE_EDNS0;
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate #endif
7170Sstevel@tonic-gate else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
7180Sstevel@tonic-gate statp->options |= RES_USE_DNAME;
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
7210Sstevel@tonic-gate if (ext == NULL)
7220Sstevel@tonic-gate goto skip;
7230Sstevel@tonic-gate cp += sizeof("nibble:") - 1;
7240Sstevel@tonic-gate i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
7250Sstevel@tonic-gate strncpy(ext->nsuffix, cp, i);
7260Sstevel@tonic-gate ext->nsuffix[i] = '\0';
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
7290Sstevel@tonic-gate if (ext == NULL)
7300Sstevel@tonic-gate goto skip;
7310Sstevel@tonic-gate cp += sizeof("nibble2:") - 1;
7320Sstevel@tonic-gate i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
7330Sstevel@tonic-gate strncpy(ext->nsuffix2, cp, i);
7340Sstevel@tonic-gate ext->nsuffix2[i] = '\0';
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
7370Sstevel@tonic-gate cp += sizeof("v6revmode:") - 1;
7380Sstevel@tonic-gate /* "nibble" and "bitstring" used to be valid */
7390Sstevel@tonic-gate if (!strncmp(cp, "single", sizeof("single") - 1)) {
7400Sstevel@tonic-gate statp->options |= RES_NO_NIBBLE2;
7410Sstevel@tonic-gate } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
7420Sstevel@tonic-gate statp->options &=
7430Sstevel@tonic-gate ~RES_NO_NIBBLE2;
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate else {
7470Sstevel@tonic-gate /* XXX - print a warning here? */
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate skip:
7500Sstevel@tonic-gate /* skip to next run of spaces */
7510Sstevel@tonic-gate while (*cp && *cp != ' ' && *cp != '\t')
7520Sstevel@tonic-gate cp++;
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate #ifdef RESOLVSORT
7570Sstevel@tonic-gate /* XXX - should really support CIDR which means explicit masks always. */
7580Sstevel@tonic-gate static u_int32_t
net_mask(in)75911038SRao.Shoaib@Sun.COM net_mask(in) /*!< XXX - should really use system's version of this */
7600Sstevel@tonic-gate struct in_addr in;
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate register u_int32_t i = ntohl(in.s_addr);
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate if (IN_CLASSA(i))
7650Sstevel@tonic-gate return (htonl(IN_CLASSA_NET));
7660Sstevel@tonic-gate else if (IN_CLASSB(i))
7670Sstevel@tonic-gate return (htonl(IN_CLASSB_NET));
7680Sstevel@tonic-gate return (htonl(IN_CLASSC_NET));
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate #endif
7710Sstevel@tonic-gate
77211038SRao.Shoaib@Sun.COM void
res_rndinit(res_state statp)77311038SRao.Shoaib@Sun.COM res_rndinit(res_state statp)
77411038SRao.Shoaib@Sun.COM {
7750Sstevel@tonic-gate struct timeval now;
77611038SRao.Shoaib@Sun.COM u_int32_t u32;
77711038SRao.Shoaib@Sun.COM u_int16_t u16;
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate gettimeofday(&now, NULL);
78011038SRao.Shoaib@Sun.COM u32 = now.tv_sec;
78111038SRao.Shoaib@Sun.COM memcpy(statp->_u._ext._rnd, &u32, 4);
78211038SRao.Shoaib@Sun.COM u32 = now.tv_usec;
78311038SRao.Shoaib@Sun.COM memcpy(statp->_u._ext._rnd + 4, &u32, 4);
78411038SRao.Shoaib@Sun.COM u32 += now.tv_sec;
78511038SRao.Shoaib@Sun.COM memcpy(statp->_u._ext._rnd + 8, &u32, 4);
78611038SRao.Shoaib@Sun.COM u16 = getpid();
78711038SRao.Shoaib@Sun.COM memcpy(statp->_u._ext._rnd + 12, &u16, 2);
78811038SRao.Shoaib@Sun.COM
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
79111038SRao.Shoaib@Sun.COM u_int
res_nrandomid(res_state statp)79211038SRao.Shoaib@Sun.COM res_nrandomid(res_state statp) {
79311038SRao.Shoaib@Sun.COM struct timeval now;
79411038SRao.Shoaib@Sun.COM u_int16_t u16;
79511038SRao.Shoaib@Sun.COM MD5_CTX ctx;
79611038SRao.Shoaib@Sun.COM
79711038SRao.Shoaib@Sun.COM gettimeofday(&now, NULL);
79811038SRao.Shoaib@Sun.COM u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec);
79911038SRao.Shoaib@Sun.COM
80011038SRao.Shoaib@Sun.COM memcpy(statp->_u._ext._rnd + 14, &u16, 2);
80111038SRao.Shoaib@Sun.COM #ifndef HAVE_MD5
80211038SRao.Shoaib@Sun.COM MD5_Init(&ctx);
80311038SRao.Shoaib@Sun.COM MD5_Update(&ctx, statp->_u._ext._rnd, 16);
80411038SRao.Shoaib@Sun.COM MD5_Final(statp->_u._ext._rnd, &ctx);
80511038SRao.Shoaib@Sun.COM #else
80611038SRao.Shoaib@Sun.COM MD5Init(&ctx);
80711038SRao.Shoaib@Sun.COM MD5Update(&ctx, statp->_u._ext._rnd, 16);
80811038SRao.Shoaib@Sun.COM MD5Final(statp->_u._ext._rnd, &ctx);
80911038SRao.Shoaib@Sun.COM #endif
81011038SRao.Shoaib@Sun.COM memcpy(&u16, statp->_u._ext._rnd + 14, 2);
81111038SRao.Shoaib@Sun.COM return ((u_int) u16);
81211038SRao.Shoaib@Sun.COM }
81311038SRao.Shoaib@Sun.COM
81411038SRao.Shoaib@Sun.COM /*%
8150Sstevel@tonic-gate * This routine is for closing the socket if a virtual circuit is used and
8160Sstevel@tonic-gate * the program wants to close it. This provides support for endhostent()
8170Sstevel@tonic-gate * which expects to close the socket.
8180Sstevel@tonic-gate *
8190Sstevel@tonic-gate * This routine is not expected to be user visible.
8200Sstevel@tonic-gate */
8210Sstevel@tonic-gate void
res_nclose(res_state statp)8220Sstevel@tonic-gate res_nclose(res_state statp) {
8230Sstevel@tonic-gate int ns;
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate if (statp->_vcsock >= 0) {
8260Sstevel@tonic-gate (void) close(statp->_vcsock);
8270Sstevel@tonic-gate statp->_vcsock = -1;
8280Sstevel@tonic-gate statp->_flags &= ~(RES_F_VC | RES_F_CONN);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
8310Sstevel@tonic-gate if (statp->_u._ext.nssocks[ns] != -1) {
8320Sstevel@tonic-gate (void) close(statp->_u._ext.nssocks[ns]);
8330Sstevel@tonic-gate statp->_u._ext.nssocks[ns] = -1;
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate void
res_ndestroy(res_state statp)8390Sstevel@tonic-gate res_ndestroy(res_state statp) {
8400Sstevel@tonic-gate res_nclose(statp);
8410Sstevel@tonic-gate if (statp->_u._ext.ext != NULL)
8420Sstevel@tonic-gate free(statp->_u._ext.ext);
8430Sstevel@tonic-gate statp->options &= ~RES_INIT;
8440Sstevel@tonic-gate statp->_u._ext.ext = NULL;
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate const char *
res_get_nibblesuffix(res_state statp)8480Sstevel@tonic-gate res_get_nibblesuffix(res_state statp) {
8490Sstevel@tonic-gate if (statp->_u._ext.ext)
8500Sstevel@tonic-gate return (statp->_u._ext.ext->nsuffix);
8510Sstevel@tonic-gate return ("ip6.arpa");
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate const char *
res_get_nibblesuffix2(res_state statp)8550Sstevel@tonic-gate res_get_nibblesuffix2(res_state statp) {
8560Sstevel@tonic-gate if (statp->_u._ext.ext)
8570Sstevel@tonic-gate return (statp->_u._ext.ext->nsuffix2);
8580Sstevel@tonic-gate return ("ip6.int");
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate void
res_setservers(res_state statp,const union res_sockaddr_union * set,int cnt)8620Sstevel@tonic-gate res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
8630Sstevel@tonic-gate int i, nserv;
8640Sstevel@tonic-gate size_t size;
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate /* close open servers */
8670Sstevel@tonic-gate res_nclose(statp);
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate /* cause rtt times to be forgotten */
8700Sstevel@tonic-gate statp->_u._ext.nscount = 0;
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate nserv = 0;
8730Sstevel@tonic-gate for (i = 0; i < cnt && nserv < MAXNS; i++) {
8740Sstevel@tonic-gate switch (set->sin.sin_family) {
8750Sstevel@tonic-gate case AF_INET:
8760Sstevel@tonic-gate size = sizeof(set->sin);
8770Sstevel@tonic-gate if (statp->_u._ext.ext)
8780Sstevel@tonic-gate memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
8790Sstevel@tonic-gate &set->sin, size);
8800Sstevel@tonic-gate if (size <= sizeof(statp->nsaddr_list[nserv]))
8810Sstevel@tonic-gate memcpy(&statp->nsaddr_list[nserv],
8820Sstevel@tonic-gate &set->sin, size);
8830Sstevel@tonic-gate else
8840Sstevel@tonic-gate statp->nsaddr_list[nserv].sin_family = 0;
8850Sstevel@tonic-gate nserv++;
8860Sstevel@tonic-gate break;
8870Sstevel@tonic-gate
8880Sstevel@tonic-gate #ifdef HAS_INET6_STRUCTS
8890Sstevel@tonic-gate case AF_INET6:
8900Sstevel@tonic-gate size = sizeof(set->sin6);
8910Sstevel@tonic-gate if (statp->_u._ext.ext)
8920Sstevel@tonic-gate memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
8930Sstevel@tonic-gate &set->sin6, size);
8940Sstevel@tonic-gate if (size <= sizeof(statp->nsaddr_list[nserv]))
8950Sstevel@tonic-gate memcpy(&statp->nsaddr_list[nserv],
8960Sstevel@tonic-gate &set->sin6, size);
8970Sstevel@tonic-gate else
8980Sstevel@tonic-gate statp->nsaddr_list[nserv].sin_family = 0;
8990Sstevel@tonic-gate nserv++;
9000Sstevel@tonic-gate break;
9010Sstevel@tonic-gate #endif
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate default:
9040Sstevel@tonic-gate break;
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate set++;
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate statp->nscount = nserv;
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate int
res_getservers(res_state statp,union res_sockaddr_union * set,int cnt)9130Sstevel@tonic-gate res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
9140Sstevel@tonic-gate int i;
9150Sstevel@tonic-gate size_t size;
9160Sstevel@tonic-gate u_int16_t family;
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate for (i = 0; i < statp->nscount && i < cnt; i++) {
9190Sstevel@tonic-gate if (statp->_u._ext.ext)
9200Sstevel@tonic-gate family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
9210Sstevel@tonic-gate else
9220Sstevel@tonic-gate family = statp->nsaddr_list[i].sin_family;
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate switch (family) {
9250Sstevel@tonic-gate case AF_INET:
9260Sstevel@tonic-gate size = sizeof(set->sin);
9270Sstevel@tonic-gate if (statp->_u._ext.ext)
9280Sstevel@tonic-gate memcpy(&set->sin,
9290Sstevel@tonic-gate &statp->_u._ext.ext->nsaddrs[i],
9300Sstevel@tonic-gate size);
9310Sstevel@tonic-gate else
9320Sstevel@tonic-gate memcpy(&set->sin, &statp->nsaddr_list[i],
9330Sstevel@tonic-gate size);
9340Sstevel@tonic-gate break;
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate #ifdef HAS_INET6_STRUCTS
9370Sstevel@tonic-gate case AF_INET6:
9380Sstevel@tonic-gate size = sizeof(set->sin6);
9390Sstevel@tonic-gate if (statp->_u._ext.ext)
9400Sstevel@tonic-gate memcpy(&set->sin6,
9410Sstevel@tonic-gate &statp->_u._ext.ext->nsaddrs[i],
9420Sstevel@tonic-gate size);
9430Sstevel@tonic-gate else
9440Sstevel@tonic-gate memcpy(&set->sin6, &statp->nsaddr_list[i],
9450Sstevel@tonic-gate size);
9460Sstevel@tonic-gate break;
9470Sstevel@tonic-gate #endif
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate default:
9500Sstevel@tonic-gate set->sin.sin_family = 0;
9510Sstevel@tonic-gate break;
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate set++;
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate return (statp->nscount);
9560Sstevel@tonic-gate }
95711038SRao.Shoaib@Sun.COM
95811038SRao.Shoaib@Sun.COM /*! \file */
959