diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2009-01-12 14:43:21 -0800 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2009-01-12 14:43:21 -0800 |
commit | a964870f5c71a1c11fa5cb34ef7d98076a70c91b (patch) | |
tree | 41f2174750a05bde711954ab0828d3a488521756 /lib/Vyatta | |
parent | 99ea2c9a2bfc9f4af221ca735d26410dae3b2823 (diff) | |
download | vyatta-cfg-a964870f5c71a1c11fa5cb34ef7d98076a70c91b.tar.gz vyatta-cfg-a964870f5c71a1c11fa5cb34ef7d98076a70c91b.zip |
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
Diffstat (limited to 'lib/Vyatta')
-rwxr-xr-x | lib/Vyatta/Misc.pm | 29 |
1 files 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, "</etc/services") or return 0; - while (<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\""); } |