- add a new set of excellent functions for various ipv4address calculations (sem@)
This commit is contained in:
Stanislav Ievlev 2009-09-02 16:57:28 +04:00
parent 80fbe7719a
commit 71c4c45b93
3 changed files with 81 additions and 2 deletions

View File

@ -1,8 +1,8 @@
%define _altdata_dir %_datadir/alterator %define _altdata_dir %_datadir/alterator
Name: alterator-net-functions Name: alterator-net-functions
Version: 0.9 Version: 1.0
Release: alt4 Release: alt1
Packager: Stanislav Ievlev <inger@altlinux.org> Packager: Stanislav Ievlev <inger@altlinux.org>
@ -40,6 +40,9 @@ helpers for etcnet administration
%_libexecdir/%name/ %_libexecdir/%name/
%changelog %changelog
* Wed Sep 02 2009 Stanislav Ievlev <inger@altlinux.org> 1.0-alt1
- add a new set of excellent functions for various ipv4address calculations (sem@)
* Thu Apr 30 2009 Vladislav Zavjalov <slazav@altlinux.org> 0.9-alt4 * Thu Apr 30 2009 Vladislav Zavjalov <slazav@altlinux.org> 0.9-alt4
- list_iface: skip ifaces with type=801 (wmaster0) (closes: #19866) - list_iface: skip ifaces with type=801 (wmaster0) (closes: #19866)

View File

@ -267,3 +267,47 @@ iface_down()
done done
return 1 return 1
} }
### ipv4address calculations
ipv4addr_to_hex()
{
if [ -n "$1" ];then
local i=0
local tmp="$(echo "$1" | tr '.' ' ')"
for b in $tmp;do
[ "$b" -ge 0 -a "$b" -le 255 ] || break
i=$(($i+1))
done
[ "$i" -eq 4 ] && printf '0x%02x%02x%02x%02x' $tmp
fi
}
ipv4addr_is_in_subnet()
{
local ip="$1";shift
local net="$1";shift
local hex_addr="$(ipv4addr_to_hex "$ip")";shift
local hex_net="$(ipv4addr_to_hex "${net%%/*}")";shift
local prefix="${net##*/}";shift
local p=0xffffffff
local hex_mask="$(( $p - ($p >> $prefix) ))"
[ "$(( $hex_net & $hex_mask ))" -eq "$(( $hex_addr & $hex_mask ))" ]
}
ipv4addr_mask_to_prefix()
{
local hex_mask="$(ipv4addr_to_hex "$1")";shift
if [ -n "$hex_mask" ];then
local p=$(( ~$hex_mask & 0xffffffff ))
local i=0 prefix=
while [ "$p" -ne 0 ];do
p=$(($p >> 1 & 0xffffffff))
i=$(($i + 1))
done
prefix=$((32 - $i))
[ "$prefix" -ge 0 -a "$prefix" -le 32 ] && echo "$prefix"
fi
}

View File

@ -0,0 +1,32 @@
#!/bin/sh
appendTests \
test_ipv4addr_to_hex \
test_ipv4addr_is_in_subnet \
test_ipv4addr_mask_to_prefix \
#
test_ipv4addr_to_hex()
{
assertEquals "0.0.0.0" "$(ipv4addr_to_hex 0.0.0.0)" "0x00000000"
assertEquals "255.255.255.255" "$(ipv4addr_to_hex 255.255.255.255)" "0xffffffff"
assertEquals "255.255.254.0" "$(ipv4addr_to_hex 255.255.254.0)" "0xfffffe00"
assertEquals "10.1.1.187" "$(ipv4addr_to_hex 10.1.1.187)" "0x0a0101bb"
}
test_ipv4addr_is_in_subnet()
{
assertTrue "10.1.1.187" "ipv4addr_is_in_subnet 10.1.1.187 10.1.0.0/23"
assertTrue "10.1.1.187" "ipv4addr_is_in_subnet 10.1.1.187 10.1.1.187/23"
assertTrue "10.1.1.187" "ipv4addr_is_in_subnet 10.1.0.1 10.1.0.0/23"
assertFalse "10.1.1.187" "ipv4addr_is_in_subnet 10.1.3.1 10.1.0.0/23"
}
test_ipv4addr_mask_to_prefix()
{
assertEquals "0.0.0.0" "$(ipv4addr_mask_to_prefix 0.0.0.0)" "0"
assertEquals "255.255.255.255" "$(ipv4addr_mask_to_prefix 255.255.255.255)" "32"
assertEquals "255.255.255.0" "$(ipv4addr_mask_to_prefix 255.255.255.0)" "24"
assertEquals "255.255.254.0" "$(ipv4addr_mask_to_prefix 255.255.254.0)" "23"
}