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>
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() {
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() {
}
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() {