Monday, June 23, 2008

Reverse DNS records for subnets larger than 24

I've been playing lately with DNS, and it seems there is a need to create zones with less than 255 ip ... so i had to dig a little around, i took a look at mkrdns tool and RFC2317, i wrote a little php script that might be useful to understand what should be done, without deploying automation tool ... the thing is ... i cant test it :(, but anyway, ill post it here, so please, if you have any comments, if you find anything that is incorrect, or any enhancements, please let me know.



<?php
/*
* Author: Maysara A. Abdulhaq
* Contact: maysara(dot)abdulhaq(at)gmail(dot)com
* Usage: Guides howto add revese domains
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
function reverse_domain_name($ip,$class=24)
{
$octets= explode(".",$ip);
$octetcount=(int) ($class/8);
for ($i=$octetcount -1 ;$i >= 0 ;$i--)
$dn= $dn."$octets[$i].";
$dn= $dn."in-addr.arpa";
$mask= (~((1<<(8-$class%8))-1)&255);
$rangebeg = (((int)$octets[$octetcount]) & $mask);
$rangeend = (((int)$octets[$octetcount]) & $mask)+((1<<8-$class%8) -1);

echo "You have Entered IP: ".$ip." with subnet: ".$class."\n";
echo "So the reverse domain is : ".$dn."\n";
echo "and the range of ips is $rangebeg to $rangeend\n";
echo "If you wish to use a domain for mapping similar to mkrdns, you can use domain name \n";
echo "A: $rangebeg-$rangeend.$dn\n";
echo "B: $rangebeg.$dn\n";
echo "Or, similar for what is suggested in RFC2317:\n";
echo "C: $rangebeg/$class.$dn\n";

echo "for each entry, a CNAME record must be added to $dn \n";
echo "A: $octets[$octetcount] CNAME $octets[$octetcount].$rangebeg-$rangeend.$dn\n";
echo "B: $octets[$octetcount] CNAME $octets[$octetcount].$rangebeg.$dn\n";
echo "C: $octets[3] CNAME $octets[3].$rangebeg/$class.$dn\n";
echo "in addition to the PTR in the above mentioned domain name\n";
echo "$octets[3] PTR some.domain.tld\n";

}

if ( $argc != 3){
echo "Usage: $argv[0] IP MASK\n";
echo "Example: $argv[0] 111.222.121.212 27\n";
exit(1);
}
if ( $argv[2] < 24){
echo "Error:the mask should be larger than 24\n";
exit(1);
}
reverse_domain_name($argv[1],$argv[2]);

?>