diff mbox

[4/7] nfs-utils: Implement srcaddr binding in network.c

Message ID 1307729379-7702-5-git-send-email-greearb@candelatech.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ben Greear June 10, 2011, 6:09 p.m. UTC
From: Ben Greear <greearb@candelatech.com>

Implement binding logic in get_socket() if local_ip
argument is not NULL.

Note that this method had issues with supporting IPv6 before
this patch, and this patch does NOT resolve them.

Signed-off-by: Ben Greear <greearb@candelatech.com>
---
:100644 100644 36c2e94... 5419c5d... M	utils/mount/network.c
 utils/mount/network.c |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

Comments

Chuck Lever June 10, 2011, 6:29 p.m. UTC | #1
On Jun 10, 2011, at 2:09 PM, greearb@candelatech.com wrote:

> From: Ben Greear <greearb@candelatech.com>
> 
> Implement binding logic in get_socket() if local_ip
> argument is not NULL.
> 
> Note that this method had issues with supporting IPv6 before
> this patch, and this patch does NOT resolve them.

If get_socket() doesn't support IPv6, then I doubt it's used for string-based mounts.  You may not need any changes here.

> Signed-off-by: Ben Greear <greearb@candelatech.com>
> ---
> :100644 100644 36c2e94... 5419c5d... M	utils/mount/network.c
> utils/mount/network.c |   21 ++++++++++++++++++---
> 1 files changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/utils/mount/network.c b/utils/mount/network.c
> index 36c2e94..5419c5d 100644
> --- a/utils/mount/network.c
> +++ b/utils/mount/network.c
> @@ -461,19 +461,34 @@ static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
> 	int so, cc, type;
> 	struct sockaddr_in laddr;
> 	socklen_t namelen = sizeof(laddr);
> +	int f = AF_INET;
> +
> +	if (local_ip && local_ip->is_set)
> +		f = local_ip->addr.sa.sa_family;
> 
> 	type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
> -	if ((so = socket (AF_INET, type, p_prot)) < 0)
> +
> +	so = socket(f, type, p_prot);
> +	if (so < 0)
> 		goto err_socket;
> 
> -	laddr.sin_family = AF_INET;
> +	laddr.sin_family = f;
> 	laddr.sin_port = 0;
> 	laddr.sin_addr.s_addr = htonl(INADDR_ANY);
> 	if (resvp) {
> +		/* TODO:  Support IPv6 */
> +		if (local_ip && local_ip->is_set
> +		    && local_ip->addr.sa.sa_family == AF_INET) {
> +			struct sockaddr_in *si = &local_ip->addr.s4;
> +			laddr.sin_addr.s_addr = si->sin_addr.s_addr;
> +		}
> 		if (bindresvport(so, &laddr) < 0)
> 			goto err_bindresvport;
> 	} else {
> -		cc = bind(so, SAFE_SOCKADDR(&laddr), namelen);
> +		if (local_ip && local_ip->is_set)
> +			cc = bind(so, &local_ip->addr.sa, local_ip->addrlen);
> +		else
> +			cc = bind(so, SAFE_SOCKADDR(&laddr), namelen);
> 		if (cc < 0)
> 			goto err_bind;
> 	}
> -- 
> 1.7.3.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Ben Greear June 10, 2011, 6:40 p.m. UTC | #2
On 06/10/2011 11:29 AM, Chuck Lever wrote:
>
> On Jun 10, 2011, at 2:09 PM, greearb@candelatech.com wrote:
>
>> From: Ben Greear<greearb@candelatech.com>
>>
>> Implement binding logic in get_socket() if local_ip
>> argument is not NULL.
>>
>> Note that this method had issues with supporting IPv6 before
>> this patch, and this patch does NOT resolve them.
>
> If get_socket() doesn't support IPv6, then I doubt it's used for string-based mounts.  You may not need any changes here.

Looks like you are correct.  I added printf to that method and
see nothing while doing mount/unmount.  I'll respin the patches
to remove that...should make things a good bit smaller.

Thanks,
Ben
diff mbox

Patch

diff --git a/utils/mount/network.c b/utils/mount/network.c
index 36c2e94..5419c5d 100644
--- a/utils/mount/network.c
+++ b/utils/mount/network.c
@@ -461,19 +461,34 @@  static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
 	int so, cc, type;
 	struct sockaddr_in laddr;
 	socklen_t namelen = sizeof(laddr);
+	int f = AF_INET;
+
+	if (local_ip && local_ip->is_set)
+		f = local_ip->addr.sa.sa_family;
 
 	type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
-	if ((so = socket (AF_INET, type, p_prot)) < 0)
+
+	so = socket(f, type, p_prot);
+	if (so < 0)
 		goto err_socket;
 
-	laddr.sin_family = AF_INET;
+	laddr.sin_family = f;
 	laddr.sin_port = 0;
 	laddr.sin_addr.s_addr = htonl(INADDR_ANY);
 	if (resvp) {
+		/* TODO:  Support IPv6 */
+		if (local_ip && local_ip->is_set
+		    && local_ip->addr.sa.sa_family == AF_INET) {
+			struct sockaddr_in *si = &local_ip->addr.s4;
+			laddr.sin_addr.s_addr = si->sin_addr.s_addr;
+		}
 		if (bindresvport(so, &laddr) < 0)
 			goto err_bindresvport;
 	} else {
-		cc = bind(so, SAFE_SOCKADDR(&laddr), namelen);
+		if (local_ip && local_ip->is_set)
+			cc = bind(so, &local_ip->addr.sa, local_ip->addrlen);
+		else
+			cc = bind(so, SAFE_SOCKADDR(&laddr), namelen);
 		if (cc < 0)
 			goto err_bind;
 	}