1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-29 11:41:21 +03:00

resolv: Reformat res_vinit and related functions to GNU style

Also remove some obsolete comments.
This commit is contained in:
Florian Weimer
2017-06-19 13:20:46 +02:00
parent 94f094f22b
commit 4c4480eecb
2 changed files with 370 additions and 334 deletions

View File

@ -1,3 +1,7 @@
2017-06-19 Florian Weimer <fweimer@redhat.com>
* resolv/res_init.c: Reformat to GNU style.
2017-06-19 Florian Weimer <fweimer@redhat.com> 2017-06-19 Florian Weimer <fweimer@redhat.com>
* resolv/res_init.c (sort_mask_chars, ISSORTMASK): Remove. * resolv/res_init.c (sort_mask_chars, ISSORTMASK): Remove.

View File

@ -1,3 +1,21 @@
/* Resolver state initialization and resolv.conf parsing.
Copyright (C) 1995-2017 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* /*
* Copyright (c) 1985, 1989, 1993 * Copyright (c) 1985, 1989, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -83,26 +101,15 @@
#include <sys/types.h> #include <sys/types.h>
#include <inet/net-internal.h> #include <inet/net-internal.h>
static void res_setoptions (res_state, const char *, const char *) static void res_setoptions (res_state, const char *, const char *);
internal_function; static uint32_t net_mask (struct in_addr);
static u_int32_t net_mask (struct in_addr) __THROW;
unsigned long long int __res_initstamp attribute_hidden; unsigned long long int __res_initstamp attribute_hidden;
/*
* Resolver state default settings.
*/
/*
* Set up default settings. If the configuration file exist, the values
* there will have precedence. Otherwise, the server address is set to
* INADDR_LOOPBACK and the default domain name comes from gethostname.
*
* Return 0 if completes successfully, -1 on error
*/
int int
res_ninit(res_state statp) { res_ninit (res_state statp)
return (__res_vinit(statp, 0)); {
return __res_vinit (statp, 0);
} }
libc_hidden_def (__res_ninit) libc_hidden_def (__res_ninit)
@ -114,349 +121,374 @@ is_sort_mask (char ch)
return ch == '/' || ch == '&'; return ch == '/' || ch == '&';
} }
/* This function has to be reachable by res_data.c but not publically. */ /* Set up default settings. If the /etc/resolv.conf configuration
file exist, the values there will have precedence. Otherwise, the
server address is set to INADDR_LOOPBACK and the default domain
name comes from gethostname. The RES_OPTIONS and LOCALDOMAIN
environment variables can be used to override some settings.
Return 0 if completes successfully, -1 on error. */
int int
__res_vinit(res_state statp, int preinit) { __res_vinit (res_state statp, int preinit)
FILE *fp; {
char *cp, **pp; char *cp, **pp;
int n; char buf[BUFSIZ];
char buf[BUFSIZ]; int nserv = 0; /* Number of nameservers read from file. */
int nserv = 0; /* number of nameservers read from file */ bool have_serv6 = false;
int have_serv6 = 0; bool haveenv = false;
int haveenv = 0; bool havesearch = false;
int havesearch = 0; int nsort = 0;
int nsort = 0; char *net;
char *net; statp->_u._ext.initstamp = __res_initstamp;
statp->_u._ext.initstamp = __res_initstamp;
if (!preinit) { if (!preinit)
statp->retrans = RES_TIMEOUT; {
statp->retry = RES_DFLRETRY; statp->retrans = RES_TIMEOUT;
statp->options = RES_DEFAULT; statp->retry = RES_DFLRETRY;
statp->id = res_randomid(); statp->options = RES_DEFAULT;
} statp->id = res_randomid ();
}
statp->nscount = 0; statp->nscount = 0;
statp->defdname[0] = '\0'; statp->defdname[0] = '\0';
statp->ndots = 1; statp->ndots = 1;
statp->pfcode = 0; statp->pfcode = 0;
statp->_vcsock = -1; statp->_vcsock = -1;
statp->_flags = 0; statp->_flags = 0;
statp->__glibc_unused_qhook = NULL; statp->__glibc_unused_qhook = NULL;
statp->__glibc_unused_rhook = NULL; statp->__glibc_unused_rhook = NULL;
statp->_u._ext.nscount = 0; statp->_u._ext.nscount = 0;
for (n = 0; n < MAXNS; n++) for (int n = 0; n < MAXNS; n++)
statp->_u._ext.nsaddrs[n] = NULL; statp->_u._ext.nsaddrs[n] = NULL;
/* Allow user to override the local domain definition */ /* Allow user to override the local domain definition. */
if ((cp = getenv("LOCALDOMAIN")) != NULL) { if ((cp = getenv ("LOCALDOMAIN")) != NULL)
(void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); {
statp->defdname[sizeof(statp->defdname) - 1] = '\0'; strncpy (statp->defdname, cp, sizeof (statp->defdname) - 1);
haveenv++; statp->defdname[sizeof (statp->defdname) - 1] = '\0';
haveenv = true;
/* /* Set search list to be blank-separated strings from rest of
* Set search list to be blank-separated strings env value. Permits users of LOCALDOMAIN to still have a
* from rest of env value. Permits users of LOCALDOMAIN search list, and anyone to set the one that they want to use
* to still have a search list, and anyone to set the as an individual (even more important now that the rfc1535
* one that they want to use as an individual (even more stuff restricts searches). */
* important now that the rfc1535 stuff restricts searches) cp = statp->defdname;
*/ pp = statp->dnsrch;
cp = statp->defdname; *pp++ = cp;
pp = statp->dnsrch; for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
*pp++ = cp; {
for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { if (*cp == '\n')
if (*cp == '\n') /* silly backwards compat */ break;
break; else if (*cp == ' ' || *cp == '\t')
else if (*cp == ' ' || *cp == '\t') { {
*cp = 0; *cp = 0;
n = 1; n = 1;
} else if (n) { }
*pp++ = cp; else if (n > 0)
n = 0; {
havesearch = 1; *pp++ = cp;
} n = 0;
} havesearch = true;
/* null terminate last domain if there are excess */ }
while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') }
cp++; /* Null terminate last domain if there are excess. */
*cp = '\0'; while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
*pp++ = 0; cp++;
} *cp = '\0';
*pp++ = 0;
}
#define MATCH(line, name) \ #define MATCH(line, name) \
(!strncmp(line, name, sizeof(name) - 1) && \ (!strncmp (line, name, sizeof (name) - 1) \
(line[sizeof(name) - 1] == ' ' || \ && (line[sizeof (name) - 1] == ' ' \
line[sizeof(name) - 1] == '\t')) || line[sizeof (name) - 1] == '\t'))
if ((fp = fopen(_PATH_RESCONF, "rce")) != NULL) { FILE *fp = fopen (_PATH_RESCONF, "rce");
/* No threads use this stream. */ if (fp != NULL)
__fsetlocking (fp, FSETLOCKING_BYCALLER); {
/* read the config file */ /* No threads use this stream. */
while (__fgets_unlocked(buf, sizeof(buf), fp) != NULL) { __fsetlocking (fp, FSETLOCKING_BYCALLER);
/* skip comments */ /* Read the config file. */
if (*buf == ';' || *buf == '#') while (__fgets_unlocked (buf, sizeof (buf), fp) != NULL)
continue; {
/* read default domain name */ /* Skip comments. */
if (MATCH(buf, "domain")) { if (*buf == ';' || *buf == '#')
if (haveenv) /* skip if have from environ */ continue;
continue; /* Read default domain name. */
cp = buf + sizeof("domain") - 1; if (MATCH (buf, "domain"))
while (*cp == ' ' || *cp == '\t') {
cp++; if (haveenv)
if ((*cp == '\0') || (*cp == '\n')) /* LOCALDOMAIN overrides the configuration file. */
continue; continue;
strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); cp = buf + sizeof ("domain") - 1;
statp->defdname[sizeof(statp->defdname) - 1] = '\0'; while (*cp == ' ' || *cp == '\t')
if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) cp++;
*cp = '\0'; if ((*cp == '\0') || (*cp == '\n'))
havesearch = 0; continue;
continue; strncpy (statp->defdname, cp, sizeof (statp->defdname) - 1);
} statp->defdname[sizeof (statp->defdname) - 1] = '\0';
/* set search list */ if ((cp = strpbrk (statp->defdname, " \t\n")) != NULL)
if (MATCH(buf, "search")) { *cp = '\0';
if (haveenv) /* skip if have from environ */ havesearch = false;
continue; continue;
cp = buf + sizeof("search") - 1; }
while (*cp == ' ' || *cp == '\t') /* Set search list. */
cp++; if (MATCH (buf, "search"))
if ((*cp == '\0') || (*cp == '\n')) {
continue; if (haveenv)
strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); /* LOCALDOMAIN overrides the configuration file. */
statp->defdname[sizeof(statp->defdname) - 1] = '\0'; continue;
if ((cp = strchr(statp->defdname, '\n')) != NULL) cp = buf + sizeof ("search") - 1;
*cp = '\0'; while (*cp == ' ' || *cp == '\t')
/* cp++;
* Set search list to be blank-separated strings if ((*cp == '\0') || (*cp == '\n'))
* on rest of line. continue;
*/ strncpy (statp->defdname, cp, sizeof (statp->defdname) - 1);
cp = statp->defdname; statp->defdname[sizeof (statp->defdname) - 1] = '\0';
pp = statp->dnsrch; if ((cp = strchr (statp->defdname, '\n')) != NULL)
*pp++ = cp; *cp = '\0';
for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { /* Set search list to be blank-separated strings on rest
if (*cp == ' ' || *cp == '\t') { of line. */
*cp = 0; cp = statp->defdname;
n = 1; pp = statp->dnsrch;
} else if (n) { *pp++ = cp;
*pp++ = cp; for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
n = 0; {
} if (*cp == ' ' || *cp == '\t')
} {
/* null terminate last domain if there are excess */ *cp = 0;
while (*cp != '\0' && *cp != ' ' && *cp != '\t') n = 1;
cp++; }
*cp = '\0'; else if (n)
*pp++ = 0; {
havesearch = 1; *pp++ = cp;
continue; n = 0;
} }
/* read nameservers to query */ }
if (MATCH(buf, "nameserver") && nserv < MAXNS) { /* Null terminate last domain if there are excess. */
struct in_addr a; while (*cp != '\0' && *cp != ' ' && *cp != '\t')
cp++;
*cp = '\0';
*pp++ = 0;
havesearch = true;
continue;
}
/* Read nameservers to query. */
if (MATCH (buf, "nameserver") && nserv < MAXNS)
{
struct in_addr a;
cp = buf + sizeof("nameserver") - 1; cp = buf + sizeof ("nameserver") - 1;
while (*cp == ' ' || *cp == '\t') while (*cp == ' ' || *cp == '\t')
cp++; cp++;
if ((*cp != '\0') && (*cp != '\n') if ((*cp != '\0') && (*cp != '\n') && __inet_aton (cp, &a))
&& __inet_aton(cp, &a)) { {
statp->nsaddr_list[nserv].sin_addr = a; statp->nsaddr_list[nserv].sin_addr = a;
statp->nsaddr_list[nserv].sin_family = AF_INET; statp->nsaddr_list[nserv].sin_family = AF_INET;
statp->nsaddr_list[nserv].sin_port = statp->nsaddr_list[nserv].sin_port = htons (NAMESERVER_PORT);
htons(NAMESERVER_PORT); nserv++;
nserv++; }
} else { else
struct in6_addr a6; {
char *el; struct in6_addr a6;
char *el;
if ((el = strpbrk(cp, " \t\n")) != NULL) if ((el = strpbrk (cp, " \t\n")) != NULL)
*el = '\0'; *el = '\0';
if ((el = strchr(cp, SCOPE_DELIMITER)) != NULL) if ((el = strchr (cp, SCOPE_DELIMITER)) != NULL)
*el = '\0'; *el = '\0';
if ((*cp != '\0') && if ((*cp != '\0') && (__inet_pton (AF_INET6, cp, &a6) > 0))
(__inet_pton(AF_INET6, cp, &a6) > 0)) { {
struct sockaddr_in6 *sa6; struct sockaddr_in6 *sa6;
sa6 = malloc(sizeof(*sa6)); sa6 = malloc (sizeof (*sa6));
if (sa6 != NULL) { if (sa6 != NULL)
sa6->sin6_family = AF_INET6; {
sa6->sin6_port = htons(NAMESERVER_PORT); sa6->sin6_family = AF_INET6;
sa6->sin6_flowinfo = 0; sa6->sin6_port = htons (NAMESERVER_PORT);
sa6->sin6_addr = a6; sa6->sin6_flowinfo = 0;
sa6->sin6_addr = a6;
sa6->sin6_scope_id = 0; sa6->sin6_scope_id = 0;
if (__glibc_likely (el != NULL)) { if (__glibc_likely (el != NULL))
/* Ignore errors, for backwards /* Ignore errors, for backwards
compatibility. */ compatibility. */
(void) __inet6_scopeid_pton __inet6_scopeid_pton
(&a6, el + 1, &sa6->sin6_scope_id); (&a6, el + 1, &sa6->sin6_scope_id);
}
statp->nsaddr_list[nserv].sin_family = 0; statp->nsaddr_list[nserv].sin_family = 0;
statp->_u._ext.nsaddrs[nserv] = sa6; statp->_u._ext.nsaddrs[nserv] = sa6;
statp->_u._ext.nssocks[nserv] = -1; statp->_u._ext.nssocks[nserv] = -1;
have_serv6 = 1; have_serv6 = true;
nserv++; nserv++;
} }
} }
} }
continue; continue;
} }
if (MATCH(buf, "sortlist")) { if (MATCH (buf, "sortlist"))
struct in_addr a; {
struct in_addr a;
cp = buf + sizeof("sortlist") - 1; cp = buf + sizeof ("sortlist") - 1;
while (nsort < MAXRESOLVSORT) { while (nsort < MAXRESOLVSORT)
while (*cp == ' ' || *cp == '\t') {
cp++; while (*cp == ' ' || *cp == '\t')
if (*cp == '\0' || *cp == '\n' || *cp == ';') cp++;
break; if (*cp == '\0' || *cp == '\n' || *cp == ';')
net = cp; break;
while (*cp && !is_sort_mask (*cp) && *cp != ';' && net = cp;
isascii(*cp) && !isspace(*cp)) while (*cp && !is_sort_mask (*cp) && *cp != ';'
cp++; && isascii (*cp) && !isspace (*cp))
n = *cp; cp++;
*cp = 0; char separator = *cp;
if (__inet_aton(net, &a)) { *cp = 0;
statp->sort_list[nsort].addr = a; if (__inet_aton (net, &a))
if (is_sort_mask (n)) { {
*cp++ = n; statp->sort_list[nsort].addr = a;
net = cp; if (is_sort_mask (separator))
while (*cp && *cp != ';' && {
isascii(*cp) && !isspace(*cp)) *cp++ = separator;
cp++; net = cp;
n = *cp; while (*cp && *cp != ';'
*cp = 0; && isascii (*cp) && !isspace (*cp))
if (__inet_aton(net, &a)) { cp++;
statp->sort_list[nsort].mask = a.s_addr; separator = *cp;
} else { *cp = 0;
statp->sort_list[nsort].mask = if (__inet_aton (net, &a))
net_mask(statp->sort_list[nsort].addr); statp->sort_list[nsort].mask = a.s_addr;
} else
} else { statp->sort_list[nsort].mask
statp->sort_list[nsort].mask = = net_mask (statp->sort_list[nsort].addr);
net_mask(statp->sort_list[nsort].addr); }
} else
nsort++; statp->sort_list[nsort].mask
} = net_mask (statp->sort_list[nsort].addr);
*cp = n; nsort++;
} }
continue; *cp = separator;
} }
if (MATCH(buf, "options")) { continue;
res_setoptions(statp, buf + sizeof("options") - 1, "conf"); }
continue; if (MATCH (buf, "options"))
} {
} res_setoptions (statp, buf + sizeof ("options") - 1, "conf");
statp->nscount = nserv; continue;
if (have_serv6) { }
/* We try IPv6 servers again. */ }
statp->ipv6_unavail = false; statp->nscount = nserv;
} if (have_serv6)
statp->nsort = nsort; /* We try IPv6 servers again. */
(void) fclose(fp); statp->ipv6_unavail = false;
} statp->nsort = nsort;
if (__glibc_unlikely (statp->nscount == 0)) { fclose (fp);
statp->nsaddr.sin_addr = __inet_makeaddr(IN_LOOPBACKNET, 1); }
statp->nsaddr.sin_family = AF_INET; if (__glibc_unlikely (statp->nscount == 0))
statp->nsaddr.sin_port = htons(NAMESERVER_PORT); {
statp->nscount = 1; statp->nsaddr.sin_addr = __inet_makeaddr (IN_LOOPBACKNET, 1);
} statp->nsaddr.sin_family = AF_INET;
if (statp->defdname[0] == 0 && statp->nsaddr.sin_port = htons (NAMESERVER_PORT);
__gethostname(buf, sizeof(statp->defdname) - 1) == 0 && statp->nscount = 1;
(cp = strchr(buf, '.')) != NULL) }
strcpy(statp->defdname, cp + 1); if (statp->defdname[0] == 0
&& __gethostname (buf, sizeof (statp->defdname) - 1) == 0
&& (cp = strchr (buf, '.')) != NULL)
strcpy (statp->defdname, cp + 1);
/* find components of local domain that might be searched */ /* Find components of local domain that might be searched. */
if (havesearch == 0) { if (!havesearch)
pp = statp->dnsrch; {
*pp++ = statp->defdname; pp = statp->dnsrch;
*pp = NULL; *pp++ = statp->defdname;
*pp = NULL;
} }
if ((cp = getenv("RES_OPTIONS")) != NULL) if ((cp = getenv ("RES_OPTIONS")) != NULL)
res_setoptions(statp, cp, "env"); res_setoptions (statp, cp, "env");
statp->options |= RES_INIT; statp->options |= RES_INIT;
return (0); return 0;
} }
static void static void
internal_function res_setoptions (res_state statp, const char *options, const char *source)
res_setoptions(res_state statp, const char *options, const char *source) { {
const char *cp = options; const char *cp = options;
int i;
while (*cp) { while (*cp)
/* skip leading and inner runs of spaces */ {
while (*cp == ' ' || *cp == '\t') /* Skip leading and inner runs of spaces. */
cp++; while (*cp == ' ' || *cp == '\t')
/* search for and process individual options */ cp++;
if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { /* Search for and process individual options. */
i = atoi(cp + sizeof("ndots:") - 1); if (!strncmp (cp, "ndots:", sizeof ("ndots:") - 1))
if (i <= RES_MAXNDOTS) {
statp->ndots = i; int i = atoi (cp + sizeof ("ndots:") - 1);
else if (i <= RES_MAXNDOTS)
statp->ndots = RES_MAXNDOTS; statp->ndots = i;
} else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { else
i = atoi(cp + sizeof("timeout:") - 1); statp->ndots = RES_MAXNDOTS;
if (i <= RES_MAXRETRANS) }
statp->retrans = i; else if (!strncmp (cp, "timeout:", sizeof ("timeout:") - 1))
else {
statp->retrans = RES_MAXRETRANS; int i = atoi (cp + sizeof ("timeout:") - 1);
} else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ if (i <= RES_MAXRETRANS)
i = atoi(cp + sizeof("attempts:") - 1); statp->retrans = i;
if (i <= RES_MAXRETRY) else
statp->retry = i; statp->retrans = RES_MAXRETRANS;
else }
statp->retry = RES_MAXRETRY; else if (!strncmp (cp, "attempts:", sizeof ("attempts:") - 1))
} else { {
static const struct int i = atoi (cp + sizeof ("attempts:") - 1);
{ if (i <= RES_MAXRETRY)
char str[22]; statp->retry = i;
uint8_t len; else
uint8_t clear; statp->retry = RES_MAXRETRY;
unsigned long int flag; }
} options[] = { else
{
static const struct
{
char str[22];
uint8_t len;
uint8_t clear;
unsigned long int flag;
} options[] = {
#define STRnLEN(str) str, sizeof (str) - 1 #define STRnLEN(str) str, sizeof (str) - 1
{ STRnLEN ("inet6"), 0, DEPRECATED_RES_USE_INET6 }, { STRnLEN ("inet6"), 0, DEPRECATED_RES_USE_INET6 },
{ STRnLEN ("rotate"), 0, RES_ROTATE }, { STRnLEN ("rotate"), 0, RES_ROTATE },
{ STRnLEN ("edns0"), 0, RES_USE_EDNS0 }, { STRnLEN ("edns0"), 0, RES_USE_EDNS0 },
{ STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP }, { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP },
{ STRnLEN ("single-request"), 0, RES_SNGLKUP }, { STRnLEN ("single-request"), 0, RES_SNGLKUP },
{ STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY }, { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY },
{ STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY }, { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY },
{ STRnLEN ("use-vc"), 0, RES_USEVC } { STRnLEN ("use-vc"), 0, RES_USEVC }
}; };
#define noptions (sizeof (options) / sizeof (options[0])) #define noptions (sizeof (options) / sizeof (options[0]))
int i; for (int i = 0; i < noptions; ++i)
for (i = 0; i < noptions; ++i) if (strncmp (cp, options[i].str, options[i].len) == 0)
if (strncmp (cp, options[i].str, options[i].len) == 0) {
{ if (options[i].clear)
if (options[i].clear) statp->options &= options[i].flag;
statp->options &= options[i].flag; else
else statp->options |= options[i].flag;
statp->options |= options[i].flag; break;
break; }
} }
if (i == noptions) { /* Skip to next run of spaces. */
/* XXX - print a warning here? */ while (*cp && *cp != ' ' && *cp != '\t')
} cp++;
} }
/* skip to next run of spaces */
while (*cp && *cp != ' ' && *cp != '\t')
cp++;
}
} }
/* XXX - should really support CIDR which means explicit masks always. */ static uint32_t
/* XXX - should really use system's version of this */
static u_int32_t
net_mask (struct in_addr in) net_mask (struct in_addr in)
{ {
u_int32_t i = ntohl(in.s_addr); uint32_t i = ntohl (in.s_addr);
if (IN_CLASSA(i)) if (IN_CLASSA (i))
return (htonl(IN_CLASSA_NET)); return htonl (IN_CLASSA_NET);
else if (IN_CLASSB(i)) else if (IN_CLASSB (i))
return (htonl(IN_CLASSB_NET)); return htonl (IN_CLASSB_NET);
return (htonl(IN_CLASSC_NET)); return htonl (IN_CLASSC_NET);
} }