]> git.ipfire.org Git - people/ms/network.git/commitdiff
Replace ipv?_is_valid by ipcalc command.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jun 2011 15:15:39 +0000 (15:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jun 2011 15:15:39 +0000 (15:15 +0000)
The ipcalc command is much faster and safer than
the shell code.

functions.ipv4
functions.ipv6

index 633c5eb4cb1e503e84a8af3e6334c5a38efebbfc..678b1e7334287d76094bc62631802b8d40e6a9be 100644 (file)
 IP_SUPPORTED_PROTOCOLS="${IP_SUPPORTED_PROTOCOLS} ipv4"
 
 function ipv4_is_valid() {
-       local address=${1}
+       ipcalc --ipv4 -c $@ >/dev/null 2>&1
 
-       assert isset address
-
-       # Cut the prefix if there is one given
-       local prefix=$(ip_get_prefix ${address})
-       address=$(ip_split_prefix ${address})
-
-       # If address is larger than 15 characters it cannot be an IPv4 address
-       [ ${#address} -gt 15 ] && return ${EXIT_ERROR}
-
-       # Check for a valid IPv4 prefix if provided
-       if [ -n "${prefix}" ]; then
-               if [ ${prefix} -lt 4 ] || [ ${prefix} -gt 30 ]; then
+       case "$?" in
+               0)
+                       return ${EXIT_OK}
+                       ;;
+               *)
                        return ${EXIT_ERROR}
-               fi
-       fi
-
-       local IFS="."
-       local octet
-       local count
-       for octet in ${address}; do
-               if [ ${octet} -ge 0 ] && [ ${octet} -le 255 ]; then
-                       count=$(( ${count} + 1 ))
-                       continue
-               fi
-
-               # If we get here the address was not valid
-               break
-       done
-
-       if [ ${count} -eq 4 ]; then
-               return ${EXIT_OK}
-       fi
-
-       return ${EXIT_ERROR}
+                       ;;
+       esac
 }
 
 function ipv4_detect_duplicate() {
index 87e450eb5cd71095f6e86030746b3fbe85a99c07..5ec4494a4d5360d046d57336cd6a11a3296e4de2 100644 (file)
@@ -138,35 +138,16 @@ function ipv6_device_privacy_extensions_disable() {
 }
 
 function ipv6_is_valid() {
-       local address=${1}
-
-       assert isset address
-
-       local prefix=$(ip_get_prefix ${address})
-       address=$(ip_split_prefix ${address})
-
-       # Check length
-       [ ${#address} -gt 39 ] && return ${EXIT_ERROR}
+       ipcalc --ipv6 -c $@ >/dev/null 2>&1
 
-       # Check prefix if provided
-       if [ -n "${prefix}" ]; then
-               # XXX need to check was largest prefix is
-               if [ ${prefix} -lt 0 ] && [ ${prefix} -gt 64 ]; then
+       case "$?" in
+               0)
+                       return ${EXIT_OK}
+                       ;;
+               *)
                        return ${EXIT_ERROR}
-               fi
-       fi
-
-       # XXX find :: twice?
-       # XXX check for documentation prefix?
-
-       # Check for bad characters
-       local char
-       for char in 0 1 2 3 4 5 6 7 8 9 a b c d e f :; do
-               address=${address//${char}/}
-       done
-       [ -n "${address}" ] && return ${EXIT_ERROR}
-
-       return ${EXIT_OK}
+                       ;;
+       esac
 }
 
 function ipv6_implode() {