res_init.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/*
* Copyright (c) 1985, 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Portions copyright (c) 1999, 2000
* Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
*
* This product includes software developed by the University of
* California, Berkeley, Intel Corporation, and its contributors.
*
* 4. Neither the name of University, Intel Corporation, or their respective
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
* INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies, and that
* the name of Digital Equipment Corporation not be used in advertising or
* publicity pertaining to distribution of the document or software without
* specific, written prior permission.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/*
* Portions Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
static char orig_rcsid[] = "From: Id: res_init.c,v 8.7 1996/11/18 09:10:04 vixie Exp $";
static char rcsid[] = "$Id: res_init.c,v 1.1.1.1 2003/11/19 01:51:37 kyu3 Exp $";
#endif /* LIBC_SCCS and not lint */
#include <ctype.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "res_config.h"
static void res_setoptions __P((char *, char *));
#ifdef RESOLVSORT
static const char sort_mask[] = "/&";
#endif
#if !defined(isascii) /* XXX - could be a function */
# define isascii(c) (!(c & 0200))
#endif
/*
* Resolver state default settings.
*/
struct __res_state _res
# if defined(__BIND_RES_TEXT)
= { RES_TIMEOUT, } /* Motorola, et al. */
# endif
;
/*
* Set up default settings. If the configuration file exist, the values
* there will have precedence. Otherwise, the server address is set to
* INADDR_ANY and the default domain name comes from the gethostname().
*
* An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
* rather than INADDR_ANY ("0.0.0.0") as the default name server address
* since it was noted that INADDR_ANY actually meant ``the first interface
* you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
* it had to be "up" in order for you to reach your own name server. It
* was later decided that since the recommended practice is to always
* install local static routes through 127.0.0.1 for all your network
* interfaces, that we could solve this problem without a code change.
*
* The configuration file should always be used, since it is the only way
* to specify a default domain. If you are running a server on your local
* machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
* in the configuration file.
*
* Return 0 if completes successfully, -1 on error
*/
int
res_init()
{
register int n;
int nserv = 0; /* number of nameserver records read from file */
int haveenv = 0;
int havesearch = 0;
#ifdef RESOLVSORT
int nsort = 0;
char *net;
#endif
#ifndef RFC1535
int dots;
#endif
/*
* These three fields used to be statically initialized. This made
* it hard to use this code in a shared library. It is necessary,
* now that we're doing dynamic initialization here, that we preserve
* the old semantics: if an application modifies one of these three
* fields of _res before res_init() is called, res_init() will not
* alter them. Of course, if an application is setting them to
* _zero_ before calling res_init(), hoping to override what used
* to be the static default, we can't detect it and unexpected results
* will follow. Zero for any of these fields would make no sense,
* so one can safely assume that the applications were already getting
* unexpected results.
*
* _res.options is tricky since some apps were known to diddle the bits
* before res_init() was first called. We can't replicate that semantic
* with dynamic initialization (they may have turned bits off that are
* set in RES_DEFAULT). Our solution is to declare such applications
* "broken". They could fool us by setting RES_INIT but none do (yet).
*/
/*
* This one used to initialize implicitly to zero, so unless the app
* has set it to something in particular, we can randomize it now.
*/
#ifdef USELOOPBACK
#else
#endif
/* Allow user to override the local domain definition */
#ifdef _ORG_FREEBSD_
#else
#endif
{
haveenv++;
/*
* Set search list to be blank-separated strings
* from rest of env value. Permits users of LOCALDOMAIN
* to still have a search list, and anyone to set the
* one that they want to use as an individual (even more
* important now that the rfc1535 stuff restricts searches)
*/
break;
*cp = 0;
n = 1;
} else if (n) {
n = 0;
havesearch = 1;
}
}
/* null terminate last domain if there are excess */
cp++;
*cp = '\0';
*pp++ = 0;
}
/* read the config file */
/* skip comments */
continue;
/* read default domain name */
if (haveenv) /* skip if have from environ */
continue;
cp++;
continue;
*cp = '\0';
havesearch = 0;
continue;
}
/* set search list */
if (haveenv) /* skip if have from environ */
continue;
cp++;
continue;
*cp = '\0';
/*
* Set search list to be blank-separated strings
* on rest of line.
*/
*cp = 0;
n = 1;
} else if (n) {
n = 0;
}
}
/* null terminate last domain if there are excess */
cp++;
*cp = '\0';
*pp++ = 0;
havesearch = 1;
continue;
}
/* read nameservers to query */
struct in_addr a;
cp++;
nserv++;
}
continue;
}
#ifdef RESOLVSORT
struct in_addr a;
while (nsort < MAXRESOLVSORT) {
cp++;
break;
cp++;
n = *cp;
*cp = 0;
if (ISSORTMASK(n)) {
*cp++ = (char)n;
cp++;
n = *cp;
*cp = 0;
} else {
}
} else {
}
nsort++;
}
*cp = (char)n;
}
continue;
}
#endif
continue;
}
}
if (nserv > 1)
#ifdef RESOLVSORT
#endif
}
/* find components of local domain that might be searched */
if (havesearch == 0) {
#ifndef RFC1535
dots = 0;
if (dots < LOCALDOMAINPARTS)
break;
dots--;
}
#ifdef DEBUG
printf(";; res_init()... default dnsrch list:\n");
printf(";;\t..END..\n");
}
#endif
#endif /* !RFC1535 */
}
#ifdef _ORG_FREEBSD_
if (issetugid())
#else
if (1)
#endif
if ( 0 == nserv ) {
return -1;
}
return (0);
}
static void
char *options,
char *source
)
{
int i;
#ifdef DEBUG
printf(";; res_setoptions(\"%s\", \"%s\")...\n",
#endif
while (*cp) {
/* skip leading and inner runs of spaces */
cp++;
/* search for and process individual options */
if (i <= RES_MAXNDOTS)
else
#ifdef DEBUG
#endif
#ifdef DEBUG
printf(";; res_setoptions(\"%s\", \"%s\")..\n",
}
printf(";;\tdebug\n");
#endif
} else {
/* XXX - print a warning here? */
}
/* skip to next run of spaces */
cp++;
}
}
#ifdef RESOLVSORT
/* XXX - should really support CIDR which means explicit masks always. */
static u_int32_t
)
{
if (IN_CLASSA(i))
return (htonl(IN_CLASSA_NET));
else if (IN_CLASSB(i))
return (htonl(IN_CLASSB_NET));
return (htonl(IN_CLASSC_NET));
}
#endif
{
}