| [4] | 1 | #!/usr/bin/perl
 | 
|---|
 | 2 | 
 | 
|---|
 | 3 | use strict;             
 | 
|---|
 | 4 | use warnings;   
 | 
|---|
| [360] | 5 | use CGI::Carp qw(fatalsToBrowser);
 | 
|---|
 | 6 | use NetAddr::IP;
 | 
|---|
 | 7 | use CommonWeb qw(:ALL);;
 | 
|---|
| [4] | 8 | 
 | 
|---|
 | 9 | #file snCalc.cgi        little subnet calculator app
 | 
|---|
| [507] | 10 | use MyIPDB;
 | 
|---|
| [4] | 11 | 
 | 
|---|
 | 12 | my %webvar = parse_post();
 | 
|---|
 | 13 | my $input;
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | print "Content-Type: text/html\n\n";
 | 
|---|
 | 16 | 
 | 
|---|
| [360] | 17 | open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
 | 
|---|
| [4] | 18 | my $start = join('', <HTML>);
 | 
|---|
 | 19 | close(HTML);
 | 
|---|
| [507] | 20 | $start =~ s/\$\$WEBPATH\$\$/$IPDB::webpath/g;
 | 
|---|
| [4] | 21 | print $start;
 | 
|---|
 | 22 | 
 | 
|---|
| [360] | 23 | # Clean up input so we don't divide by zero or something equally silly
 | 
|---|
 | 24 | if ($webvar{input} =~ m/(\d+)/) {
 | 
|---|
 | 25 |   $input = 1*$1;
 | 
|---|
 | 26 |   $input = 3 if $input < 3;
 | 
|---|
 | 27 |   $input = 29 if $input > 29;   # Not doing IPv6 yet...
 | 
|---|
 | 28 | } else {
 | 
|---|
 | 29 |   $input = 29;
 | 
|---|
| [4] | 30 | }
 | 
|---|
 | 31 | 
 | 
|---|
| [360] | 32 | my $ltinput = $input - 1;
 | 
|---|
 | 33 | my $gtinput = $input + 1;
 | 
|---|
| [4] | 34 | 
 | 
|---|
| [360] | 35 | my $prenet = new NetAddr::IP "0.0.0.0/$ltinput";
 | 
|---|
 | 36 | my $net = new NetAddr::IP "0.0.0.0/$input";
 | 
|---|
 | 37 | my $postnet = new NetAddr::IP "0.0.0.0/$gtinput";
 | 
|---|
| [4] | 38 | 
 | 
|---|
| [360] | 39 | print qq(<div class="center">
 | 
|---|
 | 40 | <table align="center" cellspacing="3" cellpadding="3">
 | 
|---|
 | 41 | <tr>
 | 
|---|
 | 42 |         <td class="heading" align="center">Results for /$ltinput</td>
 | 
|---|
 | 43 |         <td class="heading" align="center">Results for /$input</td>
 | 
|---|
 | 44 |         <td class="heading" align="center">Results for /$gtinput</td>
 | 
|---|
 | 45 | </tr>
 | 
|---|
 | 46 | );
 | 
|---|
| [4] | 47 | 
 | 
|---|
| [360] | 48 | print qq(<tr><td valign="top">\n).
 | 
|---|
 | 49 |         qq(     <div class="mask">).$prenet->mask."</div>\n".
 | 
|---|
 | 50 |         qq(     <div class="wildcard">).$prenet->wildcard."</div>\n".
 | 
|---|
 | 51 |         getranges($ltinput).
 | 
|---|
 | 52 |         qq(</td>\n<td valign="top" bgcolor="#d0e0e0">\n).
 | 
|---|
 | 53 |         qq(     <div class="mask">).$net->mask."</div>\n".
 | 
|---|
 | 54 |         qq(     <div class="wildcard">).$net->wildcard."</div>\n".
 | 
|---|
 | 55 |         getranges($input).
 | 
|---|
 | 56 |         qq(</td>\n<td valign="top">).
 | 
|---|
 | 57 |         qq(     <div class="mask">).$postnet->mask."</div>\n".
 | 
|---|
 | 58 |         qq(     <div class="wildcard">).$postnet->wildcard."</div>\n".
 | 
|---|
 | 59 |         getranges($gtinput);
 | 
|---|
| [4] | 60 | 
 | 
|---|
| [360] | 61 | print "</td></tr>\n</table>\n";
 | 
|---|
| [4] | 62 | 
 | 
|---|
| [360] | 63 | print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading">
 | 
|---|
 | 64 | </div>
 | 
|---|
 | 65 | </body>
 | 
|---|
 | 66 | </html>
 | 
|---|
 | 67 | );
 | 
|---|
 | 68 |         
 | 
|---|
 | 69 | # Just In Case
 | 
|---|
 | 70 | exit 0;
 | 
|---|
| [4] | 71 | 
 | 
|---|
| [360] | 72 | # subs
 | 
|---|
 | 73 | sub xrange {
 | 
|---|
 | 74 |   my $block = shift;
 | 
|---|
 | 75 |   my $masklen = shift;
 | 
|---|
 | 76 |   my $data = $block->range;
 | 
|---|
 | 77 |   if ($masklen >= 24) {
 | 
|---|
 | 78 |     $data =~ s/\b0\.0\.0\./x.x.x./g;
 | 
|---|
 | 79 |   } elsif ($masklen >=16) {
 | 
|---|
 | 80 |     $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g;
 | 
|---|
 | 81 |   } elsif ($masklen >=8) {
 | 
|---|
 | 82 |     $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g;
 | 
|---|
 | 83 |   }
 | 
|---|
 | 84 |   return $data;
 | 
|---|
 | 85 | } # xrange()
 | 
|---|
| [4] | 86 | 
 | 
|---|
 | 87 | 
 | 
|---|
| [360] | 88 | sub getranges {
 | 
|---|
 | 89 |   my $masklen = shift;
 | 
|---|
 | 90 |   my $ret = '';
 | 
|---|
 | 91 |   my $super;
 | 
|---|
 | 92 |   if ($masklen < 8) {
 | 
|---|
 | 93 |     $super = new NetAddr::IP "0.0.0.0/0";
 | 
|---|
 | 94 |   } elsif ($masklen < 16) {
 | 
|---|
 | 95 |     $super = new NetAddr::IP "0.0.0.0/8";
 | 
|---|
 | 96 |   } elsif ($masklen < 24) {
 | 
|---|
 | 97 |     $super = new NetAddr::IP "0.0.0.0/16";
 | 
|---|
| [4] | 98 |   } else {
 | 
|---|
| [360] | 99 |     $super = new NetAddr::IP "0.0.0.0/24";
 | 
|---|
| [4] | 100 |   }
 | 
|---|
| [360] | 101 |   foreach my $net ($super->split($masklen)) {
 | 
|---|
 | 102 |     $ret .= "\t".xrange($net,$masklen)."<br />\n";
 | 
|---|
| [4] | 103 |   }
 | 
|---|
| [360] | 104 |   return $ret;
 | 
|---|
 | 105 | } # getranges()
 | 
|---|