#!/usr/bin/perl -w 
# 0.14.3
# 0.15   25.09.2000 Added link to this wrapper
# 0.16   07.11.2000 Get version from ipcalc
# 0.17   09.01.2001 Added screenshot
# 0.18   02.02.2001 Played with the html
# 0.18.1 03.02.2001 Played even more with the html
# 0.19   01.04.2001 Help text for wildcard netmask / Credits
# 0.20   20.05.2001 Changed error messages
# 0.21   19.06.2001 Adapted to new -c option
$|=1;
$ipcalc = "/usr/bin/ipcalc";

$actionurl = $ENV{'REQUEST_URI'};
use CGI;
$query = new CGI;
$host  = $query->param('host');
$mask1 = $query->param('mask1');
$mask2 = $query->param('mask2');
$help  = $query->param('help');

$version = qx($ipcalc -v);
chomp $version;

if (! defined $host) {
	$host = '';
}

if (! defined $mask1) {
	$mask1 = '';
	$help = 1;
}

if (! defined $mask2) {
	$mask2 = '';
}

if ($mask2 eq $mask1) {
	$mask2 = '';
}

if ($host eq '') {
	$error .= "&nbsp;No host given\n";
	$host = '192.168.0.1';
}

$testhost = $host;
$testhost =~ s/\.//g;
if ($testhost !~ /^\d+$/) {
	$error .= "&nbsp;Illegal value for host ('$host')\n";
	$host = '192.168.0.1';
}


if ($mask1 eq '') {
	$error .= "&nbsp;No netmask given (using default netmask of your network's class)\n";
	$mask1 = qx($ipcalc -c $host);
}


$testhost = $mask1;
$testhost =~ s/\.//g;
if ($testhost !~ /^\d+$/) {
	$error .= "&nbsp;Illegal value for netmask ('$mask1')\n";
	$mask1 = 24;
}

if ($mask2 ne '') {
	$testhost = $mask2;
	$testhost =~ s/\.//g;
	if ($testhost !~ /^\d+$/) {
		$error .= "&nbsp;Illegal value for netmask for sub/supernet ('$mask2')\n";
		$mask2 = '';
	}
}


print $query->header;
print << "EOF";
<html>
<head> 
<title>IP Calculator</title>
EOF

if (exists $ENV{HTTP_USER_AGENT} && $ENV{HTTP_USER_AGENT} =~ /MSIE/) {
  print '<link rel="stylesheet" type="text/css" href="/styles.css">' ."\n";
  print '<link rel="stylesheet" type="text/css" href="/styles_emsie.css">' ."\n";
}
# Windows Netscape has problems with this Stylesheet. 
#else {
#  print '<link rel="stylesheet" type="text/css" href="/styles_ns.css">'  ."\n"; 
#}

print << "EOF";
<LINK REL="SHORTCUT ICON" href="http://jodies.cx/favicon.ico">
</head>
<body bgcolor="#000000" vlink="#0000ff" background="bg.gif">
EOF
#print "$ENV{HTTP_USER_AGENT}<br>\n";

