From a964870f5c71a1c11fa5cb34ef7d98076a70c91b Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Mon, 12 Jan 2009 14:43:21 -0800 Subject: Use getservbyname to find ports Rather than reading /etc/services directly, use the standard getservbyname function. This should be more efficient (uses dbm), and will work with other nameservices --- lib/Vyatta/Misc.pm | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/lib/Vyatta/Misc.pm b/lib/Vyatta/Misc.pm index e46019b..22b41db 100755 --- a/lib/Vyatta/Misc.pm +++ b/lib/Vyatta/Misc.pm @@ -386,26 +386,6 @@ sub isValidPortRange { return (1, undef); } -my %port_name_hash_tcp = (); -my %port_name_hash_udp = (); -sub buildPortNameHash { - open(IF, ") { - s/#.*$//; - my $is_tcp = /\d\/tcp\s/; - my @names = grep (!/\//, (split /\s/)); - foreach my $name (@names) { - if ($is_tcp) { - $port_name_hash_tcp{$name} = 1; - } else { - $port_name_hash_udp{$name} = 1; - } - } - } - close IF; - return 1; -} - # $str: string representing a port name # $proto: protocol to check # returns ($success, $err) @@ -416,11 +396,10 @@ sub isValidPortName { my $proto = shift; return (undef, "\"\" is not a valid port name for protocol \"$proto\"") if ($str eq ''); - buildPortNameHash() if ((keys %port_name_hash_tcp) == 0); - return (1, undef) if ($proto eq 'tcp' && defined($port_name_hash_tcp{$str})); - return (1, undef) if ($proto eq '6' && defined($port_name_hash_tcp{$str})); - return (1, undef) if ($proto eq 'udp' && defined($port_name_hash_udp{$str})); - return (1, undef) if ($proto eq '17' && defined($port_name_hash_udp{$str})); + + my $port = getservbyname($str, $proto); + return (1, undef) if $port; + return (undef, "\"$str\" is not a valid port name for protocol \"$proto\""); } -- cgit v1.2.3