| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | ▶ | if ( $i == 0 or $i == $#_ or not defined($_[$i])) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition50%3/6
$i == 0 or $i == $#_ or not defined $_[$i]
$i == 0 or $i == $#_
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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])
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])
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return 'connect timeout' if $retries-- <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $! = $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return "socket error: $!"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | return again if $obj-> bgbusy($bg_obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | if ( $simple_query) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||