Skip to content

Commit

Permalink
base-files: ipcalc.sh: Add prefix-to-netmask conversion
Browse files Browse the repository at this point in the history
Seems like it might be used in other places, so factor it into the
library.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
  • Loading branch information
pprindeville committed Nov 12, 2023
1 parent 753dd63 commit 55bae43
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package/base-files/files/bin/ipcalc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ case "$1" in
echo "Prefix out of range ($n)" >&2
exit 1
fi
netmask=$((~((1 << (32 - n)) - 1) & 0xffffffff))
netmask=$(prefix2netmask "$n") || exit 1
shift
;;
*)
Expand Down
9 changes: 9 additions & 0 deletions package/base-files/files/lib/functions/ipv4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,12 @@ int2ip() {
echo "$((n >> 24)).$(((n >> 16) & 255)).$(((n >> 8) & 255)).$((n & 255))"
}

# convert prefix into an integer bitmask
prefix2netmask() {
local n="$(check_uint32 "$1")"

[ "$n" -gt 32 ] && __die "Prefix out-of-range ($n)"

echo "$(((~(uint_max >> n)) & uint_max))"
}

0 comments on commit 55bae43

Please sign in to comment.