29b39d6
Revert this upstream commit:
29b39d6
29b39d6
commit 2212c1420c92a33b0e0bd9a34938c9814a56c0f7
29b39d6
Author: Andreas Schwab <schwab@suse.de>
29b39d6
Date:   Thu Feb 19 15:52:08 2015 +0100
29b39d6
29b39d6
    Simplify handling of nameserver configuration in resolver
29b39d6
    
29b39d6
    Remove use of ext.nsmap member of struct __res_state and always use
29b39d6
    an identity mapping betwen the nsaddr_list array and the ext.nsaddrs
29b39d6
    array.  The fact that a nameserver has an IPv6 address is signalled by
29b39d6
    setting nsaddr_list[].sin_family to zero.
29b39d6
29b39d6
reverted:
Carlos O'Donell dd72bee
Index: glibc-2.23-39-g314f6de/resolv/res_init.c
29b39d6
===================================================================
Carlos O'Donell dd72bee
--- glibc-2.23-39-g314f6de.orig/resolv/res_init.c
Carlos O'Donell dd72bee
+++ glibc-2.23-39-g314f6de/resolv/res_init.c
Carlos O'Donell dd72bee
@@ -139,8 +139,10 @@ __res_vinit(res_state statp, int preinit
29b39d6
 	char *cp, **pp;
29b39d6
 	int n;
29b39d6
 	char buf[BUFSIZ];
29b39d6
-	int nserv = 0;    /* number of nameservers read from file */
29b39d6
-	int have_serv6 = 0;
29b39d6
+	int nserv = 0;    /* number of IPv4 nameservers read from file */
29b39d6
+#ifdef _LIBC
29b39d6
+	int nservall = 0; /* number of (IPv4 + IPV6) nameservers read from file */
29b39d6
+#endif
29b39d6
 	int haveenv = 0;
29b39d6
 	int havesearch = 0;
29b39d6
 #ifdef RESOLVSORT
Carlos O'Donell dd72bee
@@ -169,9 +171,15 @@ __res_vinit(res_state statp, int preinit
29b39d6
 	statp->_flags = 0;
29b39d6
 	statp->qhook = NULL;
29b39d6
 	statp->rhook = NULL;
29b39d6
+	statp->_u._ext.nsinit = 0;
29b39d6
 	statp->_u._ext.nscount = 0;
29b39d6
-	for (n = 0; n < MAXNS; n++)
29b39d6
-	    statp->_u._ext.nsaddrs[n] = NULL;
29b39d6
+#ifdef _LIBC
29b39d6
+	statp->_u._ext.nscount6 = 0;
29b39d6
+	for (n = 0; n < MAXNS; n++) {
29b39d6
+		statp->_u._ext.nsaddrs[n] = NULL;
29b39d6
+		statp->_u._ext.nsmap[n] = MAXNS;
29b39d6
+	}
29b39d6
+#endif
29b39d6
 
29b39d6
 	/* Allow user to override the local domain definition */
29b39d6
 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
Carlos O'Donell dd72bee
@@ -275,7 +283,11 @@ __res_vinit(res_state statp, int preinit
29b39d6
 		    continue;
29b39d6
 		}
29b39d6
 		/* read nameservers to query */
29b39d6
+#ifdef _LIBC
29b39d6
+		if (MATCH(buf, "nameserver") && nservall < MAXNS) {
29b39d6
+#else
29b39d6
 		if (MATCH(buf, "nameserver") && nserv < MAXNS) {
29b39d6
+#endif
29b39d6
 		    struct in_addr a;
29b39d6
 
29b39d6
 		    cp = buf + sizeof("nameserver") - 1;
Carlos O'Donell dd72bee
@@ -283,12 +295,13 @@ __res_vinit(res_state statp, int preinit
29b39d6
 			cp++;
29b39d6
 		    if ((*cp != '\0') && (*cp != '\n')
29b39d6
 			&& __inet_aton(cp, &a)) {
29b39d6
-			statp->nsaddr_list[nserv].sin_addr = a;
29b39d6
-			statp->nsaddr_list[nserv].sin_family = AF_INET;
29b39d6
-			statp->nsaddr_list[nserv].sin_port =
29b39d6
+			statp->nsaddr_list[nservall].sin_addr = a;
29b39d6
+			statp->nsaddr_list[nservall].sin_family = AF_INET;
29b39d6
+			statp->nsaddr_list[nservall].sin_port =
29b39d6
 				htons(NAMESERVER_PORT);
29b39d6
 			nserv++;
29b39d6
 #ifdef _LIBC
29b39d6
+			nservall++;
29b39d6
 		    } else {
29b39d6
 			struct in6_addr a6;
29b39d6
 			char *el;
Carlos O'Donell dd72bee
@@ -330,11 +343,10 @@ __res_vinit(res_state statp, int preinit
29b39d6
 				    }
29b39d6
 				}
29b39d6
 
29b39d6
-				statp->nsaddr_list[nserv].sin_family = 0;
29b39d6
-				statp->_u._ext.nsaddrs[nserv] = sa6;
29b39d6
-				statp->_u._ext.nssocks[nserv] = -1;
29b39d6
-				have_serv6 = 1;
29b39d6
-				nserv++;
29b39d6
+				statp->_u._ext.nsaddrs[nservall] = sa6;
29b39d6
+				statp->_u._ext.nssocks[nservall] = -1;
29b39d6
+				statp->_u._ext.nsmap[nservall] = MAXNS + 1;
29b39d6
+				nservall++;
29b39d6
 			    }
29b39d6
 			}
29b39d6
 #endif
Carlos O'Donell dd72bee
@@ -389,9 +401,10 @@ __res_vinit(res_state statp, int preinit
29b39d6
 		    continue;
29b39d6
 		}
29b39d6
 	    }
29b39d6
-	    statp->nscount = nserv;
29b39d6
+	    statp->nscount = nservall;
29b39d6
 #ifdef _LIBC
29b39d6
-	    if (have_serv6) {
29b39d6
+	    if (nservall - nserv > 0) {
29b39d6
+		statp->_u._ext.nscount6 = nservall - nserv;
29b39d6
 		/* We try IPv6 servers again.  */
29b39d6
 		statp->ipv6_unavail = false;
29b39d6
 	    }
Carlos O'Donell dd72bee
@@ -580,7 +593,11 @@ __res_iclose(res_state statp, bool free_
29b39d6
 		statp->_vcsock = -1;
29b39d6
 		statp->_flags &= ~(RES_F_VC | RES_F_CONN);
29b39d6
 	}
29b39d6
+#ifdef _LIBC
29b39d6
+	for (ns = 0; ns < MAXNS; ns++)
29b39d6
+#else
29b39d6
 	for (ns = 0; ns < statp->_u._ext.nscount; ns++)
29b39d6
+#endif
29b39d6
 		if (statp->_u._ext.nsaddrs[ns]) {
29b39d6
 			if (statp->_u._ext.nssocks[ns] != -1) {
29b39d6
 				close_not_cancel_no_status(statp->_u._ext.nssocks[ns]);
Carlos O'Donell dd72bee
@@ -591,6 +608,8 @@ __res_iclose(res_state statp, bool free_
29b39d6
 				statp->_u._ext.nsaddrs[ns] = NULL;
29b39d6
 			}
29b39d6
 		}
29b39d6
+	if (free_addr)
29b39d6
+		statp->_u._ext.nsinit = 0;
29b39d6
 }
29b39d6
 libc_hidden_def (__res_iclose)
29b39d6
 
Carlos O'Donell dd72bee
Index: glibc-2.23-39-g314f6de/resolv/res_send.c
29b39d6
===================================================================
Carlos O'Donell dd72bee
--- glibc-2.23-39-g314f6de.orig/resolv/res_send.c
Carlos O'Donell dd72bee
+++ glibc-2.23-39-g314f6de/resolv/res_send.c
Carlos O'Donell dd72bee
@@ -193,7 +193,6 @@ evNowTime(struct timespec *res) {
29b39d6
 
29b39d6
 /* Forward. */
29b39d6
 
29b39d6
-static struct sockaddr *get_nsaddr (res_state, int);
29b39d6
 static int		send_vc(res_state, const u_char *, int,
29b39d6
 				const u_char *, int,
29b39d6
 				u_char **, int *, int *, int, u_char **,
Carlos O'Donell dd72bee
@@ -231,21 +230,20 @@ res_ourserver_p(const res_state statp, c
29b39d6
 	    in_port_t port = in4p->sin_port;
29b39d6
 	    in_addr_t addr = in4p->sin_addr.s_addr;
29b39d6
 
29b39d6
-	    for (ns = 0;  ns < statp->nscount;  ns++) {
29b39d6
+	    for (ns = 0;  ns < MAXNS;  ns++) {
29b39d6
 		const struct sockaddr_in *srv =
29b39d6
-		    (struct sockaddr_in *) get_nsaddr (statp, ns);
29b39d6
+		    (struct sockaddr_in *)EXT(statp).nsaddrs[ns];
29b39d6
 
29b39d6
-		if ((srv->sin_family == AF_INET) &&
29b39d6
+		if ((srv != NULL) && (srv->sin_family == AF_INET) &&
29b39d6
 		    (srv->sin_port == port) &&
29b39d6
 		    (srv->sin_addr.s_addr == INADDR_ANY ||
29b39d6
 		     srv->sin_addr.s_addr == addr))
29b39d6
 		    return (1);
29b39d6
 	    }
29b39d6
 	} else if (inp->sin6_family == AF_INET6) {
29b39d6
-	    for (ns = 0;  ns < statp->nscount;  ns++) {
29b39d6
-		const struct sockaddr_in6 *srv
29b39d6
-		  = (struct sockaddr_in6 *) get_nsaddr (statp, ns);
29b39d6
-		if ((srv->sin6_family == AF_INET6) &&
29b39d6
+	    for (ns = 0;  ns < MAXNS;  ns++) {
29b39d6
+		const struct sockaddr_in6 *srv = EXT(statp).nsaddrs[ns];
29b39d6
+		if ((srv != NULL) && (srv->sin6_family == AF_INET6) &&
29b39d6
 		    (srv->sin6_port == inp->sin6_port) &&
29b39d6
 		    !(memcmp(&srv->sin6_addr, &in6addr_any,
29b39d6
 			     sizeof (struct in6_addr)) &&
Carlos O'Donell dd72bee
@@ -397,48 +395,80 @@ __libc_res_nsend(res_state statp, const
29b39d6
 	 * If the ns_addr_list in the resolver context has changed, then
29b39d6
 	 * invalidate our cached copy and the associated timing data.
29b39d6
 	 */
29b39d6
-	if (EXT(statp).nscount != 0) {
29b39d6
+	if (EXT(statp).nsinit) {
29b39d6
 		int needclose = 0;
29b39d6
 
29b39d6
 		if (EXT(statp).nscount != statp->nscount)
29b39d6
 			needclose++;
29b39d6
 		else
29b39d6
-			for (ns = 0; ns < statp->nscount; ns++) {
29b39d6
-				if (statp->nsaddr_list[ns].sin_family != 0
29b39d6
+			for (ns = 0; ns < MAXNS; ns++) {
29b39d6
+				unsigned int map = EXT(statp).nsmap[ns];
29b39d6
+				if (map < MAXNS
29b39d6
 				    && !sock_eq((struct sockaddr_in6 *)
29b39d6
-						&statp->nsaddr_list[ns],
29b39d6
+						&statp->nsaddr_list[map],
29b39d6
 						EXT(statp).nsaddrs[ns]))
29b39d6
 				{
29b39d6
 					needclose++;
29b39d6
 					break;
29b39d6
 				}
29b39d6
 			}
29b39d6
-		if (needclose) {
29b39d6
+		if (needclose)
29b39d6
 			__res_iclose(statp, false);
29b39d6
-			EXT(statp).nscount = 0;
29b39d6
-		}
29b39d6
 	}
29b39d6
 
29b39d6
 	/*
29b39d6
 	 * Maybe initialize our private copy of the ns_addr_list.
29b39d6
 	 */
29b39d6
-	if (EXT(statp).nscount == 0) {
29b39d6
-		for (ns = 0; ns < statp->nscount; ns++) {
29b39d6
-			EXT(statp).nssocks[ns] = -1;
29b39d6
-			if (statp->nsaddr_list[ns].sin_family == 0)
29b39d6
-				continue;
29b39d6
-			if (EXT(statp).nsaddrs[ns] == NULL)
29b39d6
-				EXT(statp).nsaddrs[ns] =
29b39d6
+	if (EXT(statp).nsinit == 0) {
29b39d6
+		unsigned char map[MAXNS];
29b39d6
+
29b39d6
+		memset (map, MAXNS, sizeof (map));
29b39d6
+		for (n = 0; n < MAXNS; n++) {
29b39d6
+			ns = EXT(statp).nsmap[n];
29b39d6
+			if (ns < statp->nscount)
29b39d6
+				map[ns] = n;
29b39d6
+			else if (ns < MAXNS) {
29b39d6
+				free(EXT(statp).nsaddrs[n]);
29b39d6
+				EXT(statp).nsaddrs[n] = NULL;
29b39d6
+				EXT(statp).nsmap[n] = MAXNS;
29b39d6
+			}
29b39d6
+		}
29b39d6
+		n = statp->nscount;
29b39d6
+		if (statp->nscount > EXT(statp).nscount)
29b39d6
+			for (n = EXT(statp).nscount, ns = 0;
29b39d6
+			     n < statp->nscount; n++) {
29b39d6
+				while (ns < MAXNS
29b39d6
+				       && EXT(statp).nsmap[ns] != MAXNS)
29b39d6
+					ns++;
29b39d6
+				if (ns == MAXNS)
29b39d6
+					break;
29b39d6
+				/* NS never exceeds MAXNS, but gcc 4.9 somehow
29b39d6
+				   does not see this.  */
29b39d6
+				DIAG_PUSH_NEEDS_COMMENT;
29b39d6
+				DIAG_IGNORE_NEEDS_COMMENT (4.9,
29b39d6
+							   "-Warray-bounds");
29b39d6
+				EXT(statp).nsmap[ns] = n;
29b39d6
+				DIAG_POP_NEEDS_COMMENT;
29b39d6
+				map[n] = ns++;
29b39d6
+			}
29b39d6
+		EXT(statp).nscount = n;
29b39d6
+		for (ns = 0; ns < EXT(statp).nscount; ns++) {
29b39d6
+			n = map[ns];
29b39d6
+			if (EXT(statp).nsaddrs[n] == NULL)
29b39d6
+				EXT(statp).nsaddrs[n] =
29b39d6
 				    malloc(sizeof (struct sockaddr_in6));
29b39d6
-			if (EXT(statp).nsaddrs[ns] != NULL)
29b39d6
-				memset (mempcpy(EXT(statp).nsaddrs[ns],
29b39d6
+			if (EXT(statp).nsaddrs[n] != NULL) {
29b39d6
+				memset (mempcpy(EXT(statp).nsaddrs[n],
29b39d6
 						&statp->nsaddr_list[ns],
29b39d6
 						sizeof (struct sockaddr_in)),
29b39d6
 					'\0',
29b39d6
 					sizeof (struct sockaddr_in6)
29b39d6
 					- sizeof (struct sockaddr_in));
29b39d6
+				EXT(statp).nssocks[n] = -1;
29b39d6
+				n++;
29b39d6
+			}
29b39d6
 		}
29b39d6
-		EXT(statp).nscount = statp->nscount;
29b39d6
+		EXT(statp).nsinit = 1;
29b39d6
 	}
29b39d6
 
29b39d6
 	/*
Carlos O'Donell dd72bee
@@ -447,37 +477,44 @@ __libc_res_nsend(res_state statp, const
29b39d6
 	 */
29b39d6
 	if (__builtin_expect ((statp->options & RES_ROTATE) != 0, 0) &&
29b39d6
 	    (statp->options & RES_BLAST) == 0) {
29b39d6
-		struct sockaddr_in ina;
29b39d6
-		struct sockaddr_in6 *inp;
29b39d6
-		int lastns = statp->nscount - 1;
29b39d6
-		int fd;
29b39d6
-
29b39d6
-		inp = EXT(statp).nsaddrs[0];
29b39d6
-		ina = statp->nsaddr_list[0];
29b39d6
-		fd = EXT(statp).nssocks[0];
29b39d6
-		for (ns = 0; ns < lastns; ns++) {
29b39d6
-		    EXT(statp).nsaddrs[ns] = EXT(statp).nsaddrs[ns + 1];
29b39d6
-		    statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
29b39d6
-		    EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
29b39d6
-		}
29b39d6
-		EXT(statp).nsaddrs[lastns] = inp;
29b39d6
-		statp->nsaddr_list[lastns] = ina;
29b39d6
-		EXT(statp).nssocks[lastns] = fd;
29b39d6
+		struct sockaddr_in6 *ina;
29b39d6
+		unsigned int map;
29b39d6
+
29b39d6
+		n = 0;
29b39d6
+		while (n < MAXNS && EXT(statp).nsmap[n] == MAXNS)
29b39d6
+			n++;
29b39d6
+		if (n < MAXNS) {
29b39d6
+			ina = EXT(statp).nsaddrs[n];
29b39d6
+			map = EXT(statp).nsmap[n];
29b39d6
+			for (;;) {
29b39d6
+				ns = n + 1;
29b39d6
+				while (ns < MAXNS
29b39d6
+				       && EXT(statp).nsmap[ns] == MAXNS)
29b39d6
+					ns++;
29b39d6
+				if (ns == MAXNS)
29b39d6
+					break;
29b39d6
+				EXT(statp).nsaddrs[n] = EXT(statp).nsaddrs[ns];
29b39d6
+				EXT(statp).nsmap[n] = EXT(statp).nsmap[ns];
29b39d6
+				n = ns;
29b39d6
+			}
29b39d6
+			EXT(statp).nsaddrs[n] = ina;
29b39d6
+			EXT(statp).nsmap[n] = map;
29b39d6
+		}
29b39d6
 	}
29b39d6
 
29b39d6
 	/*
29b39d6
 	 * Send request, RETRY times, or until successful.
29b39d6
 	 */
29b39d6
 	for (try = 0; try < statp->retry; try++) {
29b39d6
-	    for (ns = 0; ns < statp->nscount; ns++)
29b39d6
+	    for (ns = 0; ns < MAXNS; ns++)
29b39d6
 	    {
29b39d6
 #ifdef DEBUG
29b39d6
 		char tmpbuf[40];
29b39d6
 #endif
29b39d6
-#if defined USE_HOOKS || defined DEBUG
29b39d6
-		struct sockaddr *nsap = get_nsaddr (statp, ns);
29b39d6
-#endif
29b39d6
+		struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
29b39d6
 
29b39d6
+		if (nsap == NULL)
29b39d6
+			goto next_ns;
29b39d6
 	    same_ns:
29b39d6
 #ifdef USE_HOOKS
29b39d6
 		if (__glibc_unlikely (statp->qhook != NULL))       {
Carlos O'Donell dd72bee
@@ -634,21 +671,6 @@ libresolv_hidden_def (res_nsend)
29b39d6
 
29b39d6
 /* Private */
29b39d6
 
29b39d6
-static struct sockaddr *
29b39d6
-get_nsaddr (res_state statp, int n)
29b39d6
-{
29b39d6
-
29b39d6
-  if (statp->nsaddr_list[n].sin_family == 0 && EXT(statp).nsaddrs[n] != NULL)
29b39d6
-    /* EXT(statp).nsaddrs[n] holds an address that is larger than
29b39d6
-       struct sockaddr, and user code did not update
29b39d6
-       statp->nsaddr_list[n].  */
29b39d6
-    return (struct sockaddr *) EXT(statp).nsaddrs[n];
29b39d6
-  else
29b39d6
-    /* User code updated statp->nsaddr_list[n], or statp->nsaddr_list[n]
29b39d6
-       has the same content as EXT(statp).nsaddrs[n].  */
29b39d6
-    return (struct sockaddr *) (void *) &statp->nsaddr_list[n];
29b39d6
-}
29b39d6
-
Carlos O'Donell dd72bee
 /* The send_vc function is responsible for sending a DNS query over TCP
Carlos O'Donell dd72bee
    to the nameserver numbered NS from the res_state STATP i.e.
Carlos O'Donell dd72bee
    EXT(statp).nssocks[ns].  The function supports sending both IPv4 and
Carlos O'Donell dd72bee
@@ -730,7 +752,7 @@ send_vc(res_state statp,
Carlos O'Donell dd72bee
 	const HEADER *hp = (HEADER *) buf;
Carlos O'Donell dd72bee
 	const HEADER *hp2 = (HEADER *) buf2;
Carlos O'Donell dd72bee
 	HEADER *anhp = (HEADER *) *ansp;
29b39d6
-	struct sockaddr *nsap = get_nsaddr (statp, ns);
29b39d6
+	struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
29b39d6
 	int truncating, connreset, n;
29b39d6
 	/* On some architectures compiler might emit a warning indicating
29b39d6
 	   'resplen' may be used uninitialized.  However if buf2 == NULL
Carlos O'Donell dd72bee
@@ -763,8 +785,8 @@ send_vc(res_state statp,
29b39d6
 
29b39d6
 		if (getpeername(statp->_vcsock,
29b39d6
 				(struct sockaddr *)&peer, &size) < 0 ||
29b39d6
-		    !sock_eq(&peer, (struct sockaddr_in6 *) nsap)) {
29b39d6
-			__res_iclose(statp, false);
29b39d6
+		    !sock_eq(&peer, nsap)) {
29b39d6
+		  __res_iclose(statp, false);
29b39d6
 			statp->_flags &= ~RES_F_VC;
29b39d6
 		}
29b39d6
 	}
Carlos O'Donell dd72bee
@@ -773,19 +795,20 @@ send_vc(res_state statp,
29b39d6
 		if (statp->_vcsock >= 0)
29b39d6
 		  __res_iclose(statp, false);
29b39d6
 
29b39d6
-		statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
29b39d6
+		statp->_vcsock = socket(nsap->sin6_family, SOCK_STREAM, 0);
29b39d6
 		if (statp->_vcsock < 0) {
29b39d6
 			*terrno = errno;
29b39d6
 			Perror(statp, stderr, "socket(vc)", errno);
29b39d6
 			return (-1);
29b39d6
 		}
29b39d6
 		__set_errno (0);
29b39d6
-		if (connect(statp->_vcsock, nsap,
29b39d6
-			    nsap->sa_family == AF_INET
29b39d6
+		if (connect(statp->_vcsock, (struct sockaddr *)nsap,
29b39d6
+			    nsap->sin6_family == AF_INET
29b39d6
 			    ? sizeof (struct sockaddr_in)
29b39d6
 			    : sizeof (struct sockaddr_in6)) < 0) {
29b39d6
 			*terrno = errno;
29b39d6
-			Aerror(statp, stderr, "connect/vc", errno, nsap);
29b39d6
+			Aerror(statp, stderr, "connect/vc", errno,
29b39d6
+			       (struct sockaddr *) nsap);
29b39d6
 			__res_iclose(statp, false);
29b39d6
 			return (0);
29b39d6
 		}
Carlos O'Donell dd72bee
@@ -979,7 +1002,8 @@ static int
29b39d6
 reopen (res_state statp, int *terrno, int ns)
29b39d6
 {
29b39d6
 	if (EXT(statp).nssocks[ns] == -1) {
29b39d6
-		struct sockaddr *nsap = get_nsaddr (statp, ns);
29b39d6
+		struct sockaddr *nsap
29b39d6
+		  = (struct sockaddr *) EXT(statp).nsaddrs[ns];
29b39d6
 		socklen_t slen;
29b39d6
 
29b39d6
 		/* only try IPv6 if IPv6 NS and if not failed before */