1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 | use CGI::Carp qw(fatalsToBrowser);
|
---|
6 | use NetAddr::IP;
|
---|
7 | use CommonWeb qw(:ALL);;
|
---|
8 |
|
---|
9 | #file snCalc.cgi little subnet calculator app
|
---|
10 |
|
---|
11 | my %webvar = parse_post();
|
---|
12 | my $input;
|
---|
13 |
|
---|
14 | print "Content-Type: text/html\n\n";
|
---|
15 |
|
---|
16 | open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
|
---|
17 | my $start = join('', <HTML>);
|
---|
18 | close(HTML);
|
---|
19 | print $start;
|
---|
20 |
|
---|
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;
|
---|
28 | }
|
---|
29 |
|
---|
30 | my $ltinput = $input - 1;
|
---|
31 | my $gtinput = $input + 1;
|
---|
32 |
|
---|
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";
|
---|
36 |
|
---|
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 | );
|
---|
45 |
|
---|
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()
|
---|
84 |
|
---|
85 |
|
---|
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";
|
---|
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()
|
---|