[Openvpn-devel] ovpn-dco: ovpn-cli: properly set socket options

Message ID 20210401064431.20229-1-huangya90@gmail.com
State Accepted
Headers show
Series [Openvpn-devel] ovpn-dco: ovpn-cli: properly set socket options | expand

Commit Message

Tony He March 31, 2021, 7:44 p.m. UTC
Actully We can not set two options at the same time.
Old code:

setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))

If you use strace to trace sys call, you will find only SO_REUSEPORT is
set:
>setsockopt(3, SOL_SOCKET, SO_REUSEPORT, [1], 4) = 0

This is because SD_REUSEADDR is 2(0x0010) and SR_REUSEPORT is 15(0x1111) defined in
/usr/include/asm-generic/socket.h in X86-64 platform.

We need to set them one by one. In other platforms, if you set mutli
options at the same time, you may encounter error "setsockopt: Protocol
not available". Refer to
https://stackoverflow.com/questions/58599070/socket-programming-setsockopt-protocol-not-available

Signed-off-by: Tony He <huangya90@gmail.com>
---
 tests/ovpn-cli.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Comments

Antonio Quartulli March 31, 2021, 8:33 p.m. UTC | #1
Thanks!

Your patch has been applied with commit id:
0048aac81c3e4dc3046121a7124da77233656e99

Regards,

On 01/04/2021 08:44, Tony He wrote:
> Actully We can not set two options at the same time.
> Old code:
> 
> setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))
> 
> If you use strace to trace sys call, you will find only SO_REUSEPORT is
> set:
>> setsockopt(3, SOL_SOCKET, SO_REUSEPORT, [1], 4) = 0
> 
> This is because SD_REUSEADDR is 2(0x0010) and SR_REUSEPORT is 15(0x1111) defined in
> /usr/include/asm-generic/socket.h in X86-64 platform.
> 
> We need to set them one by one. In other platforms, if you set mutli
> options at the same time, you may encounter error "setsockopt: Protocol
> not available". Refer to
> https://stackoverflow.com/questions/58599070/socket-programming-setsockopt-protocol-not-available
> 
> Signed-off-by: Tony He <huangya90@gmail.com>
> ---
>  tests/ovpn-cli.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/tests/ovpn-cli.c b/tests/ovpn-cli.c
> index c1cf3b4..68d28b4 100644
> --- a/tests/ovpn-cli.c
> +++ b/tests/ovpn-cli.c
> @@ -426,9 +426,14 @@ static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto)
>  	}
>  
>  	int opt = 1;
> -	ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
> +	ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
>  	if (ret < 0) {
> -		perror("setsockopt");
> +		perror("setsockopt for SO_REUSEADDR");
> +		return ret;
> +	}
> +	ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
> +	if (ret < 0) {
> +		perror("setsockopt for SO_REUSEPORT");
>  		return ret;
>  	}
>  
>

Patch

diff --git a/tests/ovpn-cli.c b/tests/ovpn-cli.c
index c1cf3b4..68d28b4 100644
--- a/tests/ovpn-cli.c
+++ b/tests/ovpn-cli.c
@@ -426,9 +426,14 @@  static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto)
 	}
 
 	int opt = 1;
-	ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
+	ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
 	if (ret < 0) {
-		perror("setsockopt");
+		perror("setsockopt for SO_REUSEADDR");
+		return ret;
+	}
+	ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
+	if (ret < 0) {
+		perror("setsockopt for SO_REUSEPORT");
 		return ret;
 	}