From 4ea61002cc170f20ffa79c7d1f78dd6d85610184 Mon Sep 17 00:00:00 2001 From: Mikhail Efremov Date: Thu, 13 Dec 2012 19:20:14 +0400 Subject: [PATCH] revdns: Added IPv6 support. --- alterator-net-functions/tools/revdns | 122 ++++++++++++++++++++------- 1 file changed, 92 insertions(+), 30 deletions(-) diff --git a/alterator-net-functions/tools/revdns b/alterator-net-functions/tools/revdns index 60fdd7e..9a6d15d 100755 --- a/alterator-net-functions/tools/revdns +++ b/alterator-net-functions/tools/revdns @@ -1,17 +1,15 @@ #!/bin/sh -efu +. alterator-net-functions -ip="${1%%/*}" -mask="${1##*/}" - -rdns_net() +ipv4_rdns_net() { - local net="$1";shift - local pos="$1";shift - local inc="$1";shift + local net="$1";shift + local pos="$1";shift + local inc="$1";shift - local IFS='.' - set -- $net - case "$pos" in + local IFS='.' + set -- $net + case "$pos" in 1) printf "%s\n" "$(( $1 + $inc ))" ;; @@ -24,27 +22,91 @@ rdns_net() esac } -net="$(netname "$ip/$mask"|cut -f1)" -net="${net%%/$mask}" +ipv4_revdns() +{ + local nprefix pos i num + local net="$(netname "$ip/$prefix"|cut -f1)" + net="${net%%/$prefix}" -if [ "$mask" -gt 0 -a "$mask" -le 8 ];then - nmask=8 - pos=1 + if [ "$prefix" -gt 0 -a "$prefix" -le 8 ];then + nprefix=8 + pos=1 -elif [ "$mask" -gt 8 -a "$mask" -le 16 ];then - nmask=16 - pos=2 -elif [ "$mask" -gt 16 -a "$mask" -le 24 ];then - nmask=24 - pos=3 -else - mask=24 - nmask=24 - pos=3 -fi + elif [ "$prefix" -gt 8 -a "$prefix" -le 16 ];then + nprefix=16 + pos=2 + elif [ "$prefix" -gt 16 -a "$prefix" -le 24 ];then + nprefix=24 + pos=3 + else + prefix=24 + nprefix=24 + pos=3 + 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