]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.ipv4
ipv4-static: Update hook
[people/stevee/network.git] / src / functions / functions.ipv4
index d7a6b85a71184093b8989de06670613ce0ac7046..067b87c5110fb5ec644812eab5ae99f75b51464f 100644 (file)
@@ -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 $@)