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, Chris.Mason@oracle.com
Subject: [PATCH 19/29] statd: add nsm_present_address() API
Date: Tue, 10 Nov 2009 16:59:20 -0500	[thread overview]
Message-ID: <20091110215919.23822.69508.stgit@matisse.1015granger.net> (raw)
In-Reply-To: <20091110215447.23822.15275.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>

Add an API to convert a socket addres to a presentation address
string.  This is used for displaying error messages and the like.

We prefer getnameinfo(3) over inet_?to?(3) as it supports IPv6 scope
IDs.  Since statd has to build on systems whose glibc does not have
getnameinfo(3), an inet_?to?(3) version is also provided.

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

 utils/statd/hostname.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++++
 utils/statd/statd.h    |    2 +
 2 files changed, 75 insertions(+), 0 deletions(-)

diff --git a/utils/statd/hostname.c b/utils/statd/hostname.c
index cb064fd..6f532b8 100644
--- a/utils/statd/hostname.c
+++ b/utils/statd/hostname.c
@@ -29,6 +29,7 @@
 #include <sys/socket.h>
 
 #include <stdlib.h>
+#include <string.h>
 #include <strings.h>
 #include <netdb.h>
 
@@ -39,6 +40,78 @@
 #define AI_ADDRCONFIG	0
 #endif
 
+/**
+ * nsm_present_address - convert sockaddr to presentation address
+ * @sap: pointer to socket address to convert
+ * @salen: length of socket address
+ * @buf: pointer to buffer to fill in
+ * @buflen: length of buffer
+ *
+ * Convert the passed-in sockaddr-style address to presentation format.
+ * The presentation format address is placed in @buf and is
+ * '\0'-terminated.  Callers who don't know the size of the socket
+ * address can pass a zero.
+ *
+ * Returns 1 if successful; otherwise zero.
+ *
+ * getnameinfo(3) is preferred, since it can parse IPv6 scope IDs.
+ * An alternate version of present_address() is available to deal with
+ * older glibcs that do not have getnameinfo(3).
+ */
+#ifdef IPV6_SUPPORTED
+int
+nsm_present_address(const struct sockaddr *sap, socklen_t salen,
+		char *buf, const size_t buflen)
+{
+	int error;
+
+	if (salen == 0)
+		switch (sap->sa_family) {
+		case AF_INET:
+			salen = sizeof(struct sockaddr_in);
+			break;
+		case AF_INET6:
+			salen = sizeof(struct sockaddr_in6);
+			break;
+		default:
+			xlog(L_ERROR, "%s: Unsupported address family",
+					__func__);
+			return 0;
+		}
+
+	error = getnameinfo(sap, salen, buf, buflen, NULL, 0, NI_NUMERICHOST);
+	if (error) {
+		xlog(L_ERROR, "%s: getnameinfo: %s",
+				__func__, gai_strerror(error));
+		return 0;
+	}
+
+	return 1;
+}
+
+#else	/* !IPV6_SUPPORTED */
+
+#include <arpa/inet.h>
+
+int
+nsm_present_address(const struct sockaddr *sap,
+		__attribute__((unused)) socklen_t salen,
+		char *buf, const size_t buflen)
+{
+	const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
+
+	if (sin->sin_family != AF_INET) {
+		xlog(L_ERROR, "%s: Unsupported address family", __func__);
+		return 0;
+	}
+
+	memset(buf, 0, buflen);
+	if (!inet_ntop(AF_INET, (char *)&sin->sin_addr, buf, buflen))
+		return 0;
+	return 1;
+}
+#endif	/* !IPV6_SUPPORTED */
+
 /*
  * Look up the hostname; report exceptional errors.  Caller must
  * call freeaddrinfo(3) if a valid addrinfo is returned.
diff --git a/utils/statd/statd.h b/utils/statd/statd.h
index c53b70a..71a77e2 100644
--- a/utils/statd/statd.h
+++ b/utils/statd/statd.h
@@ -23,6 +23,8 @@
  * Function prototypes.
  */
 extern int	nsm_matchhostname(const char *hostname1, const char *hostname2);
