Simplify handling of nameserver configuration in resolver

Remove use of ext.nsmap member of struct __res_state and always use
an identity mapping betwen the nsaddr_list array and the ext.nsaddrs
array.  The fact that a nameserver has an IPv6 address is signalled by
setting nsaddr_list[].sin_family to zero.
This commit is contained in:
Andreas Schwab 2015-02-19 15:52:08 +01:00
parent f58573781c
commit 2212c1420c
4 changed files with 104 additions and 136 deletions

View File

@ -1,3 +1,14 @@
2015-05-21 Andreas Schwab <schwab@suse.de>
[BZ #13028]
[BZ #17053]
* resolv/res_init.c (__res_vinit): Remove use of ext.nsmap member
of struct __res_state.
* resolv/res_send.c (__libc_res_nsend): Likewise.
(get_nsaddr): New function.
(res_ourserver_p, send_vc, reopen): Use it instead of accessing
statp directly.
2015-05-20 Joseph Myers <joseph@codesourcery.com> 2015-05-20 Joseph Myers <joseph@codesourcery.com>
* conform/GlibcConform.pm ($CFLAGS{"POSIX"}): Use * conform/GlibcConform.pm ($CFLAGS{"POSIX"}): Use

20
NEWS
View File

@ -9,16 +9,16 @@ Version 2.22
* The following bugs are resolved with this release: * The following bugs are resolved with this release:
4719, 6792, 13064, 14094, 14841, 14906, 15319, 15467, 15790, 15969, 16159, 4719, 6792, 13028, 13064, 14094, 14841, 14906, 15319, 15467, 15790, 15969,
16339, 16351, 16352, 16512, 16560, 16704, 16783, 16850, 17090, 17195, 16159, 16339, 16351, 16352, 16512, 16560, 16704, 16783, 16850, 17053,
17269, 17523, 17542, 17569, 17581, 17588, 17596, 17620, 17621, 17628, 17090, 17195, 17269, 17523, 17542, 17569, 17581, 17588, 17596, 17620,
17631, 17692, 17711, 17715, 17776, 17779, 17792, 17836, 17912, 17916, 17621, 17628, 17631, 17692, 17711, 17715, 17776, 17779, 17792, 17836,
17930, 17932, 17944, 17949, 17964, 17965, 17967, 17969, 17978, 17987, 17912, 17916, 17930, 17932, 17944, 17949, 17964, 17965, 17967, 17969,
17991, 17996, 17998, 17999, 18007, 18019, 18020, 18029, 18030, 18032, 17978, 17987, 17991, 17996, 17998, 17999, 18007, 18019, 18020, 18029,
18036, 18038, 18039, 18042, 18043, 18046, 18047, 18049, 18068, 18080, 18030, 18032, 18036, 18038, 18039, 18042, 18043, 18046, 18047, 18049,
18093, 18100, 18104, 18110, 18111, 18125, 18128, 18138, 18185, 18196, 18068, 18080, 18093, 18100, 18104, 18110, 18111, 18125, 18128, 18138,
18197, 18206, 18210, 18211, 18217, 18220, 18221, 18244, 18247, 18287, 18185, 18196, 18197, 18206, 18210, 18211, 18217, 18220, 18221, 18244,
18319, 18333, 18346, 18397, 18409, 18418, 18434. 18247, 18287, 18319, 18333, 18346, 18397, 18409, 18418, 18434.
* Cache information can be queried via sysconf() function on s390 e.g. with * Cache information can be queried via sysconf() function on s390 e.g. with
_SC_LEVEL1_ICACHE_SIZE as argument. _SC_LEVEL1_ICACHE_SIZE as argument.

View File

@ -153,10 +153,8 @@ __res_vinit(res_state statp, int preinit) {
char *cp, **pp; char *cp, **pp;
int n; int n;
char buf[BUFSIZ]; char buf[BUFSIZ];
int nserv = 0; /* number of IPv4 nameservers read from file */ int nserv = 0; /* number of nameservers read from file */
#ifdef _LIBC int have_serv6 = 0;
int nservall = 0; /* number of (IPv4 + IPV6) nameservers read from file */
#endif
int haveenv = 0; int haveenv = 0;
int havesearch = 0; int havesearch = 0;
#ifdef RESOLVSORT #ifdef RESOLVSORT
@ -184,15 +182,9 @@ __res_vinit(res_state statp, int preinit) {
statp->_flags = 0; statp->_flags = 0;
statp->qhook = NULL; statp->qhook = NULL;
statp->rhook = NULL; statp->rhook = NULL;
statp->_u._ext.nsinit = 0;
statp->_u._ext.nscount = 0; statp->_u._ext.nscount = 0;
#ifdef _LIBC for (n = 0; n < MAXNS; n++)
statp->_u._ext.nscount6 = 0; statp->_u._ext.nsaddrs[n] = NULL;
for (n = 0; n < MAXNS; n++) {
statp->_u._ext.nsaddrs[n] = NULL;
statp->_u._ext.nsmap[n] = MAXNS;
}
#endif
/* 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) {
@ -296,11 +288,7 @@ __res_vinit(res_state statp, int preinit) {
continue; continue;
} }
/* read nameservers to query */ /* read nameservers to query */
#ifdef _LIBC
if (MATCH(buf, "nameserver") && nservall < MAXNS) {
#else
if (MATCH(buf, "nameserver") && nserv < MAXNS) { if (MATCH(buf, "nameserver") && nserv < MAXNS) {
#endif
struct in_addr a; struct in_addr a;
cp = buf + sizeof("nameserver") - 1; cp = buf + sizeof("nameserver") - 1;
@ -308,13 +296,12 @@ __res_vinit(res_state statp, int preinit) {
cp++; cp++;
if ((*cp != '\0') && (*cp != '\n') if ((*cp != '\0') && (*cp != '\n')
&& __inet_aton(cp, &a)) { && __inet_aton(cp, &a)) {
statp->nsaddr_list[nservall].sin_addr = a; statp->nsaddr_list[nserv].sin_addr = a;
statp->nsaddr_list[nservall].sin_family = AF_INET; statp->nsaddr_list[nserv].sin_family = AF_INET;
statp->nsaddr_list[nservall].sin_port = statp->nsaddr_list[nserv].sin_port =
htons(NAMESERVER_PORT); htons(NAMESERVER_PORT);
nserv++; nserv++;
#ifdef _LIBC #ifdef _LIBC
nservall++;
} else { } else {
struct in6_addr a6; struct in6_addr a6;
char *el; char *el;
@ -356,10 +343,11 @@ __res_vinit(res_state statp, int preinit) {
} }
} }
statp->_u._ext.nsaddrs[nservall] = sa6; statp->nsaddr_list[nserv].sin_family = 0;
statp->_u._ext.nssocks[nservall] = -1; statp->_u._ext.nsaddrs[nserv] = sa6;
statp->_u._ext.nsmap[nservall] = MAXNS + 1; statp->_u._ext.nssocks[nserv] = -1;
nservall++; have_serv6 = 1;
nserv++;
} }
} }
#endif #endif
@ -414,10 +402,9 @@ __res_vinit(res_state statp, int preinit) {
continue; continue;
} }
} }
statp->nscount = nservall; statp->nscount = nserv;
#ifdef _LIBC #ifdef _LIBC
if (nservall - nserv > 0) { if (have_serv6) {
statp->_u._ext.nscount6 = nservall - nserv;
/* We try IPv6 servers again. */ /* We try IPv6 servers again. */
statp->ipv6_unavail = false; statp->ipv6_unavail = false;
} }
@ -606,11 +593,7 @@ __res_iclose(res_state statp, bool free_addr) {
statp->_vcsock = -1; statp->_vcsock = -1;
statp->_flags &= ~(RES_F_VC | RES_F_CONN); statp->_flags &= ~(RES_F_VC | RES_F_CONN);
} }
#ifdef _LIBC
for (ns = 0; ns < MAXNS; ns++)
#else
for (ns = 0; ns < statp->_u._ext.nscount; ns++) for (ns = 0; ns < statp->_u._ext.nscount; ns++)
#endif
if (statp->_u._ext.nsaddrs[ns]) { if (statp->_u._ext.nsaddrs[ns]) {
if (statp->_u._ext.nssocks[ns] != -1) { if (statp->_u._ext.nssocks[ns] != -1) {
close_not_cancel_no_status(statp->_u._ext.nssocks[ns]); close_not_cancel_no_status(statp->_u._ext.nssocks[ns]);
@ -621,8 +604,6 @@ __res_iclose(res_state statp, bool free_addr) {
statp->_u._ext.nsaddrs[ns] = NULL; statp->_u._ext.nsaddrs[ns] = NULL;
} }
} }
if (free_addr)
statp->_u._ext.nsinit = 0;
} }
libc_hidden_def (__res_iclose) libc_hidden_def (__res_iclose)

View File

@ -184,6 +184,7 @@ evNowTime(struct timespec *res) {
/* Forward. */ /* Forward. */
static struct sockaddr *get_nsaddr (res_state, int);
static int send_vc(res_state, const u_char *, int, static int send_vc(res_state, const u_char *, int,
const u_char *, int, const u_char *, int,
u_char **, int *, int *, int, u_char **, u_char **, int *, int *, int, u_char **,
@ -221,20 +222,21 @@ res_ourserver_p(const res_state statp, const struct sockaddr_in6 *inp)
in_port_t port = in4p->sin_port; in_port_t port = in4p->sin_port;
in_addr_t addr = in4p->sin_addr.s_addr; in_addr_t addr = in4p->sin_addr.s_addr;
for (ns = 0; ns < MAXNS; ns++) { for (ns = 0; ns < statp->nscount; ns++) {
const struct sockaddr_in *srv = const struct sockaddr_in *srv =
(struct sockaddr_in *)EXT(statp).nsaddrs[ns]; (struct sockaddr_in *) get_nsaddr (statp, ns);
if ((srv != NULL) && (srv->sin_family == AF_INET) && if ((srv->sin_family == AF_INET) &&
(srv->sin_port == port) && (srv->sin_port == port) &&
(srv->sin_addr.s_addr == INADDR_ANY || (srv->sin_addr.s_addr == INADDR_ANY ||
srv->sin_addr.s_addr == addr)) srv->sin_addr.s_addr == addr))
return (1); return (1);
} }
} else if (inp->sin6_family == AF_INET6) { } else if (inp->sin6_family == AF_INET6) {
for (ns = 0; ns < MAXNS; ns++) { for (ns = 0; ns < statp->nscount; ns++) {
const struct sockaddr_in6 *srv = EXT(statp).nsaddrs[ns]; const struct sockaddr_in6 *srv
if ((srv != NULL) && (srv->sin6_family == AF_INET6) && = (struct sockaddr_in6 *) get_nsaddr (statp, ns);
if ((srv->sin6_family == AF_INET6) &&
(srv->sin6_port == inp->sin6_port) && (srv->sin6_port == inp->sin6_port) &&
!(memcmp(&srv->sin6_addr, &in6addr_any, !(memcmp(&srv->sin6_addr, &in6addr_any,
sizeof (struct in6_addr)) && sizeof (struct in6_addr)) &&
@ -384,80 +386,48 @@ __libc_res_nsend(res_state statp, const u_char *buf, int buflen,
* If the ns_addr_list in the resolver context has changed, then * If the ns_addr_list in the resolver context has changed, then
* invalidate our cached copy and the associated timing data. * invalidate our cached copy and the associated timing data.
*/ */
if (EXT(statp).nsinit) { if (EXT(statp).nscount != 0) {
int needclose = 0; int needclose = 0;
if (EXT(statp).nscount != statp->nscount) if (EXT(statp).nscount != statp->nscount)
needclose++; needclose++;
else else
for (ns = 0; ns < MAXNS; ns++) { for (ns = 0; ns < statp->nscount; ns++) {
unsigned int map = EXT(statp).nsmap[ns]; if (statp->nsaddr_list[ns].sin_family != 0
if (map < MAXNS
&& !sock_eq((struct sockaddr_in6 *) && !sock_eq((struct sockaddr_in6 *)
&statp->nsaddr_list[map], &statp->nsaddr_list[ns],
EXT(statp).nsaddrs[ns])) EXT(statp).nsaddrs[ns]))
{ {
needclose++; needclose++;
break; break;
} }
} }
if (needclose) if (needclose) {
__res_iclose(statp, false); __res_iclose(statp, false);
EXT(statp).nscount = 0;
}
} }
/* /*
* Maybe initialize our private copy of the ns_addr_list. * Maybe initialize our private copy of the ns_addr_list.
*/ */
if (EXT(statp).nsinit == 0) { if (EXT(statp).nscount == 0) {
unsigned char map[MAXNS]; for (ns = 0; ns < statp->nscount; ns++) {
EXT(statp).nssocks[ns] = -1;
memset (map, MAXNS, sizeof (map)); if (statp->nsaddr_list[ns].sin_family == 0)
for (n = 0; n < MAXNS; n++) { continue;
ns = EXT(statp).nsmap[n]; if (EXT(statp).nsaddrs[ns] == NULL)
if (ns < statp->nscount) EXT(statp).nsaddrs[ns] =
map[ns] = n;
else if (ns < MAXNS) {
free(EXT(statp).nsaddrs[n]);
EXT(statp).nsaddrs[n] = NULL;
EXT(statp).nsmap[n] = MAXNS;
}
}
n = statp->nscount;
if (statp->nscount > EXT(statp).nscount)
for (n = EXT(statp).nscount, ns = 0;
n < statp->nscount; n++) {
while (ns < MAXNS
&& EXT(statp).nsmap[ns] != MAXNS)
ns++;
if (ns == MAXNS)
break;
/* NS never exceeds MAXNS, but gcc 4.9 somehow
does not see this. */
DIAG_PUSH_NEEDS_COMMENT;
DIAG_IGNORE_NEEDS_COMMENT (4.9,
"-Warray-bounds");
EXT(statp).nsmap[ns] = n;
DIAG_POP_NEEDS_COMMENT;
map[n] = ns++;
}
EXT(statp).nscount = n;
for (ns = 0; ns < EXT(statp).nscount; ns++) {
n = map[ns];
if (EXT(statp).nsaddrs[n] == NULL)
EXT(statp).nsaddrs[n] =
malloc(sizeof (struct sockaddr_in6)); malloc(sizeof (struct sockaddr_in6));
if (EXT(statp).nsaddrs[n] != NULL) { if (EXT(statp).nsaddrs[ns] != NULL)
memset (mempcpy(EXT(statp).nsaddrs[n], memset (mempcpy(EXT(statp).nsaddrs[ns],
&statp->nsaddr_list[ns], &statp->nsaddr_list[ns],
sizeof (struct sockaddr_in)), sizeof (struct sockaddr_in)),
'\0', '\0',
sizeof (struct sockaddr_in6) sizeof (struct sockaddr_in6)
- sizeof (struct sockaddr_in)); - sizeof (struct sockaddr_in));
EXT(statp).nssocks[n] = -1;
n++;
}
} }
EXT(statp).nsinit = 1; EXT(statp).nscount = statp->nscount;
} }
/* /*
@ -466,44 +436,37 @@ __libc_res_nsend(res_state statp, const u_char *buf, int buflen,
*/ */
if (__builtin_expect ((statp->options & RES_ROTATE) != 0, 0) && if (__builtin_expect ((statp->options & RES_ROTATE) != 0, 0) &&
(statp->options & RES_BLAST) == 0) { (statp->options & RES_BLAST) == 0) {
struct sockaddr_in6 *ina; struct sockaddr_in ina;
unsigned int map; struct sockaddr_in6 *inp;
int lastns = statp->nscount - 1;
int fd;
n = 0; inp = EXT(statp).nsaddrs[0];
while (n < MAXNS && EXT(statp).nsmap[n] == MAXNS) ina = statp->nsaddr_list[0];
n++; fd = EXT(statp).nssocks[0];
if (n < MAXNS) { for (ns = 0; ns < lastns; ns++) {
ina = EXT(statp).nsaddrs[n]; EXT(statp).nsaddrs[ns] = EXT(statp).nsaddrs[ns + 1];
map = EXT(statp).nsmap[n]; statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
for (;;) { EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
ns = n + 1;
while (ns < MAXNS
&& EXT(statp).nsmap[ns] == MAXNS)
ns++;
if (ns == MAXNS)
break;
EXT(statp).nsaddrs[n] = EXT(statp).nsaddrs[ns];
EXT(statp).nsmap[n] = EXT(statp).nsmap[ns];
n = ns;
}
EXT(statp).nsaddrs[n] = ina;
EXT(statp).nsmap[n] = map;
} }
EXT(statp).nsaddrs[lastns] = inp;
statp->nsaddr_list[lastns] = ina;
EXT(statp).nssocks[lastns] = fd;
} }
/* /*
* Send request, RETRY times, or until successful. * Send request, RETRY times, or until successful.
*/ */
for (try = 0; try < statp->retry; try++) { for (try = 0; try < statp->retry; try++) {
for (ns = 0; ns < MAXNS; ns++) for (ns = 0; ns < statp->nscount; ns++)
{ {
#ifdef DEBUG #ifdef DEBUG
char tmpbuf[40]; char tmpbuf[40];
#endif #endif
struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns]; #if defined USE_HOOKS || defined DEBUG
struct sockaddr *nsap = get_nsaddr (statp, ns);
#endif
if (nsap == NULL)
goto next_ns;
same_ns: same_ns:
#ifdef USE_HOOKS #ifdef USE_HOOKS
if (__glibc_unlikely (statp->qhook != NULL)) { if (__glibc_unlikely (statp->qhook != NULL)) {
@ -542,9 +505,9 @@ __libc_res_nsend(res_state statp, const u_char *buf, int buflen,
Dprint(statp->options & RES_DEBUG, Dprint(statp->options & RES_DEBUG,
(stdout, ";; Querying server (# %d) address = %s\n", (stdout, ";; Querying server (# %d) address = %s\n",
ns + 1, inet_ntop(nsap->sin6_family, ns + 1, inet_ntop(nsap->sa_family,
(nsap->sin6_family == AF_INET6 (nsap->sa_family == AF_INET6
? &nsap->sin6_addr ? &((struct sockaddr_in6 *) nsap)->sin6_addr
: &((struct sockaddr_in *) nsap)->sin_addr), : &((struct sockaddr_in *) nsap)->sin_addr),
tmpbuf, sizeof (tmpbuf)))); tmpbuf, sizeof (tmpbuf))));
@ -660,6 +623,21 @@ libresolv_hidden_def (res_nsend)
/* Private */ /* Private */
static struct sockaddr *
get_nsaddr (res_state statp, int n)
{
if (statp->nsaddr_list[n].sin_family == 0 && EXT(statp).nsaddrs[n] != NULL)
/* EXT(statp).nsaddrs[n] holds an address that is larger than
struct sockaddr, and user code did not update
statp->nsaddr_list[n]. */
return (struct sockaddr *) EXT(statp).nsaddrs[n];
else
/* User code updated statp->nsaddr_list[n], or statp->nsaddr_list[n]
has the same content as EXT(statp).nsaddrs[n]. */
return (struct sockaddr *) (void *) &statp->nsaddr_list[n];
}
static int static int
send_vc(res_state statp, send_vc(res_state statp,
const u_char *buf, int buflen, const u_char *buf2, int buflen2, const u_char *buf, int buflen, const u_char *buf2, int buflen2,
@ -674,7 +652,7 @@ send_vc(res_state statp,
// XXX REMOVE // XXX REMOVE
// int anssiz = *anssizp; // int anssiz = *anssizp;
HEADER *anhp = (HEADER *) ans; HEADER *anhp = (HEADER *) ans;
struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns]; struct sockaddr *nsap = get_nsaddr (statp, ns);
int truncating, connreset, n; int truncating, connreset, n;
/* On some architectures compiler might emit a warning indicating /* On some architectures compiler might emit a warning indicating
'resplen' may be used uninitialized. However if buf2 == NULL 'resplen' may be used uninitialized. However if buf2 == NULL
@ -711,8 +689,8 @@ send_vc(res_state statp,
if (getpeername(statp->_vcsock, if (getpeername(statp->_vcsock,
(struct sockaddr *)&peer, &size) < 0 || (struct sockaddr *)&peer, &size) < 0 ||
!sock_eq(&peer, nsap)) { !sock_eq(&peer, (struct sockaddr_in6 *) nsap)) {
__res_iclose(statp, false); __res_iclose(statp, false);
statp->_flags &= ~RES_F_VC; statp->_flags &= ~RES_F_VC;
} }
} }
@ -721,20 +699,19 @@ send_vc(res_state statp,
if (statp->_vcsock >= 0) if (statp->_vcsock >= 0)
__res_iclose(statp, false); __res_iclose(statp, false);
statp->_vcsock = socket(nsap->sin6_family, SOCK_STREAM, 0); statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
if (statp->_vcsock < 0) { if (statp->_vcsock < 0) {
*terrno = errno; *terrno = errno;
Perror(statp, stderr, "socket(vc)", errno); Perror(statp, stderr, "socket(vc)", errno);
return (-1); return (-1);
} }
__set_errno (0); __set_errno (0);
if (connect(statp->_vcsock, (struct sockaddr *)nsap, if (connect(statp->_vcsock, nsap,
nsap->sin6_family == AF_INET nsap->sa_family == AF_INET
? sizeof (struct sockaddr_in) ? sizeof (struct sockaddr_in)
: sizeof (struct sockaddr_in6)) < 0) { : sizeof (struct sockaddr_in6)) < 0) {
*terrno = errno; *terrno = errno;
Aerror(statp, stderr, "connect/vc", errno, Aerror(statp, stderr, "connect/vc", errno, nsap);
(struct sockaddr *) nsap);
__res_iclose(statp, false); __res_iclose(statp, false);
return (0); return (0);
} }
@ -945,8 +922,7 @@ static int
reopen (res_state statp, int *terrno, int ns) reopen (res_state statp, int *terrno, int ns)
{ {
if (EXT(statp).nssocks[ns] == -1) { if (EXT(statp).nssocks[ns] == -1) {
struct sockaddr *nsap struct sockaddr *nsap = get_nsaddr (statp, ns);
= (struct sockaddr *) EXT(statp).nsaddrs[ns];
socklen_t slen; socklen_t slen;
/* only try IPv6 if IPv6 NS and if not failed before */ /* only try IPv6 if IPv6 NS and if not failed before */