summaryrefslogtreecommitdiff
path: root/lib/Vyatta/Misc.pm
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2009-01-13 14:47:21 -0800
committerStephen Hemminger <stephen.hemminger@vyatta.com>2009-01-13 14:47:21 -0800
commit3ddcf27d4f42a5b7f1453de6832a20088a129282 (patch)
treebc48a83f715d483ae1ee83c74dc9da1a3e7f6127 /lib/Vyatta/Misc.pm
parentbfa6748211a053e31edc710626413701e66e4c50 (diff)
downloadvyatta-cfg-3ddcf27d4f42a5b7f1453de6832a20088a129282.tar.gz
vyatta-cfg-3ddcf27d4f42a5b7f1453de6832a20088a129282.zip
Better usage of NetAddr::IP
Only get address once, and don't call new() directly, use in OO style.
Diffstat (limited to 'lib/Vyatta/Misc.pm')
-rwxr-xr-xlib/Vyatta/Misc.pm27
1 files changed, 13 insertions, 14 deletions
diff --git a/lib/Vyatta/Misc.pm b/lib/Vyatta/Misc.pm
index a7bd075..dc8b89e 100755
--- a/lib/Vyatta/Misc.pm
+++ b/lib/Vyatta/Misc.pm
@@ -33,6 +33,7 @@ use strict;
use Vyatta::Config;
use Vyatta::Interface;
+use NetAddr::IP;
sub get_sysfs_value {
my ($intf, $name) = @_;
@@ -138,7 +139,7 @@ sub getNetAddrIP {
$intf or return;
foreach my $addr ($intf->addresses()) {
- my $ip = new NetAddr::IP->new($addr);
+ my $ip = new NetAddr::IP $addr;
next unless ($ip && ip->version() == 4);
return $ip;
}
@@ -148,22 +149,20 @@ sub getNetAddrIP {
sub is_ip_v4_or_v6 {
my $addr = shift;
- my $ip = NetAddr::IP->new($addr);
- if (defined $ip && $ip->version() == 4) {
+ my $ip = new NetAddr::IP $addr;
+ return unless defined $ip;
+
+ my $vers = $ip->version();
+ if ($vers == 4) {
#
- # the call to IP->new() will accept 1.1 and consider
- # it to be 1.1.0.0, so add a check to force all
- # 4 octets to be defined
- #
- return if ($addr !~ /\d+\.\d+\.\d+\.\d+/); # unndef
- return 4;
- }
- $ip = NetAddr::IP->new6($addr);
- if (defined $ip && $ip->version() == 6) {
+ # NetAddr::IP will accept short forms 1.1 and hostnames
+ # so check if all 4 octets are defined
+ return 4 unless ($addr !~ /\d+\.\d+\.\d+\.\d+/); # undef
+ } elsif ($vers == 6) {
return 6;
}
-
- return; # undef
+
+ # default return of undefined (ie false)
}
sub isIpAddress {