+extern int	nsm_present_address(const struct sockaddr *sap, socklen_t salen,
+					char *buf, const size_t buflen);
 extern void	my_svc_run(void);
 extern void	notify_hosts(void);
 extern void	shuffle_dirs(void);


  parent reply	other threads:[~2009-11-10 22:01 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-10 21:56 [PATCH 00/29] IPv6 support for statd Chuck Lever
     [not found] ` <20091110215447.23822.15275.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-11-10 21:56   ` [PATCH 01/29] statd: Replace note() with xlog() in rpc.statd Chuck Lever
2009-11-10 21:56   ` [PATCH 02/29] statd: Replace nsm_log() with xlog() in sm-notify command Chuck Lever
2009-11-10 21:56   ` [PATCH 03/29] statd: replace smn_{get, set}_port() with the shared equivalents Chuck Lever
2009-11-10 21:56   ` [PATCH 04/29] statd: fix address copy in sm-notify.c Chuck Lever
2009-11-10 21:56   ` [PATCH 05/29] libnsm.a: Move the sm_inter XDR pieces to libnsm.a Chuck Lever
2009-11-10 21:57   ` [PATCH 06/29] libnsm.a: Introduce common routines to handle persistent storage Chuck Lever
2009-11-10 21:57   ` [PATCH 07/29] statd: Use the new nsm_ file.c calls in sm_notify Chuck Lever
2009-11-10 21:57   ` [PATCH 08/29] statd: Use the new nsm_ file.c calls in rpc.statd Chuck Lever
2009-11-10 21:57   ` [PATCH 09/29] libnsm.a: Add RPC construction helper functions Chuck Lever
2009-11-10 21:57   ` [PATCH 10/29] statd: Support sending SM_NOTIFY requests to IPv6 remotes Chuck Lever
2009-11-10 21:58   ` [PATCH 11/29] statd: Update rmtcall.c Chuck Lever
2009-11-10 21:58   ` [PATCH 12/29] statd: factor socket creation out of notify() Chuck Lever
2009-11-10 21:58   ` [PATCH 13/29] statd: Support creating a PF_INET6 socket in smn_create_socket() Chuck Lever
2009-11-10 21:58   ` [PATCH 14/29] statd: IPv6 support in reserved port binding " Chuck Lever
2009-11-10 21:58   ` [PATCH 15/29] statd: Use getaddrinfo(3) to generate bind address " Chuck Lever
2009-11-10 21:58   ` [PATCH 16/29] statd: Support IPv6 DNS lookups in smn_lookup Chuck Lever
2009-11-10 21:59   ` [PATCH 17/29] statd: squelch compiler warning in sm-notify.c Chuck Lever
2009-11-10 21:59   ` Chuck Lever [this message]
2009-11-10 21:59   ` [PATCH 20/29] statd: add IPv6 support in sm_notify_1_svc() Chuck Lever
2009-11-10 21:59   ` [PATCH 21/29] statd: Support IPv6 is caller_is_localhost() Chuck Lever
2009-11-10 21:59   ` [PATCH 22/29] statd: Support IPv6 in sm_simu_crash_1_svc Chuck Lever
2009-11-10 21:59   ` [PATCH 23/29] statd: Support IPv6 in sm_mon_1_svc() Chuck Lever
2009-11-10 22:00   ` [PATCH 24/29] statd: Support IPv6 in sm_stat_1_svc() Chuck Lever
2009-11-10 22:00   ` [PATCH 25/29] libnsm.a: retain CAP_NET_BIND when dropping privileges Chuck Lever
2009-11-10 22:00   ` [PATCH 26/29] statd: Support TI-RPC statd listener Chuck Lever
2009-11-10 22:00   ` [PATCH 27/29] statd: update rpc.statd(8) and sm-notify(8) to reflect IPv6 support Chuck Lever
2009-11-10 22:01   ` [PATCH 28/29] statd: Use my_name when sending SM_NOTIFY requests Chuck Lever
2009-11-10 22:01   ` [PATCH 29/29] statd: Send unqualified and fully qualified mon_name in SM_NOTIFY Chuck Lever
2009-11-12 16:04   ` [PATCH 00/29] IPv6 support for statd Chuck Lever
2009-11-12 16:33     ` 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=20091110215919.23822.69508.stgit@matisse.1015granger.net \
    --to=chuck.lever@oracle.com \
    --cc=Chris.Mason@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