]> git.ipfire.org Git - network.git/blobdiff - src/functions/functions.ip
hook: Rename HOOK_CONFIG_SETTINGS to HOOK_SETTINGS
[network.git] / src / functions / functions.ip
index 5a82114119d774dfc40f8838d52becd1b1d3d249..f20a3d7adb15e458acb026d0749ae37ef7c22bf7 100644 (file)
@@ -62,7 +62,27 @@ ip_protocol_is_supported() {
 
        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() {
@@ -114,13 +134,13 @@ 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}
+       inetcalc -s "$@" && return ${EXIT_TRUE} || return ${EXIT_FALSE}
 }
 
 ip_address_add() {
@@ -205,3 +225,31 @@ ip_address_del() {
 
        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}
+}