From: Chuck Lever <chuck.lever@oracle.com>
To: linux-nfs@vger.kernel.org
Cc: chris.mason@oracle.com
Subject: [PATCH 15/26] statd: Use getaddrinfo(3) to generate bind address in smn_create_socket()
Date: Tue, 13 Oct 2009 10:56:46 -0400 [thread overview]
Message-ID: <20091013145646.2424.6943.stgit@matisse.1015granger.net> (raw)
In-Reply-To: <20091013142257.2424.76946.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
This patch updates the "bind to a user-specified port" arm of
smn_create_socket() so it can deal with IPv6 bind addresses.
A single getaddrinfo(3) call can convert a user-specified bind address
or hostname to a socket address, optionally plant a provided port
number, or whip up an appropriate wildcard address for use as the main
socket's bind address.
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
utils/statd/sm-notify.c | 81 ++++++++++++++++++++++++++++++-----------------
1 files changed, 52 insertions(+), 29 deletions(-)
diff --git a/utils/statd/sm-notify.c b/utils/statd/sm-notify.c
index f58783d..443fd46 100644
--- a/utils/statd/sm-notify.c
+++ b/utils/statd/sm-notify.c
@@ -59,7 +59,7 @@ static int opt_debug = 0;
static int opt_update_state = 1;
static unsigned int opt_max_retry = 15 * 60;
static char * opt_srcaddr = 0;
-static uint16_t opt_srcport = 0;
+static char * opt_srcport = 0;
static void notify(const int sock);
static int notify_host(int, struct nsm_host *);
@@ -194,6 +194,40 @@ out_close:
return -1;
}
+/*
+ * If admin specified a source address or srcport, then convert those
+ * to a sockaddr and return it. Otherwise, return an ANYADDR address.
+ */
+static struct addrinfo *
+smn_bind_address(const char *srcaddr, char *srcport)
+{
+ struct addrinfo gai_hint = {
+ .ai_family = nsm_family,
+ .ai_protocol = IPPROTO_UDP,
+ };
+ struct addrinfo *gai_results;
+ int error;
+
+ if (srcaddr == NULL)
+ gai_hint.ai_flags |= AI_PASSIVE;
+ if (srcport == NULL)
+ srcport = "";
+
+ error = getaddrinfo(srcaddr, srcport, &gai_hint, &gai_results);
+ switch (error) {
+ case 0:
+ return gai_results;
+ case EAI_SYSTEM:
+ xlog(L_ERROR, "Failed to determine bind address: %m");
+ break;
+ default:
+ xlog(L_ERROR, "Failed to determine bind address: %s",
+ gai_strerror(error));
+ }
+
+ return NULL;
+}
+
#ifdef HAVE_LIBTIRPC
static int
smn_bindresvport(int sock, struct sockaddr *sap)
@@ -221,62 +255,51 @@ smn_bindresvport(int sock, struct sockaddr *sap)
* an error occurs.
*/
static int
-smn_create_socket(const char *srcaddr, const uint16_t srcport)
+smn_create_socket(const char *srcaddr, char *srcport)
{
- struct sockaddr_storage address;
- struct sockaddr *local_addr = (struct sockaddr *)&address;
int sock, retry_cnt = 0;
+ struct addrinfo *ai;
retry:
sock = smn_socket();
if (sock < 0)
return -1;
- memset(&address, 0, sizeof(address));
- local_addr->sa_family = AF_INET; /* Default to IPv4 */
-
- /* Bind source IP if provided on command line */
- if (srcaddr) {
- struct addrinfo *ai = smn_lookup(srcaddr);
- if (!ai) {
- xlog(L_ERROR,
- "Not a valid hostname or address: \"%s\"",
- srcaddr);
- return -1;
- }
-
- /* We know it's IPv4 at this point */
- memcpy(local_addr, ai->ai_addr, ai->ai_addrlen);
-
- freeaddrinfo(ai);
- }
+ ai = smn_bind_address(srcaddr, srcport);
+ if (!ai)
+ return -1;
/* Use source port if provided on the command line,
* otherwise use bindresvport */
if (srcport) {
- nfs_set_port(local_addr, srcport);
- if (bind(sock, local_addr, sizeof(struct sockaddr_in)) < 0) {
+ if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
xlog(L_ERROR, "Failed to bind RPC socket: %m");
- return -1;
+ (void)close(sock);
+ sock = -1;
+ goto out;
}
} else {
struct servent *se;
- if (smn_bindresvport(sock, local_addr) == -1) {
+ if (smn_bindresvport(sock, ai->ai_addr) == -1) {
xlog(L_ERROR, "bindresvport(3): %m");
(void)close(sock);
- return -1;
+ sock = -1;
+ goto out;
}
/* try to avoid known ports */
- se = getservbyport(nfs_get_port(local_addr), "udp");
+ se = getservbyport(nfs_get_port(ai->ai_addr), "udp");
if (se && retry_cnt < 100) {
retry_cnt++;
(void)close(sock);
+ freeaddrinfo(ai);
goto retry;
}
}
+out:
+ freeaddrinfo(ai);
return sock;
}
@@ -308,7 +331,7 @@ main(int argc, char **argv)
opt_update_state = 0;
break;
case 'p':
- opt_srcport = atoi(optarg);
+ opt_srcport = optarg;
break;
case 'v':
opt_srcaddr = optarg;
next prev parent reply other threads:[~2009-10-13 14:57 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-13 14:54 [PATCH 00/26] Basic IPv6 support in statd (take++) Chuck Lever
[not found] ` <20091013142257.2424.76946.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-13 14:54 ` [PATCH 01/26] statd: Replace note() with xlog() in rpc.statd Chuck Lever
[not found] ` <20091013145416.2424.12787.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-14 16:57 ` J. Bruce Fields
2009-10-15 14:58 ` Chuck Lever
2009-10-13 14:54 ` [PATCH 02/26] statd: Replace nsm_log() with xlog() in sm-notify command Chuck Lever
2009-10-13 14:54 ` [PATCH 03/26] statd: replace smn_{get, set}_port() with the shared equivalents Chuck Lever
2009-10-13 14:54 ` [PATCH 04/26] statd: fix address copy in sm-notify.c Chuck Lever
2009-10-13 14:54 ` [PATCH 05/26] statd: Move the sm_inter XDR pieces to libnsm.a Chuck Lever
2009-10-13 14:55 ` [PATCH 06/26] statd: Introduce common routines to handle persistent storage Chuck Lever
[not found] ` <20091013145506.2424.10505.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-16 14:05 ` Jeff Layton
[not found] ` <20091016100544.25f686c4-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org>
2009-10-16 22:46 ` Chuck Lever
2009-10-16 23:27 ` Jeff Layton
2009-10-13 14:55 ` [PATCH 07/26] statd: Use the new nsm_ file.c calls in sm_notify Chuck Lever
2009-10-13 14:55 ` [PATCH 08/26] statd: Use the new nsm_ file.c calls in rpc.statd Chuck Lever
2009-10-13 14:55 ` [PATCH 09/26] libnsm: Add RPC construction helper functions Chuck Lever
[not found] ` <20091013145546.2424.83816.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-14 17:21 ` J. Bruce Fields
2009-10-15 15:21 ` Chuck Lever
2009-10-13 14:55 ` [PATCH 10/26] statd: Support sending SM_NOTIFY requests to IPv6 remotes Chuck Lever
2009-10-13 14:56 ` [PATCH 11/26] statd: Update rmtcall.c Chuck Lever
2009-10-13 14:56 ` [PATCH 12/26] statd: factor socket creation out of notify() Chuck Lever
2009-10-13 14:56 ` [PATCH 13/26] statd: Support creating a PF_INET6 socket in smn_create_socket() Chuck Lever
2009-10-13 14:56 ` [PATCH 14/26] statd: IPv6 support in reserved port binding " Chuck Lever
2009-10-13 14:56 ` Chuck Lever [this message]
2009-10-13 14:56 ` [PATCH 16/26] statd: Support IPv6 DNS lookups in smn_lookup Chuck Lever
2009-10-13 14:57 ` [PATCH 17/26] statd: squelch compiler warning in sm-notify.c Chuck Lever
2009-10-13 14:57 ` [PATCH 18/26] statd: Introduce statd version of matchhostname() Chuck Lever
2009-10-13 14:57 ` [PATCH 19/26] libnsm.a: add nsm_present_address() API Chuck Lever
2009-10-13 14:57 ` [PATCH 20/26] statd: add IPv6 support in sm_notify_1_svc() Chuck Lever
2009-10-13 14:57 ` [PATCH 21/26] statd: Support IPv6 is caller_is_localhost() Chuck Lever
2009-10-13 14:58 ` [PATCH 22/26] statd: Support IPv6 in sm_simu_crash_1_svc Chuck Lever
2009-10-13 14:58 ` [PATCH 23/26] statd: Support IPv6 in sm_mon_1_svc() Chuck Lever
2009-10-13 14:58 ` [PATCH 24/26] statd: Support IPv6 in sm_stat_1_svc() Chuck Lever
2009-10-13 14:58 ` [PATCH 25/26] statd: retain CAP_NET_BIND when dropping privileges Chuck Lever
2009-10-13 14:58 ` [PATCH 26/26] statd: Support TI-RPC statd listener Chuck Lever
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=20091013145646.2424.6943.stgit@matisse.1015granger.net \
--to=chuck.lever@oracle.com \
--cc=chris.mason@oracle.com \
--cc=linux-nfs@vger.kernel.org \
/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