| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2011-2015 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IO::Async::Resolver::DNS; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use IO::Async::Resolver 0.52; # returns Future | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Net::DNS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use List::UtilsBy qw( weighted_shuffle_by ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Re-export the constants | ||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use IO::Async::Resolver::DNS::Constants qw( /^ERR_/ ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 5 | our @EXPORT_OK = @IO::Async::Resolver::DNS::Constants::EXPORT_OK; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 NAME | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | C<IO::Async::Resolver::DNS> - resolve DNS queries using C<IO::Async> | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | use IO::Async::Loop; | ||||||||||||||||||||||||||||
| 34 | use IO::Async::Resolver::DNS; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||
| 37 | my $resolver = $loop->resolver; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | $resolver->res_query( | ||||||||||||||||||||||||||||
| 40 | dname => "cpan.org", | ||||||||||||||||||||||||||||
| 41 | type => "MX", | ||||||||||||||||||||||||||||
| 42 | )->then( sub { | ||||||||||||||||||||||||||||
| 43 | my ( $pkt ) = @_; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | foreach my $mx ( $pkt->answer ) { | ||||||||||||||||||||||||||||
| 46 | next unless $mx->type eq "MX"; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | printf "preference=%d exchange=%s\n", | ||||||||||||||||||||||||||||
| 49 | $mx->preference, $mx->exchange; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | })->get; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | This module extends the L<IO::Async::Resolver> class with extra methods and | ||||||||||||||||||||||||||||
| 56 | resolver functions to perform DNS-specific resolver lookups. It does not | ||||||||||||||||||||||||||||
| 57 | directly provide any methods or functions of its own. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | These functions are provided for performing DNS-specific lookups, to obtain | ||||||||||||||||||||||||||||
| 60 | C<MX> or C<SRV> records, for example. For regular name resolution, the usual | ||||||||||||||||||||||||||||
| 61 | C<getaddrinfo> and C<getnameinfo> methods on the standard | ||||||||||||||||||||||||||||
| 62 | C<IO::Async::Resolver> should be used. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | If L<Net::LibResolv> is installed then it will be used for actually sending | ||||||||||||||||||||||||||||
| 65 | and receiving DNS packets, in preference to a internally-constructed | ||||||||||||||||||||||||||||
| 66 | L<Net::DNS::Resolver> object. C<Net::LibResolv> will be more efficient and | ||||||||||||||||||||||||||||
| 67 | shares its implementation with the standard resolver used by the rest of the | ||||||||||||||||||||||||||||
| 68 | system. C<Net::DNS::Resolver> reimplements the logic itself, so it may have | ||||||||||||||||||||||||||||
| 69 | differences in behaviour from that provided by F<libresolv>. The ability to | ||||||||||||||||||||||||||||
| 70 | use the latter is provided to allow for an XS-free dependency chain, or for | ||||||||||||||||||||||||||||
| 71 | other situations where C<Net::LibResolv> is not available. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 Record Extraction | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | If certain record type queries are made, extra information is returned to the | ||||||||||||||||||||||||||||
| 76 | C<on_resolved> continuation, containing the results from the DNS packet in a | ||||||||||||||||||||||||||||
| 77 | more useful form. This information will be in a list of extra values following | ||||||||||||||||||||||||||||
| 78 | the packet value. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my ( $pkt, @data ) = $f->get; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | $on_resolved->( $pkt, @data ) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | The type of the elements in C<@data> will depend on the DNS record query type: | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =over 4 | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub _extract | ||||||||||||||||||||||||||||
| 91 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_extract: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 92 | 6 | my ( $pkt, $type ) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 6 | ▶ | my $code = __PACKAGE__->can( "_extract_$type" ) or return ( $pkt ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 6 | return $code->( $pkt ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =item * A and AAAA | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | The C<A> or C<AAAA> records will be unpacked and returned in a list of | ||||||||||||||||||||||||||||
| 102 | strings. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | @data = ( "10.0.0.1", | ||||||||||||||||||||||||||||
| 105 | "10.0.0.2" ); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | @data = ( "fd00:0:0:0:0:0:0:1" ); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 5 | *_extract_A = \&_extract_addresses; | |||||||||||||||||||||||||||
| 112 | 5 | *_extract_AAAA = \&_extract_addresses; | |||||||||||||||||||||||||||
| 113 | sub _extract_addresses | ||||||||||||||||||||||||||||
| 114 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_extract_addresses: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 115 | 4 | my ( $pkt ) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 4 | my @addrs; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4 | foreach my $rr ( $pkt->answer ) { | |||||||||||||||||||||||||||
| 120 | 12 | ▶ | push @addrs, $rr->address if $rr->type eq "A" or $rr->type eq "AAAA"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rr->type eq "A" or $rr->type eq "AAAA"
MC/DC50%1/2
$rr->type eq "A" or $rr->type eq "AAAA"
$rr->type eq "A"
$rr->type eq "AAAA"
Truth table66%2/3
A or B
A= $rr->type eq "A"B= $rr->type eq "AAAA"
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 4 | return ( $pkt, @addrs ); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item * PTR | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | The C<PTR> records will be unpacked and returned in a list of domain names. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | @data = ( "foo.example.com" ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub _extract_PTR | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_extract_PTR: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 136 | 0 | my ( $pkt ) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | my @names; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | foreach my $rr ( $pkt->answer ) { | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | push @names, $rr->ptrdname if $rr->type eq "PTR"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | return ( $pkt, @names ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =item * MX | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | The C<MX> records will be unpacked, in order of C<preference>, and returned in | ||||||||||||||||||||||||||||
| 150 | a list of HASH references. Each HASH reference will contain keys called | ||||||||||||||||||||||||||||
| 151 | C<exchange> and C<preference>. If the exchange domain name is included in the | ||||||||||||||||||||||||||||
| 152 | DNS C<additional> data, then the HASH reference will also include a key called | ||||||||||||||||||||||||||||
| 153 | C<address>, its value containing a list of C<A> and C<AAAA> record C<address> | ||||||||||||||||||||||||||||
| 154 | fields. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | @data = ( { exchange => "mail.example.com", | ||||||||||||||||||||||||||||
| 157 | preference => 10, | ||||||||||||||||||||||||||||
| 158 | address => [ "10.0.0.1", "fd00:0:0:0:0:0:0:1" ] } ); | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub _extract_MX | ||||||||||||||||||||||||||||
| 163 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_extract_MX: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 164 | 1 | my ( $pkt ) = @_; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | my @mx; | |||||||||||||||||||||||||||
| 167 | my %additional; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 1 | foreach my $rr ( $pkt->additional ) { | |||||||||||||||||||||||||||
| 170 | 3 | ▶ | push @{ $additional{$rr->name}{address} }, $rr->address if $rr->type eq "A" or $rr->type eq "AAAA"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rr->type eq "A" or $rr->type eq "AAAA"
MC/DC0%0/2
$rr->type eq "A" or $rr->type eq "AAAA"
$rr->type eq "A"
$rr->type eq "AAAA"
Truth table66%2/3
A or B
A= $rr->type eq "A"B= $rr->type eq "AAAA"
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | foreach my $ans ( sort { $a->preference <=> $b->preference } grep { $_->type eq "MX" } $pkt->answer ) { | |||||||||||||||||||||||||||
| 174 | 2 | my $exchange = $ans->exchange; | |||||||||||||||||||||||||||
| 175 | 2 | push @mx, { exchange => $exchange, preference => $ans->preference }; | |||||||||||||||||||||||||||
| 176 | 2 | ▶ | $mx[-1]{address} = $additional{$exchange}{address} if $additional{$exchange}{address}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 1 | return ( $pkt, @mx ); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item * SRV | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | The C<SRV> records will be unpacked and sorted first by order of priority, | ||||||||||||||||||||||||||||
| 184 | then by a weighted shuffle by weight, and returned in a list of HASH | ||||||||||||||||||||||||||||
| 185 | references. Each HASH reference will contain keys called C<priority>, | ||||||||||||||||||||||||||||
| 186 | C<weight>, C<target> and C<port>. If the target domain name is included in the | ||||||||||||||||||||||||||||
| 187 | DNS C<additional> data, then the HASH reference will also contain a key called | ||||||||||||||||||||||||||||
| 188 | C<address>, its value containing a list of C<A> and C<AAAA> record C<address> | ||||||||||||||||||||||||||||
| 189 | fields. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | @data = ( { priority => 10, | ||||||||||||||||||||||||||||
| 192 | weight => 10, | ||||||||||||||||||||||||||||
| 193 | target => "server1.service.example.com", | ||||||||||||||||||||||||||||
| 194 | port => 1234, | ||||||||||||||||||||||||||||
| 195 | address => [ "10.0.1.1" ] } ); | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | sub _extract_SRV | ||||||||||||||||||||||||||||
| 200 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_extract_SRV: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 201 | 1 | my ( $pkt ) = @_; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 1 | my @srv; | |||||||||||||||||||||||||||
| 204 | my %additional; | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 1 | foreach my $rr ( $pkt->additional ) { | |||||||||||||||||||||||||||
| 207 | 4 | ▶ | push @{ $additional{$rr->name}{address} }, $rr->address if $rr->type eq "A" or $rr->type eq "AAAA"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rr->type eq "A" or $rr->type eq "AAAA"
MC/DC0%0/2
$rr->type eq "A" or $rr->type eq "AAAA"
$rr->type eq "A"
$rr->type eq "AAAA"
Truth table33%1/3
A or B
A= $rr->type eq "A"B= $rr->type eq "AAAA"
| |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | my %srv_by_prio; | |||||||||||||||||||||||||||
| 211 | # Need to work in two phases. Split by priority then shuffle within | ||||||||||||||||||||||||||||
| 212 | 1 | foreach my $ans ( grep { $_->type eq "SRV" } $pkt->answer ) { | |||||||||||||||||||||||||||
| 213 | 5 | push @{ $srv_by_prio{ $ans->priority } }, $ans; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1 | foreach my $prio ( sort { $a <=> $b } keys %srv_by_prio ) { | |||||||||||||||||||||||||||
| 217 | 3 | ▶ | foreach my $ans ( weighted_shuffle_by { $_->weight || 1 } @{ $srv_by_prio{$prio} } ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 5 | my $target = $ans->target; | |||||||||||||||||||||||||||
| 219 | 5 | push @srv, { priority => $ans->priority, | |||||||||||||||||||||||||||
| 220 | weight => $ans->weight, | ||||||||||||||||||||||||||||
| 221 | target => $target, | ||||||||||||||||||||||||||||
| 222 | port => $ans->port }; | ||||||||||||||||||||||||||||
| 223 | 5 | ▶ | $srv[-1]{address} = $additional{$target}{address} if $additional{$target}{address}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | 1 | return ( $pkt, @srv ); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =back | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 Error Reporting | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | The two possible back-end modules that implement the resolver query functions | ||||||||||||||||||||||||||||
| 234 | provided here differ in their semantics for error reporting. To account for | ||||||||||||||||||||||||||||
| 235 | this difference and to lead to more portable user code, errors reported by the | ||||||||||||||||||||||||||||
| 236 | back-end modules are translated to one of the following (exported) constants. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | ERR_NO_HOST # The specified host name does not exist | ||||||||||||||||||||||||||||
| 239 | ERR_NO_ADDRESS # The specified host name does not provide answers for the | ||||||||||||||||||||||||||||
| 240 | given query type | ||||||||||||||||||||||||||||
| 241 | ERR_TEMPORARY # A temporary failure that may disappear on retry | ||||||||||||||||||||||||||||
| 242 | ERR_UNRECOVERABLE # Any other error | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head1 RESOLVER METHODS | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | The following methods documented with a trailing call to C<< ->get >> return | ||||||||||||||||||||||||||||
| 249 | L<Future> instances. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =cut | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head2 res_query | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | ( $pkt, @data ) = $resolver->res_query( %params )->get | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Performs a resolver query on the name, class and type, and invokes a | ||||||||||||||||||||||||||||
| 258 | continuation when a result is obtained. | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | Takes the following named parameters: | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =over 8 | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =item dname => STRING | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | Domain name to look up | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =item type => STRING | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Name of the record type to look up (e.g. C<MX>) | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =item class => STRING | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | Name of the record class to look up. Defaults to C<IN> so normally this | ||||||||||||||||||||||||||||
| 275 | argument is not required. | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =back | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | On failure on C<IO::Async> versions that support extended failure results | ||||||||||||||||||||||||||||
| 280 | (0.68 and later), the extra detail will be an error value matching one of the | ||||||||||||||||||||||||||||
| 281 | C<ERR_*> constants listed above. | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | ->fail( $message, resolve => res_query => $errnum ) | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | Note that due to the two possible back-end implementations it is not | ||||||||||||||||||||||||||||
| 286 | guaranteed that messages have any particular format; they are intended for | ||||||||||||||||||||||||||||
| 287 | human consumption only, and the C<$errnum> value should be used for making | ||||||||||||||||||||||||||||
| 288 | decisions in other code. | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | When not returning a C<Future>, the following extra arguments are used as | ||||||||||||||||||||||||||||
| 291 | callbacks instead: | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =over 8 | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =item on_resolved => CODE | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Continuation which is invoked after a successful lookup. Will be passed a | ||||||||||||||||||||||||||||
| 298 | L<Net::DNS::Packet> object containing the result. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | $on_resolved->( $pkt ) | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | For certain query types, this continuation may also be passed extra data in a | ||||||||||||||||||||||||||||
| 303 | list after the C<$pkt> | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | $on_resolved->( $pkt, @data ) | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | See the B<Record Extraction> section above for more detail. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =item on_error => CODE | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Continuation which is invoked after a failed lookup. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =back | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =cut | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | sub IO::Async::Resolver::res_query | ||||||||||||||||||||||||||||
| 318 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
res_query: called CC=9 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 319 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 320 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 4 | ▶ | my $dname = $args{dname} or croak "Expected 'dname'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 4 | ▶ | my $class = $args{class} || "IN"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'class'} || 'IN'
MC/DC0%0/1
$args{'class'} || 'IN'
$args{'class'}
Truth table50%1/2
A
A= $args{'class'}
| |||||||||||||||||||||||||||||
| 324 | 4 | ▶ | my $type = $args{type} or croak "Expected 'type'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 4 | my $on_resolved = delete $args{on_resolved}; | |||||||||||||||||||||||||||
| 327 | 4 | ▶ | !$on_resolved or ref $on_resolved or croak "Expected 'on_resolved' to be a reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $on_resolved or ref $on_resolved
MC/DC0%0/2
not $on_resolved or ref $on_resolved
not $on_resolved
ref $on_resolved
Truth table66%2/3
not(A) or B
A= not $on_resolvedB= ref $on_resolved
| |||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | my $f = $self->resolve( | ||||||||||||||||||||||||||||
| 330 | type => "res_query", | ||||||||||||||||||||||||||||
| 331 | data => [ $dname, $class, $type ], | ||||||||||||||||||||||||||||
| 332 | 4 | ▶ | )->then( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 333 | 4 | my ( $data ) = @_; | |||||||||||||||||||||||||||
| 334 | 4 | my $pkt = Net::DNS::Packet->new( \$data ); | |||||||||||||||||||||||||||
| 335 | 4 | Future->done( _extract( $pkt, $type ) ); | |||||||||||||||||||||||||||
| 336 | 4 | }); | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 4 | ▶ | $f->on_done( $on_resolved ) if $on_resolved; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 4 | ▶ | $f->on_fail( $args{on_error} ) if $args{on_error}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 4 | ▶ | $self->adopt_future( $f ) unless defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 4 | return $f; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =head2 res_search | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | Performs a resolver query on the name, class and type, and invokes a | ||||||||||||||||||||||||||||
| 349 | continuation when a result is obtained. Identical to C<res_query> except that | ||||||||||||||||||||||||||||
| 350 | it additionally implements the default domain name search behaviour. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =cut | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | sub IO::Async::Resolver::res_search | ||||||||||||||||||||||||||||
| 355 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
res_search: called CC=9 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 356 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 357 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 2 | ▶ | my $dname = $args{dname} or croak "Expected 'dname'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | 2 | ▶ | my $class = $args{class} || "IN"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'class'} || 'IN'
MC/DC0%0/1
$args{'class'} || 'IN'
$args{'class'}
Truth table50%1/2
A
A= $args{'class'}
| |||||||||||||||||||||||||||||
| 361 | 2 | ▶ | my $type = $args{type} or croak "Expected 'type'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 2 | my $on_resolved = delete $args{on_resolved}; | |||||||||||||||||||||||||||
| 364 | 2 | ▶ | !$on_resolved or ref $on_resolved or croak "Expected 'on_resolved' to be a reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $on_resolved or ref $on_resolved
MC/DC0%0/2
not $on_resolved or ref $on_resolved
not $on_resolved
ref $on_resolved
Truth table66%2/3
not(A) or B
A= not $on_resolvedB= ref $on_resolved
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | my $f = $self->resolve( | ||||||||||||||||||||||||||||
| 367 | type => "res_search", | ||||||||||||||||||||||||||||
| 368 | data => [ $dname, $class, $type ], | ||||||||||||||||||||||||||||
| 369 | 2 | ▶ | )->then( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 370 | 2 | my ( $data ) = @_; | |||||||||||||||||||||||||||
| 371 | 2 | my $pkt = Net::DNS::Packet->new( \$data ); | |||||||||||||||||||||||||||
| 372 | 2 | Future->done( _extract( $pkt, $type ) ); | |||||||||||||||||||||||||||
| 373 | 2 | }); | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 2 | ▶ | $f->on_done( $on_resolved ) if $on_resolved; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 2 | ▶ | $f->on_fail( $args{on_error} ) if $args{on_error}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 2 | ▶ | $self->adopt_future( $f ) unless defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 2 | return $f; | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | # We'd prefer to use libresolv to actually talk DNS as it'll be more efficient | ||||||||||||||||||||||||||||
| 384 | # and more standard to the OS | ||||||||||||||||||||||||||||
| 385 | 5 | my @impls = qw( | |||||||||||||||||||||||||||
| 386 | LibResolvImpl | ||||||||||||||||||||||||||||
| 387 | NetDNSImpl | ||||||||||||||||||||||||||||
| 388 | ); | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 5 | ▶ | while( !defined &res_query ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | 4 | ▶ | die "Unable to load an IO::Async::Resolver::DNS implementation\n" unless @impls; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 4 | eval { require "IO/Async/Resolver/DNS/" . shift(@impls) . ".pm" }; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 5 | IO::Async::Resolver::register_resolver res_query => \&res_query; | |||||||||||||||||||||||||||
| 396 | 5 | IO::Async::Resolver::register_resolver res_search => \&res_search; | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =cut | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 5 | 0x55AA; | |||||||||||||||||||||||||||