[PATCH 2/3] Give hostname.c more support for IPv6

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Allow host_pton,  host_addrinfo, and all functions that use
sockaddr_size to work correctly with IPv6 addresses.

This means host_addrinfo can now return IPv6 addresses, but I think
all code is ready for that.

Signed-off-by: NeilBrown <neilb@xxxxxxx>
---
 support/export/hostname.c |   26 ++++++++++++++++++--------
 1 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/support/export/hostname.c b/support/export/hostname.c
index f6a59f1..46ddfe2 100644
--- a/support/export/hostname.c
+++ b/support/export/hostname.c
@@ -38,9 +38,11 @@
 static socklen_t
 sockaddr_size(const struct sockaddr *sap)
 {
-	if (sap->sa_family != AF_INET)
-		return 0;
-	return (socklen_t)sizeof(struct sockaddr_in);
+	if (sap->sa_family == AF_INET)
+		return (socklen_t)sizeof(struct sockaddr_in);
+	if (sap->sa_family == AF_INET6)
+		return (socklen_t)sizeof(struct sockaddr_in6);
+	return 0;
 }
 #endif	/* HAVE_GETNAMEINFO */
 
@@ -126,10 +128,12 @@ host_pton(const char *paddr)
 	 * addresses that end with a blank.
 	 *
 	 * inet_pton(3) is much stricter.  Use it to be certain we
-	 * have a real AF_INET presentation address, before invoking
-	 * getaddrinfo(3) to generate the full addrinfo list.
+	 * have a real AF_INET or AF_INET6 presentation address,
+	 * before invoking getaddrinfo(3) to generate the full
+	 * addrinfo list.
 	 */
-	if (inet_pton(AF_INET, paddr, &sin.sin_addr) == 0)
+	if (inet_pton(AF_INET, paddr, &sin.sin_addr) == 0 &&
+	    inet_pton(AF_INET6, paddr, &sin.sin_addr) == 0)
 		return NULL;
 
 	error = getaddrinfo(paddr, NULL, &hint, &ai);
@@ -168,7 +172,7 @@ host_addrinfo(const char *hostname)
 {
 	struct addrinfo *ai = NULL;
 	struct addrinfo hint = {
-		.ai_family	= AF_INET,
+		.ai_family	= AF_UNSPEC,
 		/* don't return duplicates */
 		.ai_protocol	= (int)IPPROTO_UDP,
 		.ai_flags	= AI_ADDRCONFIG | AI_CANONNAME,
@@ -178,7 +182,13 @@ host_addrinfo(const char *hostname)
 	error = getaddrinfo(hostname, NULL, &hint, &ai);
 	switch (error) {
 	case 0:
-		return ai;
+		if (ai->ai_family == AF_INET ||
+		    ai->ai_family == AF_INET6)
+			return ai;
+		freeaddrinfo(ai);
+		xlog(D_GENERAL, "%s: resolved to neither IPv4 nor IPv6 address.",
+				hostname);
+		break;
 	case EAI_SYSTEM:
 		xlog(D_GENERAL, "%s: failed to resolve %s: (%d) %m",
 				__func__, hostname, errno);


--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Filesystem Development]     [Linux USB Development]     [Linux Media Development]     [Video for Linux]     [Linux NILFS]     [Linux Audio Users]     [Yosemite Info]     [Linux SCSI]

  Powered by Linux