summaryrefslogtreecommitdiff
path: root/lib/Vyatta
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2009-01-12 14:43:21 -0800
committerStephen Hemminger <stephen.hemminger@vyatta.com>2009-01-12 14:43:21 -0800
commita964870f5c71a1c11fa5cb34ef7d98076a70c91b (patch)
tree41f2174750a05bde711954ab0828d3a488521756 /lib/Vyatta
parent99ea2c9a2bfc9f4af221ca735d26410dae3b2823 (diff)
downloadvyatta-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-xlib/Vyatta/Misc.pm29
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\"");
}