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

Last change on this file since 608 was 593, checked in by Kris Deugau, 12 years ago

/branches/stable

Merge /trunk r517 (merge /branches/htmlform)
Conflicts all resolved towards /trunk.
Fix a minor syntax error with "while (@data..." -> "while (my @data..."
(may cause merge conflicts later)

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use CGI::Carp qw(fatalsToBrowser);
6use CGI::Simple;
7use HTML::Template;
8use NetAddr::IP;
9
10#file snCalc.cgi little subnet calculator app
11use MyIPDB;
12
13# Set up the CGI object...
14my $q = new CGI::Simple;
15# ... and get query-string params as well as POST params if necessary
16$q->parse_query_string;
17
18# Convenience; saves changing all references to %webvar
19##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
20my %webvar = $q->Vars;
21
22my $input;
23
24print "Content-Type: text/html\n\n";
25
26##fixme: need better method to find templates.
27$ENV{HTML_TEMPLATE_ROOT} = $ENV{SCRIPT_FILENAME};
28$ENV{HTML_TEMPLATE_ROOT} =~ s|cgi-bin/snCalc.cgi||;
29
30my $page = HTML::Template->new(filename => "templates/subnet-calc.tmpl");
31
32# Clean up input so we don't divide by zero or something equally silly
33if ($webvar{input} =~ m/(\d+)/) {
34 $input = 1*$1;
35 $input = 3 if $input < 3;
36 $input = 29 if $input > 29; # Not doing IPv6 yet...
37} else {
38 $input = 29;
39}
40
41my $ltinput = $input - 1;
42my $gtinput = $input + 1;
43
44my $prenet = new NetAddr::IP "0.0.0.0/$ltinput";
45my $net = new NetAddr::IP "0.0.0.0/$input";
46my $postnet = new NetAddr::IP "0.0.0.0/$gtinput";
47
48$page->param(prenet => $ltinput);
49$page->param(net => $input);
50$page->param(postnet => $gtinput);
51$page->param(premask => $prenet->mask);
52$page->param(mask => $net->mask);
53$page->param(postmask => $postnet->mask);
54$page->param(prewildcard => scalar($prenet->wildcard));
55$page->param(wildcard => scalar($net->wildcard));
56$page->param(postwildcard => scalar($postnet->wildcard));
57
58my @prenets;
59foreach (getranges($ltinput)) {
60 my %row = (netrange => $_);
61 push (@prenets, \%row);
62}
63$page->param(prenets => \@prenets);
64my @nets;
65foreach (getranges($input)) {
66 my %row = (netrange => $_);
67 push (@nets, \%row);
68}
69$page->param(nets => \@nets);
70my @postnets;
71foreach (getranges($gtinput)) {
72 my %row = (netrange => $_);
73 push @postnets, \%row;
74}
75$page->param(postnets => \@postnets);
76
77print $page->output;
78
79# Just In Case
80exit 0;
81
82# subs
83sub xrange {
84 my $block = shift;
85 my $masklen = shift;
86 my $data = $block->range;
87 if ($masklen >= 24) {
88 $data =~ s/\b0\.0\.0\./x.x.x./g;
89 } elsif ($masklen >=16) {
90 $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g;
91 } elsif ($masklen >=8) {
92 $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g;
93 }
94 return $data;
95} # xrange()
96
97
98sub getranges {
99 my $masklen = shift;
100 my @ret;
101 my $super;
102 if ($masklen < 8) {
103 $super = new NetAddr::IP "0.0.0.0/0";
104 } elsif ($masklen < 16) {
105 $super = new NetAddr::IP "0.0.0.0/8";
106 } elsif ($masklen < 24) {
107 $super = new NetAddr::IP "0.0.0.0/16";
108 } else {
109 $super = new NetAddr::IP "0.0.0.0/24";
110 }
111 foreach my $net ($super->split($masklen)) {
112 push @ret, xrange($net,$masklen);
113 }
114 return @ret;
115} # getranges()
Note: See TracBrowser for help on using the repository browser.