[v2] Add new function ip_get__assigned_addresses_from_net()

Message ID 1519835487-3506-1-git-send-email-jonatan.schlag@ipfire.org
State Accepted
Commit 1ad09828e16a559468c1e9f7491c7bd795df7e88
Headers
Series [v2] Add new function ip_get__assigned_addresses_from_net() |

Commit Message

Jonatan Schlag March 1, 2018, 3:31 a.m. UTC
  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>
---
 src/functions/functions.ip | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
  

Comments

Michael Tremer March 1, 2018, 4:21 a.m. UTC | #1
Okay, I will merge this one.

Best,
-Michael

On Wed, 2018-02-28 at 16:31 +0000, Jonatan Schlag wrote:
> 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>
> ---
>  src/functions/functions.ip | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
> 
> diff --git a/src/functions/functions.ip b/src/functions/functions.ip
> index 3b43da7..70bd92c 100644
> --- a/src/functions/functions.ip
> +++ b/src/functions/functions.ip
> @@ -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}
> +}
  
Michael Tremer March 2, 2018, 7:58 a.m. UTC | #2
All merged.

On Wed, 2018-02-28 at 17:21 +0000, Michael Tremer wrote:
> Okay, I will merge this one.
> 
> Best,
> -Michael
> 
> On Wed, 2018-02-28 at 16:31 +0000, Jonatan Schlag wrote:
> > 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>
> > ---
> >  src/functions/functions.ip | 28 ++++++++++++++++++++++++++++
> >  1 file changed, 28 insertions(+)
> > 
> > diff --git a/src/functions/functions.ip b/src/functions/functions.ip
> > index 3b43da7..70bd92c 100644
> > --- a/src/functions/functions.ip
> > +++ b/src/functions/functions.ip
> > @@ -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}
> > +}
  

Patch

diff --git a/src/functions/functions.ip b/src/functions/functions.ip
index 3b43da7..70bd92c 100644
--- a/src/functions/functions.ip
+++ b/src/functions/functions.ip
@@ -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}
+}