From patchwork Thu Mar 1 03:31:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonatan Schlag X-Patchwork-Id: 1679 Return-Path: Received: from mail01.ipfire.org (unknown [172.28.1.200]) by web02.i.ipfire.org (Postfix) with ESMTP id 4612E60B0F for ; Wed, 28 Feb 2018 17:31:36 +0100 (CET) X-Virus-Scanned: ClamAV at mail01.ipfire.org Received: from mail01.i.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id 766EF111C4E3; Wed, 28 Feb 2018 16:31:57 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ipfire.org; s=201801; t=1519835517; x=1522427517; bh=HH9Cer5/2lLV0/P3JZXsdBDAfq7iRcgv4mw6qNdg0R8=; h=From:To:Subject:Date:Message-Id:Sender:From:To:Cc:Date: Content-Type:Message-ID:In-Reply-To:Subject:Reply-To:Sender; b=PPj1+vR4O96Rjpz1CHb5EH+8GIEJC/ZqEB2KCpnhRB3EF8ihgKIqlUYJ6ZvBSBiYL xIBdBvuDoU/AKQNv5th+cTzzBoL1a6ZSvUBGI6XRwexY6yNcghuVGbqxeDoVeIVKdf VI4HbOyL2xUISFyNfdyJbXgU1u8I3f+bqj/3yS9U+c13YDxYnnXsTyg4CBtztJxOZK kGsXdoQ3E4cGpHzLb31u42hz9IirUzRnFtjZQENO7yqDfHGl8RXXaz2m1LtrvTUATw ZXLojkjMSpKQqu82PtFTVrJRlWBjvWWnzObLAoWP5KsL1JmDgkWXvt7H5jIoSLJCuG kBpx5qj43NolQ== X-Virus-Scanned: ClamAV at mail01.ipfire.org Received: from localhost.localdomain (unknown [10.172.1.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPSA id D0C661063E65; Wed, 28 Feb 2018 16:31:54 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ipfire.org; s=201801; t=1519835515; x=1522427515; bh=HH9Cer5/2lLV0/P3JZXsdBDAfq7iRcgv4mw6qNdg0R8=; h=From:To:Cc:Subject:Date:Message-Id:From:To:Cc:Date:Content-Type: Message-ID:In-Reply-To:Subject:Reply-To:Sender; b=q+Fj789szwpKSyRMVrSPq5tDpJf9SnQkM9FQ8D/GOuBwksaZ8MQfU49IdWmbZ/CD+ GKUhgdwEenBAnVtf/SQf98HtF7e2LJMkV4VpITRwv9qQc5Q5Sc6SGXmGQZ91cTUca0 o/Q4C6q6TZoJvL2WBp2FZT417cI3eyGRqjFJBpoCkAEnhiEG0st8Hg5D3ihjE5Qnul 98gJFP80eY48C016R7XbclhnAwmJc22Osvo/P/EgUfjC3mtrbfN85oAzBhNv8K3w5P +YTpH1sHeA28AjIUwZqeMboMOob0QxRMYtjvYiUJJ5seR0M4sPazUYB/OdQ/J18rcd TpVlkvGj70SRg== From: Jonatan Schlag To: network@lists.ipfire.org Subject: [PATCH v2] Add new function ip_get__assigned_addresses_from_net() Date: Wed, 28 Feb 2018 16:31:27 +0000 Message-Id: <1519835487-3506-1-git-send-email-jonatan.schlag@ipfire.org> X-Mailer: git-send-email 2.6.3 X-BeenThere: network@lists.ipfire.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List for the network package List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: network-bounces@lists.ipfire.org Sender: "network" 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 --- 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} +}