From fa6df98ced1dde9d07dd4fe286f6b268f401141e Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 11 Jun 2011 15:15:39 +0000 Subject: [PATCH] Replace ipv?_is_valid by ipcalc command. The ipcalc command is much faster and safer than the shell code. --- functions.ipv4 | 42 ++++++++---------------------------------- functions.ipv6 | 35 ++++++++--------------------------- 2 files changed, 16 insertions(+), 61 deletions(-) diff --git a/functions.ipv4 b/functions.ipv4 index 633c5eb4..678b1e73 100644 --- a/functions.ipv4 +++ b/functions.ipv4 @@ -22,42 +22,16 @@ 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() { diff --git a/functions.ipv6 b/functions.ipv6 index 87e450eb..5ec4494a 100644 --- a/functions.ipv6 +++ b/functions.ipv6 @@ -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() { -- 2.47.2