[pve-devel] [PATCH 4/4] add utility to fetch the socket family for a hostname

Wolfgang Bumiller w.bumiller at proxmox.com
Fri May 8 12:24:44 CEST 2015


---
 src/PVE/Tools.pm | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

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;
-- 
2.1.4





More information about the pve-devel mailing list