#!/usr/bin/perl use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use NetAddr::IP; use CommonWeb qw(:ALL);; #file snCalc.cgi little subnet calculator app use MyIPDB; my %webvar = parse_post(); my $input; print "Content-Type: text/html\n\n"; open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!"; my $start = join('', ); close(HTML); $start =~ s/\$\$WEBPATH\$\$/$IPDB::webpath/g; print $start; # Clean up input so we don't divide by zero or something equally silly if ($webvar{input} =~ m/(\d+)/) { $input = 1*$1; $input = 3 if $input < 3; $input = 29 if $input > 29; # Not doing IPv6 yet... } else { $input = 29; } my $ltinput = $input - 1; my $gtinput = $input + 1; my $prenet = new NetAddr::IP "0.0.0.0/$ltinput"; my $net = new NetAddr::IP "0.0.0.0/$input"; my $postnet = new NetAddr::IP "0.0.0.0/$gtinput"; print qq(
); print qq(\n\n\n
Results for /$ltinput Results for /$input Results for /$gtinput
\n). qq(
).$prenet->mask."
\n". qq(
).$prenet->wildcard."
\n". getranges($ltinput). qq(
\n). qq(
).$net->mask."
\n". qq(
).$net->wildcard."
\n". getranges($input). qq(
). qq(
).$postnet->mask."
\n". qq(
).$postnet->wildcard."
\n". getranges($gtinput); print "
\n"; print qq(
); # Just In Case exit 0; # subs sub xrange { my $block = shift; my $masklen = shift; my $data = $block->range; if ($masklen >= 24) { $data =~ s/\b0\.0\.0\./x.x.x./g; } elsif ($masklen >=16) { $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g; } elsif ($masklen >=8) { $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g; } return $data; } # xrange() sub getranges { my $masklen = shift; my $ret = ''; my $super; if ($masklen < 8) { $super = new NetAddr::IP "0.0.0.0/0"; } elsif ($masklen < 16) { $super = new NetAddr::IP "0.0.0.0/8"; } elsif ($masklen < 24) { $super = new NetAddr::IP "0.0.0.0/16"; } else { $super = new NetAddr::IP "0.0.0.0/24"; } foreach my $net ($super->split($masklen)) { $ret .= "\t".xrange($net,$masklen)."
\n"; } return $ret; } # getranges()