lib: Simplify is_ipaddress_v6
authorVolker Lendecke <vl@samba.org>
Mon, 13 Nov 2017 15:54:09 +0000 (16:54 +0100)
committerJeremy Allison <jra@samba.org>
Fri, 17 Nov 2017 23:09:16 +0000 (00:09 +0100)
Do an early return, avoid an "else", avoid an indentation level

Review with git show -b

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/util_net.c

index b3ed9f262feae9d74f053dac74a8c58519ea06c5..d52d401e4c7bffe0e50218df960f7c03bc596b01 100644 (file)
@@ -490,76 +490,75 @@ bool is_ipaddress_v6(const char *str)
 #if defined(HAVE_IPV6)
        int ret = -1;
        char *p = NULL;
+       char buf[INET6_ADDRSTRLEN] = { 0, };
+       size_t len;
+       const char *addr = str;
+       const char *idxs = NULL;
+       unsigned int idx = 0;
+       struct in6_addr ip6;
 
        p = strchr_m(str, ':');
        if (p == NULL) {
                return is_ipv6_literal(str);
-       } else {
-               char buf[INET6_ADDRSTRLEN] = { 0, };
-               size_t len;
-               const char *addr = str;
-               const char *idxs = NULL;
-               unsigned int idx = 0;
-               struct in6_addr ip6;
-
-               p = strchr_m(str, SCOPE_DELIMITER);
-               if (p && (p > str)) {
-                       len = PTR_DIFF(p, str);
-                       idxs = p + 1;
-               } else {
-                       len = strlen(str);
-               }
+       }
 
-               if (len >= sizeof(buf)) {
-                       return false;
-               }
-               if (idxs != NULL) {
-                       strncpy(buf, str, len);
-                       addr = buf;
-               }
+       p = strchr_m(str, SCOPE_DELIMITER);
+       if (p && (p > str)) {
+               len = PTR_DIFF(p, str);
+               idxs = p + 1;
+       } else {
+               len = strlen(str);
+       }
 
-               /*
-                * Cope with link-local.
-                * This is IP:v6:addr%ifidx.
-                */
-               if (idxs != NULL) {
-                       char c;
+       if (len >= sizeof(buf)) {
+               return false;
+       }
+       if (idxs != NULL) {
+               strncpy(buf, str, len);
+               addr = buf;
+       }
 
-                       ret = sscanf(idxs, "%5u%c", &idx, &c);
-                       if (ret != 1) {
-                               idx = 0;
-                       }
+       /*
+        * Cope with link-local.
+        * This is IP:v6:addr%ifidx.
+        */
+       if (idxs != NULL) {
+               char c;
 
-                       if (idx > 0 && idx < UINT16_MAX) {
-                               /* a valid index */
-                               idxs = NULL;
-                       }
+               ret = sscanf(idxs, "%5u%c", &idx, &c);
+               if (ret != 1) {
+                       idx = 0;
                }
 
-               /*
-                * Cope with link-local.
-                * This is IP:v6:addr%ifname.
-                */
-               if (idxs != NULL) {
-                       idx = if_nametoindex(idxs);
-
-                       if (idx > 0) {
-                               /* a valid index */
-                               idxs = NULL;
-                       }
+               if (idx > 0 && idx < UINT16_MAX) {
+                       /* a valid index */
+                       idxs = NULL;
                }
+       }
 
-               if (idxs != NULL) {
-                       return false;
-               }
+       /*
+        * Cope with link-local.
+        * This is IP:v6:addr%ifname.
+        */
+       if (idxs != NULL) {
+               idx = if_nametoindex(idxs);
 
-               ret = inet_pton(AF_INET6, addr, &ip6);
-               if (ret <= 0) {
-                       return false;
+               if (idx > 0) {
+                       /* a valid index */
+                       idxs = NULL;
                }
+       }
 
-               return true;
+       if (idxs != NULL) {
+               return false;
+       }
+
+       ret = inet_pton(AF_INET6, addr, &ip6);
+       if (ret <= 0) {
+               return false;
        }
+
+       return true;
 #endif
        return false;
 }