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 <huangy...@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;
        }
 
-- 
2.17.1



_______________________________________________
Openvpn-devel mailing list
Openvpn-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to