IP_SUPPORTED_PROTOCOLS="${IP_SUPPORTED_PROTOCOLS} ipv4"
-function ipv4_is_valid() {
+ipv4_is_valid() {
ipcalc --ipv4 -c $@ >/dev/null 2>&1
case "$?" in
esac
}
-function ipv4_prefix_is_valid() {
+ipv4_prefix_is_valid() {
local prefix=${1}
isset prefix || return ${EXIT_FALSE}
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}
return ${EXIT_ERROR}
}
-function ipv4_update_neighbours() {
+ipv4_update_neighbours() {
local device=${1}
local address=${2}
( 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
return ${EXIT_OK}
}
-function ipv4_get_prefix() {
+ipv4_get_prefix() {
local address=${1}
local broadcast=${2}
return ${EXIT_OK}
}
-function ipv4_get_netmask() {
+ipv4_get_netmask() {
local address=${1}
assert isset address
return ${EXIT_OK}
}
-function ipv4_flush_device() {
+ipv4_flush_device() {
#
# Flushes all routes, addresses from the device
# and clears the ARP cache.
return 0
}
-function ipv4_prefix2netmask() {
+ipv4_prefix2netmask() {
local prefix=${1}
shift
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})
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})
print "%d" $(( $(( ${addr} & ${netmask} )) | ${broadcast} ))
}
-function ipv4_encode() {
+ipv4_encode() {
local addr=${1}
local int=0
print "${int}"
}
-function ipv4_decode() {
+ipv4_decode() {
local int=${1}
local addr=$(( ${int} & 255 ))
print "${addr}"
}
-function ipv4_addr_eq() {
+ipv4_addr_eq() {
local addr1=${1}
assert isset addr1
&& return ${EXIT_TRUE} || return ${EXIT_FALSE}
}
-function ipv4_addr_gt() {
+ipv4_addr_gt() {
local addr1=${1}
assert isset addr1
&& return ${EXIT_TRUE} || return ${EXIT_FALSE}
}
-function ipv4_range() {
+ipv4_range() {
local range=${1}
local first=${1%-*}
_ipv4_range "$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
}
-function _ipv4_range() {
+_ipv4_range() {
local first=${1}
local last=${2}
done
}
-function ipv4_range_explicit() {
+ipv4_range_explicit() {
local range=${1}
local first last
_ipv4_range_explicit "$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
}
-function _ipv4_range_explicit() {
+_ipv4_range_explicit() {
local first=${1}
local last=${2}
done
}
-function ipv4_in_subnet() {
+ipv4_in_subnet() {
local addr=${1}
assert isset addr
return ${EXIT_FALSE}
}
-function ipv4_ttl_valid() {
+ipv4_ttl_valid() {
local ttl="${1}"
isinteger ttl || return ${EXIT_FALSE}