]> git.proxmox.com Git - pve-common.git/commitdiff
add utility to fetch the socket family for a hostname
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 8 May 2015 10:24:44 +0000 (12:24 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 8 May 2015 10:36:30 +0000 (12:36 +0200)
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;