From: Thomas Lamprecht Date: Fri, 21 Feb 2020 12:49:18 +0000 (+0100) Subject: get_ip_from_hostname: check all address we get from getaddrinfo_all for non-local IP X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=5bd1e56b7ab7100dcd8d3e75394e1508306ba69d get_ip_from_hostname: check all address we get from getaddrinfo_all for non-local IP 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 --- 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; }