]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.ipv4
hostapd: Enable WMM by default.
[people/ms/network.git] / functions.ipv4
index aada214a90fd490f19b0a8e48bbd5c47ba78625b..d7a6b85a71184093b8989de06670613ce0ac7046 100644 (file)
@@ -34,6 +34,17 @@ function ipv4_is_valid() {
        esac
 }
 
+function ipv4_prefix_is_valid() {
+       local prefix=${1}
+
+       isset prefix || return ${EXIT_FALSE}
+
+       [ ${prefix} -le  0 ] && return ${EXIT_FALSE}
+       [ ${prefix} -gt 32 ] && return ${EXIT_FALSE}
+
+       return ${EXIT_TRUE}
+}
+
 function ipv4_detect_duplicate() {
        local device=${1}
        local address=${2}
@@ -100,6 +111,22 @@ function ipv4_get_prefix() {
        return ${EXIT_OK}
 }
 
+function ipv4_get_netmask() {
+       local address=${1}
+       assert isset address
+
+       # Add prefix if none given.
+       local prefix=$(ip_get_prefix ${address})
+       isset prefix || address="${address}/32"
+
+       local NETMASK
+       eval $(ipcalc --netmask ${address})
+       assert isset NETMASK
+
+       print "${NETMASK}"
+       return ${EXIT_OK}
+}
+
 function ipv4_flush_device() {
        #
        # Flushes all routes, addresses from the device
@@ -133,3 +160,217 @@ function ipv4_prefix2netmask() {
                        ;;
        esac
 }
+
+function ipv4_get_network() {
+       local network=$(ipv4_get_network $@)
+
+       ipv4_decode ${network}
+}
+
+function ipv4_get_network_encoded() {
+       local net=${1}
+
+       local prefix=$(ip_get_prefix ${net})
+       isset prefix || prefix=32
+
+       local mask=0
+       if [ ${prefix} -ne 0 ]; then
+               mask=$(( -1 << $(( 32 - ${prefix} )) ))
+       fi
+
+       local addr=$(ip_split_prefix ${net})
+       addr=$(ipv4_encode ${addr})
+
+       print "%d" $(( ${addr} & ${mask} ))
+}
+
+function ipv4_get_broadcast() {
+       local broadcast=$(ipv4_get_broadcast_encoded $@)
+
+       ipv4_decode ${broadcast}
+}
+
+function ipv4_get_broadcast_encoded() {
+       local net=${1}
+
+       local prefix=$(ip_get_prefix ${net})
+       assert isset prefix
+
+       prefix=$(( 32 - ${prefix} ))
+
+       local netmask=0
+       local broadcast=-1
+       if [ ${prefix} -eq 32 ]; then
+               :
+       else
+               netmask=$(( -1 << ${prefix} ))
+               broadcast=$(( $(( 1 << ${prefix} )) - 1))
+       fi
+
+       local addr=$(ip_split_prefix ${net})
+       addr=$(ipv4_encode ${addr})
+
+       print "%d" $(( $(( ${addr} & ${netmask} )) | ${broadcast} ))
+}
+
+function ipv4_encode() {
+       local addr=${1}
+       local int=0
+
+       local field
+       for field in ${addr//./ }; do
+               int=$(( $(( ${int} << 8 )) | ${field} ))
+       done
+
+       print "${int}"
+}
+
+function ipv4_decode() {
+       local int=${1}
+
+       local addr=$(( ${int} & 255 ))
+
+       local i
+       for i in 1 2 3; do
+               int=$(( ${int} >> 8 ))
+               addr="$(( ${int} & 255 )).${addr}"
+       done
+
+       print "${addr}"
+}
+
+function ipv4_addr_eq() {
+       local addr1=${1}
+       assert isset addr1
+
+       local addr2=${2}
+       assert isset addr2
+
+       [[ "${addr1}" = "${addr2}" ]] \
+               && return ${EXIT_TRUE} || return ${EXIT_FALSE}
+}
+
+function ipv4_addr_gt() {
+       local addr1=${1}
+       assert isset addr1
+
+       local addr2=${2}
+       assert isset addr2
+
+       local addr
+       for addr in addr1 addr2; do
+               printf -v ${addr} "%s" "$(ip_encode ${!addr})"
+       done
+
+       [[ ${addr1} -gt ${addr2} ]] \
+               && return ${EXIT_TRUE} || return ${EXIT_FALSE}
+}
+
+function ipv4_range() {
+       local range=${1}
+
+       local first=${1%-*}
+       local last=${1#*-}
+
+       _ipv4_range "$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
+}
+
+function _ipv4_range() {
+       local first=${1}
+       local last=${2}
+
+       if [ ${first} -gt ${last} ]; then
+               local range="$(ipv4_decode ${first})-$(ipv4_decode ${last})"
+
+               error "Invalid IPv4 address range: ${range}"
+               return ${EXIT_ERROR}
+       fi
+
+       last=$(( ${last} + 1 ))
+
+       local prefix
+       local x y z
+       while [ ${last} -gt ${first} ]; do
+               prefix=
+               x=31
+               y=2
+               z=1
+
+               while [ $(( ${first} % ${y} )) -eq 0 ] && [ ${last} -gt $(( ${first} + ${y} )) ]; do
+                       prefix="/${x}"
+                       x=$(( ${x} - 1 ))
+                       z=${y}
+                       y=$(( ${y} * 2 ))
+               done
+
+               print "$(ipv4_decode ${first})${prefix}"
+               first=$(( ${first} + ${z} ))
+       done
+}
+
+function ipv4_range_explicit() {
+       local range=${1}
+
+       local first last
+
+       case "${range}" in
+               *.*.*.*-*.*.*.*)
+                       first=${range%-*}
+                       last=${range#*-}
+                       ;;
+               *.*.*.*/*)
+                       first=$(ipv4_get_network ${range})
+                       last=$(ipv4_get_broadcast ${range})
+                       ;;
+       esac
+
+       _ipv4_range_explicit "$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
+}
+
+function _ipv4_range_explicit() {
+       local first=${1}
+       local last=${2}
+
+       if [ ${first} -gt ${last} ]; then
+               local range="$(ipv4_decode ${first})-$(ipv4_decode ${last})"
+
+               error "Invalid IPv4 address range: ${range}"
+               return ${EXIT_ERROR}
+       fi
+
+       while [ ${first} -le ${last} ]; do
+               ipv4_decode ${first}
+               first=$(( ${first} + 1 ))
+       done
+}
+
+function ipv4_in_subnet() {
+       local addr=${1}
+       assert isset addr
+
+       local subnet=${2}
+       assert isset subnet
+
+       local subnet_first=$(ipv4_get_network_encoded ${subnet})
+       local subnet_last=$(ipv4_get_broadcast_encoded ${subnet})
+
+       addr=$(ipv4_encode ${addr})
+
+       if [[ "${addr}" -ge "${subnet_first}" ]] && [[ "${addr}" -le "${subnet_last}" ]]; then
+               return ${EXIT_TRUE}
+       fi
+
+       return ${EXIT_FALSE}
+}
+
+function ipv4_ttl_valid() {
+       local ttl="${1}"
+
+       isinteger ttl || return ${EXIT_FALSE}
+
+       # Must be between 10 and 255.
+       [ "${ttl}" -lt  10 ] && return ${EXIT_FALSE}
+       [ "${ttl}" -gt 255 ] && return ${EXIT_FALSE}
+
+       return ${EXIT_TRUE}
+}