| [4] | 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 |  | 
|---|
| [333] | 18 | if($webvar{input} =~ m/(\d\d)/) | 
|---|
| [4] | 19 | { | 
|---|
| [333] | 20 | $input = 1*$1; | 
|---|
| [4] | 21 | } | 
|---|
|  | 22 | else | 
|---|
|  | 23 | { | 
|---|
| [333] | 24 | $input = 29; | 
|---|
| [4] | 25 | } | 
|---|
|  | 26 |  | 
|---|
| [333] | 27 | if ($input > 32 || $input < 24) | 
|---|
| [4] | 28 | { | 
|---|
| [333] | 29 | printAndExit("Don't know what to do with '$input' as a masklength."); | 
|---|
| [4] | 30 | } | 
|---|
|  | 31 | else | 
|---|
| [333] | 32 | { | 
|---|
| [4] | 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 | ); | 
|---|
|  | 39 |  | 
|---|
|  | 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 | } | 
|---|
|  | 85 |  | 
|---|
|  | 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 | } | 
|---|
|  | 96 |  | 
|---|
|  | 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 | } | 
|---|
|  | 107 |  | 
|---|
|  | 108 |  | 
|---|
|  | 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}); | 
|---|
|  | 150 | } else { | 
|---|
|  | 151 | $buffer = $ENV{'QUERY_STRING'}; | 
|---|
|  | 152 | $buffer || read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); | 
|---|
|  | 153 | } | 
|---|
|  | 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; | 
|---|
|  | 164 | } | 
|---|
|  | 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 | } | 
|---|