[pve-devel] [PATCH common] Add utility subroutine to get the fully qualified domain name of a host

Emmanuel Kasper e.kasper at proxmox.com
Wed Mar 22 12:41:26 CET 2017


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

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 69c8730..4815d61 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 {
-- 
2.1.4





More information about the pve-devel mailing list