From 4bcfe92678e6a0f4d31ea198936a21638012158f Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Sat, 24 Jan 2009 09:42:49 +1100 Subject: Cleanup type checker Use standard getprotoent() rather than manual parsing. Fix perlcritic warnings. --- lib/Vyatta/TypeChecker.pm | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/lib/Vyatta/TypeChecker.pm b/lib/Vyatta/TypeChecker.pm index 086fee7..45803ea 100755 --- a/lib/Vyatta/TypeChecker.pm +++ b/lib/Vyatta/TypeChecker.pm @@ -46,11 +46,11 @@ # } package Vyatta::TypeChecker; +use strict; + our @EXPORT = qw(findType validateType); use base qw(Exporter); -use strict; - my %type_handler = ( 'ipv4' => \&validate_ipv4, 'ipv4net' => \&validate_ipv4net, @@ -124,24 +124,13 @@ sub validate_protocol { my $value = shift; $value = lc $value; return 1 if ($value eq 'all'); + if ($value =~ /^\d+$/) { # 0 has special meaning to iptables return 1 if $value >= 1 and $value <= 255; } - if (!open(IN, ") { - s/^([^#]*)#.*$/$1/; - if ((/^$value\s/) || (/^\S+\s+$value\s/)) { - $ret = 1; - last; - } - } - close IN; - return $ret; + + return defined getprotobyname($value); } sub validate_protocol_negate { @@ -215,9 +204,8 @@ sub validateType { sub findType { my ($value, @candidates) = @_; - if (!defined($value) || ((scalar @candidates) < 1)) { - return undef; - } + return if (!defined($value) || ((scalar @candidates) < 1)); # undef + foreach my $type (@candidates) { if (!defined($type_handler{$type})) { next; @@ -227,7 +215,6 @@ sub findType { return $type; } } - return undef; } 1; -- cgit v1.2.3