1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use CGI::Carp qw(fatalsToBrowser); |
---|
6 | use CGI::Simple; |
---|
7 | use NetAddr::IP; |
---|
8 | use CommonWeb qw(:ALL);; |
---|
9 | |
---|
10 | #file snCalc.cgi little subnet calculator app |
---|
11 | |
---|
12 | # Set up the CGI object... |
---|
13 | my $q = new CGI::Simple; |
---|
14 | # ... and get query-string params as well as POST params if necessary |
---|
15 | $q->parse_query_string; |
---|
16 | |
---|
17 | # Convenience; saves changing all references to %webvar |
---|
18 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection) |
---|
19 | my %webvar = $q->Vars; |
---|
20 | |
---|
21 | my $input; |
---|
22 | |
---|
23 | print "Content-Type: text/html\n\n"; |
---|
24 | |
---|
25 | open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!"; |
---|
26 | my $start = join('', <HTML>); |
---|
27 | close(HTML); |
---|
28 | print $start; |
---|
29 | |
---|
30 | # Clean up input so we don't divide by zero or something equally silly |
---|
31 | if ($webvar{input} =~ m/(\d+)/) { |
---|
32 | $input = 1*$1; |
---|
33 | $input = 3 if $input < 3; |
---|
34 | $input = 29 if $input > 29; # Not doing IPv6 yet... |
---|
35 | } else { |
---|
36 | $input = 29; |
---|
37 | } |
---|
38 | |
---|
39 | my $ltinput = $input - 1; |
---|
40 | my $gtinput = $input + 1; |
---|
41 | |
---|
42 | my $prenet = new NetAddr::IP "0.0.0.0/$ltinput"; |
---|
43 | my $net = new NetAddr::IP "0.0.0.0/$input"; |
---|
44 | my $postnet = new NetAddr::IP "0.0.0.0/$gtinput"; |
---|
45 | |
---|
46 | print qq(<div class="center"> |
---|
47 | <table align="center" cellspacing="3" cellpadding="3"> |
---|
48 | <tr> |
---|
49 | <td class="heading" align="center">Results for /$ltinput</td> |
---|
50 | <td class="heading" align="center">Results for /$input</td> |
---|
51 | <td class="heading" align="center">Results for /$gtinput</td> |
---|
52 | </tr> |
---|
53 | ); |
---|
54 | |
---|
55 | print qq(<tr><td valign="top">\n). |
---|
56 | qq( <div class="mask">).$prenet->mask."</div>\n". |
---|
57 | qq( <div class="wildcard">).$prenet->wildcard."</div>\n". |
---|
58 | getranges($ltinput). |
---|
59 | qq(</td>\n<td valign="top" bgcolor="#d0e0e0">\n). |
---|
60 | qq( <div class="mask">).$net->mask."</div>\n". |
---|
61 | qq( <div class="wildcard">).$net->wildcard."</div>\n". |
---|
62 | getranges($input). |
---|
63 | qq(</td>\n<td valign="top">). |
---|
64 | qq( <div class="mask">).$postnet->mask."</div>\n". |
---|
65 | qq( <div class="wildcard">).$postnet->wildcard."</div>\n". |
---|
66 | getranges($gtinput); |
---|
67 | |
---|
68 | print "</td></tr>\n</table>\n"; |
---|
69 | |
---|
70 | print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading"> |
---|
71 | </div> |
---|
72 | </body> |
---|
73 | </html> |
---|
74 | ); |
---|
75 | |
---|
76 | # Just In Case |
---|
77 | exit 0; |
---|
78 | |
---|
79 | # subs |
---|
80 | sub xrange { |
---|
81 | my $block = shift; |
---|
82 | my $masklen = shift; |
---|
83 | my $data = $block->range; |
---|
84 | if ($masklen >= 24) { |
---|
85 | $data =~ s/\b0\.0\.0\./x.x.x./g; |
---|
86 | } elsif ($masklen >=16) { |
---|
87 | $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g; |
---|
88 | } elsif ($masklen >=8) { |
---|
89 | $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g; |
---|
90 | } |
---|
91 | return $data; |
---|
92 | } # xrange() |
---|
93 | |
---|
94 | |
---|
95 | sub getranges { |
---|
96 | my $masklen = shift; |
---|
97 | my $ret = ''; |
---|
98 | my $super; |
---|
99 | if ($masklen < 8) { |
---|
100 | $super = new NetAddr::IP "0.0.0.0/0"; |
---|
101 | } elsif ($masklen < 16) { |
---|
102 | $super = new NetAddr::IP "0.0.0.0/8"; |
---|
103 | } elsif ($masklen < 24) { |
---|
104 | $super = new NetAddr::IP "0.0.0.0/16"; |
---|
105 | } else { |
---|
106 | $super = new NetAddr::IP "0.0.0.0/24"; |
---|
107 | } |
---|
108 | foreach my $net ($super->split($masklen)) { |
---|
109 | $ret .= "\t".xrange($net,$masklen)."<br />\n"; |
---|
110 | } |
---|
111 | return $ret; |
---|
112 | } # getranges() |
---|