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

Last change on this file since 719 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
RevLine 
[4]1#!/usr/bin/perl
2
3use strict;
4use warnings;
[360]5use CGI::Carp qw(fatalsToBrowser);
[593]6use CGI::Simple;
7use HTML::Template;
[360]8use NetAddr::IP;
[4]9
10#file snCalc.cgi little subnet calculator app
[507]11use MyIPDB;
[4]12
[593]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
[4]22my $input;
23
24print "Content-Type: text/html\n\n";
25
[593]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||;
[4]29
[593]30my $page = HTML::Template->new(filename => "templates/subnet-calc.tmpl");
31
[360]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;
[4]39}
40
[360]41my $ltinput = $input - 1;
42my $gtinput = $input + 1;
[4]43
[360]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";
[4]47
[593]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));
[4]57
[593]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);
[4]76
[593]77print $page->output;
[360]78
79# Just In Case
80exit 0;
[4]81
[360]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()
[4]96
97
[360]98sub getranges {
99 my $masklen = shift;
[593]100 my @ret;
[360]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";
[4]108 } else {
[360]109 $super = new NetAddr::IP "0.0.0.0/24";
[4]110 }
[360]111 foreach my $net ($super->split($masklen)) {
[593]112 push @ret, xrange($net,$masklen);
[4]113 }
[593]114 return @ret;
[360]115} # getranges()
Note: See TracBrowser for help on using the repository browser.