X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=src%2Ffunctions%2Ffunctions.ipv4;h=067b87c5110fb5ec644812eab5ae99f75b51464f;hb=ccbc0dd493ed72bfd7174855041201ff7c4239ac;hp=d7a6b85a71184093b8989de06670613ce0ac7046;hpb=ea699552edea8032ddfbc2ef8c6e176b1010eeb4;p=people%2Fms%2Fnetwork.git diff --git a/src/functions/functions.ipv4 b/src/functions/functions.ipv4 index d7a6b85a..067b87c5 100644 --- a/src/functions/functions.ipv4 +++ b/src/functions/functions.ipv4 @@ -45,6 +45,15 @@ function ipv4_prefix_is_valid() { return ${EXIT_TRUE} } +function ipv4_netmask_is_valid() { + local netmask="${1}" + + # XXX this check could be much better by checking + # if the netmask only contains leading ones + + ipv4_is_valid "${netmask}" +} + function ipv4_detect_duplicate() { local device=${1} local address=${2} @@ -161,6 +170,30 @@ function ipv4_prefix2netmask() { esac } +function ipv4_netmask2prefix() { + local netmask="${1}" + assert isset netmask + + local mask=0 + + local field + for field in ${netmask//\./ }; do + mask=$(( $(( ${mask} << 8 )) | ${field} )) + done + + local cidr=0 + local x="$(( 128 << 24 ))" # 0x80000000 + + while [ $(( ${x} & ${mask} )) -ne 0 ]; do + [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 )) + cidr=$(( ${cidr} + 1 )) + done + + assert [ $(( ${mask} & 2147483647 )) -eq 0 ] + + print "${cidr}" +} + function ipv4_get_network() { local network=$(ipv4_get_network $@)