blib/lib/AnyEvent/DNS/EtcHosts.pm

filter: Statementstmt 94.0% 110 / 117 Branchbran 69.0% 29 / 42 Conditioncond 72.2% 13 / 18 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 91.3% 21 / 23 Podpod 100% 2 / 2 totaltotal 83.9% 178 / 212 CC 32 SCAR 17.1
CC
32
Cov
86%
__ANON__
12.3
register
7.4
_parse_hosts
7.0
CRAP 5.5
↑ Summary
1 package AnyEvent::DNS::EtcHosts;
2  
3 =head1 NAME
4  
5 AnyEvent::DNS::EtcHosts - Use /etc/hosts before DNS
6  
7 =head1 SYNOPSIS
8  
9 =for markdown ```perl
10  
11 use AnyEvent::DNS::EtcHosts;
12  
13 use AnyEvent::DNS;
14 my $cv = AE::cv;
15 AnyEvent::DNS::any 'example.com', sub {
16 say foreach map { $_->[4] } grep { $_->[1] =~ /^(a|aaaa)$/ } @_;
17 $cv->send;
18 };
19  
20 use AnyEvent::Socket;
21 my $cv = AE::cv;
22 AnyEvent::Socket::resolve_sockaddr $domain, $service, $proto, $family, undef, sub {
23 say foreach map { format_address((AnyEvent::Socket::unpack_sockaddr($_->[3]))[1]) } @_;
24 $cv->send;
25 };
26  
27 =for markdown ```
28  
29 =head1 DESCRIPTION
30  
31 AnyEvent::DNS::EtcHosts changes AnyEvent::DNS behavior. The F</etc/hosts> file
32 is searched before DNS, so it is possible to override DNS entries.
33  
34 The DNS lookups are emulated. This resolver returns the standard DNS reply
35 based on F</etc/hosts> file rather than real DNS.
36  
37 You can choose a different file by changing C<PERL_ANYEVENT_HOSTS> environment
38 variable.
39  
40 This module also disables the original L<AnyEvent::Socket>'s helper function
41 which reads F</etc/hosts> file after the DNS entry was not found. It prevents
42 reading this file twice.
43  
44 The L<AnyEvent::Socket> resolver searches IPv4 and IPv6 addresses separately.
45 If you don't want to check the addresses in DNS, both IPv4 and IPv6 addresses
46 should be placed in F</etc/hosts> or the protocol family should be set
47 explicitly for C<resolve_sockaddr> function.
48  
49 =for readme stop
50  
51 =cut
52  
53 3 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 3 our $VERSION = '0.0105';
59  
60 3 ▶use base 'AnyEvent::DNS';
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 3 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 3 ▶use constant DEBUG => $ENV{PERL_ANYEVENT_DNS_ETCHOSTS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 ▶use if DEBUG, 'Data::Dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 3 our $GUARD;
69  
70 =head1 IMPORTS
71  
72 =head2 use AnyEvent::DNS::EtcHosts %args
73  
74 =for markdown ```perl
75  
76 use AnyEvent::DNS::EtcHosts server => '8.8.8.8';
77  
78 $ perl -MAnyEvent::DNS::EtcHosts script.pl
79  
80 =for markdown ```
81  
82 Enables this module globally. Additional arguments will be passed to
83 L<AnyEvent::DNS> constructor.
84  
85 =cut
86  
87 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
88 1 my ($class, %args) = @_;
89 1 $GUARD = $class->register(%args);
90 }
91  
92 =head2 no AnyEvent::DNS::EtcHosts
93  
94 Disables this module globally.
95  
96 =cut
97  
98 0 ▶sub unimport {
Subroutine unimport: not called CC=1 SCAR=6.9
99 0 my ($class) = @_;
100 0 undef $GUARD;
101 }
102  
103 =head1 METHODS
104  
105 =head2 register
106  
107 =for markdown ```perl
108  
109 require AnyEvent::DNS::EtcHosts;
110  
111 $guard = AnyEvent::DNS::EtcHosts->register(%args);
112  
113 undef $guard;
114  
115 =for markdown ```
116  
117 Enables this module in lexical scope. The module will be disabled out of
118 scope. Additional arguments will be passed to L<AnyEvent::DNS> constructor.
119  
120 If you want to use AnyEvent::DNS::EtcHosts in lexical scope only, you should
121 use C<require> rather than C<use> keyword, because C<import> method enables
122 AnyEvent::DNS::EtcHosts globally.
123  
124 =cut
125  
126 3 ▶sub register {
Subroutine register: called CC=7 SCAR=20.0
Pod documented
127 3 my ($class, %args) = @_;
128  
129 3 my $old_resolver = $AnyEvent::DNS::RESOLVER;
130  
131 3 $AnyEvent::DNS::RESOLVER = do {
132 my $resolver = AnyEvent::DNS::EtcHosts->new(
133 untaint => 1,
134 3 ▶ max_outstanding => $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS} || 1,
Condition50%1/2
$ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'} || 1
l!l
03
MC/DC0%0/1
$ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'} || 1
$ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'}
Truth table50%1/2
A
A= $ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'}
Aresult
11
00
135 %args
136 );
137 3 ▶ if (not $args{server}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"server"}) 3 0 3
138 $ENV{PERL_ANYEVENT_RESOLV_CONF}
139 ? $resolver->_load_resolv_conf_file($ENV{PERL_ANYEVENT_RESOLV_CONF})
140 3 ▶ : $resolver->os_config;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PERL_ANYEVENT_RESOLV_CONF'} ? : 0 3 3
141 }
142 3 $resolver;
143 };
144  
145 # Overwrite original helper function only if exists
146 3 my $old_helper = do {
147 3 ▶ \&AnyEvent::Socket::_load_hosts_unless
Branch: 1 branch
BranchTrueFalseTotal
if (prototype "AnyEvent::Socket::_load_hosts_unless" || "") eq "&\$\@" 3 0 3
Condition50%1/2
prototype 'AnyEvent::Socket::_load_hosts_unless' || ''
l!l
30
MC/DC0%0/1
prototype 'AnyEvent::Socket::_load_hosts_unless' || ''
prototype 'AnyEvent::Socket::_load_hosts_unless'
Truth table50%1/2
A
A= prototype 'AnyEvent::Socket::_load_hosts_unless'
Aresult
11
00
148 if ((prototype 'AnyEvent::Socket::_load_hosts_unless') || '') eq '&$@';
149 };
150  
151 3 ▶ if ($old_helper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_helper) 3 0 3
152 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 6 ▶ *AnyEvent::Socket::_load_hosts_unless = sub (&$@) {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 6 my ($cont, $cv, @dns) = @_;
155 6 $cv->end;
156 3 };
157 }
158  
159 2 ▶ return AnyEvent::Util::guard {
Subroutine __ANON__: called CC=2 SCAR=7.0
160 2 $AnyEvent::DNS::RESOLVER = $old_resolver;
161 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 2 ▶ *AnyEvent::Socket::_load_hosts_unless = $old_helper if $old_helper;
Branch: 1 branch
BranchTrueFalseTotal
if $old_helper 2 0 2
163 3 };
164 }
165  
166 # Helper functions taken from AnyEvent::Socket 7.05
167  
168 3 our %HOSTS; # $HOSTS{$nodename}[$ipv6] = [@aliases...]
169 3 our @HOSTS_CHECKING; # callbacks to call when hosts have been loaded
170 3 our $HOSTS_MTIME;
171  
172 2 ▶sub _parse_hosts($) {
Subroutine _parse_hosts: called CC=7 SCAR=19.5
173 2 %HOSTS = ();
174  
175 2 for (split /\n/, $_[0]) {
176 6 s/#.*$//;
177 6 s/^[ \t]+//;
178 6 y/A-Z/a-z/;
179  
180 6 my ($addr, @aliases) = split /[ \t]+/;
181 6 ▶ next unless @aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless @aliases 0 6 6
182  
183 6 ▶ if (my $ipv4 = AnyEvent::Socket::parse_ipv4 $addr) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $ipv4 = &AnyEvent::Socket::parse_ipv4($addr)) { } 4 2 6
elsif (my $ipv6 = &AnyEvent::Socket::parse_ipv6($addr)) { } 2 0 2
184 4 ($ipv4) = $ipv4 =~ /^(.*)$/s if AnyEvent::TAINT;
185 4 push @{ $HOSTS{$_}[0] }, $ipv4 for @aliases;
186 } elsif (my $ipv6 = AnyEvent::Socket::parse_ipv6 $addr) {
187 2 ($ipv6) = $ipv6 =~ /^(.*)$/s if AnyEvent::TAINT;
188 2 push @{ $HOSTS{$_}[1] }, $ipv6 for @aliases;
189 }
190 }
191 }
192  
193 # helper function - unless dns delivered results, check and parse hosts, then call continuation code
194 13 ▶sub _load_hosts_unless(&$@) {
Subroutine _load_hosts_unless: called CC=4 SCAR=13.9
195 13 my ($cont, $cv, @dns) = @_;
196  
197 13 ▶ if (@dns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dns) { } 0 13 13
198 0 $cv->end;
199 } else {
200 my $etc_hosts
201 = length $ENV{PERL_ANYEVENT_HOSTS} ? $ENV{PERL_ANYEVENT_HOSTS}
202 13 ▶ : AnyEvent::WIN32 ? "$ENV{SystemRoot}/system32/drivers/etc/hosts"
Branch: 1 branch
BranchTrueFalseTotal
length $ENV{'PERL_ANYEVENT_HOSTS'} ? : 13 0 13
203 : "/etc/hosts";
204  
205 13 ▶ push @HOSTS_CHECKING, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 13 $cont->();
207 13 $cv->end;
208 13 };
209  
210 13 ▶ unless ($#HOSTS_CHECKING) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#AnyEvent::DNS::EtcHosts::HOSTS_CHECKING) 13 0 13
211  
212 # we are not the first, so we actually have to do the work
213 13 require AnyEvent::IO;
214  
215 AnyEvent::IO::aio_stat(
216 $etc_hosts,
217 13 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
218 13 ▶ if ((stat _)[9] ne ($HOSTS_MTIME || 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((stat _)[9] ne ($AnyEvent::DNS::EtcHosts::HOSTS_MTIME || 0)) { } 2 11 13
Condition100%2/2
$AnyEvent::DNS::EtcHosts::HOSTS_MTIME || 0
l!l
112
MC/DC100%1/1
$AnyEvent::DNS::EtcHosts::HOSTS_MTIME || 0
$AnyEvent::DNS::EtcHosts::HOSTS_MTIME
Truth table100%2/2
A
A= $AnyEvent::DNS::EtcHosts::HOSTS_MTIME
Aresult
11
00
219 2 AE::log 8 => "(re)loading $etc_hosts.";
220 2 $HOSTS_MTIME = (stat _)[9];
221  
222 # we might load a newer version of hosts,but that's a harmless race,
223 # as the next call will just load it again.
224 AnyEvent::IO::aio_load(
225 $etc_hosts,
226 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
227 2 _parse_hosts $_[0];
228 2 ▶ (shift @HOSTS_CHECKING)->() while @HOSTS_CHECKING;
Branch: 1 branch
BranchTrueFalseTotal
@AnyEvent::DNS::EtcHosts::HOSTS_CHECKING and do { shift(@AnyEvent::DNS::EtcHosts::HOSTS_CHECKING)->() } 2 2 4
229 }
230 2 );
231 } else {
232 11 ▶ (shift @HOSTS_CHECKING)->() while @HOSTS_CHECKING;
Branch: 1 branch
BranchTrueFalseTotal
@AnyEvent::DNS::EtcHosts::HOSTS_CHECKING and do { shift(@AnyEvent::DNS::EtcHosts::HOSTS_CHECKING)->() } 11 11 22
233 }
234 }
235 13 );
236 }
237 }
238 }
239  
240 =head2 request
241  
242 =for markdown ```perl
243  
244 $resolver->request($req, $cb->($res))
245  
246 =for markdown ```
247  
248 This is a wrapper for L<AnyEvent::DNS>->request method.
249  
250 =cut
251  
252 13 ▶sub request {
Subroutine request: called CC=2 SCAR=7.0
Pod documented
253 13 my ($self, $req, $cb) = @_;
254 13 warn "req = " . Dumper $req if DEBUG;
255  
256 13 my $node = my $domain = $req->{qd}[0][0];
257 13 ▶ $node =~ s/^_[a-z0-9-]*\._[a-z0-9-]*\.// if ($req->{qd}[0][1] eq 'srv');
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"qd"}[0][1] eq "srv" 4 9 13
258  
259 13 my $type = $req->{qd}[0][1];
260  
261 13 my (@ipv4, @ipv6, @srv);
262  
263 13 my $cv = AE::cv;
264  
265 13 $cv->begin;
266 13 ▶ _load_hosts_unless {
Subroutine __ANON__: called CC=11 SCAR=25.1
267 13 ▶ if (exists $HOSTS{$node}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}) 13 0 13
268 13 ▶ if ($type =~ /^([*]|srv)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^([*]|srv)$/) 5 8 13
269 5 push @srv, $node;
270 }
271 13 ▶ if (ref $HOSTS{$node} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $AnyEvent::DNS::EtcHosts::HOSTS{$node} eq "ARRAY") 13 0 13
272 13 ▶ if ($type =~ /^([*]|a)$/ and exists $HOSTS{$node}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^([*]|a)$/ and exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[0]) 5 8 13
Condition66%2/3
$type =~ /^([*]|a)$/ and exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[0]
!ll&&!rl&&r
805
MC/DC50%1/2
$type =~ /^([*]|a)$/ and exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[0]
$type =~ /^([*]|a)$/ exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[0]
Truth table66%2/3
A and B
A= $type =~ /^([*]|a)$/B= exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[0]
ABresult
0X0
100
111
273 5 push @ipv4, @{ $HOSTS{$node}[0] };
274 }
275 13 ▶ if ($type =~ /^([*]|aaaa)$/ and exists $HOSTS{$node}[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^([*]|aaaa)$/ and exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[1]) 5 8 13
Condition66%2/3
$type =~ /^([*]|aaaa)$/ and exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[1]
!ll&&!rl&&r
805
MC/DC50%1/2
$type =~ /^([*]|aaaa)$/ and exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[1]
$type =~ /^([*]|aaaa)$/ exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[1]
Truth table66%2/3
A and B
A= $type =~ /^([*]|aaaa)$/B= exists $AnyEvent::DNS::EtcHosts::HOSTS{$node}[1]
ABresult
0X0
100
111
276 5 push @ipv6, @{ $HOSTS{$node}[1] };
277 }
278 }
279 }
280  
281 13 ▶ if (@ipv4 or @ipv6 or @srv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ipv4 or @ipv6 or @srv) 13 0 13
Condition83%5/6
@ipv4 or @ipv6 or @srv
l!l&&r!l&&!r
940
@ipv4 or @ipv6
l!l&&r!l&&!r
544
MC/DC0%0/3
@ipv4 or @ipv6 or @srv
@ipv4 @ipv6 @srv
Truth table75%3/4
A or B or C
A= @ipv4B= @ipv6C= @srv
ABCresult
1XX1
01X1
0011
0000
282 my $res = {
283 id => int rand(0xffff),
284 op => 'query',
285 rc => 'noerror',
286 qr => 1,
287 aa => '',
288 tc => '',
289 rd => $req->{rd},
290 ra => 1,
291 ad => '',
292 cd => '',
293 qd => $req->{qd},
294 an => [
295 5 (map { [$domain, 'srv', 'in', 0, 0, 0, 0, $_] } @srv),
296 10 (map { [$node, 'a', 'in', 0, AnyEvent::Socket::format_ipv4 $_ ] } @ipv4),
297 13 (map { [$node, 'aaaa', 'in', 0, AnyEvent::Socket::format_ipv6 $_ ] } @ipv6),
298 ],
299 ns => [],
300 ar => [],
301 };
302  
303 13 warn "res = " . Dumper $res if DEBUG;
304  
305 13 return $cb->($res);
306 }
307  
308 return $self->SUPER::request(
309 $req,
310 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
311 0 my ($res) = @_;
312 0 warn "SUPER::request res = " . Dumper $res if DEBUG;
313 0 $cb->($res);
314 }
315 0 );
316  
317 }
318 13 $cv;
319  
320 13 return;
321 }
322  
323 3 1;
324  
325 __END__
↑ Summary