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

Last change on this file since 447 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
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use CGI::Carp qw(fatalsToBrowser);
6use CGI::Simple;
7use NetAddr::IP;
8use CommonWeb qw(:ALL);;
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
25open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
26my $start = join('', <HTML>);
27close(HTML);
28print $start;
29
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;
37}
38
39my $ltinput = $input - 1;
40my $gtinput = $input + 1;
41
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";
45
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);
54
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);
67
68print "</td></tr>\n</table>\n";
69
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;
78
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()
93
94
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";
105 } else {
106 $super = new NetAddr::IP "0.0.0.0/24";
107 }
108 foreach my $net ($super->split($masklen)) {
109 $ret .= "\t".xrange($net,$masklen)."<br />\n";
110 }
111 return $ret;
112} # getranges()
Note: See TracBrowser for help on using the repository browser.