From 5600327322a78a3a803368c0fe4f923cf14a4cf7 Mon Sep 17 00:00:00 2001 From: Chuck Lever Date: Tue, 14 Jul 2009 16:47:09 -0400 Subject: [PATCH] support: Introduce sockaddr helpers to get and set IP port numbers Introduce address family-agnostic functions that get and set IP port numbers in socket addresses. We can already replace a few similar functions in the mount command, and a few more will come up with statd and sm-notify. Signed-off-by: Chuck Lever Signed-off-by: Steve Dickson --- support/include/nfsrpc.h | 10 +++++++ support/nfs/getport.c | 24 ++------------- support/nfs/rpc_socket.c | 63 ++++++++++++++++++++++++++++------------ utils/mount/network.c | 15 ---------- 4 files changed, 58 insertions(+), 54 deletions(-) diff --git a/support/include/nfsrpc.h b/support/include/nfsrpc.h index b3cdb8c..dff6af7 100644 --- a/support/include/nfsrpc.h +++ b/support/include/nfsrpc.h @@ -58,6 +58,16 @@ static inline void nfs_clear_rpc_createerr(void) memset(&rpc_createerr, 0, sizeof(rpc_createerr)); } +/* + * Extract port value from a socket address + */ +extern uint16_t nfs_get_port(const struct sockaddr *); + +/* + * Set port value in a socket address + */ +extern void nfs_set_port(struct sockaddr *, const uint16_t); + /* * Look up an RPC program name in /etc/rpc */ diff --git a/support/nfs/getport.c b/support/nfs/getport.c index f5ba4ef..4d4b1c4 100644 --- a/support/nfs/getport.c +++ b/support/nfs/getport.c @@ -112,24 +112,6 @@ static int nfs_gp_loopback_address(struct sockaddr *sap, socklen_t *salen) return ret; } -/* - * Plant port number in @sap. @port is already in network byte order. - */ -static void nfs_gp_set_port(struct sockaddr *sap, const in_port_t port) -{ - struct sockaddr_in *sin = (struct sockaddr_in *)sap; - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; - - switch (sap->sa_family) { - case AF_INET: - sin->sin_port = port; - break; - case AF_INET6: - sin6->sin6_port = port; - break; - } -} - /* * Look up a network service in /etc/services and return the * network-order port number of that service. @@ -210,7 +192,7 @@ static CLIENT *nfs_gp_get_rpcbclient(struct sockaddr *sap, rpcprog_t rpcb_prog = nfs_getrpcbyname(RPCBPROG, rpcb_pgmtbl); CLIENT *clnt; - nfs_gp_set_port(sap, nfs_gp_get_rpcb_port(transport)); + nfs_set_port(sap, ntohs(nfs_gp_get_rpcb_port(transport))); clnt = nfs_get_rpcclient(sap, salen, transport, rpcb_prog, version, timeout); nfs_gp_map_tcp_errorcodes(transport); @@ -773,7 +755,7 @@ int nfs_getport_ping(struct sockaddr *sap, const socklen_t salen, struct sockaddr *saddr = (struct sockaddr *)&address; memcpy(saddr, sap, (size_t)salen); - nfs_gp_set_port(saddr, htons(port)); + nfs_set_port(saddr, port); nfs_clear_rpc_createerr(); @@ -787,7 +769,7 @@ int nfs_getport_ping(struct sockaddr *sap, const socklen_t salen, } if (result) - nfs_gp_set_port(sap, htons(port)); + nfs_set_port(sap, port); return result; } diff --git a/support/nfs/rpc_socket.c b/support/nfs/rpc_socket.c index a080487..9c20f61 100644 --- a/support/nfs/rpc_socket.c +++ b/support/nfs/rpc_socket.c @@ -415,6 +415,49 @@ static CLIENT *nfs_get_tcpclient(const struct sockaddr *sap, return client; } +/** + * nfs_get_port - extract port value from a socket address + * @sap: pointer to socket address + * + * Returns port value in host byte order. + */ +uint16_t +nfs_get_port(const struct sockaddr *sap) +{ + const struct sockaddr_in *sin = (const struct sockaddr_in *)sap; + const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap; + + switch (sap->sa_family) { + case AF_INET: + return ntohs(sin->sin_port); + case AF_INET6: + return ntohs(sin6->sin6_port); + } + return 0; +} + +/** + * nfs_set_port - set port value in a socket address + * @sap: pointer to socket address + * @port: port value to set + * + */ +void +nfs_set_port(struct sockaddr *sap, const uint16_t port) +{ + struct sockaddr_in *sin = (struct sockaddr_in *)sap; + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; + + switch (sap->sa_family) { + case AF_INET: + sin->sin_port = htons(port); + break; + case AF_INET6: + sin6->sin6_port = htons(port); + break; + } +} + /** * nfs_get_rpcclient - acquire an RPC client * @sap: pointer to socket address of RPC server @@ -440,9 +483,6 @@ CLIENT *nfs_get_rpcclient(const struct sockaddr *sap, const rpcvers_t version, struct timeval *timeout) { - struct sockaddr_in *sin = (struct sockaddr_in *)sap; - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; - nfs_clear_rpc_createerr(); switch (sap->sa_family) { @@ -450,13 +490,8 @@ CLIENT *nfs_get_rpcclient(const struct sockaddr *sap, return nfs_get_localclient(sap, salen, program, version, timeout); case AF_INET: - if (sin->sin_port == 0) { - rpc_createerr.cf_stat = RPC_UNKNOWNADDR; - return NULL; - } - break; case AF_INET6: - if (sin6->sin6_port == 0) { + if (nfs_get_port(sap) == 0) { rpc_createerr.cf_stat = RPC_UNKNOWNADDR; return NULL; } @@ -505,9 +540,6 @@ CLIENT *nfs_get_priv_rpcclient(const struct sockaddr *sap, const rpcvers_t version, struct timeval *timeout) { - struct sockaddr_in *sin = (struct sockaddr_in *)sap; - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; - nfs_clear_rpc_createerr(); switch (sap->sa_family) { @@ -515,13 +547,8 @@ CLIENT *nfs_get_priv_rpcclient(const struct sockaddr *sap, return nfs_get_localclient(sap, salen, program, version, timeout); case AF_INET: - if (sin->sin_port == 0) { - rpc_createerr.cf_stat = RPC_UNKNOWNADDR; - return NULL; - } - break; case AF_INET6: - if (sin6->sin6_port == 0) { + if (nfs_get_port(sap) == 0) { rpc_createerr.cf_stat = RPC_UNKNOWNADDR; return NULL; } diff --git a/utils/mount/network.c b/utils/mount/network.c index 5b6f4c2..ad6c146 100644 --- a/utils/mount/network.c +++ b/utils/mount/network.c @@ -170,21 +170,6 @@ static const unsigned long probe_mnt3_first[] = { 0, }; -static void nfs_set_port(struct sockaddr *sap, const unsigned short port) -{ - switch (sap->sa_family) { - case AF_INET: - ((struct sockaddr_in *)sap)->sin_port = htons(port); - break; - case AF_INET6: - ((struct sockaddr_in6 *)sap)->sin6_port = htons(port); - break; - default: - nfs_error(_("%s: unrecognized address family in %s"), - progname, __func__); - } -} - static int nfs_lookup(const char *hostname, const sa_family_t family, struct sockaddr *sap, socklen_t *salen) { -- 2.39.5