print << "EOF";
<center><table cellpadding=15 border=0 cellspacing=50><tr><td bgcolor="#ffffff">
<table border=0 width=100%>
<tr><td><img src="ipcal03.gif" align=right width=100 height=95></td></tr>
</table>
EOF
if ($help) {
print << "EOF";
<table border=0 bgcolor="#000000" border=0 cellpadding=0 cellspacing=1>
<tr><td>

<table bgcolor="#e9edf5" border=0 width=550 cellpadding=10 cellspacing=1><tr><td>
<font size=-1>
<h1><span class="section-header">IP Calculator</span></h1>
<p>
ipcalc takes an IP address and netmask and calculates the resulting broadcast,
network, Cisco wildcard mask, and host range. By giving a second netmask, you
can design sub- and supernetworks. It is also intended to be a teaching tool
and presents the results as easy-to-understand binary values.

<p>
Enter your netmask(s) in <a href="http://www.ietf.org/rfc/rfc1517.txt">CIDR</a> notation (/25) or dotted decimals (255.255.255.0). 
Inverse netmasks are recognized.
If you omit the netmask ipcalc uses the default netmask for the class of your network.
<p>
Look at the space between the bits of the addresses: The bits before it are
the network part of the address, the bits after it are the host part. You can
see two simple facts: In a network address all host bits are zero, in a
broadcast address they are all set.

<p>
The class of your network is determined by its first <font color="#009900">bits</font>.

<p>

<p>
If your network is a private internet according to RFC 1918 this is remarked.
When displaying subnets the new bits in the network part of the netmask are
marked in a <font color="#663366">different color</font>

<p>
The <a href="http://www.cisco.com/univercd/cc/td/doc/product/software/ssr83/ptc_r/22057.htm">wildcard</a> is the inverse
netmask as used for access control lists in Cisco routers.

<p>
Do you want to split your network into subnets? Enter the address and netmask
of your original network and play with the second netmask until the result matches your needs.

<p>

You can have all this fun at your shell prompt. Originally ipcalc was not intended for 
creating HTML and still works happily in /usr/local/bin/ :-)
<p>
Questions? Comments? Drop me a <a href="mailto:krischan&#64;jodies.cx">mail</a>...
<p>
Thanks for your ideas and help to make this tool more useful:
</font>
<font size=-1>
<pre>
Hermann J. Beckers   hj.beckers(a)kreis-steinfurt.de
Kevin Ivory          ki(a)sernet.de
Frank Quotschalla    gutschy(a)netzwerkinfo.de
Sven Anderson        sven(a)anderson.de
Scott Davis          sdavis(a)austin-texas.net
Denis A. Hainsworth  denis(a)ans.net
Steve Kent           stevek(a)onshore.com
Igor Zozulya         izozulya(a)yahoo.com

</pre>
</font>
</td></tr></table>

</tr></td>
</table>

EOF
}

print <<"EOF";

<form action="$actionurl" method="get">
<table border=0>
 <tr>
   <td><b>Address</b> (Host or Network)</td>
   <td><b>Netmask</b> (i.e. 24)</td>
   <td><b>Netmask</b> for sub/supernet (optional)</td>
 </tr>
 <tr>
   <td nowrap><input class=text name=host value="$host"> / </td>
   <!-- td nowrap><textarea class=text cols=5 rows=5 name=mask1 value="$mask1"></textarea></td-->
   <td nowrap><input class=text name=mask1 value="$mask1"></td>
   <td nowrap> move to: <input class=text name=mask2 value="$mask2"></td>
 </tr>
 <tr>
   <td colspan=3>
     <input class=submit type="submit" value="Calculate">
EOF
if (! $help) {
   print '&nbsp;<input name=help class=submit type="submit" value="Help">';
}
print <<"EOF"; 
 </td>
 </tr>
</table>
</form>
EOF

if (defined $error) {
	$error =~ s/</&lt;/gm;
	$error =~ s/>/&gt;/gm;
	$error =~ s/\n/<br>/g;
	print qq(<font color="#ff0000"><tt>\n);
	print "$error<br>";
	print qq(</tt><font color="#000000">\n);
	
}

system("$ipcalc -h $host $mask1 $mask2");

print <<"EOF";
<font color="#000000">
 <!-- hr noshade -->
<br>
<table border=0 width="100%" cellspacing=10>
 <tr>
  
  <td nowrap valign=top>
   <img src="ipcalculator.png" alt="Thanks to http://www.netzwerkinfo.de/daemons/ for this icon :-)"><br>
   <tt><span style="font-size: 8pt;">Version $version</span></tt></td>
  <td valign=top align="right" width=100%>
   <br>
   Download the script <a href="http://jodies.cx/ipcalc.pl"><b>ipcalc.pl</b></a>.  Works also at the prompt (<a href="ipcalc.gif">screenshot</a>)<br>
   Download the <a href="http://jodies.cx/ipcalc_cgi">CGI wrapper</a>.<br>
   <a href="ipcalc-archive">Archive</a> (gzip'ed version)<br>
   2/2000 <a href="mailto:krischan&#64;jodies.cx">Krischan Jodies</a>
  </td>
  <td valign=top>
    <img src="linux_powered.gif" width=152 height=75 align=right> 
  </td>
 </tr>
</table>

</td></tr></table>

</body>
</html>
EOF
