]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
add utility to fetch the socket family for a hostname
[pve-common.git] / src / PVE / Tools.pm
index 75ca5a6a70ebabf726c1b771fe98ed10a89feed7..0e1af0908400f07e3bb6c6f62e5acd1d8943edc6 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use POSIX qw(EINTR);
 use IO::Socket::IP;
-use Socket qw(AF_INET AF_INET6);
+use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED);
 use IO::Select;
 use File::Basename;
 use File::Path qw(make_path);
@@ -1052,4 +1052,14 @@ sub unpack_sockaddr_in46 {
     return ($family, $port, $host);
 }
 
+sub get_host_address_family {
+    my ($hostname, $socktype) = @_;
+    my %hints = ( flags => AI_V4MAPPED | AI_ALL,
+                 socktype => $socktype );
+    my ($err, @res) = Socket::getaddrinfo($hostname, '0', \%hints);
+    die "failed to resolve $hostname: $err\n" if $err;
+
+    return ${res[0]}->{family};
+}
+
 1;