source: branches/stable/cgi-bin/snCalc.cgi@ 450

Last change on this file since 450 was 360, checked in by Kris Deugau, 18 years ago

/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.
  • Property svn:executable set to *
File size: 2.5 KB
RevLine 
[4]1#!/usr/bin/perl
2
3use strict;
4use warnings;
[360]5use CGI::Carp qw(fatalsToBrowser);
6use NetAddr::IP;
7use CommonWeb qw(:ALL);;
[4]8
9#file snCalc.cgi little subnet calculator app
10
11my %webvar = parse_post();
12my $input;
13
14print "Content-Type: text/html\n\n";
15
[360]16open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
[4]17my $start = join('', <HTML>);
18close(HTML);
19print $start;
20
[360]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;
[4]28}
29
[360]30my $ltinput = $input - 1;
31my $gtinput = $input + 1;
[4]32
[360]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";
[4]36
[360]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);
[4]45
[360]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);
[4]58
[360]59print "</td></tr>\n</table>\n";
[4]60
[360]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;
[4]69
[360]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()
[4]84
85
[360]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";
[4]96 } else {
[360]97 $super = new NetAddr::IP "0.0.0.0/24";
[4]98 }
[360]99 foreach my $net ($super->split($masklen)) {
100 $ret .= "\t".xrange($net,$masklen)."<br />\n";
[4]101 }
[360]102 return $ret;
103} # getranges()
Note: See TracBrowser for help on using the repository browser.