source: branches/htmlform/cgi-bin/snCalc.cgi@ 455

Last change on this file since 455 was 447, checked in by Kris Deugau, 14 years ago

/branches/htmlform

Switch all scripts to use CGI::Simple for HTML form data munging
instead of legacy CommonWeb.pm sub. Remove parse_post() sub from
CommonWeb.pm. See #15.

  • Property svn:executable set to *
File size: 2.8 KB
RevLine 
[4]1#!/usr/bin/perl
2
3use strict;
4use warnings;
[371]5use CGI::Carp qw(fatalsToBrowser);
[447]6use CGI::Simple;
[371]7use NetAddr::IP;
8use CommonWeb qw(:ALL);;
[4]9
10#file snCalc.cgi little subnet calculator app
11
[447]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
[4]21my $input;
22
23print "Content-Type: text/html\n\n";
24
[371]25open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
[4]26my $start = join('', <HTML>);
27close(HTML);
28print $start;
29
[371]30# Clean up input so we don't divide by zero or something equally silly
31if ($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]39my $ltinput = $input - 1;
40my $gtinput = $input + 1;
[4]41
[371]42my $prenet = new NetAddr::IP "0.0.0.0/$ltinput";
43my $net = new NetAddr::IP "0.0.0.0/$input";
44my $postnet = new NetAddr::IP "0.0.0.0/$gtinput";
[4]45
[371]46print 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]55print 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]68print "</td></tr>\n</table>\n";
[4]69
[371]70print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading">
71</div>
72</body>
73</html>
74);
75
76# Just In Case
77exit 0;
[4]78
[371]79# subs
80sub 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]95sub 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()
Note: See TracBrowser for help on using the repository browser.