From: Emmanuel Kasper Date: Wed, 22 Mar 2017 11:41:26 +0000 (+0100) Subject: Add utility subroutine to get the fully qualified domain name of a host X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=d3c8f0c182fb739a7e3e4da58b0652a791b43772 Add utility subroutine to get the fully qualified domain name of a host --- diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 406aa2a..f642286 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -4,7 +4,7 @@ use strict; use warnings; use POSIX qw(EINTR EEXIST EOPNOTSUPP); use IO::Socket::IP; -use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED); +use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED AI_CANONNAME SOCK_DGRAM); use IO::Select; use File::Basename; use File::Path qw(make_path); @@ -1197,6 +1197,24 @@ sub get_host_address_family { return $res[0]->{family}; } +# get the fully qualified domain name of a host +# same logic as hostname(1): The FQDN is the name getaddrinfo(3) returns, +# given a nodename as a parameter +sub get_fqdn { + my ($nodename) = @_; + + my $hints = { + flags => AI_CANONNAME, + socktype => SOCK_DGRAM + }; + + my ($err, @addrs) = Socket::getaddrinfo($nodename, undef, $hints); + + die "getaddrinfo: $err" if $err; + + return $addrs[0]->{canonname}; +} + # Parses any sane kind of host, or host+port pair: # The port is always optional and thus may be undef. sub parse_host_and_port {