source: trunk/cgi-bin/snCalc.cgi@ 937

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

/trunk

Finally merge conversion to HTML::Template from /branches/htmlform

  • Node "hack" showed conflict due to having been added to all branches in parallel
  • editDisplay.html was apparently changed enough that the merged delete caused an irrelevant conflict

Closes #3.

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