diff options
Diffstat (limited to 'lib/libc/net/res_init.c')
-rw-r--r-- | lib/libc/net/res_init.c | 108 |
1 files changed, 71 insertions, 37 deletions
diff --git a/lib/libc/net/res_init.c b/lib/libc/net/res_init.c index 347e5b6..c6292c8 100644 --- a/lib/libc/net/res_init.c +++ b/lib/libc/net/res_init.c @@ -53,11 +53,12 @@ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; -static char rcsid[] = "$Id: res_init.c,v 1.3 1994/09/25 17:45:39 pst Exp $"; +static char rcsid[] = "$Id: res_init.c,v 1.4 1995/05/30 05:40:55 rgrimes Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/param.h> #include <sys/socket.h> +#include <sys/time.h> #include <netinet/in.h> #include <arpa/inet.h> #include <arpa/nameser.h> @@ -69,9 +70,13 @@ static char rcsid[] = "$Id: res_init.c,v 1.3 1994/09/25 17:45:39 pst Exp $"; #include <stdlib.h> #include <string.h> +#include <res_config.h> + static void res_setoptions __P((char *, char *)); #ifdef RESOLVSORT +static const char sort_mask[] = "/&"; +#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) static u_int32_t net_mask __P((struct in_addr)); #endif @@ -102,11 +107,12 @@ struct __res_state _res; * * Return 0 if completes successfully, -1 on error */ +int res_init() { register FILE *fp; register char *cp, **pp; - register int n, dots; + register int n; char buf[BUFSIZ]; int nserv = 0; /* number of nameserver records read from file */ int haveenv = 0; @@ -115,9 +121,12 @@ res_init() int nsort = 0; char *net; #endif +#ifndef RFC1535 + int dots; +#endif /* - * These four fields used to be statically initialized. This made + * 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 @@ -128,14 +137,27 @@ res_init() * 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). */ if (!_res.retrans) _res.retrans = RES_TIMEOUT; if (!_res.retry) _res.retry = 4; - if (!_res.options) + if (!(_res.options & RES_INIT)) _res.options = RES_DEFAULT; + /* + * This one used to initialize implicitly to zero, so unless the app + * has set it to something in particular, we can randomize it now. + */ + if (!_res.id) + _res.id = res_randomid(); + #ifdef USELOOPBACK _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); #else @@ -181,14 +203,19 @@ res_init() *pp++ = 0; } +#define MATCH(line, name) \ + (!strncmp(line, name, sizeof(name) - 1) && \ + (line[sizeof(name) - 1] == ' ' || \ + line[sizeof(name) - 1] == '\t')) + if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { /* read the config file */ while (fgets(buf, sizeof(buf), fp) != NULL) { /* skip comments */ - if ((*buf == ';') || (*buf == '#')) + if (*buf == ';' || *buf == '#') continue; /* read default domain name */ - if (!strncmp(buf, "domain", sizeof("domain") - 1)) { + if (MATCH(buf, "domain")) { if (haveenv) /* skip if have from environ */ continue; cp = buf + sizeof("domain") - 1; @@ -204,7 +231,7 @@ res_init() continue; } /* set search list */ - if (!strncmp(buf, "search", sizeof("search") - 1)) { + if (MATCH(buf, "search")) { if (haveenv) /* skip if have from environ */ continue; cp = buf + sizeof("search") - 1; @@ -223,9 +250,7 @@ res_init() cp = _res.defdname; pp = _res.dnsrch; *pp++ = cp; - for (n = 0; - *cp && pp < _res.dnsrch + MAXDNSRCH; - cp++) { + for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { if (*cp == ' ' || *cp == '\t') { *cp = 0; n = 1; @@ -243,8 +268,7 @@ res_init() continue; } /* read nameservers to query */ - if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) && - nserv < MAXNS) { + if (MATCH(buf, "nameserver") && nserv < MAXNS) { struct in_addr a; cp = buf + sizeof("nameserver") - 1; @@ -260,7 +284,7 @@ res_init() continue; } #ifdef RESOLVSORT - if (!strncmp(buf, "sortlist", sizeof("sortlist") -1)) { + if (MATCH(buf, "sortlist")) { struct in_addr a; cp = buf + sizeof("sortlist") - 1; @@ -270,17 +294,18 @@ res_init() if (*cp == '\0' || *cp == '\n' || *cp == ';') break; net = cp; - while (*cp && *cp != '/' && + while (*cp && !ISSORTMASK(*cp) && *cp != ';' && isascii(*cp) && !isspace(*cp)) cp++; n = *cp; *cp = 0; if (inet_aton(net, &a)) { _res.sort_list[nsort].addr = a; - if (n == '/') { + if (ISSORTMASK(n)) { *cp++ = n; net = cp; - while (*cp && isascii(*cp) && !isspace(*cp)) + while (*cp && *cp != ';' && + isascii(*cp) && !isspace(*cp)) cp++; n = *cp; *cp = 0; @@ -296,12 +321,12 @@ res_init() } nsort++; } - *cp++ = n; + *cp = n; } continue; } #endif - if (!strncmp(buf, "options", sizeof("options") -1)) { + if (MATCH(buf, "options")) { res_setoptions(buf + sizeof("options") - 1, "conf"); continue; } @@ -312,13 +337,11 @@ res_init() _res.nsort = nsort; #endif (void) fclose(fp); - } /*if(fopen)*/ - if (_res.defdname[0] == 0) { - if (gethostname(buf, sizeof(_res.defdname) - 1) == 0 && - (cp = strchr(buf, '.'))) { - (void)strcpy(_res.defdname, cp + 1); - } } + if (_res.defdname[0] == 0 && + gethostname(buf, sizeof(_res.defdname) - 1) == 0 && + (cp = strchr(buf, '.')) != NULL) + strcpy(_res.defdname, cp + 1); /* find components of local domain that might be searched */ if (havesearch == 0) { @@ -333,32 +356,30 @@ res_init() cp = _res.defdname; while (pp < _res.dnsrch + MAXDFLSRCH) { - if (dots < LOCALDOMAINPARTS) { + if (dots < LOCALDOMAINPARTS) break; - } cp = strchr(cp, '.') + 1; /* we know there is one */ *pp++ = cp; dots--; } *pp = NULL; +#ifdef DEBUG if (_res.options & RES_DEBUG) { printf(";; res_init()... default dnsrch list:\n"); - for (pp = _res.dnsrch; *pp; pp++) { + for (pp = _res.dnsrch; *pp; pp++) printf(";;\t%s\n", *pp); - } printf(";;\t..END..\n"); } +#endif /* DEBUG */ #endif /*!RFC1535*/ } - if ((cp = getenv("RES_OPTIONS")) != NULL) { + if ((cp = getenv("RES_OPTIONS")) != NULL) res_setoptions(cp, "env"); - } _res.options |= RES_INIT; return (0); } - static void res_setoptions(options, source) char *options, *source; @@ -366,10 +387,11 @@ res_setoptions(options, source) char *cp = options; int i; - if (_res.options & RES_DEBUG) { +#ifdef DEBUG + if (_res.options & RES_DEBUG) printf(";; res_setoptions(\"%s\", \"%s\")...\n", options, source); - } +#endif while (*cp) { /* skip leading and inner runs of spaces */ while (*cp == ' ' || *cp == '\t') @@ -381,16 +403,19 @@ res_setoptions(options, source) _res.ndots = i; else _res.ndots = RES_MAXNDOTS; - if (_res.options & RES_DEBUG) { +#ifdef DEBUG + if (_res.options & RES_DEBUG) printf(";;\tndots=%d\n", _res.ndots); - } +#endif } else if (!strncmp(cp, "debug", sizeof("debug")-1)) { +#ifdef DEBUG if (!(_res.options & RES_DEBUG)) { printf(";; res_setoptions(\"%s\", \"%s\")..\n", options, source); _res.options |= RES_DEBUG; } printf(";;\tdebug\n"); +#endif } else { /* XXX - print a warning here? */ } @@ -401,6 +426,7 @@ res_setoptions(options, source) } #ifdef RESOLVSORT +/* XXX - should really support CIDR which means explicit masks always. */ static u_int32_t net_mask(in) /* XXX - should really use system's version of this */ struct in_addr in; @@ -411,7 +437,15 @@ net_mask(in) /* XXX - should really use system's version of this */ return (htonl(IN_CLASSA_NET)); else if (IN_CLASSB(i)) return (htonl(IN_CLASSB_NET)); - else - return (htonl(IN_CLASSC_NET)); + return (htonl(IN_CLASSC_NET)); } #endif + +u_int16_t +res_randomid() +{ + struct timeval now; + + gettimeofday(&now, NULL); + return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); +} |