From 2abadfe9fda3679c623075d0b8dc2ca15b8e0a54 Mon Sep 17 00:00:00 2001 From: Gernot Fuchs Date: Thu, 23 Feb 2017 18:01:14 +0100 Subject: [PATCH] TT#11537 Add helper template for gethostbyname For iptables firewall rules it is necessary to translate hostnames, which may be used in config.yml into a list of IPs as returned by "gethostbyname". Change-Id: I149227d5031534d3826a2c91012a599f7c7c4756 --- lib/get_host_by_name | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/get_host_by_name diff --git a/lib/get_host_by_name b/lib/get_host_by_name new file mode 100644 index 00000000..dc06d8bb --- /dev/null +++ b/lib/get_host_by_name @@ -0,0 +1,27 @@ +[% + # Return an array of IPs from a given hostname + # + # @param argv.name The hostname to lookup + # @param argv.type The address family to lookup ("AF_INET" for IPv4, + # "AF_INET6" for IPv6, and "" for both) + # @return out The array of ips +-%] +[% out = [] -%] +[% LOAD_PERL => 1 -%] +[% PERL -%] + use Socket qw(:all); + + my $err; + my $ipaddr; + my @res; + my @ips; + + ($err, @res) = getaddrinfo("[% argv.name %]", "", {socktype => SOCK_RAW, family => [% argv.type %]}); + while( my $ai = shift @res ) { + ($err, $ipaddr) = getnameinfo($ai->{addr}, NI_NUMERICHOST, NIx_NOSERV); + push(@ips, $ipaddr); + } + + $stash->set(out => \@ips); +[% END -%] +[% out = out.sort -%]