<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php
/***********************************************************************
 * IP Calculator - Written 100% by WormFood (which means I didn't look *
 *                                           other code to write this) *
 ***********************************************************************
 * THIS SOFTWARE IS PROVIDED BY WORMFOOD ``AS IS'' AND ANY EXPRESSED   *
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  *
 * ARE DISCLAIMED.  IN NO EVENT SHALL WORMFOOD OR ITS CONTRIBUTORS BE  *
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT   *
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE   *
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH    *
 * DAMAGE.                               (sorry, didn't mean to shout) *
 ***********************************************************************
 * If you make changes or improvements to this program please share    *
 * your changes with with me.                                          *
 * If you have comments or suggestions, please share them with me too. *
 * You can contact me as "WormFood" on FreeNode IRC network. If I am   *
 * not online, then please leave a messagefor me with MemoServ.        *
 *                                                                     *
 * I love recursive subroutines, and this program has 5 of them        *
 ***********************************************************************
 *                          Revision History                           *
 *---------------------------------------------------------------------*
 * Ver 1.0.5 - 2011 March 31 - Changed the short tags to php tags to   *
 *                             match a change my php configuration.    *
 * Ver 1.0.4 - 2008 June 19 - Fixed 2 lines that were creating error   *
 *                          - entries in the log file.                 *
 * Ver 1.0.3 - 2005 March 7 - register_globals = Off now works         *
 *                          - also added version to title              *
 * Ver 1.0.2 - 2004 October 10 - additional checks on $count           *
 * Ver 1.0.1 - 2001 August 11 - Added count_bits subroutine            *
 * Ver 1.0.0 - 2001 April 02 - Initial Release by WormFood             *
 ***********************************************************************/

if(isset($_GET['ip']))
    
$ip=$_GET['ip'];
if(isset(
$_GET['start']))
    
$start=$_GET['start'];
if(isset(
$_GET['stop']))
    
$stop=$_GET['stop'];
if(isset(
$_GET['count']))
    
$count=$_GET['count'];




function 
check($start$stop)
{ if(
$start!=abs($start)) return 0;
  if(!
mod2($stop-$start+1)) return 0;
  return 
checking($start$stop-$start+1); 
}

function 
checking($num$count)
{ if(
$num<0) return 0;
  if(
$num==0) return 1;
  return 
checking($num-$count,$count); 
}

function 
mod2($num)
{ if(
$num==2) return 1;
  if(
$num<2) return 0;
  if(
$num/2!=(int)($num/2)) return 0;
  return 
mod2($num/2); 
}

function 
mod($num)
{ if(
$num<=2) return 0;
  return 
mod($num/2)+1
}

function 
count_bits($num)
{
if(
$num<128)
 return 
0;
 else
 return 
count_bits(($num<<1)&255)+1;
}

function 
divide($start$stop$echo$level)
{
  global 
$ip;
  if(
    (
$stop>255) ||  # \
    
($stop<3) ||    #  \___ Invalid
    
($start>252) || #  /
    
($start<0) ||   # /
    
(($stop-$start+1)/2)!=(int)(($stop-$start+1)/2) || 
    ((
$stop-$start)<=2) ||
    (!
$level)
  )
    return;
  if(
mod($stop-$start)<$level)
    
$level=mod($stop-$start);
?>
<TD ROWSPAN="<?php echo pow(2,$level-1); ?>">
Network Address:<BR>&nbsp;<?php $num=24; echo $ip,$start,"/",$num+count_bits(255-($stop-$start)); ?><BR>
Host Addresses:<BR>&nbsp;<?php echo $ip; echo $start+1; echo "-"; echo $stop-1?><BR>
Broadcast Address:<BR>&nbsp;<?php echo $ip.$stop?><BR>
Netmask:<BR>&nbsp;255.255.255.<?php echo 255-($stop-$start); ?><BR>
Total of <?php echo $stop-$start-1?> hosts<BR>
</TD>
<?php
divide
($start,$start+((($stop-$start)+1)/2-1),"</TR><TR>",$level-1);
divide($start+((($stop-$start)+1)/2),$stop,"",$level-1);
echo 
$echo;
}

/***** END OF FUNCTIONS *****/

 
if(!isset($start)||$start==""$start=0;
 if(!isset(
$stop)||$stop==""$stop=255;
 if(!isset(
$count)||$count==""$count=2;
 
$count=abs((int)$count);
 if(!isset(
$ip)||$ip==""$ip="192.168.0.";
?>
<HTML><HEAD><TITLE>WormFood's IP Subnet Calculator Ver 1.0.3</TITLE></HEAD><BODY>
<CENTER><H1>WormFood's IP Subnet Calculator</H1></CENTER>
<FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>">
<TABLE><TR><TD ROWSPAN="2">
<INPUT TYPE="text" NAME="ip" SIZE="12" MAXLENGTH="12" VALUE="<?php echo isset($ip)&&$ip!="" $ip "192.168.0."?>">
<TD><SELECT NAME="start"><?php for($a=0;$a<255;$a+=32) { ?>
<OPTION <?php if($a==$start)echo "SELECTED"; echo ">".$a ?></OPTION><?php ?></SELECT>Start address<BR>
<TR><TD><SELECT NAME="stop"><?php for($a=31;$a<256;$a+=32) { ?>
<OPTION <?php if($a==$stop)echo "SELECTED"; echo ">".$a ?></OPTION><?php ?></SELECT>Stop address<BR>
<TR><TD><TD><SELECT NAME="count"><?php for($a=1;$a<8;$a++) { ?>
<OPTION <?php if($a==$count)echo "SELECTED"; echo ">".$a ?></OPTION><?php ?></SELECT>Depth
</TABLE>
<INPUT TYPE="submit" VALUE="Make Table">
</FORM>
<?php
  
if(check($start,$stop))
?> <TABLE BORDER=1 CELLPADDING=5><TR>
<?php divide($start,$stop,"",$count); ?>
</TR>
</TABLE><?php }
else
  { 
?>Start and Stop are not set properly<BR>

<?php }?>
<p>
<a href="http://validator.w3.org/check/referer">
<img border="0" src="http://www.w3.org/Icons/valid-html40" alt="Valid HTML 4.0!" height="31" width="88"></a>
</p>
</BODY>
</HTML>