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