blib/lib/Zuzu/Module/DNS.pm

filter: Statementstmt 11.9% 19 / 159 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 17.2% 5 / 29 Podpod 0.0% 0 / 1 totaltotal 8.0% 24 / 299 CC 48 SCAR 38.8
CC
48
Cov
7%
_record_dict
132.0
_record_value
90.0
_addresses
90.0
CRAP 48.5
« blib/lib/Zuzu/Module/Time.pm ↑ Summary blib/lib/Zuzu/Parser/_Impl.pm »
1 package Zuzu::Module::DNS;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6 2 our $DEFAULT_TIMEOUT = 5;
7  
8 2 ▶use Net::DNS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 AF_INET
11 AF_INET6
12 AF_UNSPEC
13 NI_NUMERICHOST
14 SOCK_STREAM
15 getaddrinfo
16 getnameinfo
17 inet_pton
18 2 );
19  
20 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 native_function
22 perl_to_zuzu
23 2 );
24  
25 2 my %SUPPORTED = map { $_ => 1 } qw(
26 A
27 AAAA
28 CNAME
29 MX
30 NS
31 PTR
32 SRV
33 TXT
34 );
35  
36 0 ▶sub _str {
Subroutine _str: not called CC=2 SCAR=17.9
37 0 my ( $value, $default ) = @_;
38  
39 0 ▶ return $default if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
40 0 return "$value";
41 }
42  
43 0 ▶sub _resolver {
Subroutine _resolver: not called CC=3 SCAR=24.8
44 0 my $resolver = Net::DNS::Resolver->new;
45 0 for my $method ( qw( retrans retry tcp_timeout udp_timeout ) ) {
46 0 ▶ $resolver->$method($DEFAULT_TIMEOUT) if $resolver->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $resolver->can($method) 0 0 0
47 }
48 0 return $resolver;
49 }
50  
51 0 ▶sub _record_type {
Subroutine _record_type: not called CC=2 SCAR=17.9
52 0 my ( $value ) = @_;
53  
54 0 my $type = uc _str( $value, 'A' );
55 die "DNSException: unsupported DNS record type '$type'"
56 0 ▶ if !$SUPPORTED{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $SUPPORTED{$type} 0 0 0
57 0 return $type;
58 }
59  
60 0 ▶sub _expect_arity {
Subroutine _expect_arity: not called CC=4 SCAR=30.0
61 0 my ( $name, $args, $min, $max ) = @_;
62  
63 0 my $count = scalar @{ $args };
64 0 ▶ return if $count >= $min and $count <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if $count >= $min and $count <= $max 0 0 0
Condition0%0/3
$count >= $min and $count <= $max
!ll&&!rl&&r
000
MC/DC0%0/2
$count >= $min and $count <= $max
$count >= $min $count <= $max
Truth table0%0/3
A and B
A= $count >= $minB= $count <= $max
ABresult
0X0
100
111
65  
66 0 ▶ my $range = $min == $max ? $min : "$min or $max";
Branch: 1 branch
BranchTrueFalseTotal
$min == $max ? : 0 0 0
67 0 die "DNSException: $name expects $range arguments";
68 }
69  
70 0 ▶sub _no_data_error {
Subroutine _no_data_error: not called CC=4 SCAR=30.0
71 0 my ( $error ) = @_;
72  
73 0 ▶ return 1 if !defined $error or $error eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $error or $error eq "" 0 0 0
Condition0%0/3
not defined $error or $error eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $error or $error eq ""
not defined $error $error eq ""
Truth table0%0/3
not(A) or B
A= not defined $errorB= $error eq ""
ABresult
1X1
011
000
74 0 ▶ return $error =~ /\A(?:NOERROR|NXDOMAIN|NODATA|NO DATA)\z/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$error =~ /\A(?:NOERROR|NXDOMAIN|NODATA|NO DATA)\z/i ? : 0 0 0
75 }
76  
77 0 ▶sub _txt_strings {
Subroutine _txt_strings: not called CC=2 SCAR=17.9
78 0 my ( $rr ) = @_;
79  
80 my @strings = map {
81 0 ▶ defined $_ ? "$_" : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
82 } $rr->txtdata;
83 0 return @strings;
84 }
85  
86 0 ▶sub _record_value {
Subroutine _record_value: not called CC=9 SCAR=45.0
87 0 my ( $type, $rr ) = @_;
88  
89 0 ▶ return $rr->address if $type eq 'A' or $type eq 'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "A" or $type eq "AAAA" 0 0 0
Condition0%0/3
$type eq "A" or $type eq "AAAA"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "A" or $type eq "AAAA"
$type eq "A" $type eq "AAAA"
Truth table0%0/3
A or B
A= $type eq "A"B= $type eq "AAAA"
ABresult
1X1
011
000
90 0 ▶ return $rr->cname if $type eq 'CNAME';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "CNAME" 0 0 0
91 0 ▶ return $rr->nsdname if $type eq 'NS';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NS" 0 0 0
92 0 ▶ return $rr->ptrdname if $type eq 'PTR';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "PTR" 0 0 0
93 0 ▶ return $rr->exchange if $type eq 'MX';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "MX" 0 0 0
94 0 ▶ return $rr->target if $type eq 'SRV';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SRV" 0 0 0
95 0 ▶ if ( $type eq 'TXT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "TXT") 0 0 0
96 0 my @strings = _txt_strings($rr);
97 0 return join '', @strings;
98 }
99 0 return "$rr";
100 }
101  
102 0 ▶sub _record_dict {
Subroutine _record_dict: not called CC=11 SCAR=48.8
103 0 my ( $query_name, $type, $rr ) = @_;
104  
105 0 my $value = _record_value( $type, $rr );
106 0 ▶ my %out = (
Condition0%0/5
$rr->name // $query_name
l!l&&r!l&&!r
000
$rr->ttl // 0
l!l
00
MC/DC0%0/2
$rr->name // $query_name
$rr->name $query_name
MC/DC0%0/1
$rr->ttl // 0
$rr->ttl
Truth table0%0/3
A // B
A= $rr->nameB= $query_name
ABresult
1X1
011
000
Truth table0%0/2
A
A= $rr->ttl
Aresult
11
00
107 type => $type,
108 name => scalar( $rr->name // $query_name ),
109 value => $value,
110 ttl => 0 + ( $rr->ttl // 0 ),
111 );
112  
113 0 ▶ if ( $type eq 'A' or $type eq 'AAAA' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'A' or $type eq 'AAAA') { } 0 0 0
elsif ($type eq 'CNAME' or $type eq 'NS' or $type eq 'PTR') { } 0 0 0
elsif ($type eq 'MX') { } 0 0 0
elsif ($type eq 'TXT') { } 0 0 0
elsif ($type eq 'SRV') { } 0 0 0
Condition0%0/3
$type eq "A" or $type eq "AAAA"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "A" or $type eq "AAAA"
$type eq "A" $type eq "AAAA"
Truth table0%0/3
A or B
A= $type eq "A"B= $type eq "AAAA"
ABresult
1X1
011
000
114 0 $out{address} = $value;
115 }
116 ▶ elsif ( $type eq 'CNAME' or $type eq 'NS' or $type eq 'PTR' ) {
Condition0%0/6
$type eq "CNAME" or $type eq "NS" or $type eq "PTR"
l!l&&r!l&&!r
000
$type eq "CNAME" or $type eq "NS"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "CNAME" or $type eq "NS" or $type eq "PTR"
$type eq "CNAME" $type eq "NS" $type eq "PTR"
Truth table0%0/4
A or B or C
A= $type eq "CNAME"B= $type eq "NS"C= $type eq "PTR"
ABCresult
1XX1
01X1
0011
0000
117 0 $out{target} = $value;
118 }
119 elsif ( $type eq 'MX' ) {
120 0 $out{exchange} = $value;
121 0 $out{preference} = 0 + $rr->preference;
122 }
123 elsif ( $type eq 'TXT' ) {
124 0 my @strings = _txt_strings($rr);
125 0 $out{text} = $value;
126 0 $out{strings} = \@strings;
127 }
128 elsif ( $type eq 'SRV' ) {
129 0 $out{target} = $value;
130 0 $out{port} = 0 + $rr->port;
131 0 $out{priority} = 0 + $rr->priority;
132 0 $out{weight} = 0 + $rr->weight;
133 }
134  
135 0 return \%out;
136 }
137  
138 0 ▶sub _lookup_records {
Subroutine _lookup_records: not called CC=6 SCAR=37.4
139 0 my ( $name, $type_value ) = @_;
140  
141 0 my $query_name = _str( $name, '' );
142 0 my $type = _record_type($type_value);
143 0 my $resolver = _resolver();
144 0 my $packet = $resolver->query( $query_name, $type );
145 0 ▶ if ( !$packet ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($packet) 0 0 0
146 0 my $error = $resolver->errorstring;
147 0 ▶ return [] if _no_data_error($error);
Branch: 1 branch
BranchTrueFalseTotal
if _no_data_error($error) 0 0 0
148 0 die "DNSException: $error";
149 }
150  
151 0 my @records;
152 0 for my $rr ( $packet->answer ) {
153 0 ▶ next if uc( $rr->type // '' ) ne $type;
Branch: 1 branch
BranchTrueFalseTotal
if uc($rr->type // "") ne $type 0 0 0
Condition0%0/2
$rr->type // ""
l!l
00
MC/DC0%0/1
$rr->type // ""
$rr->type
Truth table0%0/2
A
A= $rr->type
Aresult
11
00
154 0 push @records, _record_dict( $query_name, $type, $rr );
155 }
156  
157 0 return \@records;
158 }
159  
160 0 ▶sub _addresses {
Subroutine _addresses: not called CC=9 SCAR=45.0
161 0 my ( $name, $family_value ) = @_;
162  
163 0 my $query_name = _str( $name, '' );
164 0 my $family = lc _str( $family_value, 'any' );
165 0 my $socket_family;
166 0 ▶ if ( $family eq 'any' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($family eq 'any') { } 0 0 0
elsif ($family eq 'ipv4') { } 0 0 0
elsif ($family eq 'ipv6') { } 0 0 0
167 0 $socket_family = AF_UNSPEC;
168 }
169 elsif ( $family eq 'ipv4' ) {
170 0 $socket_family = AF_INET;
171 }
172 elsif ( $family eq 'ipv6' ) {
173 0 $socket_family = AF_INET6;
174 }
175 else {
176 0 die "DNSException: unsupported address family '$family'";
177 }
178  
179 0 my ( $err, @results ) = getaddrinfo(
180 $query_name,
181 undef,
182 {
183 family => $socket_family,
184 socktype => SOCK_STREAM,
185 },
186 );
187 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
188 0 ▶ return [] if $err =~ /(?:Name or service not known|nodename nor servname|no address)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /(?:Name or service not known|nodename nor servname|no address)/i 0 0 0
189 0 die "DNSException: $err";
190 }
191  
192 0 my %seen;
193 my @addresses;
194 0 for my $result ( @results ) {
195 0 my ( $host_err, $host ) = getnameinfo( $result->{addr}, NI_NUMERICHOST );
196 0 ▶ next if $host_err;
Branch: 1 branch
BranchTrueFalseTotal
if $host_err 0 0 0
197 0 ▶ next if $seen{$host}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$host}++ 0 0 0
198 0 push @addresses, $host;
199 }
200  
201 0 return \@addresses;
202 }
203  
204 0 ▶sub _reverse_name {
Subroutine _reverse_name: not called CC=4 SCAR=30.0
205 0 my ( $address ) = @_;
206  
207 0 ▶ if ( $address =~ /\A([0-9]{1,3}(?:\.[0-9]{1,3}){3})\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($address =~ /\A([0-9]{1,3}(?:\.[0-9]{1,3}){3})\z/) 0 0 0
208 0 my @octets = split /\./, $1;
209 die "DNSException: invalid IPv4 address '$address'"
210 0 ▶ if grep { $_ > 255 } @octets;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} @octets 0 0 0
211 0 return join( '.', reverse @octets ) . '.in-addr.arpa';
212 }
213  
214 0 my $packed = inet_pton( AF_INET6, $address );
215 0 ▶ if ( defined $packed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packed) 0 0 0
216 0 my $hex = unpack 'H*', $packed;
217 0 return join( '.', reverse split //, $hex ) . '.ip6.arpa';
218 }
219  
220 0 die "DNSException: invalid IP address '$address'";
221 }
222  
223 0 ▶sub _reverse {
Subroutine _reverse: not called CC=1 SCAR=6.9
224 0 my ( $address_value ) = @_;
225  
226 0 my $address = _str( $address_value, '' );
227 0 my $records = _lookup_records( _reverse_name($address), 'PTR' );
228 0 return [ map { $_->{target} } @{ $records } ];
229 }
230  
231 0 ▶sub _async_task {
Subroutine _async_task: not called CC=1 SCAR=6.9
232 0 my ( $runtime, $name, $work, $wrap ) = @_;
233  
234 0 my $worker = $runtime->_new_task(
235 name => "$name.worker",
236 start => 1,
237 schedule => 1,
238 thunk => $work,
239 );
240 return $runtime->_new_task(
241 name => $name,
242 schedule => 1,
243 0 ▶ thunk => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
244 0 my $value = $worker->await;
245 0 ▶ return defined $wrap ? $wrap->($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $wrap ? : 0 0 0
246 },
247 0 );
248 }
249  
250 0 ▶sub _warn_blocking_operation {
Subroutine _warn_blocking_operation: not called CC=2 SCAR=17.9
251 0 my ( $runtime, $operation ) = @_;
252  
253 0 ▶ $runtime->_warn_blocking_operation($operation)
Branch: 1 branch
BranchTrueFalseTotal
if $runtime->can("_warn_blocking_operation") 0 0 0
254 if $runtime->can('_warn_blocking_operation');
255  
256 0 return;
257 }
258  
259 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=6.0
Pod undocumented
260 2 my ( $class, $runtime ) = @_;
261  
262 return {
263 lookup => native_function(
264 name => 'lookup',
265 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 0 my @args = @_;
267 0 _expect_arity( 'lookup()', \@args, 1, 2 );
268 0 my ( $name, $type ) = @args;
269 0 _warn_blocking_operation( $runtime, 'std/net/dns lookup' );
270 0 return perl_to_zuzu(
271 _lookup_records( $name, $type ),
272 );
273 },
274 ),
275 lookup_async => native_function(
276 name => 'lookup_async',
277 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 0 my @args = @_;
279 0 _expect_arity( 'lookup_async()', \@args, 1, 2 );
280 0 my ( $name, $type ) = @args;
281 return _async_task(
282 $runtime,
283 'dns.lookup_async',
284 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 return perl_to_zuzu(
286 _lookup_records( $name, $type ),
287 );
288 },
289 0 );
290 },
291 ),
292 addresses => native_function(
293 name => 'addresses',
294 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 my @args = @_;
296 0 _expect_arity( 'addresses()', \@args, 1, 2 );
297 0 my ( $name, $family ) = @args;
298 0 _warn_blocking_operation( $runtime, 'std/net/dns addresses' );
299 0 return perl_to_zuzu(
300 _addresses( $name, $family ),
301 );
302 },
303 ),
304 addresses_async => native_function(
305 name => 'addresses_async',
306 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
307 0 my @args = @_;
308 0 _expect_arity( 'addresses_async()', \@args, 1, 2 );
309 0 my ( $name, $family ) = @args;
310 return _async_task(
311 $runtime,
312 'dns.addresses_async',
313 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 0 return perl_to_zuzu(
315 _addresses( $name, $family ),
316 );
317 },
318 0 );
319 },
320 ),
321 reverse => native_function(
322 name => 'reverse',
323 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 0 my @args = @_;
325 0 _expect_arity( 'reverse()', \@args, 1, 1 );
326 0 my ( $address ) = @args;
327 0 _warn_blocking_operation( $runtime, 'std/net/dns reverse' );
328 0 return perl_to_zuzu( _reverse($address) );
329 },
330 ),
331 reverse_async => native_function(
332 name => 'reverse_async',
333 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
334 0 my @args = @_;
335 0 _expect_arity( 'reverse_async()', \@args, 1, 1 );
336 0 my ( $address ) = @args;
337 return _async_task(
338 $runtime,
339 'dns.reverse_async',
340 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
341 0 return perl_to_zuzu(
342 _reverse($address),
343 );
344 },
345 0 );
346 },
347 2 ),
348 };
349 }
350  
351 2 1;
352  
353 =pod
354  
355 =head1 NAME
356  
357 Zuzu::Module::DNS - std/net/dns bindings for ZuzuScript.
358  
359 =head1 DESCRIPTION
360  
361 Implements the C<std/net/dns> runtime-supported module.
362  
363 =head1 COPYRIGHT AND LICENCE
364  
365 B<< Zuzu::Module::DNS >> is copyright Toby Inkster.
366  
367 It is free software; you may redistribute it and/or modify it under
368 the terms of either the Artistic License 1.0 or the GNU General Public
369 License version 2.
370  
371 =cut
« blib/lib/Zuzu/Module/Time.pm ↑ Summary blib/lib/Zuzu/Parser/_Impl.pm »