Skip to contentSkip to conversation

ovpn-dco: ovpn-cli: properly set socket options

PATCH In review Tony He 2 participants 2 messages Apr 1, 2021

lore
Share & tools
lore

Tony He

author
#0 lore raw

PATCH 1/1 ovpn-dco: ovpn-cli: properly set socket options

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:

Quoted 1 line

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

---
 tests/ovpn-cli.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tests/ovpn-cli.c +7 -2
index c1cf3b4..68d28b4 100644
@@ -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;
 	}
 
Signature · 1 line
2.17.1
#1 lore raw

Thanks!

Your patch has been applied with commit id:
0048aac81c3e4dc3046121a7124da77233656e99

Regards,

Quoted 44 lines · Tony He

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:

Quoted 1 line

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

---
 tests/ovpn-cli.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tests/ovpn-cli.c +7 -2
index c1cf3b4..68d28b4 100644
@@ -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;
 	}
 
Signature · 1 line
Antonio Quartulli