public inbox for linux-nfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Chuck Lever <chuck.lever@oracle.com>
To: steved@redhat.com
Cc: linux-nfs@vger.kernel.org
Subject: [PATCH 5/7] mount: Introduce IPv6-aware DNS resolver API function
Date: Mon, 30 Jun 2008 13:10:36 -0400	[thread overview]
Message-ID: <20080630171035.9905.87279.stgit@ellison.1015granger.net> (raw)
In-Reply-To: <20080630170420.9905.90111.stgit-ewv44WTpT0t9HhUboXbp9zCvJB+x5qRC@public.gmane.org>

Introduce a new DNS resolver function in utils/mount/network.c that uses
getaddrinfo(3), which supports AF_INET6, to resolve host names.

Replace the guts of nfs_gethostbyname() with a call to the new function.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---

 utils/mount/network.c |   78 +++++++++++++++++++++++++++++++++++++------------
 utils/mount/network.h |    2 +
 2 files changed, 61 insertions(+), 19 deletions(-)


diff --git a/utils/mount/network.c b/utils/mount/network.c
index ab7f6d0..30a4d40 100644
--- a/utils/mount/network.c
+++ b/utils/mount/network.c
@@ -144,32 +144,72 @@ static const unsigned long probe_mnt3_first[] = {
 };
 
 /**
+ * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
+ * @hostname: pointer to C string containing DNS hostname to resolve
+ * @sap: pointer to buffer to fill with socket address
+ * @len: IN: size of buffer to fill; OUT: size of socket address
+ *
+ * Returns 1 and places a socket address at @sap if successful;
+ * otherwise zero.
+ */
+int nfs_name_to_address(const char *hostname,
+			const sa_family_t af_hint,
+			struct sockaddr *sap, socklen_t *salen)
+{
+	struct addrinfo *gai_results;
+	struct addrinfo gai_hint = {
+		.ai_family	= af_hint,
+		.ai_flags	= AI_ADDRCONFIG,
+	};
+	socklen_t len = *salen;
+	int error, ret = 0;
+
+	if (af_hint == AF_INET6)
+		gai_hint.ai_flags |= AI_V4MAPPED|AI_ALL;
+
+	*salen = 0;
+
+	error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
+	if (error) {
+		nfs_error(_("%s: DNS resolution failed for %s: %s"),
+			progname, hostname, (error == EAI_SYSTEM ?
+				strerror(errno) : gai_strerror(error)));
+		return ret;
+	}
+
+	switch (gai_results->ai_addr->sa_family) {
+	case AF_INET:
+	case AF_INET6:
+		if (len >= gai_results->ai_addrlen) {
+			*salen = gai_results->ai_addrlen;
+			memcpy(sap, gai_results->ai_addr, *salen);
+			ret = 1;
+		}
+		break;
+	default:
+		/* things are really broken if we get here, so warn */
+		nfs_error(_("%s: unrecognized DNS resolution results for %s"),
+				progname, hostname);
+		break;
+	}
+
+	freeaddrinfo(gai_results);
+	return ret;
+}
+
+/**
  * nfs_gethostbyname - resolve a hostname to an IPv4 address
  * @hostname: pointer to a C string containing a DNS hostname
  * @saddr: returns an IPv4 address 
  *
  * Returns 1 if successful, otherwise zero.
  */
-int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
+int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
 {
-	struct hostent *hp;
-
-	saddr->sin_family = AF_INET;
-	if (!inet_aton(hostname, &saddr->sin_addr)) {
-		if ((hp = gethostbyname(hostname)) == NULL) {
-			nfs_error(_("%s: can't get address for %s\n"),
-					progname, hostname);
-			return 0;
-		} else {
-			if (hp->h_length > sizeof(*saddr)) {
-				nfs_error(_("%s: got bad hp->h_length\n"),
-						progname);
-				hp->h_length = sizeof(*saddr);
-			}
-			memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
-		}
-	}
-	return 1;
+	socklen_t len = sizeof(*sin);
+
+	return nfs_name_to_address(hostname, AF_INET,
+					(struct sockaddr *)sin, &len);
 }
 
 /*
diff --git a/utils/mount/network.h b/utils/mount/network.h
index e0a5dd5..9de13b5 100644
--- a/utils/mount/network.h
+++ b/utils/mount/network.h
@@ -51,6 +51,8 @@ static const struct timeval RETRY_TIMEOUT = { 3, 0 };
 
 int probe_bothports(clnt_addr_t *, clnt_addr_t *);
 int nfs_gethostbyname(const char *, struct sockaddr_in *);
+int nfs_name_to_address(const char *, const sa_family_t,
+		struct sockaddr *, socklen_t *);
 int get_client_address(struct sockaddr_in *, struct sockaddr_in *);
 int nfs_call_umount(clnt_addr_t *, dirpath *);
 int clnt_ping(struct sockaddr_in *, const unsigned long,


  parent reply	other threads:[~2008-06-30 17:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-30 17:10 [PATCH 0/7] Patches for nfs-utils-1.1.3 Chuck Lever
     [not found] ` <20080630170420.9905.90111.stgit-ewv44WTpT0t9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2008-06-30 17:10   ` [PATCH 1/7] Python scripts: Report bandwidth in "kB/s" instead of "Kb/s" Chuck Lever
2008-06-30 17:10   ` [PATCH 2/7] text-based mount command: Make "internal error" message more explicit Chuck Lever
2008-06-30 17:10   ` [PATCH 3/7] mount command: don't include headers multiple times Chuck Lever
2008-06-30 17:10   ` [PATCH 4/7] nfs-utils: Add dependency for getaddrinfo(3) in configure.ac Chuck Lever
2008-06-30 17:10   ` Chuck Lever [this message]
2008-06-30 17:10   ` [PATCH 6/7] mount command: move function to get kernel version to separate header Chuck Lever
2008-06-30 17:10   ` [PATCH 7/7] text-based mount command: pass "sloppy" when -s is set Chuck Lever
2008-07-15 19:30   ` [PATCH 0/7] Patches for nfs-utils-1.1.3 Steve Dickson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080630171035.9905.87279.stgit@ellison.1015granger.net \
    --to=chuck.lever@oracle.com \
    --cc=linux-nfs@vger.kernel.org \
    --cc=steved@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox