[pve-devel] applied: [PATCH common] get_ip_from_hostname: check all address we get from getaddrinfo_all for non-local IP

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Feb 21 13:55:17 CET 2020


This was limited without reason to checking only the first IP we get
returned from getaddrinfo_all, but we can have multiple IPs for a
hostname, and possible one of them is local but another not, so check
all and only die if no non-local address at all got found.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/Network.pm | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm
index 5f40353..98a58fa 100644
--- a/src/PVE/Network.pm
+++ b/src/PVE/Network.pm
@@ -607,19 +607,22 @@ sub addr_to_ip {
 sub get_ip_from_hostname {
     my ($hostname, $noerr) = @_;
 
-    my ($family, $ip);
-
-    eval {
-	my @res = PVE::Tools::getaddrinfo_all($hostname);
-	$family = $res[0]->{family};
-	$ip = addr_to_ip($res[0]->{addr})
-    };
+    my @res = eval { PVE::Tools::getaddrinfo_all($hostname) };
     if ($@) {
 	die "hostname lookup '$hostname' failed - $@" if !$noerr;
 	return undef;
     }
 
-    if ($ip =~ m/^127\.|^::1$/) {
+    my ($ip, $family);
+    for my $ai (@res) {
+	$family = $ai->{family};
+	my $tmpip = addr_to_ip($ai->{addr});
+	if ($tmpip !~ m/^127\.|^::1$/) {
+	    $ip = $tmpip;
+	    last;
+	}
+    }
+    if (!defined($ip) ) {
 	die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr;
 	return undef;
     }
-- 
2.20.1





More information about the pve-devel mailing list