Skip to contentSkip to conversation

tests/socket-helpers: Only fail socket protocol check if it is really necessary

PATCH In review Thomas Huth 2 participants 4 messages Sep 8, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/socket-helpers: Only fail socket protocol check if it is really necessary

The tests/test-char test is currently always failing on my system since socket_can_bind_connect("::1", PF_INET6) fails with EINVAL and thus socket_check_protocol_support() is returning -1 for an error. But IPv4 is working fine. The logic in socket_check_protocol_support() seems to be wrong here, if either IPv6 or IPv4 is working, we should not return an error here. Thus rework the function to only return errors if both checks failed.

---
 tests/socket-helpers.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
tests/socket-helpers.c +9 -14
index 19a51e887e..62a0e0f2d9 100644
@@ -136,22 +136,17 @@ static int socket_can_bind_connect(const char *hostname, int family)
 
 int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6)
 {
-    *has_ipv4 = *has_ipv6 = false;
+    int errv4, errv6;
 
-    if (socket_can_bind_connect("127.0.0.1", PF_INET) < 0) {
-        if (errno != EADDRNOTAVAIL) {
-            return -1;
-        }
-    } else {
-        *has_ipv4 = true;
-    }
+    errv4 = socket_can_bind_connect("127.0.0.1", PF_INET);
+    *has_ipv4 = (errv4 == 0);
 
-    if (socket_can_bind_connect("::1", PF_INET6) < 0) {
-        if (errno != EADDRNOTAVAIL) {
-            return -1;
-        }
-    } else {
-        *has_ipv6 = true;
+    errv6 = socket_can_bind_connect("::1", PF_INET6);
+    *has_ipv6 = (errv6 == 0);
+
+    if (!*has_ipv4 && !*has_ipv6 &&
+        (errv4 != EADDRNOTAVAIL || errv6 != EADDRNOTAVAIL)) {
+        return -1;
     }
 
     return 0;
Signature · 1 line
2.18.2
#1 lore raw
Quoted 7 lines · Thomas Huth

The tests/test-char test is currently always failing on my system since socket_can_bind_connect("::1", PF_INET6) fails with EINVAL and thus socket_check_protocol_support() is returning -1 for an error. But IPv4 is working fine. The logic in socket_check_protocol_support() seems to be wrong here, if either IPv6 or IPv4 is working, we should not return an error here. Thus rework the function to only return errors if both checks failed.

Can you tell me which exact syscall is giving EINVAL in this scenario ?

Quoted 30 lines
---
 tests/socket-helpers.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
tests/socket-helpers.c +3 -11
index 19a51e887e..62a0e0f2d9 100644
@@ -136,22 +136,17 @@ static int socket_can_bind_connect(const char *hostname, int family)
 
 int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6)
 {
-    *has_ipv4 = *has_ipv6 = false;
+    int errv4, errv6;
 
-    if (socket_can_bind_connect("127.0.0.1", PF_INET) < 0) {
-        if (errno != EADDRNOTAVAIL) {
-            return -1;
-        }
-    } else {
-        *has_ipv4 = true;
-    }
+    errv4 = socket_can_bind_connect("127.0.0.1", PF_INET);
+    *has_ipv4 = (errv4 == 0);
 
-    if (socket_can_bind_connect("::1", PF_INET6) < 0) {
-        if (errno != EADDRNOTAVAIL) {
-            return -1;

tests/socket-helpers.c static int socket_can_bind_connect(const char *hostname, int family) · line 150

Quoted 10 lines
tests/socket-helpers.c +6 -3
-        }
-    } else {
-        *has_ipv6 = true;
+    errv6 = socket_can_bind_connect("::1", PF_INET6);
+    *has_ipv6 = (errv6 == 0);
+
+    if (!*has_ipv4 && !*has_ipv6 &&
+        (errv4 != EADDRNOTAVAIL || errv6 != EADDRNOTAVAIL)) {
+        return -1;
     }

The return value of socket_can_bind_connect is either 0 or -1, but you're treating it an errno which isn't right.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw
Quoted 10 lines · Daniel P. Berrangé
Quoted 7 lines · Thomas Huth

The tests/test-char test is currently always failing on my system since socket_can_bind_connect("::1", PF_INET6) fails with EINVAL and thus socket_check_protocol_support() is returning -1 for an error. But IPv4 is working fine. The logic in socket_check_protocol_support() seems to be wrong here, if either IPv6 or IPv4 is working, we should not return an error here. Thus rework the function to only return errors if both checks failed.

Can you tell me which exact syscall is giving EINVAL in this scenario ?

getaddrinfo() fails with -2 (EAI_NONAME ?). The logic in socket_can_bind_connect() then translates this into EINVAL.

[...]

Quoted 14 lines

tests/socket-helpers.c static int socket_can_bind_connect(const char *hostname, int family) · line 150

Quoted 13 lines
tests/socket-helpers.c +6 -6
-    if (socket_can_bind_connect("::1", PF_INET6) < 0) {
-        if (errno != EADDRNOTAVAIL) {
-            return -1;
-        }
-    } else {
-        *has_ipv6 = true;
+    errv6 = socket_can_bind_connect("::1", PF_INET6);
+    *has_ipv6 = (errv6 == 0);
+
+    if (!*has_ipv4 && !*has_ipv6 &&
+        (errv4 != EADDRNOTAVAIL || errv6 != EADDRNOTAVAIL)) {
+        return -1;
     }
Quoted 2 lines

The return value of socket_can_bind_connect is either 0 or -1, but you're treating it an errno which isn't right.

Uh, where's my brown paperbag? ... looks like I need more coffee today...

Thomas

#3 lore raw
Quoted 10 lines · Thomas Huth
Quoted 9 lines · Daniel P. Berrangé
Quoted 7 lines · Thomas Huth

The tests/test-char test is currently always failing on my system since socket_can_bind_connect("::1", PF_INET6) fails with EINVAL and thus socket_check_protocol_support() is returning -1 for an error. But IPv4 is working fine. The logic in socket_check_protocol_support() seems to be wrong here, if either IPv6 or IPv4 is working, we should not return an error here. Thus rework the function to only return errors if both checks failed.

Quoted 4 lines
Quoted 1 line

Can you tell me which exact syscall is giving EINVAL in this scenario ?

getaddrinfo() fails with -2 (EAI_NONAME ?). The logic in socket_can_bind_connect() then translates this into EINVAL.

Ok, lets just translate EAI_NONAME into EADDRNOTAVAIL as we do for the other getaddrinfo errors we want to treat as soft-failures

Quoted 16 lines

[...]

Quoted 14 lines
Quoted 13 lines
tests/socket-helpers.c +6 -6
-    if (socket_can_bind_connect("::1", PF_INET6) < 0) {
-        if (errno != EADDRNOTAVAIL) {
-            return -1;
-        }
-    } else {
-        *has_ipv6 = true;
+    errv6 = socket_can_bind_connect("::1", PF_INET6);
+    *has_ipv6 = (errv6 == 0);
+
+    if (!*has_ipv4 && !*has_ipv6 &&
+        (errv4 != EADDRNOTAVAIL || errv6 != EADDRNOTAVAIL)) {
+        return -1;
     }
Quoted 7 lines
Quoted 2 lines

The return value of socket_can_bind_connect is either 0 or -1, but you're treating it an errno which isn't right.

Uh, where's my brown paperbag? ... looks like I need more coffee today...

Thomas

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|