blib/lib/IO/Async/Resolver/DNS.pm

filter: Statementstmt 94.7% 90 / 95 Branchbran 62.5% 20 / 32 Conditioncond 53.8% 7 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 2 totaltotal 80.1% 133 / 166 SCAR 34.5
CC
28
Cov
84%
_extract_PTR
12.0
_extract_SRV
8.1
_extract_MX
6.0
CRAP 31.5
↑ Summary Build »
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-2012 -- 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.04';
12  
13 5 ▶use IO::Async::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use List::UtilsBy qw( weighted_shuffle_by );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 C<IO::Async::Resolver::DNS> - resolve DNS queries using C<IO::Async>
23  
24 =head1 SYNOPSIS
25  
26 use IO::Async::Loop;
27 use IO::Async::Resolver::DNS;
28
29 my $loop = IO::Async::Loop->new;
30 my $resolver = $loop->resolver;
31
32 $resolver->res_query(
33 dname => "cpan.org",
34 type => "MX",
35 on_resolved => sub {
36 my ( $pkt ) = @_;
37
38 foreach my $mx ( $pkt->answer ) {
39 next unless $mx->type eq "MX";
40
41 printf "preference=%d exchange=%s\n",
42 $mx->preference, $mx->exchange;
43 }
44 $loop->stop;
45 },
46 on_error => sub { die "Cannot resolve - $_[-1]\n" },
47 );
48
49 $loop->run;
50  
51 =head1 DESCRIPTION
52  
53 This module extends the L<IO::Async::Resolver> class with extra methods and
54 resolver functions to perform DNS-specific resolver lookups. It does not
55 directly provide any methods or functions of its own.
56  
57 These functions are provided for performing DNS-specific lookups, to obtain
58 C<MX> or C<SRV> records, for example. For regular name resolution, the usual
59 C<getaddrinfo> and C<getnameinfo> methods on the standard
60 C<IO::Async::Resolver> should be used.
61  
62 If L<Net::LibResolv> is installed then it will be used for actually sending
63 and receiving DNS packets, in preference to a internally-constructed
64 L<Net::DNS::Resolver> object. C<Net::LibResolv> will be more efficient and
65 shares its implementation with the standard resolver used by the rest of the
66 system. C<Net::DNS::Resolver> reimplements the logic itself, so it may have
67 differences in behaviour from that provided by F<libresolv>. The ability to
68 use the latter is provided to allow for an XS-free dependency chain, or for
69 other situations where C<Net::LibResolv> is not available.
70  
71 =head2 Record Extraction
72  
73 If certain record type queries are made, extra information is returned to the
74 C<on_resolved> continuation, containing the results from the DNS packet in a
75 more useful form. This information will be in a list of extra values following
76 the packet value
77  
78 $on_resolved->( $pkt, @data )
79  
80 The type of the elements in C<@data> will depend on the DNS record query type:
81  
82 =over 4
83  
84 =cut
85  
86 sub _extract
87 {
88 6 ▶ my ( $pkt, $type ) = @_;
Subroutine _extract: called CC=2 SCAR=7.1
89  
90 6 ▶ my $code = __PACKAGE__->can( "_extract_$type" ) or return ( $pkt );
Branch: 1 branch
BranchTrueFalseTotal
my $code = __PACKAGE__->can("_extract_$type") or return $pkt 0 6 6
91  
92 6 return $code->( $pkt );
93 }
94  
95 =item * A and AAAA
96  
97 The C<A> or C<AAAA> records will be unpacked and returned in a list of
98 strings.
99  
100 @data = ( "10.0.0.1",
101 "10.0.0.2" );
102  
103 @data = ( "fd00:0:0:0:0:0:0:1" );
104  
105 =cut
106  
107 5 *_extract_A = \&_extract_addresses;
108 5 *_extract_AAAA = \&_extract_addresses;
109 sub _extract_addresses
110 {
111 4 ▶ my ( $pkt ) = @_;
Subroutine _extract_addresses: called CC=4 SCAR=13.9
112  
113 4 my @addrs;
114  
115 4 foreach my $rr ( $pkt->answer ) {
116 12 ▶ push @addrs, $rr->address if $rr->type eq "A" or $rr->type eq "AAAA";
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type eq "A" or $rr->type eq "AAAA" 10 2 12
Condition66%2/3
$rr->type eq "A" or $rr->type eq "AAAA"
l!l&&r!l&&!r
1002
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"
ABresult
1X1
011
000
117 }
118  
119 4 return ( $pkt, @addrs );
120 }
121  
122 =item * PTR
123  
124 The C<PTR> records will be unpacked and returned in a list of domain names.
125  
126 @data = ( "foo.example.com" );
127  
128 =cut
129  
130 sub _extract_PTR
131 {
132 0 ▶ my ( $pkt ) = @_;
Subroutine _extract_PTR: not called CC=3 SCAR=24.8
133  
134 0 my @names;
135  
136 0 foreach my $rr ( $pkt->answer ) {
137 0 ▶ push @names, $rr->ptrdname if $rr->type eq "PTR";
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type eq "PTR" 0 0 0
138 }
139  
140 0 return ( $pkt, @names );
141 }
142  
143 =item * MX
144  
145 The C<MX> records will be unpacked, in order of C<preference>, and returned in
146 a list of HASH references. Each HASH reference will contain keys called
147 C<exchange> and C<preference>. If the exchange domain name is included in the
148 DNS C<additional> data, then the HASH reference will also include a key called
149 C<address>, its value containing a list of C<A> and C<AAAA> record C<address>
150 fields.
151  
152 @data = ( { exchange => "mail.example.com",
153 preference => 10,
154 address => [ "10.0.0.1", "fd00:0:0:0:0:0:0:1" ] } );
155  
156 =cut
157  
158 sub _extract_MX
159 {
160 1 ▶ my ( $pkt ) = @_;
Subroutine _extract_MX: called CC=6 SCAR=18.0
161  
162 1 my @mx;
163 my %additional;
164  
165 1 foreach my $rr ( $pkt->additional ) {
166 3 ▶ push @{ $additional{$rr->name}{address} }, $rr->address if $rr->type eq "A" or $rr->type eq "AAAA";
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type eq "A" or $rr->type eq "AAAA" 3 0 3
Condition66%2/3
$rr->type eq "A" or $rr->type eq "AAAA"
l!l&&r!l&&!r
210
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"
ABresult
1X1
011
000
167 }
168  
169 1 foreach my $ans ( sort { $a->preference <=> $b->preference } grep { $_->type eq "MX" } $pkt->answer ) {
170 2 my $exchange = $ans->exchange;
171 2 push @mx, { exchange => $exchange, preference => $ans->preference };
172 2 ▶ $mx[-1]{address} = $additional{$exchange}{address} if $additional{$exchange}{address};
Branch: 1 branch
BranchTrueFalseTotal
if $additional{$exchange}{"address"} 1 1 2
173 }
174 1 return ( $pkt, @mx );
175 }
176  
177 =item * SRV
178  
179 The C<SRV> records will be unpacked and sorted first by order of priority,
180 then by a weighted shuffle by weight, and returned in a list of HASH
181 references. Each HASH reference will contain keys called C<priority>,
182 C<weight>, C<target> and C<port>. If the target domain name is included in the
183 DNS C<additional> data, then the HASH reference will also contain a key called
184 C<address>, its value containing a list of C<A> and C<AAAA> record C<address>
185 fields.
186  
187 @data = ( { priority => 10,
188 weight => 10,
189 target => "server1.service.example.com",
190 port => 1234,
191 address => [ "10.0.1.1" ] } );
192  
193 =cut
194  
195 sub _extract_SRV
196 {
197 1 ▶ my ( $pkt ) = @_;
Subroutine _extract_SRV: called CC=8 SCAR=20.9
198  
199 1 my @srv;
200 my %additional;
201  
202 1 foreach my $rr ( $pkt->additional ) {
203 4 ▶ push @{ $additional{$rr->name}{address} }, $rr->address if $rr->type eq "A" or $rr->type eq "AAAA";
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type eq "A" or $rr->type eq "AAAA" 4 0 4
Condition33%1/3
$rr->type eq "A" or $rr->type eq "AAAA"
l!l&&r!l&&!r
400
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"
ABresult
1X1
011
000
204 }
205  
206 1 my %srv_by_prio;
207 # Need to work in two phases. Split by priority then shuffle within
208 1 foreach my $ans ( grep { $_->type eq "SRV" } $pkt->answer ) {
209 5 push @{ $srv_by_prio{ $ans->priority } }, $ans;
210 }
211  
212 1 foreach my $prio ( sort { $a <=> $b } keys %srv_by_prio ) {
213 3 ▶ foreach my $ans ( weighted_shuffle_by { $_->weight || 1 } @{ $srv_by_prio{$prio} } ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_->weight or 1 2 3 5
214 5 my $target = $ans->target;
215 5 push @srv, { priority => $ans->priority,
216 weight => $ans->weight,
217 target => $target,
218 port => $ans->port };
219 5 ▶ $srv[-1]{address} = $additional{$target}{address} if $additional{$target}{address};
Branch: 1 branch
BranchTrueFalseTotal
if $additional{$target}{"address"} 4 1 5
220 }
221 }
222 1 return ( $pkt, @srv );
223 }
224  
225 =back
226  
227 =cut
228  
229 =head1 RESOLVER METHODS
230  
231 =cut
232  
233 =head2 $resolver->res_query( %params )
234  
235 Performs a resolver query on the name, class and type, and invokes a
236 continuation when a result is obtained.
237  
238 Takes the following named parameters:
239  
240 =over 8
241  
242 =item dname => STRING
243  
244 Domain name to look up
245  
246 =item type => STRING
247  
248 Name of the record type to look up (e.g. C<MX>)
249  
250 =item class => STRING
251  
252 Name of the record class to look up. Defaults to C<IN> so normally this
253 argument is not required.
254  
255 =item on_resolved => CODE
256  
257 Continuation which is invoked after a successful lookup. Will be passed a
258 L<Net::DNS::Packet> object containing the result.
259  
260 $on_resolved->( $pkt )
261  
262 For certain query types, this continuation may also be passed extra data in a
263 list after the C<$pkt>
264  
265 $on_resolved->( $pkt, @data )
266  
267 See the B<Record Extraction> section above for more detail.
268  
269 =item on_error => CODE
270  
271 Continuation which is invoked after a failed lookup.
272  
273 =back
274  
275 =cut
276  
277 sub IO::Async::Resolver::res_query
278 {
279 4 ▶ my $self = shift;
Subroutine res_query: called CC=5 SCAR=16.6
Pod undocumented
280 4 my %args = @_;
281  
282 4 ▶ my $dname = $args{dname} or croak "Expected 'dname'";
Branch: 1 branch
BranchTrueFalseTotal
my $dname = $args{"dname"} or croak("Expected 'dname'") 0 4 4
283 4 ▶ my $class = $args{class} || "IN";
Condition50%1/2
$args{'class'} || 'IN'
l!l
04
MC/DC0%0/1
$args{'class'} || 'IN'
$args{'class'}
Truth table50%1/2
A
A= $args{'class'}
Aresult
11
00
284 4 ▶ my $type = $args{type} or croak "Expected 'type'";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $args{"type"} or croak("Expected 'type'") 0 4 4
285  
286 4 my $on_resolved = $args{on_resolved};
287 4 ▶ ref $on_resolved or croak "Expected 'on_resolved' to be a reference";
Branch: 1 branch
BranchTrueFalseTotal
ref $on_resolved or croak("Expected 'on_resolved' to be a reference") 0 4 4
288  
289 $self->resolve(
290 type => "res_query",
291 data => [ $dname, $class, $type ],
292 on_resolved => sub {
293 4 ▶ my ( $data ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
294 4 my $pkt = Net::DNS::Packet->new( \$data );
295 4 $on_resolved->( _extract( $pkt, $type ) );
296 },
297 on_error => $args{on_error},
298 4 );
299 }
300  
301 =head2 $resolver->res_search( %params )
302  
303 Performs a resolver query on the name, class and type, and invokes a
304 continuation when a result is obtained. Identical to C<res_query> except that
305 it additionally implements the default domain name search behaviour.
306  
307 =cut
308  
309 sub IO::Async::Resolver::res_search
310 {
311 2 ▶ my $self = shift;
Subroutine res_search: called CC=5 SCAR=16.6
Pod undocumented
312 2 my %args = @_;
313  
314 2 ▶ my $dname = $args{dname} or croak "Expected 'dname'";
Branch: 1 branch
BranchTrueFalseTotal
my $dname = $args{"dname"} or croak("Expected 'dname'") 0 2 2
315 2 ▶ my $class = $args{class} || "IN";
Condition50%1/2
$args{'class'} || 'IN'
l!l
02
MC/DC0%0/1
$args{'class'} || 'IN'
$args{'class'}
Truth table50%1/2
A
A= $args{'class'}
Aresult
11
00
316 2 ▶ my $type = $args{type} or croak "Expected 'type'";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $args{"type"} or croak("Expected 'type'") 0 2 2
317  
318 2 my $on_resolved = $args{on_resolved};
319 2 ▶ ref $on_resolved or croak "Expected 'on_resolved' to be a reference";
Branch: 1 branch
BranchTrueFalseTotal
ref $on_resolved or croak("Expected 'on_resolved' to be a reference") 0 2 2
320  
321 $self->resolve(
322 type => "res_search",
323 data => [ $dname, $class, $type ],
324 on_resolved => sub {
325 2 ▶ my ( $data ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
326 2 my $pkt = Net::DNS::Packet->new( \$data );
327 2 $on_resolved->( _extract( $pkt, $type ) );
328 },
329 on_error => $args{on_error},
330 2 );
331 }
332  
333 # We'd prefer to use libresolv to actually talk DNS as it'll be more efficient
334 # and more standard to the OS
335 5 my @impls = qw(
336 LibResolvImpl
337 NetDNSImpl
338 );
339  
340 5 ▶while( !defined &res_query ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &IO::Async::Resolver::DNS::res_query) 4 5 9
341 4 ▶ die "Unable to load an IO::Async::Resolver::DNS implementation\n" unless @impls;
Branch: 1 branch
BranchTrueFalseTotal
unless @impls 0 4 4
342 4 eval { require "IO/Async/Resolver/DNS/" . shift(@impls) . ".pm" };
343 }
344  
345 5 IO::Async::Resolver::register_resolver res_query => \&res_query;
346 5 IO::Async::Resolver::register_resolver res_search => \&res_search;
347  
348 =head1 AUTHOR
349  
350 Paul Evans <leonerd@leonerd.org.uk>
351  
352 =cut
353  
354 5 0x55AA;
↑ Summary Build »