]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.ipv4
Remove the function keyword which is a bashism
[people/stevee/network.git] / src / functions / functions.ipv4
index d7a6b85a71184093b8989de06670613ce0ac7046..d22b25f6c00ddddd38abec9dfe3c8b73282126ac 100644 (file)
@@ -21,7 +21,7 @@
 
 IP_SUPPORTED_PROTOCOLS="${IP_SUPPORTED_PROTOCOLS} ipv4"
 
-function ipv4_is_valid() {
+ipv4_is_valid() {
        ipcalc --ipv4 -c $@ >/dev/null 2>&1
 
        case "$?" in
@@ -34,7 +34,7 @@ function ipv4_is_valid() {
        esac
 }
 
-function ipv4_prefix_is_valid() {
+ipv4_prefix_is_valid() {
        local prefix=${1}
 
        isset prefix || return ${EXIT_FALSE}
@@ -45,7 +45,16 @@ function ipv4_prefix_is_valid() {
        return ${EXIT_TRUE}
 }
 
-function ipv4_detect_duplicate() {
+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}"
+}
+
+ipv4_detect_duplicate() {
        local device=${1}
        local address=${2}
 
@@ -64,7 +73,7 @@ function ipv4_detect_duplicate() {
        return ${EXIT_ERROR}
 }
 
-function ipv4_update_neighbours() {
+ipv4_update_neighbours() {
        local device=${1}
        local address=${2}
 
@@ -75,7 +84,7 @@ function ipv4_update_neighbours() {
        ( sleep 2; arping -q -U -c 1 -I ${device} ${address} ) >/dev/null 2>&1 </dev/null &
 }
 
-function ipv4_get_netaddress() {
+ipv4_get_netaddress() {
        local address=${1}
        assert isset address
 
@@ -96,7 +105,7 @@ function ipv4_get_netaddress() {
        return ${EXIT_OK}
 }
 
-function ipv4_get_prefix() {
+ipv4_get_prefix() {
        local address=${1}
        local broadcast=${2}
 
@@ -111,7 +120,7 @@ function ipv4_get_prefix() {
        return ${EXIT_OK}
 }
 
-function ipv4_get_netmask() {
+ipv4_get_netmask() {
        local address=${1}
        assert isset address
 
@@ -127,7 +136,7 @@ function ipv4_get_netmask() {
        return ${EXIT_OK}
 }
 
-function ipv4_flush_device() {
+ipv4_flush_device() {
        #
        # Flushes all routes, addresses from the device
        # and clears the ARP cache.
@@ -143,7 +152,7 @@ function ipv4_flush_device() {
        return 0
 }
 
-function ipv4_prefix2netmask() {
+ipv4_prefix2netmask() {
        local prefix=${1}
        shift
 
@@ -161,13 +170,37 @@ function ipv4_prefix2netmask() {
        esac
 }
 
-function ipv4_get_network() {
-       local network=$(ipv4_get_network $@)
+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}"
+}
+
+ipv4_get_network() {
+       local network=$(ipv4_get_network_encoded $@)
 
        ipv4_decode ${network}
 }
 
-function ipv4_get_network_encoded() {
+ipv4_get_network_encoded() {
        local net=${1}
 
        local prefix=$(ip_get_prefix ${net})
@@ -184,13 +217,13 @@ function ipv4_get_network_encoded() {
        print "%d" $(( ${addr} & ${mask} ))
 }
 
-function ipv4_get_broadcast() {
+ipv4_get_broadcast() {
        local broadcast=$(ipv4_get_broadcast_encoded $@)
 
        ipv4_decode ${broadcast}
 }
 
-function ipv4_get_broadcast_encoded() {
+ipv4_get_broadcast_encoded() {
        local net=${1}
 
        local prefix=$(ip_get_prefix ${net})
@@ -213,7 +246,7 @@ function ipv4_get_broadcast_encoded() {
        print "%d" $(( $(( ${addr} & ${netmask} )) | ${broadcast} ))
 }
 
-function ipv4_encode() {
+ipv4_encode() {
        local addr=${1}
        local int=0
 
@@ -225,7 +258,7 @@ function ipv4_encode() {
        print "${int}"
 }
 
-function ipv4_decode() {
+ipv4_decode() {
        local int=${1}
 
        local addr=$(( ${int} & 255 ))
@@ -239,7 +272,7 @@ function ipv4_decode() {
        print "${addr}"
 }
 
-function ipv4_addr_eq() {
+ipv4_addr_eq() {
        local addr1=${1}
        assert isset addr1
 
@@ -250,7 +283,7 @@ function ipv4_addr_eq() {
                && return ${EXIT_TRUE} || return ${EXIT_FALSE}
 }
 
-function ipv4_addr_gt() {
+ipv4_addr_gt() {
        local addr1=${1}
        assert isset addr1
 
@@ -266,7 +299,7 @@ function ipv4_addr_gt() {
                && return ${EXIT_TRUE} || return ${EXIT_FALSE}
 }
 
-function ipv4_range() {
+ipv4_range() {
        local range=${1}
 
        local first=${1%-*}
@@ -275,7 +308,7 @@ function ipv4_range() {
        _ipv4_range "$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
 }
 
-function _ipv4_range() {
+_ipv4_range() {
        local first=${1}
        local last=${2}
 
@@ -308,7 +341,7 @@ function _ipv4_range() {
        done
 }
 
-function ipv4_range_explicit() {
+ipv4_range_explicit() {
        local range=${1}
 
        local first last
@@ -327,7 +360,7 @@ function ipv4_range_explicit() {
        _ipv4_range_explicit "$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
 }
 
-function _ipv4_range_explicit() {
+_ipv4_range_explicit() {
        local first=${1}
        local last=${2}
 
@@ -344,7 +377,7 @@ function _ipv4_range_explicit() {
        done
 }
 
-function ipv4_in_subnet() {
+ipv4_in_subnet() {
        local addr=${1}
        assert isset addr
 
@@ -363,7 +396,7 @@ function ipv4_in_subnet() {
        return ${EXIT_FALSE}
 }
 
-function ipv4_ttl_valid() {
+ipv4_ttl_valid() {
        local ttl="${1}"
 
        isinteger ttl || return ${EXIT_FALSE}