questions
Marten Terpstra
Fri Feb 18 10:47:00 CET 1994
Andrew Adams <ala at merit.edu> writes * Finally, ripe-108 mentions a guardian syntax checker called "checkguard." * Is this available? Of course I forgot. You might have to change the #! and @INC lines. -Marten #!/bin/perl # # This is a simple program to check the syntax of a guarded file # to make help the guardians out a little bit. # # $RCSfile: checkguard.pl,v $ # $Revision: 0.11 $ # $Author: tony $ # $Date: 1994/01/14 16:09:12 $ @INC = ("/lib", at INC); require "getopts.pl"; &Getopts('v'); if ( $opt_v ) { $verbose = 1; } if ( $#ARGV) { print STDERR "No configfile given, assuming stdin\n"; } $lineno = 0; $B = &quad2int("128.0.0.0"); $C = &quad2int("192.0.0.0"); while (<ARGV>) { chop; $lineno++; if (/^\#/ || /^\;/) { $comment++; next; } s/^inetnum:\s*//; if (/^\s*$/) { $empty++; next; } if (/ \- /) { # Assume a range @range = split(/\s+/, $_, 3); if ($range[1] ne "-") { &ps; next; } elsif (!&isnetnum($range[0])) { &ps(" = $range[0] is not a valid IP network"); next; } elsif (!&isnetnum($range[2])) { &ps(" = $range[0] is not a valid IP network"); next; } else { $bot = &quad2int($range[0]); $top = &quad2int($range[2]); if ($top <= $bot) { &ps(" = invalid range given"); next; } # Now break out the range and add to the $seen array if($bot >= $C) { $inc = 256; $class = "c"; } elsif ($bot >= $B) { $inc = 256**2; $class = "b"; } else { $inc = 256**3; $class = "a"; } for ($i = $bot ; $i <= $top ; $i += $inc) { $val = &int2quad($i); if ($seen{"$val"}) { &pw("= $val already seen on line $seen{$val}"); } eval "\$class$class ++;"; # This is yuck !! $seen{"$val"} = $lineno; } next; } } else { if (!&isnetnum($_)) { &ps(" = $_ is not a valid IP network"); next; } } if ($seen{"$_"}) { &pw(" = $_ already seen on line $seen{$_}"); } $netval = &quad2int($_); if($netval >= $C) { $class = "c"; } elsif ($netval >= $B) { $class = "b"; } else { $class = "a"; } eval "\$class$class ++;"; # This is yuck !! $seen{"$_"} = $lineno; next; } if($opt_v) { printf "The file contains:\n%s%s%s%s%s%s", $lineno ? "\t$lineno lines\n" : "", $comment ? "\t$comment comment lines\n" : "", $empty ? "\t$empty emtpy lines\n" : "", $classa ? "\t$classa Class A networks\n" : "", $classb ? "\t$classb Class B networks\n" : "", $classc ? "\t$classc Class C networks\n" : ""; } sub pw { local($str) = @_; printf "warning at line $lineno $str\n"; } sub ps { local($str) = @_; printf "syntax error at line $lineno $str\n"; } sub isnetnum { local($str) = @_; local($ind) = 0; local(@add) = split(/\./, $str); if ($#add != 3) { return 0; } foreach $ind (0..$#add) { if (($add[$ind] !~ /^[0-9][0-9]*/) || ($add[$ind] > 255)) { return 0; } } if (($add[0] < 128) && (($add[1] != 0 ) || ($add[2] != 0 ) || ($add[3] != 0 ))) { return 0; } elsif (($add[0] < 192) && (($add[2] != 0 ) || ($add[3] != 0 ))) { return 0; } elsif (($add[0] < 224) && ($add[3] != 0 )) { return 0; } return 1; } sub quad2int { local($quad) = @_; local(@quads); local($i, $j, $result); # @quads = split(/\./, $quad); $result = 0; for ($i=0; $i<4; $i++) { $j = $quads[$i]; ($j>=0&&$j<256) || die "quad2int: illegal ip number '$quad'\n"; $result += ($j<<(8*(3-$i))); } return $result; } sub int2quad { local($int) = @_; local($i, $j, $result); $result = ""; for ($i=3; $i>=0; $i--) { $j = ($int>>($i*8))&0xff; $result = $result . "$j."; } chop($result); return $result; } -------- Logged at Fri Feb 18 10:54:59 MET 1994 ---------
[ rr-impl Archive ]