aboutsummaryrefslogtreecommitdiff
path: root/package/base-files/files/lib
diff options
context:
space:
mode:
authorPhilip Prindeville <philipp@redfish-solutions.com>2023-11-03 21:11:49 -0600
committerPhilip Prindeville <philipp@redfish-solutions.com>2023-12-12 12:30:35 -0700
commit6cdc429a484392afb4ca7c8a32355987d9e0cc73 (patch)
tree96ba38ee72ba6cf03498613f8e9ef0ec377009bd /package/base-files/files/lib
parentf0612c0d844505e214f0569285d4cafe4016a1ec (diff)
base-files: ipcalc.sh: Add prefix-to-netmask conversion
Seems like it might be used in other places, so factor it into the library. Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Diffstat (limited to 'package/base-files/files/lib')
-rw-r--r--package/base-files/files/lib/functions/ipv4.sh13
1 files changed, 13 insertions, 0 deletions
diff --git a/package/base-files/files/lib/functions/ipv4.sh b/package/base-files/files/lib/functions/ipv4.sh
index e12f6f56a7..9405a63552 100644
--- a/package/base-files/files/lib/functions/ipv4.sh
+++ b/package/base-files/files/lib/functions/ipv4.sh
@@ -144,3 +144,16 @@ ip2str() {
export -- "$__var=$((__n >> 24)).$(((__n >> 16) & 255)).$(((__n >> 8) & 255)).$((__n & 255))"
}
+# convert prefix into an integer bitmask
+prefix2netmask() {
+ local __var="$1" __n="$2"
+ assert_uint32 "$__n" || return 1
+
+ if [ "$__n" -gt 32 ]; then
+ printf "Prefix out-of-range (%s)" "$__n" >&2
+ return 1
+ fi
+
+ export -- "$__var=$(((~(uint_max >> __n)) & uint_max))"
+}
+