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() {
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() {
}
ip_get_network() {
- inetcalc -n $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
+ 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() {
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}
+}