]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/Utils.pm
PMG/ClusterConfig.pm: better code to read/write cluster config
[pmg-api.git] / PMG / Utils.pm
index e7e7cc0ea7111d121d6f72f773292c38c7292474..a04518d5c1726c1bd62c90c5b67def4eee1e75a9 100644 (file)
@@ -15,6 +15,7 @@ use Time::HiRes qw (gettimeofday);
 use Xdgmime;
 use Data::Dumper;
 use Net::IP;
+use Socket;
 
 use PVE::Network;
 use PVE::Tools;
@@ -401,4 +402,30 @@ sub service_cmd {
     PVE::Tools::run_command(['systemctl', $cmd, $service]);
 };
 
+# this is also used to get the IP of the local node
+sub lookup_node_ip {
+    my ($nodename, $noerr) = @_;
+
+    my ($family, $packed_ip);
+
+    eval {
+       my @res = PVE::Tools::getaddrinfo_all($nodename);
+       $family = $res[0]->{family};
+       $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
+    };
+
+    if ($@) {
+       die "hostname lookup failed:\n$@" if !$noerr;
+       return undef;
+    }
+
+    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 wantarray ? ($ip, $family) : $ip;
+}
+
 1;