| 1 | #!/usr/bin/perl
 | 
|---|
| 2 | 
 | 
|---|
| 3 | use strict;             
 | 
|---|
| 4 | use warnings;   
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #file snCalc.cgi        little subnet calculator app
 | 
|---|
| 7 | 
 | 
|---|
| 8 | my %webvar = parse_post();
 | 
|---|
| 9 | my $input;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | print "Content-Type: text/html\n\n";
 | 
|---|
| 12 | 
 | 
|---|
| 13 | open(HTML, "../startsn.html")|| die "Could not open starsn.html :$!";
 | 
|---|
| 14 | my $start = join('', <HTML>);
 | 
|---|
| 15 | close(HTML);
 | 
|---|
| 16 | print $start;
 | 
|---|
| 17 | 
 | 
|---|
| 18 | if($webvar{input} =~ m/(\/\d\d)/)
 | 
|---|
| 19 | {
 | 
|---|
| 20 |         $input = $1;
 | 
|---|
| 21 | }
 | 
|---|
| 22 | else
 | 
|---|
| 23 | {
 | 
|---|
| 24 |         $input = '/29';
 | 
|---|
| 25 | }
 | 
|---|
| 26 | 
 | 
|---|
| 27 | if(substr($input, 1, 2) > 32 || substr($input, 1, 2) < 24)
 | 
|---|
| 28 | {
 | 
|---|
| 29 |         printAndExit("'$input' is an invalid netmask.");
 | 
|---|
| 30 | }
 | 
|---|
| 31 | else
 | 
