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

Last change on this file since 497 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
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use CGI::Carp qw(fatalsToBrowser);
6use NetAddr::IP;
7use CommonWeb qw(:ALL);;
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
16open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
17my $start = join('', <HTML>);
18close(HTML);
19print $start;
20
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;
28}
29
30my $ltinput = $input - 1;
31my $gtinput = $input + 1;
32
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";
36
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);
45
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()
84
85
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";
96 } else {
97 $super = new NetAddr::IP "0.0.0.0/24";
98 }
99 foreach my $net ($super->split($masklen)) {
100 $ret .= "\t".xrange($net,$masklen)."<br />\n";
101 }
102 return $ret;
103} # getranges()
Note: See TracBrowser for help on using the repository browser.