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

Last change on this file since 523 was 507, checked in by Kris Deugau, 13 years ago

/branches/stable

Make the fixed web path at least configurable in one place rather
than completely hardcoded across many files.
Update initial database tabledef SQL
Bump version

  • Property svn:executable set to *
File size: 2.6 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
[507]10use MyIPDB;
[4]11
12my %webvar = parse_post();
13my $input;
14
15print "Content-Type: text/html\n\n";
16
[360]17open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
[4]18my $start = join('', <HTML>);
19close(HTML);
[507]20$start =~ s/\$\$WEBPATH\$\$/$IPDB::webpath/g;
[4]21print $start;
22
[360]23# Clean up input so we don't divide by zero or something equally silly
24if ($webvar{input} =~ m/(\d+)/) {
25 $input = 1*$1;
26 $input = 3 if $input < 3;
27 $input = 29 if $input > 29; # Not doing IPv6 yet...
28} else {
29 $input = 29;
[4]30}
31
[360]32my $ltinput = $input - 1;
33my $gtinput = $input + 1;
[4]34
[360]35my $prenet = new NetAddr::IP "0.0.0.0/$ltinput";
36my $net = new NetAddr::IP "0.0.0.0/$input";
37my $postnet = new NetAddr::IP "0.0.0.0/$gtinput";
[4]38
[360]39print qq(<div class="center">
40<table align="center" cellspacing="3" cellpadding="3">
41<tr>
42 <td class="heading" align="center">Results for /$ltinput</td>
43 <td class="heading" align="center">Results for /$input</td>
44 <td class="heading" align="center">Results for /$gtinput</td>
45</tr>
46);
[4]47
[360]48print qq(<tr><td valign="top">\n).
49 qq( <div class="mask">).$prenet->mask."</div>\n".
50 qq( <div class="wildcard">).$prenet->wildcard."</div>\n".
51 getranges($ltinput).
52 qq(</td>\n<td valign="top" bgcolor="#d0e0e0">\n).
53 qq( <div class="mask">).$net->mask."</div>\n".
54 qq( <div class="wildcard">).$net->wildcard."</div>\n".
55 getranges($input).
56 qq(</td>\n<td valign="top">).
57 qq( <div class="mask">).$postnet->mask."</div>\n".
58 qq( <div class="wildcard">).$postnet->wildcard."</div>\n".
59 getranges($gtinput);
[4]60
[360]61print "</td></tr>\n</table>\n";
[4]62
[360]63print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading">
64</div>
65</body>
66</html>
67);
68
69# Just In Case
70exit 0;
[4]71
[360]72# subs
73sub xrange {
74 my $block = shift;
75 my $masklen = shift;
76 my $data = $block->range;
77 if ($masklen >= 24) {
78 $data =~ s/\b0\.0\.0\./x.x.x./g;
79 } elsif ($masklen >=16) {
80 $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g;
81 } elsif ($masklen >=8) {
82 $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g;
83 }
84 return $data;
85} # xrange()
[4]86
87
[360]88sub getranges {
89 my $masklen = shift;
90 my $ret = '';
91 my $super;
92 if ($masklen < 8) {
93 $super = new NetAddr::IP "0.0.0.0/0";
94 } elsif ($masklen < 16) {
95 $super = new NetAddr::IP "0.0.0.0/8";
96 } elsif ($masklen < 24) {
97 $super = new NetAddr::IP "0.0.0.0/16";
[4]98 } else {
[360]99 $super = new NetAddr::IP "0.0.0.0/24";
[4]100 }
[360]101 foreach my $net ($super->split($masklen)) {
102 $ret .= "\t".xrange($net,$masklen)."<br />\n";
[4]103 }
[360]104 return $ret;
105} # getranges()
Note: See TracBrowser for help on using the repository browser.