]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.ip
Refactor network-hotplug-rename
[people/ms/network.git] / src / functions / functions.ip
index 69d0c512cdf79a648bef3d37ec9797b983034ebb..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() {
@@ -79,26 +99,18 @@ 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() {
@@ -122,7 +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}
 }
 
 ip_address_add() {
@@ -207,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}
+}