#!/usr/bin/perl  

# CGI Wrapper for IPv4 Calculator
# 
# Copyright (C) Krischan Jodies 2000 - 2021
# krischan()jodies.de, http://jodies.de/ipcalc
# 
# Copyright (C) for the graphics ipcalc.gif and ipcalculator.png 
# Frank Quotschalla. 2002 
#  
# 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.
#  
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# 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
# 0.22   30.07.2002 Stole javascript at dict.leo.org
# 0.23	 28.10.2004 Remove whitespace in input fields
#                   Idea by David Shirlay David.Shirley(a)team.telstra.com
# 0.24   07.07.2005 Added license text to cgi-wrapper. Add style into cgi script
# 0.25	 11.01.2006 Link to screenshot was wrong.
# 0.26   27.07.2006 Replaced REQUEST_URI with SCRIPT_URL to prevent cross-site-scripting attacks

$|=1;
$ipcalc = "/usr/local/bin/ipcalc";
$MAIL_ADDRESS="ipcalc-200502@jodies.de";
# history:
# 200404 
$actionurl = $ENV{'SCRIPT_URL'};
$actionurl =~ s/&/&/g;
use CGI;
$query = new CGI;
$host  = $query->param('host');
$mask1 = $query->param('mask1');
$mask2 = $query->param('mask2');
$help  = $query->param('help');

$host  =~ s/ //g;
$mask1 =~ s/ //g;
$mask2 =~ s/ //g;

$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 .= " No host given\n";
	$host = '192.168.0.1';
}

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

if ($mask1 eq '') {
	$error .= " 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 .= " Illegal value for netmask ('$mask1')\n";
	$mask1 = 24;
}

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

print $query->header;
print << "EOF";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

   IP Calculator / IP Subnetting

EOF
#print "$ENV{HTTP_USER_AGENT}
\n";

print << "EOF";

  

EOF
if ($help) {
print << "EOF";

IP Calculator

ipcalc (http://jodies.de/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 subnets and supernets. It is also intended to be a teaching tool
and presents the subnetting results as easy-to-understand binary values.

Enter your netmask(s) in CIDR (http://www.ietf.org/rfc/rfc1517.txt) 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.

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.

The class of your network is determined by its first bits.

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 different color

The wildcard (http://www.cisco.com/univercd/cc/td/doc/product/software/ssr83/ptc_r/22057.htm) is the inverse
netmask as used for access control lists in Cisco routers.

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.

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/ :-)

Questions? Comments? Drop me a mail (mailto:$MAIL_ADDRESS)...

Thanks for your ideas and help to make this tool more useful:

Bartosz Fenski
Christan Ebnoether
Denis A. Hainsworth 
Edward
Foxfair Hu
Frank Quotschalla  
Hermann J. Beckers  
Igor Zozulya        
Kevin Ivory        
Lars Mueller
Lutz Pressler       
Nick Clifford 
Oliver Seufer
Scott Davis         
Steve Kent          
Sven Anderson       
Torgen Foertsch
Tim Brown
Victor Engmark

EOF
}

print <<"EOF";

       Address (Host or Network)
       Netmask (i.e. 24)
       Netmask for sub/supernet (optional)

     / 
    
     move to: 

EOF
if (! $help) {
   print ' ';
}
print <<"EOF"; 

EOF

if (defined $error) {
	$error =~ s/</</gm;
	$error =~ s/>/>/gm;
	$error =~ s/\n/
/g;
	print qq(\n);
	print "$error
";
	print qq(\n);
	
}

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

print <<"EOF";

   Version $version

Download (http://jodies.de/ipcalc-archive/)
Screenshot (ipcalc works also at the prompt)
CGI wrapper that produced this page (http://jodies.de/ipcalc_cgi).
Archive
Have a look in the archives for the new version 0.38, with the capability to deaggregate network ranges
How to run this under windows
Debian users can apt-get install ipcalc
2000-2021 Krischan Jodies (mailto:$MAIL_ADDRESS)

EOF