|---|
| 32 | {               $input = substr($input, 1, 2);
 | 
|---|
| 33 |                 my $ltinput = $input -1;
 | 
|---|
| 34 |                 my $gtinput = $input +1;
 | 
|---|
| 35 |                 
 | 
|---|
| 36 |                 print qq(       <div class="center">
 | 
|---|
| 37 |                 <table align="center" cellspacing="3" cellpadding="3"><tr>
 | 
|---|
| 38 |                 <td class="heading" align="center"> Results for /$ltinput</td><td class="heading" align="center"> Results for /$input</td><td class="heading" align="center"> Results for /$gtinput</td></tr>
 | 
|---|
| 39 |                 );      
 | 
|---|
| 40 | 
 | 
|---|
| 41 |                 if( $input =~ m|.*24.*|){
 | 
|---|
| 42 |                         $input = 256;
 | 
|---|
| 43 |                         $ltinput =128;
 | 
|---|
| 44 |                         undef($gtinput);
 | 
|---|
| 45 |                 }
 | 
|---|
| 46 |                 elsif( $input =~ m|.*25.*|){
 | 
|---|
| 47 |                         $input = 128;
 | 
|---|
| 48 |                         $ltinput = 64;
 | 
|---|
| 49 |                         $gtinput = 256;
 | 
|---|
| 50 |                 }
 | 
|---|
| 51 |                 elsif( $input =~ m|.*26.*|){
 | 
|---|
| 52 |                         $input = 64;
 | 
|---|
| 53 |                         $ltinput = 32;
 | 
|---|
| 54 |                         $gtinput = 128;
 | 
|---|
| 55 |                 }
 | 
|---|
| 56 |                 elsif( $input =~ m|.*27.*|){
 | 
|---|
| 57 |                         $input = 32;
 | 
|---|
| 58 |                         $ltinput = 16;
 | 
|---|
| 59 |                         $gtinput = 64;
 | 
|---|
| 60 |                 }
 | 
|---|
| 61 |                 elsif( $input =~ m|.*28.*|){
 | 
|---|
| 62 |                         $input = 16;
 | 
|---|
| 63 |                         $ltinput = 8;
 | 
|---|
| 64 |                         $gtinput = 32;
 | 
|---|
| 65 |                 }
 | 
|---|
| 66 |                 elsif( $input =~ m|.*29.*|){
 | 
|---|
| 67 |                         $input = 8;
 | 
|---|
| 68 |                         $ltinput = 4;
 | 
|---|
| 69 |                         $gtinput = 16;
 | 
|---|
| 70 |                 }
 | 
|---|
| 71 |                 elsif( $input =~ m|.*30.*|){
 | 
|---|
| 72 |                         $input = 4;
 | 
|---|
| 73 |                         $ltinput = 2;
 | 
|---|
| 74 |                         $gtinput = 8;
 | 
|---|
| 75 |                 }
 | 
|---|
| 76 |                 elsif( $input =~ m|.*31.*|){
 | 
|---|
| 77 |                         $input = 2;
 | 
|---|
| 78 |                         $ltinput = 1;
 | 
|---|
| 79 |                         $gtinput = 4;
 | 
|---|
| 80 |                 }
 | 
|---|
| 81 |                 elsif( $input =~ m|.*32.*|){
 | 
|---|
| 82 |                         $input = 1;
 | 
|---|
| 83 |                         $gtinput = 2;
 | 
|---|
| 84 |                         undef($ltinput);
 | 
|---|
| 85 |                 }
 | 
|---|
| 86 | 
 | 
|---|
| 87 |                 my ($center, $left, $right) = ('','','');
 | 
|---|
| 88 |                 my $subnet;
 | 
|---|
| 89 |                 
 | 
|---|
| 90 |                 #add the subnet masks
 | 
|---|
| 91 |                 
 | 
|---|
| 92 |                 if($input) 
 | 
|---|
| 93 |                 {               
 | 
|---|
| 94 |                   $subnet = 256 - $input;
 | 
|---|
| 95 |                   $center = qq(<div style="background-color: #00ff00">255.255.255.$subnet </div>);
 | 
|---|
| 96 |                 }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |                 if($ltinput)
 | 
|---|
| 99 |                 {
 | 
|---|
| 100 |                   $subnet = 256 - $ltinput;
 | 
|---|
| 101 |                   $right = qq(<div style="background-color: #00ff00">255.255.255.$subnet</div>);
 | 
|---|
| 102 |                 }
 | 
|---|
| 103 |                 if($gtinput)
 | 
|---|
| 104 |                 {
 | 
|---|
| 105 |                   $subnet = 256 - $gtinput;
 | 
|---|
| 106 |                   $left = qq(<div style="background-color: #00ff00">255.255.255.$subnet</div>);
 | 
|---|
| 107 |                 }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | 
 | 
|---|
| 110 |                 for(my $i = 0; $i < 256; $i++)
 | 
|---|
| 111 |                 {
 | 
|---|
| 112 |                         #left display -- one less than requested
 | 
|---|
| 113 |                         if(defined($gtinput) && $i % $gtinput == 0)
 | 
|---|
| 114 |                         {
 | 
|---|
| 115 |                                 my $upper = $i + $gtinput -1;
 | 
|---|
| 116 |                                 $left .= "x.x.x.$i - x.x.x.$upper</br>\n";
 | 
|---|
| 117 |                         }
 | 
|---|
| 118 | 
 | 
|---|
| 119 |                         #center display -- the one requested
 | 
|---|
| 120 |                         if($i % $input == 0 )
 | 
|---|
| 121 |                         {
 | 
|---|
| 122 |                                 my $upper = $i + $input - 1;
 | 
|---|
| 123 |                                 $center .= "x.x.x.$i - x.x.x.$upper  </br>\n";
 | 
|---|
| 124 |                         }
 | 
|---|
| 125 | 
 | 
|---|
| 126 |                         #right display -- one more than requested
 | 
|---|
| 127 |                         if(defined($ltinput) && $i % $ltinput == 0)
 | 
|---|
| 128 |                         {
 | 
|---|
| 129 |                                 my $upper = $i + $ltinput -1;
 | 
|---|
| 130 |                                 $right .= "x.x.x.$i - x.x.x.$upper  </br>\n";
 | 
|---|
| 131 |                         }
 | 
|---|
| 132 |                 }
 | 
|---|
| 133 |                 
 | 
|---|
| 134 |                 print qq(<tr><td valign="top" >$left</td>
 | 
|---|
| 135 |                                 <td valign="top" bgcolor="#d0e0e0">$center</td>
 | 
|---|
| 136 |                                 <td valign="top" >$right</td></tr>
 | 
|---|
| 137 |                                 );
 | 
|---|
| 138 |                 print "</table>\n";
 | 
|---|
| 139 | 
 | 
|---|
| 140 |                 print '<input type="button" value="Back" onclick="history.go(-1)" class="heading"></div></body></html>';
 | 
|---|
| 141 |         
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | 
 | 
|---|
| 145 | sub parse_post {
 | 
|---|
| 146 |   my $buffer;
 | 
|---|
| 147 |   if ($ENV{'REQUEST_METHOD'} eq "GET") {
 | 
|---|
| 148 |     $buffer=$ENV{'QUERY_STRING'}
 | 
|---|
| 149 |   } elsif ($ENV{'REQUEST_METHOD'} eq 'POST' && $ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded") {
 | 
|---|
| 150 |     read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
 | 
|---|
| 151 |   } else {
 | 
|---|
| 152 |     $buffer = $ENV{'QUERY_STRING'};
 | 
|---|
| 153 |     $buffer || read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
 | 
|---|
| 154 |   }
 | 
|---|
| 155 |   my @pairs = split(/&/, $buffer);
 | 
|---|
| 156 |   my %webvarLocal;
 | 
|---|
| 157 |   foreach my $pair (@pairs) {
 | 
|---|
| 158 |     my ($name, $value) = split(/=/, $pair);
 | 
|---|
| 159 |     $name  =~ tr/+/ /;
 | 
|---|
| 160 |     $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 | 
|---|
| 161 |     $value =~ tr/+/ /;
 | 
|---|
| 162 |     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 | 
|---|
| 163 |     $value =~ s/\'/\\\'/g;
 | 
|---|
| 164 |     $webvarLocal{$name} = $value;
 | 
|---|
| 165 |   }
 | 
|---|
| 166 |   return %webvarLocal;
 | 
|---|
| 167 | }
 | 
|---|
| 168 | sub printAndExit
 | 
|---|
| 169 | {
 | 
|---|
| 170 |         my $errStr = $_[0];
 | 
|---|
| 171 |         print qq(
 | 
|---|
| 172 |         <center><p class="regular"> $errStr </p>
 | 
|---|
| 173 |         <input type="button" value="Back" onclick="history.go(-1)" class="heading">
 | 
|---|
| 174 |         </center>
 | 
|---|
| 175 |         );
 | 
|---|
| 176 |         exit(0);
 | 
|---|
| 177 | }
 | 
|---|