[4] | 1 | #!/usr/bin/perl
|
---|
| 2 |
|
---|
| 3 | use strict;
|
---|
| 4 | use warnings;
|
---|
[371] | 5 | use CGI::Carp qw(fatalsToBrowser);
|
---|
[447] | 6 | use CGI::Simple;
|
---|
[371] | 7 | use NetAddr::IP;
|
---|
| 8 | use CommonWeb qw(:ALL);;
|
---|
[4] | 9 |
|
---|
| 10 | #file snCalc.cgi little subnet calculator app
|
---|
| 11 |
|
---|
[447] | 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 |
|
---|
[4] | 21 | my $input;
|
---|
| 22 |
|
---|
| 23 | print "Content-Type: text/html\n\n";
|
---|
| 24 |
|
---|
[371] | 25 | open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
|
---|
[4] | 26 | my $start = join('', <HTML>);
|
---|
| 27 | close(HTML);
|
---|
| 28 | print $start;
|
---|
| 29 |
|
---|
[371] | 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;
|
---|
[4] | 37 | }
|
---|
| 38 |
|
---|
[371] | 39 | my $ltinput = $input - 1;
|
---|
| 40 | my $gtinput = $input + 1;
|
---|
[4] | 41 |
|
---|
[371] | 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";
|
---|
[4] | 45 |
|
---|
[371] | 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 | );
|
---|
[4] | 54 |
|
---|
[371] | 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);
|
---|
[4] | 67 |
|
---|
[371] | 68 | print "</td></tr>\n</table>\n";
|
---|
[4] | 69 |
|
---|
[371] | 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;
|
---|
[4] | 78 |
|
---|
[371] | 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()
|
---|
[4] | 93 |
|
---|
| 94 |
|
---|
[371] | 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";
|
---|
[4] | 105 | } else {
|
---|
[371] | 106 | $super = new NetAddr::IP "0.0.0.0/24";
|
---|
[4] | 107 | }
|
---|
[371] | 108 | foreach my $net ($super->split($masklen)) {
|
---|
| 109 | $ret .= "\t".xrange($net,$masklen)."<br />\n";
|
---|
[4] | 110 | }
|
---|
[371] | 111 | return $ret;
|
---|
| 112 | } # getranges()
|
---|