[pve-devel] [PATCH] remote_node_ip: option to include the packet family

Wolfgang Bumiller w.bumiller at proxmox.com
Tue May 12 11:19:09 CEST 2015


If an array is requested, the function now returns ($ip, $family),
otherweise just the IP alone.
Several ipv6 related changes in other packages need to pass the packet
family to functions and will make use of this functionality.
---
 data/PVE/Cluster.pm | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/data/PVE/Cluster.pm b/data/PVE/Cluster.pm
index 60d6cf1..69eda6d 100644
--- a/data/PVE/Cluster.pm
+++ b/data/PVE/Cluster.pm
@@ -1010,26 +1010,32 @@ sub remote_node_ip {
     my $nodelist = $clinfo->{nodelist};
     if ($nodelist && $nodelist->{$nodename}) {
 	if (my $ip = $nodelist->{$nodename}->{ip}) {
-	    return $ip;
+	    return wantarray ? ($ip, PVE::Tools::get_host_address_family($ip))
+	                     : $ip;
 	}
     }
 
     # fallback: try to get IP by other means
-    my $packed_ip = gethostbyname($nodename);
-    if (defined $packed_ip) {
-        my $ip = inet_ntoa($packed_ip);
+    my ($family, $packed_ip);
 
-	if ($ip =~ m/^127\./) {
-	    die "hostname lookup failed - got local IP address ($nodename = $ip)\n" if !$noerr;
-	    return undef;
-	}
+    eval {
+	my @res = PVE::Tools::getaddrinfo_all($nodename);
+	$family = $res[0]->{family};
+	$packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
+    };
 
-	return $ip;
+    if ($@) {
+	die "hostname lookup failed:\n$@" if !$noerr;
+	return undef;
     }
 
-    die "unable to get IP for node '$nodename' - node offline?\n" if !$noerr;
+    my $ip = Socket::inet_ntop($family, $packed_ip);
+    if ($ip =~ m/^127\.|^::1$/) {
+	die "hostname lookup failed - got local IP address ($nodename = $ip)\n" if !$noerr;
+	return undef;
+    }
 
-    return undef;
+    return wantarray ? ($ip, $family) : $ip;
 }
 
 # ssh related utility functions
-- 
2.1.4





More information about the pve-devel mailing list