}
ip_detect_protocol() {
- local address=${1}
-
+ local address="${1}"
assert isset address
+ # Remove prefix so that we can handle subnet, too
+ address=$(ip_split_prefix ${address})
+
local protocol
for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
- if ${protocol}_is_valid ${address}; then
+ if ${protocol}_is_valid "${address}"; then
echo "${protocol}"
return ${EXIT_OK}
fi
assert isset proto
- listmatch ${proto} ${IP_SUPPORTED_PROTOCOLS}
+ list_match ${proto} ${IP_SUPPORTED_PROTOCOLS}
+}
+
+# Returns true if all IP addresses are of the same protocol
+ip_protocol_match() {
+ local address="${1}"
+ shift
+
+ # Get protocol of the first address
+ local protocol="$(ip_detect_protocol "${address}")"
+
+ # Check if all other addresses match the protocol
+ for address in $@; do
+ local p="$(ip_detect_protocol "${address}")"
+
+ if [ "${p}" != "${protocol}" ]; then
+ return ${EXIT_FALSE}
+ fi
+ done
+
+ return ${EXIT_TRUE}
}
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() {
+ip_net_is_valid() {
local network=${1}
assert isset network
- # Get the address part.
- local address=$(ip_split_prefix ${network})
- isset address || return ${EXIT_FALSE}
-
- # Get the prefix.
- local prefix=$(ip_get_prefix ${network})
- isset prefix || return ${EXIT_FALSE}
-
- # Detect the protocol.
- local proto=$(ip_detect_protocol ${address})
- assert isset proto
-
- # Check if the prefix is correct.
- ip_prefix_is_valid ${proto} ${prefix} || return ${EXIT_FALSE}
+ local protocol
+ for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
+ if ${protocol}_net_is_valid "${network}"; then
+ return ${EXIT_TRUE}
+ fi
+ done
- return ${EXIT_TRUE}
+ return ${EXIT_FALSE}
}
ip_prefix_is_valid() {
assert ip_protocol_is_supported ${proto}
}
+ip_get_network() {
+ inetcalc -n "$@" && return ${EXIT_OK} || return ${EXIT_ERROR}
+}
+
+ip_network_is_subset_of() {
+ assert [ $# -eq 2 ]
+
+ inetcalc -s "$@" && return ${EXIT_TRUE} || return ${EXIT_FALSE}
+}
+
ip_address_add() {
local device=${1}
local address=${2}
address=$(ip_split_prefix ${address})
assert isset prefix
+ assert isset address
+
+ echo "ADDRESS = $address"
# Detect the protocol version
- local protocol=$(ip_detect_protocol ${address}/${prefix})
- assert ip_protocol_is_supported ${protocol}
+ local protocol=$(ip_detect_protocol "${address}")
+ assert ip_protocol_is_supported "${protocol}"
+
+ case "${protocol}" in
+ ipv6)
+ assert ipv6_prefix_is_valid "${prefix}"
+ ;;
+ ipv4)
+ assert ipv4_prefix_is_valid "${prefix}"
+ ;;
+ esac
case "${protocol}" in
ipv4)
assert isset prefix
# Detect the protocol version
- local protocol=$(ip_detect_protocol ${address}/${prefix})
- assert ip_protocol_is_supported ${protocol}
+ local protocol=$(ip_detect_protocol "${address}")
+ assert ip_protocol_is_supported "${protocol}"
if device_has_ip ${device} ${address}/${prefix}; then
assert ip addr del ${address}/${prefix} dev ${device}
return ${EXIT_OK}
}
+
+# Get all currently assigned addresse for a given network
+ip_get_assigned_addresses_from_net() {
+ local net=${1}
+ shift
+ local args="$@"
+
+ if ! ip_net_is_valid ${net}; then
+ log ERROR "IP net ${net} is invalid"
+ return ${EXIT_ERROR}
+ fi
+
+ local line
+ local addresses
+
+ # We read the output of $(ip addr show to ${net} ${args})
+ while read -r line; do
+ # We are only interested in lines which start with inet or inet6
+ [[ "${line}" =~ ^(inet6 |inet ) ]] || continue
+
+ # We need the second word the line
+ line=(${line})
+ list_append "addresses" "$(ip_split_prefix "${line[1]}")"
+ done <<< "$(ip addr show to "${net}" ${args})"
+
+ # We sort the list to get the lowest IP as first item
+ list_sort ${addresses}
+}