]> git.ipfire.org Git - people/stevee/network.git/commitdiff
ip_is_valid: Refactor functions
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Tue, 30 May 2017 09:26:16 +0000 (11:26 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Tue, 30 May 2017 09:26:16 +0000 (11:26 +0200)
These now check more precisely if a prefix that is given with
an IP address is valid, too

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
src/functions/functions.ip
src/functions/functions.ipv4
src/functions/functions.ipv6

index e938a5355b475078067472274a5045d340394992..69d0c512cdf79a648bef3d37ec9797b983034ebb 100644 (file)
@@ -69,8 +69,14 @@ ip_is_valid() {
        local address=${1}
        assert isset address
 
-       local proto=$(ip_detect_protocol ${address})
-       isset proto && return ${EXIT_TRUE} || return ${EXIT_FALSE}
+       local protocol
+       for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
+               if ${protocol}_is_valid "${address}"; then
+                       return ${EXIT_TRUE}
+               fi
+       done
+
+       return ${EXIT_FALSE}
 }
 
 ip_is_network() {
index 961358882b0478102c7c6f0f105b7593639a33bd..f63c2b7e4ab2fb904ff6f091c8b182ae54c9dd9c 100644 (file)
 IP_SUPPORTED_PROTOCOLS="${IP_SUPPORTED_PROTOCOLS} ipv4"
 
 ipv4_is_valid() {
-       inetcalc -4 -c $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
+       local address=${1}
+
+       local prefix=$(ip_get_prefix ${address})
+       address=$(ip_split_prefix ${address})
+
+       # If prefix is set, we check if it is correct
+       if isset prefix; then
+               # Must be numeric
+               isinteger prefix || return ${EXIT_FALSE}
+
+               # Must be 32 if present
+               [ ${prefix} -eq 32 ] || return ${EXIT_FALSE}
+       fi
+
+       inetcalc -4 -c ${address} && return ${EXIT_TRUE} || return ${EXIT_FALSE}
 }
 
 ipv4_prefix_is_valid() {
index 9f1b2a939bf50ea69fa5985adbf8d61e212d2af3..3d22b4d16281d32b6723a084b7d2c9465447403c 100644 (file)
@@ -105,7 +105,21 @@ ipv6_device_privacy_extensions_disable() {
 }
 
 ipv6_is_valid() {
-       inetcalc -6 -c $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
+       local address=${1}
+
+       local prefix=$(ip_get_prefix ${address})
+       address=$(ip_split_prefix ${address})
+
+       # If prefix is set, we check if it is correct
+       if isset prefix; then
+               # Must be numeric
+               isinteger prefix || return ${EXIT_FALSE}
+
+               # Must be 128 if present
+               [ ${prefix} -eq 128 ] || return ${EXIT_FALSE}
+       fi
+
+       inetcalc -6 -c ${address} && return ${EXIT_TRUE} || return ${EXIT_FALSE}
 }
 
 ipv6_net_is_valid() {