mirror of
git://git.openwrt.org/openwrt/openwrt.git
synced 2025-01-01 11:52:32 +00:00
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>
This commit is contained in:
parent
f0612c0d84
commit
6cdc429a48
@ -55,7 +55,7 @@ case "$1" in
|
||||
printf "Prefix out of range (%s)\n" "$prefix" >&2
|
||||
exit 1
|
||||
fi
|
||||
netmask=$(((0xffffffff << (32 - prefix)) & 0xffffffff))
|
||||
prefix2netmask netmask "$prefix" || exit 1
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
|
@ -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))"
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user