| 1 | package POE::Component::Client::DNS::Recursive; | ||||||||||||||||||||||||||||
| 2 | 8 | $POE::Component::Client::DNS::Recursive::VERSION = '1.12'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: A recursive DNS client for POE | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Socket qw[:all]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Net::IP::Minimal qw(:PROC); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use IO::Socket::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use POE qw(NFA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Net::DNS::Packet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | my @hc_hints = qw( | |||||||||||||||||||||||||||
| 15 | 198.41.0.4 | ||||||||||||||||||||||||||||
| 16 | 192.228.79.201 | ||||||||||||||||||||||||||||
| 17 | 192.33.4.12 | ||||||||||||||||||||||||||||
| 18 | 128.8.10.90 | ||||||||||||||||||||||||||||
| 19 | 192.203.230.10 | ||||||||||||||||||||||||||||
| 20 | 192.5.5.241 | ||||||||||||||||||||||||||||
| 21 | 192.112.36.4 | ||||||||||||||||||||||||||||
| 22 | 128.63.2.53 | ||||||||||||||||||||||||||||
| 23 | 192.36.148.17 | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub resolve { | ||||||||||||||||||||||||||||
| 27 | 6 | ▶ | my $package = shift; | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 6 | my %opts = @_; | |||||||||||||||||||||||||||
| 29 | 6 | $opts{lc $_} = delete $opts{$_} for keys %opts; | |||||||||||||||||||||||||||
| 30 | croak "$package requires a 'host' argument\n" | ||||||||||||||||||||||||||||
| 31 | 6 | ▶ | unless $opts{host}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | croak "$package requires an 'event' argument\n" | ||||||||||||||||||||||||||||
| 33 | 6 | ▶ | unless $opts{event}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | $opts{nameservers} = [ ] unless $opts{nameservers} and ref $opts{nameservers} eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts{"nameservers"} and ref $opts{"nameservers"} eq "ARRAY"
MC/DC50%1/2
$opts{"nameservers"} and ref $opts{"nameservers"} eq "ARRAY"
$opts{"nameservers"}
ref $opts{"nameservers"} eq "ARRAY"
Truth table66%2/3
A and B
A= $opts{"nameservers"}B= ref $opts{"nameservers"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 35 | 6 | @{ $opts{nameservers} } = grep { ip_get_version( $_ ) } @{ $opts{nameservers} }; | |||||||||||||||||||||||||||
| 36 | 6 | my $options = delete $opts{options}; | |||||||||||||||||||||||||||
| 37 | 6 | my $self = bless \%opts, $package; | |||||||||||||||||||||||||||
| 38 | 6 | my $sender = $poe_kernel->get_active_session(); | |||||||||||||||||||||||||||
| 39 | 6 | $self->{_sender} = $sender; | |||||||||||||||||||||||||||
| 40 | 6 | POE::NFA->spawn( | |||||||||||||||||||||||||||
| 41 | object_states => { | ||||||||||||||||||||||||||||
| 42 | initial => [ | ||||||||||||||||||||||||||||
| 43 | $self => { setup => '_start' }, | ||||||||||||||||||||||||||||
| 44 | $self => [qw(_default)], | ||||||||||||||||||||||||||||
| 45 | ], | ||||||||||||||||||||||||||||
| 46 | hints => [ | ||||||||||||||||||||||||||||
| 47 | $self => { | ||||||||||||||||||||||||||||
| 48 | _init => '_hints_go', | ||||||||||||||||||||||||||||
| 49 | _setup => '_send', | ||||||||||||||||||||||||||||
| 50 | _read => '_hints', | ||||||||||||||||||||||||||||
| 51 | _timeout => '_hints_timeout', | ||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | ], | ||||||||||||||||||||||||||||
| 54 | query => [ | ||||||||||||||||||||||||||||
| 55 | $self => { | ||||||||||||||||||||||||||||
| 56 | _setup => '_send', | ||||||||||||||||||||||||||||
| 57 | _read => '_query', | ||||||||||||||||||||||||||||
| 58 | _timeout => '_query_timeout', | ||||||||||||||||||||||||||||
| 59 | }, | ||||||||||||||||||||||||||||
| 60 | ], | ||||||||||||||||||||||||||||
| 61 | done => [ | ||||||||||||||||||||||||||||
| 62 | $self => [qw(_close _error)], | ||||||||||||||||||||||||||||
| 63 | ], | ||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||
| 65 | runstate => $self, | ||||||||||||||||||||||||||||
| 66 | )->goto_state( 'initial' => 'setup' ); | ||||||||||||||||||||||||||||
| 67 | 6 | return $self; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub _default { | ||||||||||||||||||||||||||||
| 71 | 6 | ▶ | return 0; | ||||||||||||||||||||||||||
|
Subroutine
_default: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub _start { | ||||||||||||||||||||||||||||
| 75 | 6 | ▶ | my ($kernel,$machine,$runstate) = @_[KERNEL,MACHINE,RUNSTATE]; | ||||||||||||||||||||||||||
|
Subroutine
_start: called CC=9 SCAR=25.7
| |||||||||||||||||||||||||||||
| 76 | 6 | my $sender = $runstate->{_sender}; | |||||||||||||||||||||||||||
| 77 | 6 | ▶ | if ( $kernel == $sender and !$runstate->{session} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$kernel == $sender and not $runstate->{"session"}
MC/DC0%0/2
$kernel == $sender and not $runstate->{"session"}
$kernel == $sender
not $runstate->{"session"}
Truth table33%1/3
A and not(B)
A= $kernel == $senderB= not $runstate->{"session"}
| |||||||||||||||||||||||||||||
| 78 | 0 | croak "Not called from another POE session and 'session' wasn't set\n"; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 6 | my $sender_id; | |||||||||||||||||||||||||||
| 81 | 6 | ▶ | if ( $runstate->{session} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( my $ref = $kernel->alias_resolve( $runstate->{session} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $sender_id = $ref->ID(); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 0 | croak "Could not resolve 'session' to a valid POE session\n"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||
| 90 | 6 | $sender_id = $sender->ID(); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | $kernel->refcount_increment( $sender_id, __PACKAGE__ ) | ||||||||||||||||||||||||||||
| 93 | 6 | ▶ | unless ref $runstate->{event} eq 'POE::Session::AnonEvent'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 6 | $kernel->detach_myself(); | |||||||||||||||||||||||||||
| 95 | 6 | $runstate->{sender_id} = $sender_id; | |||||||||||||||||||||||||||
| 96 | 6 | ▶ | my $type = $runstate->{type} || ( ip_get_version( $runstate->{host} ) ? 'PTR' : 'A' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$runstate->{'type'} || (ip_get_version($runstate->{'host'}) ? 'PTR' : 'A')
MC/DC0%0/2
$runstate->{'type'} || (ip_get_version($runstate->{'host'}) ? 'PTR' : 'A')
$runstate->{'type'}
(ip_get_version($runstate->{'host'}) ? 'PTR' : 'A')
Truth table33%1/3
A || B
A= $runstate->{'type'}B= (ip_get_version($runstate->{'host'}) ? 'PTR' : 'A')
| |||||||||||||||||||||||||||||
| 97 | 6 | ▶ | my $class = $runstate->{class} || 'IN'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$runstate->{'class'} || 'IN'
MC/DC0%0/1
$runstate->{'class'} || 'IN'
$runstate->{'class'}
Truth table50%1/2
A
A= $runstate->{'class'}
| |||||||||||||||||||||||||||||
| 98 | 6 | $runstate->{qstack} = [ ]; | |||||||||||||||||||||||||||
| 99 | $runstate->{current} = { | ||||||||||||||||||||||||||||
| 100 | query => $runstate->{host}, | ||||||||||||||||||||||||||||
| 101 | type => $type, | ||||||||||||||||||||||||||||
| 102 | 6 | packet => Net::DNS::Packet->new($runstate->{host},$type,$class), | |||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||
| 104 | 6 | $runstate->{socket} = IO::Socket::IP->new( Proto => 'udp' ); | |||||||||||||||||||||||||||
| 105 | 6 | $machine->goto_state( 'hints', '_init' ); | |||||||||||||||||||||||||||
| 106 | 6 | return; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub _hints_go { | ||||||||||||||||||||||||||||
| 110 | 8 | ▶ | my ($kernel,$machine,$runstate) = @_[KERNEL,MACHINE,RUNSTATE]; | ||||||||||||||||||||||||||
|
Subroutine
_hints_go: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | 8 | my $hints; | |||||||||||||||||||||||||||
| 112 | 8 | ▶ | if ( scalar @{ $runstate->{nameservers} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 2 | $hints = $runstate->{nameservers}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 6 | $hints = [ @hc_hints ]; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 8 | $runstate->{_hints} = $hints; | |||||||||||||||||||||||||||
| 119 | 8 | $machine->goto_state( 'hints', '_setup', Net::DNS::Packet->new('.','NS','IN'), splice( @$hints, rand($#{$hints}), 1) ); | |||||||||||||||||||||||||||
| 120 | 8 | return; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub _send { | ||||||||||||||||||||||||||||
| 124 | 25 | ▶ | my ($machine,$runstate,$state,$packet,$ns) = @_[MACHINE,RUNSTATE,STATE,ARG0,ARG1]; | ||||||||||||||||||||||||||
|
Subroutine
_send: called CC=4 SCAR=15.1
| |||||||||||||||||||||||||||||
| 125 | 25 | my $socket = $runstate->{socket}; | |||||||||||||||||||||||||||
| 126 | 25 | my $data = $packet->data; | |||||||||||||||||||||||||||
| 127 | 25 | my $ai; | |||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | 25 | my %hints = (flags => AI_NUMERICHOST, socktype => SOCK_DGRAM, protocol => IPPROTO_UDP); | |||||||||||||||||||||||||||
| 130 | 25 | my ($err, @res) = getaddrinfo($ns, '53', \%hints); | |||||||||||||||||||||||||||
| 131 | 25 | ▶ | if ( $err ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | warn "'$ns' didn't produce an valid server address\n"; | |||||||||||||||||||||||||||
| 133 | 0 | $machine->goto_state( 'done', '_error', $err ); | |||||||||||||||||||||||||||
| 134 | 0 | return; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 25 | $ai = shift @res; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 25 | $socket->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} ); | |||||||||||||||||||||||||||
| 139 | 25 | ▶ | unless ( send( $socket, $data, 0, $ai->{addr} ) == length($data) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | $machine->goto_state( 'done', '_error', "$ns: $!" ); | |||||||||||||||||||||||||||
| 141 | 0 | return; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 25 | $poe_kernel->select_read( $socket, '_read' ); | |||||||||||||||||||||||||||
| 144 | 25 | ▶ | $poe_kernel->delay( '_timeout', $runstate->{timeout} || 5 ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$runstate->{'timeout'} || 5
MC/DC0%0/1
$runstate->{'timeout'} || 5
$runstate->{'timeout'}
Truth table50%1/2
A
A= $runstate->{'timeout'}
| |||||||||||||||||||||||||||||
| 145 | 25 | return; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub _hints { | ||||||||||||||||||||||||||||
| 149 | 8 | ▶ | my ($machine,$runstate,$socket) = @_[MACHINE,RUNSTATE,ARG0]; | ||||||||||||||||||||||||||
|
Subroutine
_hints: called CC=12 SCAR=25.1
| |||||||||||||||||||||||||||||
| 150 | 8 | $poe_kernel->delay( '_timeout' ); | |||||||||||||||||||||||||||
| 151 | 8 | my $packet = _read_socket( $socket ); | |||||||||||||||||||||||||||
| 152 | 8 | my %hints; | |||||||||||||||||||||||||||
| 153 | 8 | ▶ | if (my @ans = $packet->answer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 8 | foreach my $rr (@ans) { | |||||||||||||||||||||||||||
| 155 | 104 | ▶ | if ($rr->name =~ /^\.?$/ and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rr->name =~ /^\.?$/ and $rr->type eq "NS"
MC/DC0%0/2
$rr->name =~ /^\.?$/ and $rr->type eq "NS"
$rr->name =~ /^\.?$/
$rr->type eq "NS"
Truth table33%1/3
A and B
A= $rr->name =~ /^\.?$/B= $rr->type eq "NS"
| |||||||||||||||||||||||||||||
| 156 | $rr->type eq "NS") { | ||||||||||||||||||||||||||||
| 157 | # Found root authority | ||||||||||||||||||||||||||||
| 158 | 104 | my $server = lc $rr->rdatastr; | |||||||||||||||||||||||||||
| 159 | 104 | $server =~ s/\.$//; | |||||||||||||||||||||||||||
| 160 | 104 | $hints{$server} = []; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 8 | foreach my $rr ($packet->additional) { | |||||||||||||||||||||||||||
| 164 | 86 | ▶ | if (my $server = lc $rr->name){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 86 | ▶ | if ( $rr->type eq "A") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 66 | ▶ | if ($hints{$server}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 66 | push @{ $hints{$server} }, $rr->rdatastr; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 8 | ▶ | if ( $runstate->{trace} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 2 | ▶ | if ( ref $runstate->{trace} eq 'POE::Session::AnonEvent' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 1 | $runstate->{trace}->( $packet ); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||
| 178 | 1 | $poe_kernel->post( $runstate->{sender_id}, $runstate->{trace}, $packet ); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 8 | $runstate->{hints} = \%hints; | |||||||||||||||||||||||||||
| 182 | 8 | my @ns = _ns_from_cache( $runstate->{hints} ); | |||||||||||||||||||||||||||
| 183 | 8 | ▶ | unless ( scalar @ns ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 2 | $machine->goto_state( 'hints', '_init' ); | |||||||||||||||||||||||||||
| 185 | 2 | return; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 6 | my $query = $runstate->{current}; | |||||||||||||||||||||||||||
| 188 | 6 | $query->{servers} = \@ns; | |||||||||||||||||||||||||||
| 189 | 6 | my ($nameserver) = splice @ns, rand($#ns), 1; | |||||||||||||||||||||||||||
| 190 | 6 | $machine->goto_state( 'query', '_setup', $query->{packet}, $nameserver ); | |||||||||||||||||||||||||||
| 191 | 6 | return; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub _hints_timeout { | ||||||||||||||||||||||||||||
| 195 | 1 | ▶ | my ($machine,$runstate) = @_[MACHINE,RUNSTATE]; | ||||||||||||||||||||||||||
|
Subroutine
_hints_timeout: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 196 | 1 | my $hints = $runstate->{_hints}; | |||||||||||||||||||||||||||
| 197 | 1 | ▶ | if ( scalar @{ $hints } ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 198 | 1 | $machine->goto_state( 'hints', '_setup', Net::DNS::Packet->new('.','NS','IN'), splice( @$hints, rand($#{$hints}), 1) ); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | elsif ( defined $runstate->{nameservers} ) { | ||||||||||||||||||||||||||||
| 201 | 0 | $machine->goto_state( 'hints', '_init' ); | |||||||||||||||||||||||||||
| 202 | 0 | return; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | else { | ||||||||||||||||||||||||||||
| 205 | 0 | $machine->goto_state( 'done', '_error', 'Ran out of authority records' ); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 1 | return; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | sub _query { | ||||||||||||||||||||||||||||
| 211 | 16 | ▶ | my ($machine,$runstate,$socket) = @_[MACHINE,RUNSTATE,ARG0]; | ||||||||||||||||||||||||||
|
Subroutine
_query: called CC=7 SCAR=21.6
| |||||||||||||||||||||||||||||
| 212 | 16 | $poe_kernel->delay( '_timeout' ); | |||||||||||||||||||||||||||
| 213 | 16 | my $packet = _read_socket( $socket ); | |||||||||||||||||||||||||||
| 214 | 16 | my @ns; | |||||||||||||||||||||||||||
| 215 | 16 | my $status = $packet->header->rcode; | |||||||||||||||||||||||||||
| 216 | 16 | ▶ | if ( $status ne 'NOERROR' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 1 | $machine->goto_state( 'done', '_error', $status ); | |||||||||||||||||||||||||||
| 218 | 1 | return; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 15 | ▶ | if (my @ans = $packet->answer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | # This is the end of the chain. | ||||||||||||||||||||||||||||
| 222 | 5 | ▶ | unless ( scalar @{ $runstate->{qstack} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 5 | $machine->goto_state( 'done', '_close', $packet ); | |||||||||||||||||||||||||||
| 224 | 5 | return; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | # Okay we have queries pending. | ||||||||||||||||||||||||||||
| 227 | 0 | push @ns, $_->rdatastr for grep { $_->type eq 'A' } @ans; | |||||||||||||||||||||||||||
| 228 | 0 | $runstate->{current} = pop @{ $runstate->{qstack} }; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||
| 231 | 10 | ▶ | if ( $runstate->{trace} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 4 | $poe_kernel->post( $runstate->{sender_id}, $runstate->{trace}, $packet ); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 10 | my $authority = _authority( $packet ); | |||||||||||||||||||||||||||
| 235 | 10 | @ns = _ns_from_cache( $authority ); | |||||||||||||||||||||||||||
| 236 | 10 | ▶ | unless ( scalar @ns ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 0 | $runstate->{current}->{authority} = $authority; | |||||||||||||||||||||||||||
| 238 | 0 | push @{ $runstate->{qstack} }, $runstate->{current}; | |||||||||||||||||||||||||||
| 239 | 0 | my $host = ( keys %{ $authority } )[rand scalar keys %{ $authority }]; | |||||||||||||||||||||||||||
| 240 | 0 | delete $authority->{$host}; | |||||||||||||||||||||||||||
| 241 | $runstate->{current} = { | ||||||||||||||||||||||||||||
| 242 | 0 | query => $host, | |||||||||||||||||||||||||||
| 243 | type => 'A', | ||||||||||||||||||||||||||||
| 244 | packet => Net::DNS::Packet->new($host,'A','IN'), | ||||||||||||||||||||||||||||
| 245 | }; | ||||||||||||||||||||||||||||
| 246 | 0 | @ns = _ns_from_cache( $runstate->{hints} ); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | 10 | my $query = $runstate->{current}; | |||||||||||||||||||||||||||
| 250 | 10 | $query->{servers} = \@ns; | |||||||||||||||||||||||||||
| 251 | 10 | my ($nameserver) = splice @ns, rand($#ns), 1; | |||||||||||||||||||||||||||
| 252 | 10 | $poe_kernel->yield( '_setup', $query->{packet}, $nameserver ); | |||||||||||||||||||||||||||
| 253 | 10 | return; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | sub _query_timeout { | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | my ($machine,$runstate) = @_[MACHINE,RUNSTATE]; | ||||||||||||||||||||||||||
|
Subroutine
_query_timeout: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 258 | 0 | my $query = $runstate->{current}; | |||||||||||||||||||||||||||
| 259 | 0 | my $servers = $query->{servers}; | |||||||||||||||||||||||||||
| 260 | 0 | my ($nameserver) = splice @{ $servers }, rand($#{ $servers }), 1; | |||||||||||||||||||||||||||
| 261 | # actually check here if there is something on the stack. | ||||||||||||||||||||||||||||
| 262 | # pop off the most recent, and get the next authority record | ||||||||||||||||||||||||||||
| 263 | # push back on to the stack and do a lookup for the authority | ||||||||||||||||||||||||||||
| 264 | # record. No authority records left, then complain and bailout. | ||||||||||||||||||||||||||||
| 265 | 0 | ▶ | unless ( $nameserver ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if ( scalar @{ $runstate->{qstack} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | $runstate->{current} = pop @{ $runstate->{qstack} }; | |||||||||||||||||||||||||||
| 268 | 0 | my $host = ( keys %{ $runstate->{current}->{authority} } )[rand scalar keys %{ $runstate->{current}->{authority} }]; | |||||||||||||||||||||||||||
| 269 | 0 | ▶ | unless ( $host ) { # Oops | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 0 | $machine->goto_state( 'done', '_error', 'Ran out of authority records' ); | |||||||||||||||||||||||||||
| 271 | 0 | return; # OMG | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | 0 | delete $runstate->{current}->{authority}->{ $host }; | |||||||||||||||||||||||||||
| 274 | 0 | push @{ $runstate->{qstack} }, $runstate->{current}; | |||||||||||||||||||||||||||
| 275 | $runstate->{current} = { | ||||||||||||||||||||||||||||
| 276 | 0 | query => $host, | |||||||||||||||||||||||||||
| 277 | type => 'A', | ||||||||||||||||||||||||||||
| 278 | packet => Net::DNS::Packet->new($host,'A','IN'), | ||||||||||||||||||||||||||||
| 279 | }; | ||||||||||||||||||||||||||||
| 280 | 0 | my @ns = _ns_from_cache( $runstate->{hints} ); | |||||||||||||||||||||||||||
| 281 | 0 | $runstate->{current}->{servers} = \@ns; | |||||||||||||||||||||||||||
| 282 | 0 | ($nameserver) = splice @ns, rand($#ns), 1; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | else { | ||||||||||||||||||||||||||||
| 285 | 0 | $machine->goto_state( 'done', '_error', 'Ran out of authority records' ); | |||||||||||||||||||||||||||
| 286 | 0 | return; # OMG | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | unless ( $nameserver ) { # SERVFAIL? maybe | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 0 | $machine->goto_state( 'done', '_error', 'Ran out of nameservers to query' ); | |||||||||||||||||||||||||||
| 291 | 0 | return; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | 0 | $poe_kernel->yield( '_setup', $query->{packet}, $nameserver ); | |||||||||||||||||||||||||||
| 294 | 0 | return; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | sub _error { | ||||||||||||||||||||||||||||
| 298 | 1 | ▶ | my ($kernel,$machine,$runstate,$error) = @_[KERNEL,MACHINE,RUNSTATE,ARG0]; | ||||||||||||||||||||||||||
|
Subroutine
_error: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 299 | 1 | $kernel->select_read( $runstate->{socket} ); # Just in case | |||||||||||||||||||||||||||
| 300 | 1 | my $resp = {}; | |||||||||||||||||||||||||||
| 301 | 1 | $resp->{$_} = $runstate->{$_} for qw(host type class context); | |||||||||||||||||||||||||||
| 302 | 1 | $resp->{response} = undef; | |||||||||||||||||||||||||||
| 303 | 1 | $resp->{error} = $error; | |||||||||||||||||||||||||||
| 304 | 1 | delete $runstate->{trace}; | |||||||||||||||||||||||||||
| 305 | 1 | ▶ | if ( ref $runstate->{event} eq 'POE::Session::AnonEvent' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | my $postback = delete $runstate->{event}; | |||||||||||||||||||||||||||
| 307 | 0 | $postback->( $resp ); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||
| 310 | 1 | $kernel->post( $runstate->{sender_id}, $runstate->{event}, $resp ); | |||||||||||||||||||||||||||
| 311 | 1 | $kernel->refcount_decrement( $runstate->{sender_id}, __PACKAGE__ ); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | 1 | return; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | sub _close { | ||||||||||||||||||||||||||||
| 317 | 5 | ▶ | my ($kernel,$machine,$runstate,$packet) = @_[KERNEL,MACHINE,RUNSTATE,ARG0]; | ||||||||||||||||||||||||||
|
Subroutine
_close: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 318 | 5 | $kernel->select_read( $runstate->{socket} ); # Just in case | |||||||||||||||||||||||||||
| 319 | 5 | my $resp = {}; | |||||||||||||||||||||||||||
| 320 | 5 | $resp->{$_} = $runstate->{$_} for qw(host type class context); | |||||||||||||||||||||||||||
| 321 | 5 | $resp->{response} = $packet; | |||||||||||||||||||||||||||
| 322 | 5 | delete $runstate->{trace}; | |||||||||||||||||||||||||||
| 323 | 5 | ▶ | if ( ref $runstate->{event} eq 'POE::Session::AnonEvent' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 2 | my $postback = delete $runstate->{event}; | |||||||||||||||||||||||||||
| 325 | 2 | $postback->( $resp ); | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | else { | ||||||||||||||||||||||||||||
| 328 | 3 | $kernel->post( $runstate->{sender_id}, $runstate->{event}, $resp ); | |||||||||||||||||||||||||||
| 329 | 3 | $kernel->refcount_decrement( $runstate->{sender_id}, __PACKAGE__ ); | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | 5 | return; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | sub _authority { | ||||||||||||||||||||||||||||
| 335 | 10 | ▶ | my $packet = shift || return; | ||||||||||||||||||||||||||
|
Subroutine
_authority: called CC=9 SCAR=22.6
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 336 | 10 | my %hints; | |||||||||||||||||||||||||||
| 337 | 10 | ▶ | if (my @ans = $packet->authority) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | 10 | foreach my $rr (@ans) { | |||||||||||||||||||||||||||
| 339 | 85 | ▶ | if ( $rr->type eq 'NS') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | # Found root authority | ||||||||||||||||||||||||||||
| 341 | 85 | my $server = lc $rr->rdatastr; | |||||||||||||||||||||||||||
| 342 | 85 | $server =~ s/\.$//; | |||||||||||||||||||||||||||
| 343 | 85 | $hints{$server} = []; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | 10 | foreach my $rr ($packet->additional) { | |||||||||||||||||||||||||||
| 347 | 108 | ▶ | if (my $server = lc $rr->name){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 108 | ▶ | push @{ $hints{$server} }, $rr->rdatastr if $rr->type eq 'A' and $hints{$server}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rr->type eq "A" and $hints{$server}
MC/DC50%1/2
$rr->type eq "A" and $hints{$server}
$rr->type eq "A"
$hints{$server}
Truth table66%2/3
A and B
A= $rr->type eq "A"B= $hints{$server}
| |||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | 10 | return \%hints; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | sub _read_socket { | ||||||||||||||||||||||||||||
| 356 | 24 | ▶ | my $socket = shift || return; | ||||||||||||||||||||||||||
|
Subroutine
_read_socket: called CC=5 SCAR=19.9
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 357 | 24 | $poe_kernel->select_read( $socket ); | |||||||||||||||||||||||||||
| 358 | 24 | my $message; | |||||||||||||||||||||||||||
| 359 | 24 | ▶ | unless ( $socket->recv( $message, 512 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | 0 | warn "$!\n"; | |||||||||||||||||||||||||||
| 361 | 0 | return; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | 24 | my ($in, $len) = Net::DNS::Packet->new( \$message, 0 ); | |||||||||||||||||||||||||||
| 364 | 24 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 0 | warn "$@\n"; | |||||||||||||||||||||||||||
| 366 | 0 | return; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | 24 | ▶ | unless ( $len ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 0 | warn "Bad size\n"; | |||||||||||||||||||||||||||
| 370 | 0 | return; | |||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | 24 | return $in; | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | sub _ns_from_cache { | ||||||||||||||||||||||||||||
| 376 | 18 | ▶ | my $hashref = shift || return; | ||||||||||||||||||||||||||
|
Subroutine
_ns_from_cache: called CC=4 SCAR=13.9
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 377 | 18 | my @ns; | |||||||||||||||||||||||||||
| 378 | 18 | foreach my $ns (keys %{ $hashref }) { | |||||||||||||||||||||||||||
| 379 | 189 | ▶ | push @ns, @{ $hashref->{$ns} } if scalar @{ $hashref->{$ns} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | 18 | return @ns; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 8 | 'Recursive lookup, recursive lookup, recursive lookup ....'; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | __END__ | ||||||||||||||||||||||||||||