From: Wolfgang Bumiller Date: Fri, 8 May 2015 10:24:44 +0000 (+0200) Subject: add utility to fetch the socket family for a hostname X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=a956854f8df53f62fa60e5cf3ec2d953948a2e22 add utility to fetch the socket family for a hostname --- diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 75ca5a6..0e1af09 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -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;