Changeset 360


Ignore:
Timestamp:
04/20/07 15:48:42 (17 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Update subnet calculator:

  • Clean up HTML output
  • Include "Wildcard" mask
  • Behave correctly on many more subnet lengths - valid range is now 3-29. NetAddr::IP starts assuming IPv6 with a mask of /1 (sort of, sometimes). Assumes a /29 subnet if input is invalid.
Location:
branches/stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/cgi-bin/snCalc.cgi

    r333 r360  
    33use strict;             
    44use warnings;   
     5use CGI::Carp qw(fatalsToBrowser);
     6use NetAddr::IP;
     7use CommonWeb qw(:ALL);;
    58
    69#file snCalc.cgi        little subnet calculator app
     
    1114print "Content-Type: text/html\n\n";
    1215
    13 open(HTML, "../startsn.html")|| die "Could not open starsn.html :$!";
     16open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
    1417my $start = join('', <HTML>);
    1518close(HTML);
    1619print $start;
    1720
    18 if($webvar{input} =~ m/(\d\d)/)
    19 {
    20         $input = 1*$1;
    21 }
    22 else
    23 {
    24         $input = 29;
     21# Clean up input so we don't divide by zero or something equally silly
     22if ($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;
    2528}
    2629
    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                 );     
     30my $ltinput = $input - 1;
     31my $gtinput = $input + 1;
    3932
    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                 }
     33my $prenet = new NetAddr::IP "0.0.0.0/$ltinput";
     34my $net = new NetAddr::IP "0.0.0.0/$input";
     35my $postnet = new NetAddr::IP "0.0.0.0/$gtinput";
    8536
    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                 }
     37print 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);
    9645
    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                 }
     46print 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
     59print "</td></tr>\n</table>\n";
     60
     61print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading">
     62</div>
     63</body>
     64</html>
     65);
     66       
     67# Just In Case
     68exit 0;
     69
     70# subs
     71sub 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()
    10784
    10885
    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&nbsp;&nbsp;</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&nbsp;&nbsp;</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});
     86sub 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";
    15096  } else {
    151     $buffer = $ENV{'QUERY_STRING'};
    152     $buffer || read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
     97    $super = new NetAddr::IP "0.0.0.0/24";
    15398  }
    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";
    164101  }
    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()
  • branches/stable/help.html

    r359 r360  
    5050<td colspan="2">
    5151<form method="POST" action="cgi-bin/snCalc.cgi">
    52 <input type="text" size="5" maxlength="10" name="input" class="regular">
    53 <input type="submit" value="Calculate" class="heading">
     52 / <input type="text" size="5" maxlength="10" name="input" class="regular">
     53<input type="submit" value="Calculate" class="heading">&nbsp; Show
     54<span class="mask">subnet mask</span>, <span class="wildcard">wildcard mask</span>,
     55and possible subnet ranges for the entered mask length.
    5456</form>
    5557</tr>
  • branches/stable/ipdb.css

    r286 r360  
    8181        color: red;
    8282}
     83
     84.mask {
     85        background: #00ff00;
     86}
     87
     88.wildcard {
     89        background: #ffff00;
     90}
Note: See TracChangeset for help on using the changeset viewer.