From: Jonatan Schlag Date: Tue, 30 May 2017 09:26:16 +0000 (+0200) Subject: ip_is_valid: Refactor functions X-Git-Tag: 009~250^2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=76b0e7fc9bc35103d2caa5578a643fe030cbcb36;p=network.git ip_is_valid: Refactor functions These now check more precisely if a prefix that is given with an IP address is valid, too Signed-off-by: Jonatan Schlag --- diff --git a/src/functions/functions.ip b/src/functions/functions.ip index e938a535..69d0c512 100644 --- a/src/functions/functions.ip +++ b/src/functions/functions.ip @@ -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() { diff --git a/src/functions/functions.ipv4 b/src/functions/functions.ipv4 index 96135888..f63c2b7e 100644 --- a/src/functions/functions.ipv4 +++ b/src/functions/functions.ipv4 @@ -22,7 +22,21 @@ 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() { diff --git a/src/functions/functions.ipv6 b/src/functions/functions.ipv6 index 9f1b2a93..3d22b4d1 100644 --- a/src/functions/functions.ipv6 +++ b/src/functions/functions.ipv6 @@ -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() {