1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-28 00:21:52 +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,22 +121,27 @@ 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. */
int have_serv6 = 0; bool have_serv6 = false;
int haveenv = 0; bool haveenv = false;
int havesearch = 0; bool havesearch = false;
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->retrans = RES_TIMEOUT;
statp->retry = RES_DFLRETRY; statp->retry = RES_DFLRETRY;
statp->options = RES_DEFAULT; statp->options = RES_DEFAULT;
@ -145,38 +157,41 @@ __res_vinit(res_state statp, int preinit) {
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); {
strncpy (statp->defdname, cp, sizeof (statp->defdname) - 1);
statp->defdname[sizeof (statp->defdname) - 1] = '\0'; statp->defdname[sizeof (statp->defdname) - 1] = '\0';
haveenv++; 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; cp = statp->defdname;
pp = statp->dnsrch; pp = statp->dnsrch;
*pp++ = cp; *pp++ = cp;
for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
if (*cp == '\n') /* silly backwards compat */ {
if (*cp == '\n')
break; break;
else if (*cp == ' ' || *cp == '\t') { else if (*cp == ' ' || *cp == '\t')
{
*cp = 0; *cp = 0;
n = 1; n = 1;
} else if (n) { }
else if (n > 0)
{
*pp++ = cp; *pp++ = cp;
n = 0; n = 0;
havesearch = 1; havesearch = true;
} }
} }
/* null terminate last domain if there are excess */ /* Null terminate last domain if there are excess. */
while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
cp++; cp++;
*cp = '\0'; *cp = '\0';
@ -184,21 +199,26 @@ __res_vinit(res_state statp, int preinit) {
} }
#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");
if (fp != NULL)
{
/* No threads use this stream. */ /* No threads use this stream. */
__fsetlocking (fp, FSETLOCKING_BYCALLER); __fsetlocking (fp, FSETLOCKING_BYCALLER);
/* read the config file */ /* Read the config file. */
while (__fgets_unlocked(buf, sizeof(buf), fp) != NULL) { while (__fgets_unlocked (buf, sizeof (buf), fp) != NULL)
/* skip comments */ {
/* Skip comments. */
if (*buf == ';' || *buf == '#') if (*buf == ';' || *buf == '#')
continue; continue;
/* read default domain name */ /* Read default domain name. */
if (MATCH(buf, "domain")) { if (MATCH (buf, "domain"))
if (haveenv) /* skip if have from environ */ {
if (haveenv)
/* LOCALDOMAIN overrides the configuration file. */
continue; continue;
cp = buf + sizeof ("domain") - 1; cp = buf + sizeof ("domain") - 1;
while (*cp == ' ' || *cp == '\t') while (*cp == ' ' || *cp == '\t')
@ -209,12 +229,14 @@ __res_vinit(res_state statp, int preinit) {
statp->defdname[sizeof (statp->defdname) - 1] = '\0'; statp->defdname[sizeof (statp->defdname) - 1] = '\0';
if ((cp = strpbrk (statp->defdname, " \t\n")) != NULL) if ((cp = strpbrk (statp->defdname, " \t\n")) != NULL)
*cp = '\0'; *cp = '\0';
havesearch = 0; havesearch = false;
continue; continue;
} }
/* set search list */ /* Set search list. */
if (MATCH(buf, "search")) { if (MATCH (buf, "search"))
if (haveenv) /* skip if have from environ */ {
if (haveenv)
/* LOCALDOMAIN overrides the configuration file. */
continue; continue;
cp = buf + sizeof ("search") - 1; cp = buf + sizeof ("search") - 1;
while (*cp == ' ' || *cp == '\t') while (*cp == ' ' || *cp == '\t')
@ -225,45 +247,49 @@ __res_vinit(res_state statp, int preinit) {
statp->defdname[sizeof (statp->defdname) - 1] = '\0'; statp->defdname[sizeof (statp->defdname) - 1] = '\0';
if ((cp = strchr (statp->defdname, '\n')) != NULL) if ((cp = strchr (statp->defdname, '\n')) != NULL)
*cp = '\0'; *cp = '\0';
/* /* Set search list to be blank-separated strings on rest
* Set search list to be blank-separated strings of line. */
* on rest of line.
*/
cp = statp->defdname; cp = statp->defdname;
pp = statp->dnsrch; pp = statp->dnsrch;
*pp++ = cp; *pp++ = cp;
for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
if (*cp == ' ' || *cp == '\t') { {
if (*cp == ' ' || *cp == '\t')
{
*cp = 0; *cp = 0;
n = 1; n = 1;
} else if (n) { }
else if (n)
{
*pp++ = cp; *pp++ = cp;
n = 0; n = 0;
} }
} }
/* null terminate last domain if there are excess */ /* Null terminate last domain if there are excess. */
while (*cp != '\0' && *cp != ' ' && *cp != '\t') while (*cp != '\0' && *cp != ' ' && *cp != '\t')
cp++; cp++;
*cp = '\0'; *cp = '\0';
*pp++ = 0; *pp++ = 0;
havesearch = 1; havesearch = true;
continue; continue;
} }
/* read nameservers to query */ /* Read nameservers to query. */
if (MATCH(buf, "nameserver") && nserv < MAXNS) { if (MATCH (buf, "nameserver") && nserv < MAXNS)
{
struct in_addr a; 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; struct in6_addr a6;
char *el; char *el;
@ -271,102 +297,107 @@ __res_vinit(res_state statp, int preinit) {
*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_family = AF_INET6;
sa6->sin6_port = htons (NAMESERVER_PORT); sa6->sin6_port = htons (NAMESERVER_PORT);
sa6->sin6_flowinfo = 0; sa6->sin6_flowinfo = 0;
sa6->sin6_addr = a6; 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') while (*cp == ' ' || *cp == '\t')
cp++; cp++;
if (*cp == '\0' || *cp == '\n' || *cp == ';') if (*cp == '\0' || *cp == '\n' || *cp == ';')
break; break;
net = cp; net = cp;
while (*cp && !is_sort_mask (*cp) && *cp != ';' && while (*cp && !is_sort_mask (*cp) && *cp != ';'
isascii(*cp) && !isspace(*cp)) && isascii (*cp) && !isspace (*cp))
cp++; cp++;
n = *cp; char separator = *cp;
*cp = 0; *cp = 0;
if (__inet_aton(net, &a)) { if (__inet_aton (net, &a))
{
statp->sort_list[nsort].addr = a; statp->sort_list[nsort].addr = a;
if (is_sort_mask (n)) { if (is_sort_mask (separator))
*cp++ = n; {
*cp++ = separator;
net = cp; net = cp;
while (*cp && *cp != ';' && while (*cp && *cp != ';'
isascii(*cp) && !isspace(*cp)) && isascii (*cp) && !isspace (*cp))
cp++; cp++;
n = *cp; separator = *cp;
*cp = 0; *cp = 0;
if (__inet_aton(net, &a)) { if (__inet_aton (net, &a))
statp->sort_list[nsort].mask = a.s_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 {
statp->sort_list[nsort].mask =
net_mask(statp->sort_list[nsort].addr);
} }
else
statp->sort_list[nsort].mask
= net_mask (statp->sort_list[nsort].addr);
nsort++; nsort++;
} }
*cp = n; *cp = separator;
} }
continue; continue;
} }
if (MATCH(buf, "options")) { if (MATCH (buf, "options"))
{
res_setoptions (statp, buf + sizeof ("options") - 1, "conf"); res_setoptions (statp, buf + sizeof ("options") - 1, "conf");
continue; continue;
} }
} }
statp->nscount = nserv; statp->nscount = nserv;
if (have_serv6) { if (have_serv6)
/* We try IPv6 servers again. */ /* We try IPv6 servers again. */
statp->ipv6_unavail = false; statp->ipv6_unavail = false;
}
statp->nsort = nsort; statp->nsort = nsort;
(void) fclose(fp); fclose (fp);
} }
if (__glibc_unlikely (statp->nscount == 0)) { if (__glibc_unlikely (statp->nscount == 0))
{
statp->nsaddr.sin_addr = __inet_makeaddr (IN_LOOPBACKNET, 1); statp->nsaddr.sin_addr = __inet_makeaddr (IN_LOOPBACKNET, 1);
statp->nsaddr.sin_family = AF_INET; statp->nsaddr.sin_family = AF_INET;
statp->nsaddr.sin_port = htons (NAMESERVER_PORT); statp->nsaddr.sin_port = htons (NAMESERVER_PORT);
statp->nscount = 1; statp->nscount = 1;
} }
if (statp->defdname[0] == 0 && if (statp->defdname[0] == 0
__gethostname(buf, sizeof(statp->defdname) - 1) == 0 && && __gethostname (buf, sizeof (statp->defdname) - 1) == 0
(cp = strchr(buf, '.')) != NULL) && (cp = strchr (buf, '.')) != NULL)
strcpy (statp->defdname, cp + 1); 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->dnsrch;
*pp++ = statp->defdname; *pp++ = statp->defdname;
*pp = NULL; *pp = NULL;
@ -376,39 +407,46 @@ __res_vinit(res_state statp, int preinit) {
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 */ {
/* Skip leading and inner runs of spaces. */
while (*cp == ' ' || *cp == '\t') while (*cp == ' ' || *cp == '\t')
cp++; cp++;
/* search for and process individual options */ /* Search for and process individual options. */
if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { if (!strncmp (cp, "ndots:", sizeof ("ndots:") - 1))
i = atoi(cp + sizeof("ndots:") - 1); {
int i = atoi (cp + sizeof ("ndots:") - 1);
if (i <= RES_MAXNDOTS) if (i <= RES_MAXNDOTS)
statp->ndots = i; statp->ndots = i;
else else
statp->ndots = RES_MAXNDOTS; statp->ndots = RES_MAXNDOTS;
} else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { }
i = atoi(cp + sizeof("timeout:") - 1); else if (!strncmp (cp, "timeout:", sizeof ("timeout:") - 1))
{
int i = atoi (cp + sizeof ("timeout:") - 1);
if (i <= RES_MAXRETRANS) if (i <= RES_MAXRETRANS)
statp->retrans = i; statp->retrans = i;
else else
statp->retrans = RES_MAXRETRANS; statp->retrans = RES_MAXRETRANS;
} else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ }
i = atoi(cp + sizeof("attempts:") - 1); else if (!strncmp (cp, "attempts:", sizeof ("attempts:") - 1))
{
int i = atoi (cp + sizeof ("attempts:") - 1);
if (i <= RES_MAXRETRY) if (i <= RES_MAXRETRY)
statp->retry = i; statp->retry = i;
else else
statp->retry = RES_MAXRETRY; statp->retry = RES_MAXRETRY;
} else { }
else
{
static const struct static const struct
{ {
char str[22]; char str[22];
@ -427,8 +465,7 @@ res_setoptions(res_state statp, const char *options, const char *source) {
{ 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)
@ -437,26 +474,21 @@ res_setoptions(res_state statp, const char *options, const char *source) {
statp->options |= options[i].flag; statp->options |= options[i].flag;
break; break;
} }
if (i == noptions) {
/* XXX - print a warning here? */
} }
} /* Skip to next run of spaces. */
/* skip to next run of spaces */
while (*cp && *cp != ' ' && *cp != '\t') while (*cp && *cp != ' ' && *cp != '\t')
cp++; 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);
} }