revdns: Added IPv6 support.

This commit is contained in:
Mikhail Efremov 2012-12-13 19:20:14 +04:00
parent 7ecdc4ced0
commit 4ea61002cc

View File

@ -1,17 +1,15 @@
#!/bin/sh -efu #!/bin/sh -efu
. alterator-net-functions
ip="${1%%/*}" ipv4_rdns_net()
mask="${1##*/}"
rdns_net()
{ {
local net="$1";shift local net="$1";shift
local pos="$1";shift local pos="$1";shift
local inc="$1";shift local inc="$1";shift
local IFS='.' local IFS='.'
set -- $net set -- $net
case "$pos" in case "$pos" in
1) 1)
printf "%s\n" "$(( $1 + $inc ))" printf "%s\n" "$(( $1 + $inc ))"
;; ;;
@ -24,27 +22,91 @@ rdns_net()
esac esac
} }
net="$(netname "$ip/$mask"|cut -f1)" ipv4_revdns()
net="${net%%/$mask}" {
local nprefix pos i num
local net="$(netname "$ip/$prefix"|cut -f1)"
net="${net%%/$prefix}"
if [ "$mask" -gt 0 -a "$mask" -le 8 ];then if [ "$prefix" -gt 0 -a "$prefix" -le 8 ];then
nmask=8 nprefix=8
pos=1 pos=1
elif [ "$mask" -gt 8 -a "$mask" -le 16 ];then elif [ "$prefix" -gt 8 -a "$prefix" -le 16 ];then
nmask=16 nprefix=16
pos=2 pos=2
elif [ "$mask" -gt 16 -a "$mask" -le 24 ];then elif [ "$prefix" -gt 16 -a "$prefix" -le 24 ];then
nmask=24 nprefix=24
pos=3 pos=3
else else
mask=24 prefix=24
nmask=24 nprefix=24
pos=3 pos=3
fi fi
num="$(( 2**($nmask - $mask) - 1 ))" num="$(( 2**($nprefix - $prefix) - 1 ))"
for i in `seq 0 $num` ;do
ipv4_rdns_net "$net" "$pos" "$i"
done
}
ipv6_rdns_net()
{
local net="$1";shift
local pos="$1";shift
local inc="$1";shift
local c=0
local out tmp i p
net="$(ipv6addr_expand "${net%%/$prefix}")"
local IFS=':'
for i in $net; do
[ $c -lt $pos ] || break
tmp="$tmp$i"
c=$(($c + 1))
done
tmp="$tmp$(printf '%04x' $((0x$i + 0x$inc)))"
for p in $(echo $tmp | sed -r 's;([[:xdigit:]]);\1:;g'); do
out="$p${out:+.}$out"
done
echo "$out"
}
ipv6_revdns()
{
local nprefix pos i num
local net="$(ipv6_network "$ip/$prefix")"
pos=$(($prefix / 16))
nprefix=$((16 - $prefix % 16))
if [ $nprefix -eq 16 ]; then
nprefix=0
pos=$(($pos - 1))
fi
num="$(( 2**$nprefix - 1 ))"
for i in $(printf "%x " $(seq 0 $num)) ;do
ipv6_rdns_net "$net" "$pos" "$i"
done
}
ip="${1%%/*}"
prefix="${1##*/}"
ipv="${2:-4}"
case "$ipv" in
4) valid_ipv4 "$ip" &&
valid_ipv4prefix "$prefix" &&
ipv4_revdns
;;
6) valid_ipv6addr "$ip" &&
valid_ipv6prefix "$prefix" &&
ipv6_revdns
;;
*) exit 1 ;;
esac
for i in `seq 0 $num` ;do
rdns_net "$net" "$pos" "$i"
done