Changeset 360 for branches/stable/cgi-bin/snCalc.cgi
- Timestamp:
- 04/20/07 15:48:42 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/cgi-bin/snCalc.cgi
r333 r360 3 3 use strict; 4 4 use warnings; 5 use CGI::Carp qw(fatalsToBrowser); 6 use NetAddr::IP; 7 use CommonWeb qw(:ALL);; 5 8 6 9 #file snCalc.cgi little subnet calculator app … … 11 14 print "Content-Type: text/html\n\n"; 12 15 13 open(HTML, "../startsn.html")|| die "Could not open star sn.html :$!";16 open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!"; 14 17 my $start = join('', <HTML>); 15 18 close(HTML); 16 19 print $start; 17 20 18 if($webvar{input} =~ m/(\d\d)/) 19 {20 21 } 22 else 23 {24 21 # Clean up input so we don't divide by zero or something equally silly 22 if ($webvar{input} =~ m/(\d+)/) { 23 $input = 1*$1; 24 $input = 3 if $input < 3; 25 $input = 29 if $input > 29; # Not doing IPv6 yet... 26 } else { 27 $input = 29; 25 28 } 26 29 27 if ($input > 32 || $input < 24) 28 { 29 printAndExit("Don't know what to do with '$input' as a masklength."); 30 } 31 else 32 { 33 my $ltinput = $input -1; 34 my $gtinput = $input +1; 35 print qq( <div class="center"> 36 <table align="center" cellspacing="3" cellpadding="3"><tr> 37 <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> 38 ); 30 my $ltinput = $input - 1; 31 my $gtinput = $input + 1; 39 32 40 if( $input =~ m|.*24.*|){ 41 $input = 256; 42 $ltinput =128; 43 undef($gtinput); 44 } 45 elsif( $input =~ m|.*25.*|){ 46 $input = 128; 47 $ltinput = 64; 48 $gtinput = 256; 49 } 50 elsif( $input =~ m|.*26.*|){ 51 $input = 64; 52 $ltinput = 32; 53 $gtinput = 128; 54 } 55 elsif( $input =~ m|.*27.*|){ 56 $input = 32; 57 $ltinput = 16; 58 $gtinput = 64; 59 } 60 elsif( $input =~ m|.*28.*|){ 61 $input = 16; 62 $ltinput = 8; 63 $gtinput = 32; 64 } 65 elsif( $input =~ m|.*29.*|){ 66 $input = 8; 67 $ltinput = 4; 68 $gtinput = 16; 69 } 70 elsif( $input =~ m|.*30.*|){ 71 $input = 4; 72 $ltinput = 2; 73 $gtinput = 8; 74 } 75 elsif( $input =~ m|.*31.*|){ 76 $input = 2; 77 $ltinput = 1; 78 $gtinput = 4; 79 } 80 elsif( $input =~ m|.*32.*|){ 81 $input = 1; 82 $gtinput = 2; 83 undef($ltinput); 84 } 33 my $prenet = new NetAddr::IP "0.0.0.0/$ltinput"; 34 my $net = new NetAddr::IP "0.0.0.0/$input"; 35 my $postnet = new NetAddr::IP "0.0.0.0/$gtinput"; 85 36 86 my ($center, $left, $right) = ('','',''); 87 my $subnet; 88 89 #add the subnet masks 90 91 if($input) 92 { 93 $subnet = 256 - $input; 94 $center = qq(<div style="background-color: #00ff00">255.255.255.$subnet </div>); 95 } 37 print qq(<div class="center"> 38 <table align="center" cellspacing="3" cellpadding="3"> 39 <tr> 40 <td class="heading" align="center">Results for /$ltinput</td> 41 <td class="heading" align="center">Results for /$input</td> 42 <td class="heading" align="center">Results for /$gtinput</td> 43 </tr> 44 ); 96 45 97 if($ltinput) 98 { 99 $subnet = 256 - $ltinput; 100 $right = qq(<div style="background-color: #00ff00">255.255.255.$subnet</div>); 101 } 102 if($gtinput) 103 { 104 $subnet = 256 - $gtinput; 105 $left = qq(<div style="background-color: #00ff00">255.255.255.$subnet</div>); 106 } 46 print qq(<tr><td valign="top">\n). 47 qq( <div class="mask">).$prenet->mask."</div>\n". 48 qq( <div class="wildcard">).$prenet->wildcard."</div>\n". 49 getranges($ltinput). 50 qq(</td>\n<td valign="top" bgcolor="#d0e0e0">\n). 51 qq( <div class="mask">).$net->mask."</div>\n". 52 qq( <div class="wildcard">).$net->wildcard."</div>\n". 53 getranges($input). 54 qq(</td>\n<td valign="top">). 55 qq( <div class="mask">).$postnet->mask."</div>\n". 56 qq( <div class="wildcard">).$postnet->wildcard."</div>\n". 57 getranges($gtinput); 58 59 print "</td></tr>\n</table>\n"; 60 61 print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading"> 62 </div> 63 </body> 64 </html> 65 ); 66 67 # Just In Case 68 exit 0; 69 70 # subs 71 sub xrange { 72 my $block = shift; 73 my $masklen = shift; 74 my $data = $block->range; 75 if ($masklen >= 24) { 76 $data =~ s/\b0\.0\.0\./x.x.x./g; 77 } elsif ($masklen >=16) { 78 $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g; 79 } elsif ($masklen >=8) { 80 $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g; 81 } 82 return $data; 83 } # xrange() 107 84 108 85 109 for(my $i = 0; $i < 256; $i++) 110 { 111 #left display -- one less than requested 112 if(defined($gtinput) && $i % $gtinput == 0) 113 { 114 my $upper = $i + $gtinput -1; 115 $left .= "x.x.x.$i - x.x.x.$upper</br>\n"; 116 } 117 118 #center display -- the one requested 119 if($i % $input == 0 ) 120 { 121 my $upper = $i + $input - 1; 122 $center .= "x.x.x.$i - x.x.x.$upper </br>\n"; 123 } 124 125 #right display -- one more than requested 126 if(defined($ltinput) && $i % $ltinput == 0) 127 { 128 my $upper = $i + $ltinput -1; 129 $right .= "x.x.x.$i - x.x.x.$upper </br>\n"; 130 } 131 } 132 133 print qq(<tr><td valign="top" >$left</td> 134 <td valign="top" bgcolor="#d0e0e0">$center</td> 135 <td valign="top" >$right</td></tr> 136 ); 137 print "</table>\n"; 138 139 print '<input type="button" value="Back" onclick="history.go(-1)" class="heading"></div></body></html>'; 140 141 } 142 143 144 sub parse_post { 145 my $buffer; 146 if ($ENV{'REQUEST_METHOD'} eq "GET") { 147 $buffer=$ENV{'QUERY_STRING'} 148 } elsif ($ENV{'REQUEST_METHOD'} eq 'POST' && $ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded") { 149 read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); 86 sub getranges { 87 my $masklen = shift; 88 my $ret = ''; 89 my $super; 90 if ($masklen < 8) { 91 $super = new NetAddr::IP "0.0.0.0/0"; 92 } elsif ($masklen < 16) { 93 $super = new NetAddr::IP "0.0.0.0/8"; 94 } elsif ($masklen < 24) { 95 $super = new NetAddr::IP "0.0.0.0/16"; 150 96 } else { 151 $buffer = $ENV{'QUERY_STRING'}; 152 $buffer || read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); 97 $super = new NetAddr::IP "0.0.0.0/24"; 153 98 } 154 my @pairs = split(/&/, $buffer); 155 my %webvarLocal; 156 foreach my $pair (@pairs) { 157 my ($name, $value) = split(/=/, $pair); 158 $name =~ tr/+/ /; 159 $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; 160 $value =~ tr/+/ /; 161 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; 162 $value =~ s/\'/\\\'/g; 163 $webvarLocal{$name} = $value; 99 foreach my $net ($super->split($masklen)) { 100 $ret .= "\t".xrange($net,$masklen)."<br />\n"; 164 101 } 165 return %webvarLocal; 166 } 167 sub printAndExit 168 { 169 my $errStr = $_[0]; 170 print qq( 171 <center><p class="regular"> $errStr </p> 172 <input type="button" value="Back" onclick="history.go(-1)" class="heading"> 173 </center> 174 ); 175 exit(0); 176 } 102 return $ret; 103 } # getranges()
Note:
See TracChangeset
for help on using the changeset viewer.