| 1 | package Net::DNS::Resolver::Recurse; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | our $VERSION = (qw$Id: Recurse.pm 2002 2025-01-07 09:57:46Z willem $)[2]; | |||||||||||||
| 6 | |||||||||||||||
| 7 | |||||||||||||||
| 8 | =head1 NAME | ||||||||||||||
| 9 | |||||||||||||||
| 10 | Net::DNS::Resolver::Recurse - DNS recursive resolver | ||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||
| 14 | |||||||||||||||
| 15 | use Net::DNS::Resolver::Recurse; | ||||||||||||||
| 16 | |||||||||||||||
| 17 | my $resolver = new Net::DNS::Resolver::Recurse(); | ||||||||||||||
| 18 | |||||||||||||||
| 19 | $resolver->hints('198.41.0.4'); # A.ROOT-SERVER.NET. | ||||||||||||||
| 20 | |||||||||||||||
| 21 | my $packet = $resolver->send( 'www.rob.com.au.', 'A' ); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||
| 25 | |||||||||||||||
| 26 | This module resolves queries by following the delegation path from the DNS root. | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =cut | ||||||||||||||
| 29 | |||||||||||||||
| 30 | |||||||||||||||
| 31 | 2 | ▶ | use base qw(Net::DNS::Resolver::Base); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | |||||||||||||||
| 34 | =head1 METHODS | ||||||||||||||
| 35 | |||||||||||||||
| 36 | This module inherits almost all the methods from Net::DNS::Resolver. | ||||||||||||||
| 37 | Additional module-specific methods are described below. | ||||||||||||||
| 38 | |||||||||||||||
| 39 | |||||||||||||||
| 40 | =head2 hints | ||||||||||||||
| 41 | |||||||||||||||
| 42 | This method specifies a list of the IP addresses of nameservers to | ||||||||||||||
| 43 | be used to discover the addresses of the root nameservers. | ||||||||||||||
| 44 | |||||||||||||||
| 45 | $resolver->hints(@ip); | ||||||||||||||
| 46 | |||||||||||||||
| 47 | If no hints are passed, the priming query is directed to nameservers | ||||||||||||||
| 48 | drawn from a built-in list of IP addresses. | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =cut | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 2 | my @hints; | |||||||||||||
| 53 | my $root; | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 4 | ▶ | sub hints { | ||||||||||||
|
Subroutine
hints: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 56 | 4 | my ( undef, @argument ) = @_; | |||||||||||||
| 57 | 4 | ▶ | return @hints unless scalar @argument; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 2 | undef $root; | |||||||||||||
| 59 | 2 | return @hints = @argument; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | |||||||||||||||
| 63 | =head2 query, search, send | ||||||||||||||
| 64 | |||||||||||||||
| 65 | The query(), search() and send() methods produce the same result | ||||||||||||||
| 66 | as their counterparts in Net::DNS::Resolver. | ||||||||||||||
| 67 | |||||||||||||||
| 68 | $packet = $resolver->send( 'www.example.com.', 'A' ); | ||||||||||||||
| 69 | |||||||||||||||
| 70 | Server-side recursion is suppressed by clearing the recurse flag in | ||||||||||||||
| 71 | query packets and recursive name resolution is performed explicitly. | ||||||||||||||
| 72 | |||||||||||||||
| 73 | The query() and search() methods are inherited from Net::DNS::Resolver | ||||||||||||||
| 74 | and invoke send() indirectly. | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =cut | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 16 | ▶ | sub send { | ||||||||||||
|
Subroutine
send: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 79 | 16 | my ( $self, @q ) = @_; | |||||||||||||
| 80 | 16 | my @conf = ( recurse => 0, udppacketsize => 1232 ); | |||||||||||||
| 81 | 16 | return bless( {persistent => {'.' => $root}, %$self, @conf}, ref($self) )->_send(@q); | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | |||||||||||||||
| 85 | 1 | ▶ | sub query_dorecursion { ## historical | ||||||||||||
|
Subroutine
query_dorecursion: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 86 | 1 | my ($self) = @_; # uncoverable pod | |||||||||||||
| 87 | 1 | $self->_deprecate('prefer $resolver->send(...)'); | |||||||||||||
| 88 | 1 | return &send; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | |||||||||||||||
| 92 | 16 | ▶ | sub _send { | ||||||||||||
|
Subroutine
_send: called CC=3 SCAR=11.0
| |||||||||||||||
| 93 | 16 | my ( $self, @q ) = @_; | |||||||||||||
| 94 | 16 | my $query = $self->_make_query_packet(@q); | |||||||||||||
| 95 | |||||||||||||||
| 96 | 16 | ▶ | unless ($root) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 4 | $self->_diag('resolver priming query'); | |||||||||||||
| 98 | 4 | ▶ | $self->nameservers( scalar(@hints) ? @hints : $self->_hints ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 4 | $self->_referral( $self->SUPER::send(qw(. NS)) ); | |||||||||||||
| 100 | 4 | $root = $self->{persistent}->{'.'}; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 16 | return $self->_recurse( $query, '.' ); | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | |||||||||||||||
| 107 | 43 | ▶ | sub _recurse { | ||||||||||||
|
Subroutine
_recurse: called CC=9 SCAR=22.0
| |||||||||||||||
| 108 | 43 | my ( $self, $query, $apex ) = @_; | |||||||||||||
| 109 | 43 | $self->_diag("using cached nameservers for $apex"); | |||||||||||||
| 110 | 43 | my $cache = $self->{persistent}->{$apex}; | |||||||||||||
| 111 | 43 | my @nslist = keys %$cache; | |||||||||||||
| 112 | 43 | my @glue = grep { $$cache{$_} } @nslist; | |||||||||||||
| 113 | 43 | my @noglue = grep { !$$cache{$_} } @nslist; | |||||||||||||
| 114 | 43 | my $reply; | |||||||||||||
| 115 | 43 | foreach my $ns ( @glue, @noglue ) { | |||||||||||||
| 116 | 40 | ▶ | if ( my $iplist = $$cache{$ns} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | 35 | $self->nameservers(@$iplist); | |||||||||||||
| 118 | } else { | ||||||||||||||
| 119 | 5 | $self->_diag("recover missing glue for $ns"); | |||||||||||||
| 120 | 5 | ▶ | next if substr( lc($ns), -length($apex) ) eq $apex; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 4 | my @ip = $self->nameservers($ns); | |||||||||||||
| 122 | 4 | $$cache{$ns} = \@ip; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | 39 | $query->header->id(undef); | |||||||||||||
| 125 | 39 | ▶ | last if $reply = $self->SUPER::send($query); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 1 | $$cache{$ns} = undef; # park non-responder | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | 43 | $self->_callback($reply); | |||||||||||||
| 129 | 43 | ▶ | return unless $reply; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 38 | ▶ | my $zone = $self->_referral($reply) || return $reply; | ||||||||||||
|
Condition100%2/2
$self->_referral($reply) || (return $reply)
MC/DC100%1/1
$self->_referral($reply) || (return $reply)
$self->_referral($reply)
Truth table100%2/2
A
A= $self->_referral($reply)
| |||||||||||||||
| 131 | 26 | ▶ | die '_recurse exceeded depth limit' if $self->{recurse_depth}++ > 50; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 25 | my $qname = lc( ( $query->question )[0]->qname ); | |||||||||||||
| 133 | 25 | my $suffix = substr( $qname, -length($zone) ); | |||||||||||||
| 134 | 25 | ▶ | return $zone eq $suffix ? $self->_recurse( $query, $zone ) : undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | |||||||||||||||
| 138 | 45 | ▶ | sub _referral { | ||||||||||||
|
Subroutine
_referral: called CC=8 SCAR=20.8
| |||||||||||||||
| 139 | 45 | my ( $self, $packet ) = @_; | |||||||||||||
| 140 | 45 | ▶ | return unless $packet; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 43 | my @ans = $packet->answer; | |||||||||||||
| 142 | 43 | my @auth = grep { $_->type eq 'NS' } $packet->authority, @ans; | |||||||||||||
| 143 | 43 | ▶ | return unless scalar(@auth); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 38 | my $owner = lc( $auth[0]->owner ); | |||||||||||||
| 145 | 38 | my $cache = $self->{persistent}->{$owner}; | |||||||||||||
| 146 | 38 | ▶ | return scalar(@ans) ? undef : $owner if $cache; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 147 | |||||||||||||||
| 148 | 23 | $self->_diag("caching nameservers for $owner"); | |||||||||||||
| 149 | 23 | my %addr; | |||||||||||||
| 150 | 23 | my @addr = grep { $_->can('address') } $packet->additional; | |||||||||||||
| 151 | 23 | push @{$addr{lc $_->owner}}, $_->address foreach @addr; | |||||||||||||
| 152 | |||||||||||||||
| 153 | 23 | my %cache; | |||||||||||||
| 154 | 23 | foreach my $ns ( map { lc( $_->nsdname ) } @auth ) { | |||||||||||||
| 155 | 136 | $cache{$ns} = $addr{$ns}; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 23 | $self->{persistent}->{$owner} = \%cache; | |||||||||||||
| 159 | 23 | ▶ | return scalar(@ans) ? undef : $owner; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | |||||||||||||||
| 163 | =head2 callback | ||||||||||||||
| 164 | |||||||||||||||
| 165 | This method specifies a code reference to a subroutine, | ||||||||||||||
| 166 | which is then invoked at each stage of the recursive lookup. | ||||||||||||||
| 167 | |||||||||||||||
| 168 | For example to emulate dig's C<+trace> function: | ||||||||||||||
| 169 | |||||||||||||||
| 170 | my $coderef = sub { | ||||||||||||||
| 171 | my $packet = shift; | ||||||||||||||
| 172 | |||||||||||||||
| 173 | printf ";; Received %d bytes from %s\n\n", | ||||||||||||||
| 174 | $packet->answersize, $packet->answerfrom; | ||||||||||||||
| 175 | }; | ||||||||||||||
| 176 | |||||||||||||||
| 177 | $resolver->callback($coderef); | ||||||||||||||
| 178 | |||||||||||||||
| 179 | The callback subroutine is not called | ||||||||||||||
| 180 | for queries for missing glue records. | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =cut | ||||||||||||||
| 183 | |||||||||||||||
| 184 | 4 | ▶ | sub callback { | ||||||||||||
|
Subroutine
callback: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 185 | 4 | my ( $self, @argument ) = @_; | |||||||||||||
| 186 | 4 | for ( grep { ref($_) eq 'CODE' } @argument ) { | |||||||||||||
| 187 | 3 | $self->{callback} = $_; | |||||||||||||
| 188 | } | ||||||||||||||
| 189 | 4 | return; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | |||||||||||||||
| 192 | 43 | ▶ | sub _callback { | ||||||||||||
|
Subroutine
_callback: called CC=2 SCAR=6.9
| |||||||||||||||
| 193 | 43 | my ( $self, @argument ) = @_; | |||||||||||||
| 194 | 43 | my $callback = $self->{callback}; | |||||||||||||
| 195 | 43 | ▶ | $callback->(@argument) if $callback; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 196 | 43 | return; | |||||||||||||
| 197 | } | ||||||||||||||
| 198 | |||||||||||||||
| 199 | 1 | ▶ | sub recursion_callback { ## historical | ||||||||||||
|
Subroutine
recursion_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 200 | 1 | my ($self) = @_; # uncoverable pod | |||||||||||||
| 201 | 1 | $self->_deprecate('prefer $resolver->callback(...)'); | |||||||||||||
| 202 | 1 | &callback; | |||||||||||||
| 203 | 1 | return; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | |||||||||||||||
| 207 | 2 | 1; | |||||||||||||
| 208 | |||||||||||||||
| 209 | __END__ | ||||||||||||||