2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * University Copyright- Copyright (c) 1982, 1986, 1988
2N/A * The Regents of the University of California
2N/A * All Rights Reserved
2N/A *
2N/A * University Acknowledgment- Portions of this document are derived from
2N/A * software developed by the University of California, Berkeley, and its
2N/A * contributors.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/sockio.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <stdio.h>
2N/A#include <arpa/nameser.h>
2N/A#include <resolv.h>
2N/A
2N/A#include <netinet/in.h>
2N/A#include <net/if.h>
2N/A#include <netinet/if_ether.h>
2N/A#include <arpa/inet.h>
2N/A
2N/A#define MAXIFS 256
2N/A
2N/A/*
2N/A * Resolver state default settings
2N/A */
2N/A
2N/Astruct state _res = {
2N/A RES_TIMEOUT, /* retransmition time interval */
2N/A 4, /* number of times to retransmit */
2N/A RES_DEFAULT, /* options flags */
2N/A 1, /* number of name servers */
2N/A};
2N/A
2N/A/*
2N/A * Set up default settings. If the configuration file exist, the values
2N/A * there will have precedence. Otherwise, the server address is set to
2N/A * INADDR_LOOPBACK and the default domain name comes from the gethostname().
2N/A * BUT if the NIS/RPC domain name is set, that is used if all else fails.
2N/A *
2N/A * The configuration file should only be used if you want to redefine your
2N/A * domain or run without a server on your machine.
2N/A *
2N/A * Note the user can always override then domain name with the environment
2N/A * variable LOCALDOMAIN which has absolute priority.
2N/A *
2N/A *
2N/A * Return 0 if completes successfully, -1 on error
2N/A */
2N/Aint
2N/Ares_init(void)
2N/A{
2N/A register FILE *fp;
2N/A register char *cp, **pp;
2N/A register int n;
2N/A char buf[BUFSIZ];
2N/A#ifdef SYSV
2N/A extern char *strchr();
2N/A#else
2N/A extern char *index();
2N/A#endif
2N/A extern char *strcpy(), *strncpy();
2N/A extern char *getenv();
2N/A int nserv = 0; /* number of nameserver records read from file */
2N/A int haveenv = 0;
2N/A int havesearch = 0;
2N/A
2N/A _res.nsaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* INADDR_ANY */
2N/A _res.nsaddr.sin_family = AF_INET;
2N/A _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
2N/A _res.nscount = 1;
2N/A
2N/A#ifdef SIOCGIFNUM
2N/A { int numifs, s, n, int_up;
2N/A struct ifconf ifc;
2N/A register struct ifreq *ifrp;
2N/A struct ifreq ifr;
2N/A unsigned bufsize;
2N/A unsigned int flags;
2N/A char *buf;
2N/A extern void *malloc();
2N/A
2N/A if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2N/A perror("socket");
2N/A return (-1);
2N/A }
2N/A if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
2N/A numifs = MAXIFS;
2N/A }
2N/A bufsize = numifs * sizeof (struct ifreq);
2N/A buf = (char *)malloc(bufsize);
2N/A if (buf == NULL) {
2N/A perror("out of memory");
2N/A close(s);
2N/A return (-1);
2N/A }
2N/A ifc.ifc_len = bufsize;
2N/A ifc.ifc_buf = buf;
2N/A if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
2N/A perror("ifconfig: SIOCGIFCONF");
2N/A close(s);
2N/A free(buf);
2N/A return (-1);
2N/A }
2N/A
2N/A int_up = 0;
2N/A ifrp = ifc.ifc_req;
2N/A for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0;
2N/A n--, ifrp++) {
2N/A memset((void *) &ifr, 0, sizeof (ifr));
2N/A strncpy(ifr.ifr_name, ifrp->ifr_name,
2N/A sizeof (ifr.ifr_name));
2N/A if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
2N/A perror("SIOCGIFFLAGS");
2N/A close(s);
2N/A free(buf);
2N/A return (-1);
2N/A }
2N/A flags = ifr.ifr_flags;
2N/A /* we are looking for a non-loopback interface */
2N/A if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0))
2N/A int_up = 1;
2N/A }
2N/A close(s);
2N/A free(buf);
2N/A if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */
2N/A return (-1);
2N/A }
2N/A#endif /* SIOCGIFNUM */
2N/A
2N/A
2N/A /*
2N/A * for the benefit of hidden NIS domains, we use the same procedure
2N/A * as sendmail: convert leading + to dot, then drop to first dot
2N/A */
2N/A getdomainname(buf, BUFSIZ);
2N/A if (buf[0] == '+')
2N/A buf[0] = '.';
2N/A#ifdef SYSV
2N/A cp = strchr(buf, (int)'.');
2N/A#else
2N/A cp = index(buf, '.');
2N/A#endif
2N/A if (cp == NULL)
2N/A strcpy(_res.defdname, buf);
2N/A else
2N/A strcpy(_res.defdname, cp+1);
2N/A
2N/A /* Allow user to override the local domain definition */
2N/A if ((cp = getenv("LOCALDOMAIN")) != NULL) {
2N/A (void) strncpy(_res.defdname, cp, sizeof (_res.defdname));
2N/A haveenv++;
2N/A }
2N/A
2N/A if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
2N/A /* read the config file */
2N/A while (fgets(buf, sizeof (buf), fp) != NULL) {
2N/A /* read default domain name */
2N/A if (!strncmp(buf, "domain", sizeof ("domain") - 1)) {
2N/A if (haveenv) /* skip if have from environ */
2N/A continue;
2N/A cp = buf + sizeof ("domain") - 1;
2N/A while (*cp == ' ' || *cp == '\t')
2N/A cp++;
2N/A if ((*cp == '\0') || (*cp == '\n'))
2N/A continue;
2N/A (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
2N/A#ifdef SYSV
2N/A if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
2N/A#else
2N/A if ((cp = index(_res.defdname, '\n')) != NULL)
2N/A#endif
2N/A *cp = '\0';
2N/A havesearch = 0;
2N/A continue;
2N/A }
2N/A /* set search list */
2N/A if (!strncmp(buf, "search", sizeof ("search") - 1)) {
2N/A if (haveenv) /* skip if have from environ */
2N/A continue;
2N/A cp = buf + sizeof ("search") - 1;
2N/A while (*cp == ' ' || *cp == '\t')
2N/A cp++;
2N/A if ((*cp == '\0') || (*cp == '\n'))
2N/A continue;
2N/A (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
2N/A#ifdef SYSV
2N/A if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
2N/A#else
2N/A if ((cp = index(_res.defdname, '\n')) != NULL)
2N/A#endif
2N/A *cp = '\0';
2N/A /*
2N/A * Set search list to be blank-separated strings
2N/A * on rest of line.
2N/A */
2N/A cp = _res.defdname;
2N/A pp = _res.dnsrch;
2N/A *pp++ = cp;
2N/A for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
2N/A if (*cp == ' ' || *cp == '\t') {
2N/A *cp = 0;
2N/A n = 1;
2N/A } else if (n) {
2N/A *pp++ = cp;
2N/A n = 0;
2N/A }
2N/A }
2N/A /* null terminate last domain if there are excess */
2N/A while (*cp != '\0' && *cp != ' ' && *cp != '\t')
2N/A cp++;
2N/A *cp = '\0';
2N/A *pp++ = 0;
2N/A havesearch = 1;
2N/A continue;
2N/A }
2N/A /* read nameservers to query */
2N/A if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) &&
2N/A (nserv < MAXNS)) {
2N/A cp = buf + sizeof ("nameserver") - 1;
2N/A while (*cp == ' ' || *cp == '\t')
2N/A cp++;
2N/A if ((*cp == '\0') || (*cp == '\n'))
2N/A continue;
2N/A if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
2N/A inet_addr(cp)) == (unsigned) -1) {
2N/A _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
2N/A continue;
2N/A }
2N/A _res.nsaddr_list[nserv].sin_family = AF_INET;
2N/A _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
2N/A nserv++;
2N/A continue;
2N/A }
2N/A }
2N/A if (nserv > 1)
2N/A _res.nscount = nserv;
2N/A (void) fclose(fp);
2N/A }
2N/A if (_res.defdname[0] == 0) {
2N/A if (gethostname(buf, sizeof (_res.defdname)) == 0 &&
2N/A#ifdef SYSV
2N/A (cp = strchr(buf, (int)'.')))
2N/A#else
2N/A (cp = index(buf, '.')))
2N/A#endif
2N/A (void) strcpy(_res.defdname, cp + 1);
2N/A }
2N/A
2N/A /* find components of local domain that might be searched */
2N/A if (havesearch == 0) {
2N/A pp = _res.dnsrch;
2N/A *pp++ = _res.defdname;
2N/A for (cp = _res.defdname, n = 0; *cp; cp++)
2N/A if (*cp == '.')
2N/A n++;
2N/A cp = _res.defdname;
2N/A for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) {
2N/A#ifdef SYSV
2N/A cp = strchr(cp, (int)'.');
2N/A#else
2N/A cp = index(cp, '.');
2N/A#endif
2N/A *pp++ = ++cp;
2N/A }
2N/A *pp++ = 0;
2N/A }
2N/A _res.options |= RES_INIT;
2N/A return (0);
2N/A}