Lambda/DNS.pm

filter: Statementstmt 81.6% 49 / 60 Branchbran 42.3% 11 / 26 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 68.4% 76 / 111 CC 18 SCAR 23.1
CC
18
Cov
68%
new
14.1
__ANON__
13.7
__ANON__
2.1
CRAP 10.0
« Lambda/Socket.pm ↑ Summary Lambda/Signal.pm »
1 # $Id: DNS.pm,v 1.10 2009/08/02 21:24:40 dk Exp $
2 package IO::Lambda::DNS;
3 2 ▶use vars qw($DEBUG $TIMEOUT $RETRIES @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 @ISA = qw(Exporter);
5 2 @EXPORT_OK = qw(dns);
6 2 %EXPORT_TAGS = ( all => \@EXPORT_OK);
7 2 $TIMEOUT = 4.0; # seconds
8 2 $RETRIES = 4; # times
9
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::DNS::Resolver 1.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Lambda qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 # given the options, returns new dns lambda
16 sub new
17 {
18 5 ▶ shift;
Subroutine new: called CC=9 SCAR=26.4
Pod documented
19
20 # get the options
21 5 my @ctx;
22 5 my $timeout = $TIMEOUT;
23 5 my $retries = $RETRIES;
24 5 my %opt;
25 5 ▶ for ( my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 6 5 11
26 6 ▶ if ( $i == 0 or $i == $#_ or not defined($_[$i])) {
Branch: 4 branches
BranchTrueFalseTotal
if ($i == 0 or $i == $#_ or not defined $_[$i]) { } 6 0 6
elsif ($_[$i] =~ /^(timeout|deadline)$/) { } 0 0 0
elsif ($_[$i] eq 'retry') { } 0 0 0
elsif ($_[$i] =~ /^( nameservers|recurse|debug|config_file| domain|port|srcaddr|srcport|retrans| usevc|stayopen|igntc|defnames|dnsrch| persistent_tcp|persistent_udp|dnssec )$/x) { } 0 0 0
Condition50%3/6
$i == 0 or $i == $#_ or not defined $_[$i]
l!l&&r!l&&!r
600
$i == 0 or $i == $#_
l!l&&r!l&&!r
510
MC/DC0%0/3
$i == 0 or $i == $#_ or not defined $_[$i]
$i == 0 $i == $#_ not defined $_[$i]
Truth table50%2/4
A or B or not(C)
A= $i == 0B= $i == $#_C= not defined $_[$i]
ABCresult
1XX1
01X1
0011
0000
27 # first or last or undef parameter in no way can be an option
28 6 push @ctx, $_[$i];
29 } elsif ( $_[$i] =~ /^(timeout|deadline)$/) {
30 0 $timeout = $_[++$i];
31 } elsif ( $_[$i] eq 'retry') {
32 0 $retries = $_[++$i];
33 } elsif ( $_[$i] =~ /^(
34 nameservers|recurse|debug|config_file|
35 domain|port|srcaddr|srcport|retrans|
36 usevc|stayopen|igntc|defnames|dnsrch|
37 persistent_tcp|persistent_udp|dnssec
38 )$/x) {
39 0 $opt{$_[$i]} = $_[$i + 1];
40 0 $i++;
41 } else {
42 0 push @ctx, $_[$i];
43 }
44 }
45
46 5 ▶ my $simple_query = (( 1 == @ctx) and not ref($ctx[0]));
Condition66%2/3
1 == @ctx && !ref($ctx[0])
!ll&&!rl&&r
104
MC/DC50%1/2
1 == @ctx && !ref($ctx[0])
1 == @ctx !ref($ctx[0])
Truth table66%2/3
A && not(B)
A= 1 == @ctxB= !ref($ctx[0])
ABresult
0X0
100
111
47
48 # proceed
49 lambda {
50 5 ▶ my $obj = Net::DNS::Resolver-> new( %opt);
Subroutine __ANON__: called CC=2 SCAR=7.6
51 5 my $bg_obj = $obj-> bgsend( @ctx);
52 5 ▶ return "send error: " . $obj-> errorstring unless $bg_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $bg_obj 0 5 5
53
54 5 my $sock = $bg_obj;
55 5 context $sock, $timeout;
56 readable {
57 5 ▶ unless ( shift) {
Subroutine __ANON__: called CC=9 SCAR=26.2
Branch: 1 branch
BranchTrueFalseTotal
unless (shift()) 0 5 5
58 0 ▶ return 'connect timeout' if $retries-- <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $retries-- <= 0 0 0 0
59 0 return this-> start; # restart the whole lambda
60 }
61
62 5 my $err = unpack('i', getsockopt($sock, SOL_SOCKET, SO_ERROR));
63 5 ▶ if ( $err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 5 5
64 0 $! = $err;
65 0 return "socket error: $!";
66 }
67 5 ▶ return again if $obj-> bgbusy($bg_obj);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->bgbusy($bg_obj) 0 5 5
68
69 5 my $packet = $obj-> bgread( $bg_obj);
70 5 undef $sock;
71 5 undef $bg_obj;
72
73 5 ▶ return "recv error: " . $obj-> errorstring unless $packet;
Branch: 1 branch
BranchTrueFalseTotal
unless $packet 0 5 5
74
75 5 ▶ if ( $simple_query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($simple_query) 4 1 5
76 # behave like inet_aton, return single IP address
77 4 for ( $packet-> answer) {
78 4 ▶ return $_-> address if $_-> type eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
if $_->type eq "A" 4 0 4
79 }
80 0 return 'response doesn\'t contain an IP address';
81 }
82
83 1 return $packet;
84 5 }}}
85
86 0 ▶sub dns(&) { IO::Lambda::DNS-> new(context)-> condition(shift, \&dns, 'dns') }
Subroutine dns: not called CC=1 SCAR=6.9
Pod documented
87
88 2 1;
89
90 __DATA__
« Lambda/Socket.pm ↑ Summary Lambda/Signal.pm »