From d0c5d7439bc01534981c402dc518623af56138dd Mon Sep 17 00:00:00 2001 From: jbjohnso Date: Wed, 26 Jan 2011 15:43:26 +0000 Subject: [PATCH] -Expose more capability, for multiple address returns and requesting only v4 or v6 addresses git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@8747 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- perl-xCAT/xCAT/NetworkUtils.pm | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/perl-xCAT/xCAT/NetworkUtils.pm b/perl-xCAT/xCAT/NetworkUtils.pm index a0a1ebdf6..f2ba6313f 100755 --- a/perl-xCAT/xCAT/NetworkUtils.pm +++ b/perl-xCAT/xCAT/NetworkUtils.pm @@ -132,7 +132,7 @@ sub gethostname() { if ($socket6support) # the getaddrinfo and getnameinfo supports both IPv4 and IPv6 { - my ($family, $socket, $protocol, $ip, $name) = Socket6::getaddrinfo($iporhost,0); + my ($family, $socket, $protocol, $ip, $name) = Socket6::getaddrinfo($iporhost,0,AF_UNSPEC,SOCK_STREAM,6); #specifically ask for TCP capable records, maximizing chance of no more than one return per v4/v6 my $host = (Socket6::getnameinfo($ip))[0]; if ($host eq $iporhost) # can not resolve { @@ -223,8 +223,16 @@ sub getipaddr { if ($socket6support) # the getaddrinfo and getnameinfo supports both IPv4 and IPv6 { - my ($family, $socket, $protocol, $ip, $name) = Socket6::getaddrinfo($iporhost,0); - if ($ip) + my @returns; + my $family=AF_UNSPEC; + if ($extraarguments{OnlyV6}) { + $family=AF_INET6; + } elsif ($extraarguments{OnlyV4}) { + $family=AF_INET; + } + my @addrinfo = Socket6::getaddrinfo($iporhost,0,$family,SOCK_STREAM,6); + my ($family, $socket, $protocol, $ip, $name) = splice(@addrinfo,0,5); + while ($ip) { if ($extraarguments{GetNumber}) { #return a BigInt for compare, e.g. for comparing ip addresses for determining if they are in a common network or range my $ip = (Socket6::getnameinfo($ip, Socket6::NI_NUMERICHOST()))[0]; @@ -233,12 +241,17 @@ sub getipaddr $bignumber->blsft(32); $bignumber->badd($_); } - return $bignumber; + push(@returns,$bignumber); } else { - return (Socket6::getnameinfo($ip, Socket6::NI_NUMERICHOST()))[0]; + push @returns,(Socket6::getnameinfo($ip, Socket6::NI_NUMERICHOST()))[0]; + } + if (scalar @addrinfo and $extraargumests{GetAllAddresses}) { + ($family, $socket, $protocol, $ip, $name) = splice(@addrinfo,0,5); + } else { + $ip=0; } } - return undef; + return @returns; } else {