From 67a0e6ca22220ab77720f04bba04eb3c5895fec9 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 21 Apr 2014 22:08:50 +0100 Subject: [PATCH] fix poking into DNS HOSTS --- Netdisco/lib/App/Netdisco/Util/DNS.pm | 32 ++++++++++++--------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/Util/DNS.pm b/Netdisco/lib/App/Netdisco/Util/DNS.pm index 3ebd846b..9441db6f 100644 --- a/Netdisco/lib/App/Netdisco/Util/DNS.pm +++ b/Netdisco/lib/App/Netdisco/Util/DNS.pm @@ -8,6 +8,13 @@ use Net::DNS; use AnyEvent::DNS; use NetAddr::IP::Lite ':lower'; +# AE::DNS::EtcHosts only works for A/AAAA/SRV, but we want PTR. +# this loads+parses /etc/hosts file using AE. dirty hack. +use AnyEvent::Socket 'format_address'; +use AnyEvent::DNS::EtcHosts; +AnyEvent::DNS::EtcHosts::_load_hosts_unless(sub{},AE::cv); +no AnyEvent::DNS::EtcHosts; # unimport + use base 'Exporter'; our @EXPORT = (); our @EXPORT_OK = qw/ @@ -94,26 +101,15 @@ addresses which resolved. =cut -# AE::DNS::EtcHosts only works for A/AAAA/SRV, but we want PTR. -# this loads+parses /etc/hosts file using AE. dirty hack. - -BEGIN { - use AnyEvent::Socket 'format_address'; - - use AnyEvent::DNS::EtcHosts; - AnyEvent::DNS::EtcHosts::_load_hosts_unless(sub{},AE::cv); - no AnyEvent::DNS::EtcHosts; # unimport - - $AnyEvent::DNS::EtcHosts::HOSTS{$_} - = [ map { [ format_address $_->[0] ] } - @{$AnyEvent::DNS::EtcHosts::HOSTS{$_}} ] - for keys %AnyEvent::DNS::EtcHosts::HOSTS; -} - sub hostnames_resolve_async { my $ips = shift; my $resolver = AnyEvent::DNS->new(); + + my %HOSTS = (); + $HOSTS{$_} = [ map { [ format_address $_->[0] ] } + @{$AnyEvent::DNS::EtcHosts::HOSTS{$_}} ] + for keys %AnyEvent::DNS::EtcHosts::HOSTS; # Set up the condvar my $done = AE::cv; @@ -124,8 +120,8 @@ sub hostnames_resolve_async { next IP if no_resolve($ip); # check /etc/hosts file and short-circuit if found - foreach my $name (reverse sort keys %AnyEvent::DNS::EtcHosts::HOSTS) { - if ($AnyEvent::DNS::EtcHosts::HOSTS{$name}->[0]->[0] eq $ip) { + foreach my $name (reverse sort keys %HOSTS) { + if ($HOSTS{$name}->[0]->[0] eq $ip) { $hash_ref->{'dns'} = $name; next IP; }