From 3ddcf27d4f42a5b7f1453de6832a20088a129282 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Tue, 13 Jan 2009 14:47:21 -0800 Subject: Better usage of NetAddr::IP Only get address once, and don't call new() directly, use in OO style. --- lib/Vyatta/Misc.pm | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'lib') 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 { -- cgit v1.2.3