]> git.proxmox.com Git - pve-common.git/commitdiff
get_ip_from_hostname: check all address we get from getaddrinfo_all for non-local IP
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 21 Feb 2020 12:49:18 +0000 (13:49 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 21 Feb 2020 12:54:16 +0000 (13:54 +0100)
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@proxmox.com>
src/PVE/Network.pm

index 5f40353ccd6e01ef170c1626c10503ed0294c650..98a58fa98ee683ff2148a50cd24f0e9be44cbd25 100644 (file)
@@ -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;
     }