]> git.ipfire.org Git - network.git/commitdiff
Add new function ip_get__assigned_addresses_from_net()
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Wed, 28 Feb 2018 16:31:27 +0000 (16:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Mar 2018 14:51:31 +0000 (14:51 +0000)
This function is neede by IPsec to set the routes correctly.
We can now now find a source IP for a given net.
This way is ugly because the source IP
is unpredictable if we get multiple IPs.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.ip

index 3b43da77a3a39b367587cb8347e0cd939779e7cb..70bd92c036c95835a37c74f8d311aa1882730d07 100644 (file)
@@ -205,3 +205,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}
+}