blib/lib/AnyEvent/DNS.pm

filter: Statementstmt 61.1% 269 / 440 Branchbran 37.2% 64 / 172 Conditioncond 29.5% 18 / 61 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 51.0% 47 / 92 Podpod 100% 21 / 21 totaltotal 50.9% 419 / 822 CC 122 SCAR 28.9
CC
122
Cov
52%
os_config
126.4
__ANON__
56.0
_munge_ptr
30.0
CRAP 18.0
« blib/lib/AnyEvent/Log.pm ↑ Summary blib/lib/AnyEvent.pm »
1 =head1 NAME
2  
3 AnyEvent::DNS - fully asynchronous DNS resolution
4  
5 =head1 SYNOPSIS
6  
7 use AnyEvent::DNS;
8
9 my $cv = AnyEvent->condvar;
10 AnyEvent::DNS::a "www.google.de", $cv;
11 # ... later
12 my @addrs = $cv->recv;
13  
14 =head1 DESCRIPTION
15  
16 This module offers both a number of DNS convenience functions as well
17 as a fully asynchronous and high-performance pure-perl stub resolver.
18  
19 The stub resolver supports DNS over IPv4 and IPv6, UDP and TCP, optional
20 EDNS0 support for up to 4kiB datagrams and automatically falls back to
21 virtual circuit mode for large responses.
22  
23 =head2 CONVENIENCE FUNCTIONS
24  
25 =over 4
26  
27 =cut
28  
29 package AnyEvent::DNS;
30  
31 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use Socket qw(AF_INET SOCK_DGRAM SOCK_STREAM);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 5 ▶use AnyEvent (); BEGIN { AnyEvent::common_sense }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use AnyEvent::Util qw(AF_INET6);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 5 our $VERSION = $AnyEvent::VERSION;
38 5 our @DNS_FALLBACK; # some public dns servers as fallback
39  
40 {
41 5 ▶ my $prep = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
42 10 $_ = $_->[rand @$_] for @_;
43 10 push @_, splice @_, rand $_, 1 for reverse 1..@_; # shuffle
44 10 $_ = pack "H*", $_ for @_;
45 \@_
46 10 };
47  
48 5 my $ipv4 = $prep->(
49 ["08080808", "08080404"], # 8.8.8.8, 8.8.4.4 - google public dns
50 ["01010101", "01000001"], # 1.1.1.1, 1.0.0.1 - cloudflare public dns
51 ["50505050", "50505151"], # 80.80.80.80, 80.80.81.81 - freenom.world
52 ## ["d1f40003", "d1f30004"], # v209.244.0.3/4 - resolver1/2.level3.net - status unknown
53 ## ["04020201", "04020203", "04020204", "04020205", "04020206"], # v4.2.2.1/3/4/5/6 - vnsc-pri.sys.gtei.net - effectively public
54 ## ["cdd22ad2", "4044c8c8"], # 205.210.42.205, 64.68.200.200 - cache1/2.dnsresolvers.com - verified public
55 # ["8d010101"], # 141.1.1.1 - cable&wireless, now vodafone - status unknown
56 # 84.200.69.80 # dns.watch
57 # 84.200.70.40 # dns.watch
58 # 37.235.1.174 # freedns.zone
59 # 37.235.1.177 # freedns.zone
60 # 213.73.91.35 # dnscache.berlin.ccc.de
61 # 194.150.168.168 # dns.as250.net; Berlin/Frankfurt
62 # 85.214.20.141 # FoeBud (digitalcourage.de)
63 # 77.109.148.136 # privacyfoundation.ch
64 # 77.109.148.137 # privacyfoundation.ch
65 # 91.239.100.100 # anycast.censurfridns.dk
66 # 89.233.43.71 # ns1.censurfridns.dk
67 # 204.152.184.76 # f.6to4-servers.net, ISC, USA
68 );
69  
70 5 my $ipv6 = $prep->(
71 ["20014860486000000000000000008888", "20014860486000000000000000008844"], # 2001:4860:4860::8888/8844 - google ipv6
72 ["26064700470000000000000000001111", "26064700470000000000000000001001"], # 2606:4700:4700::1111/1001 - cloudflare dns
73 );
74  
75 5 ▶ undef $ipv4 unless $AnyEvent::PROTOCOL{ipv4};
Branch: 1 branch
BranchTrueFalseTotal
unless $AnyEvent::PROTOCOL{"ipv4"} 0 5 5
76 5 ▶ undef $ipv6 unless $AnyEvent::PROTOCOL{ipv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $AnyEvent::PROTOCOL{"ipv6"} 0 5 5
77  
78 ($ipv6, $ipv4) = ($ipv4, $ipv6)
79 5 ▶ if $AnyEvent::PROTOCOL{ipv6} > $AnyEvent::PROTOCOL{ipv4};
Branch: 1 branch
BranchTrueFalseTotal
if $AnyEvent::PROTOCOL{"ipv6"} > $AnyEvent::PROTOCOL{"ipv4"} 0 5 5
80  
81 5 @DNS_FALLBACK = (@$ipv4, @$ipv6);
82 }
83  
84 =item AnyEvent::DNS::a $domain, $cb->(@addrs)
85  
86 Tries to resolve the given domain to IPv4 address(es).
87  
88 =item AnyEvent::DNS::aaaa $domain, $cb->(@addrs)
89  
90 Tries to resolve the given domain to IPv6 address(es).
91  
92 =item AnyEvent::DNS::mx $domain, $cb->(@hostnames)
93  
94 Tries to resolve the given domain into a sorted (lower preference value
95 first) list of domain names.
96  
97 =item AnyEvent::DNS::ns $domain, $cb->(@hostnames)
98  
99 Tries to resolve the given domain name into a list of name servers.
100  
101 =item AnyEvent::DNS::txt $domain, $cb->(@hostnames)
102  
103 Tries to resolve the given domain name into a list of text records. Only
104 the first text string per record will be returned. If you want all
105 strings, you need to call the resolver manually:
106  
107 resolver->resolve ($domain => "txt", sub {
108 for my $record (@_) {
109 my (undef, undef, undef, @txt) = @$record;
110 # strings now in @txt
111 }
112 });
113  
114 =item AnyEvent::DNS::srv $service, $proto, $domain, $cb->(@srv_rr)
115  
116 Tries to resolve the given service, protocol and domain name into a list
117 of service records.
118  
119 Each C<$srv_rr> is an array reference with the following contents:
120 C<[$priority, $weight, $transport, $target]>.
121  
122 They will be sorted with lowest priority first, then randomly
123 distributed by weight as per RFC 2782.
124  
125 Example:
126  
127 AnyEvent::DNS::srv "sip", "udp", "schmorp.de", sub { ...
128 # @_ = ( [10, 10, 5060, "sip1.schmorp.de" ] )
129  
130 =item AnyEvent::DNS::any $domain, $cb->(@rrs)
131  
132 Tries to resolve the given domain and passes all resource records found
133 to the callback. Note that this uses a DNS C<ANY> query, which, as of RFC
134 8482, are officially deprecated.
135  
136 =item AnyEvent::DNS::ptr $domain, $cb->(@hostnames)
137  
138 Tries to make a PTR lookup on the given domain. See C<reverse_lookup>
139 and C<reverse_verify> if you want to resolve an IP address to a hostname
140 instead.
141  
142 =item AnyEvent::DNS::reverse_lookup $ipv4_or_6, $cb->(@hostnames)
143  
144 Tries to reverse-resolve the given IPv4 or IPv6 address (in textual form)
145 into its hostname(s). Handles V4MAPPED and V4COMPAT IPv6 addresses
146 transparently.
147  
148 =item AnyEvent::DNS::reverse_verify $ipv4_or_6, $cb->(@hostnames)
149  
150 The same as C<reverse_lookup>, but does forward-lookups to verify that
151 the resolved hostnames indeed point to the address, which makes spoofing
152 harder.
153  
154 If you want to resolve an address into a hostname, this is the preferred
155 method: The DNS records could still change, but at least this function
156 verified that the hostname, at one point in the past, pointed at the IP
157 address you originally resolved.
158  
159 Example:
160  
161 AnyEvent::DNS::reverse_verify "2001:500:2f::f", sub { print shift };
162 # => f.root-servers.net
163  
164 =cut
165  
166 sub MAX_PKT() { 4096 } # max packet size we advertise and accept
167  
168 sub DOMAIN_PORT() { 53 } # if this changes drop me a note
169  
170 sub resolver ();
171  
172 2 ▶sub a($$) {
Subroutine a: called CC=1 SCAR=0.0
Pod documented
173 2 my ($domain, $cb) = @_;
174  
175 2 ▶ resolver->resolve ($domain => "a", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
176 2 $cb->(map $_->[4], @_);
177 2 });
178 }
179  
180 2 ▶sub aaaa($$) {
Subroutine aaaa: called CC=1 SCAR=0.0
Pod documented
181 2 my ($domain, $cb) = @_;
182  
183 2 ▶ resolver->resolve ($domain => "aaaa", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
184 2 $cb->(map $_->[4], @_);
185 2 });
186 }
187  
188 0 ▶sub mx($$) {
Subroutine mx: not called CC=1 SCAR=6.9
Pod documented
189 0 my ($domain, $cb) = @_;
190  
191 0 ▶ resolver->resolve ($domain => "mx", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 $cb->(map $_->[5], sort { $a->[4] <=> $b->[4] } @_);
193 0 });
194 }
195  
196 0 ▶sub ns($$) {
Subroutine ns: not called CC=1 SCAR=6.9
Pod documented
197 0 my ($domain, $cb) = @_;
198  
199 0 ▶ resolver->resolve ($domain => "ns", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 0 $cb->(map $_->[4], @_);
201 0 });
202 }
203  
204 0 ▶sub txt($$) {
Subroutine txt: not called CC=1 SCAR=6.9
Pod documented
205 0 my ($domain, $cb) = @_;
206  
207 0 ▶ resolver->resolve ($domain => "txt", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 $cb->(map $_->[4], @_);
209 0 });
210 }
211  
212 0 ▶sub srv($$$$) {
Subroutine srv: not called CC=1 SCAR=6.9
Pod documented
213 0 my ($service, $proto, $domain, $cb) = @_;
214  
215 # todo, ask for any and check glue records
216 0 ▶ resolver->resolve ("_$service._$proto.$domain" => "srv", sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
217 0 my @res;
218  
219 # classify by priority
220 my %pri;
221 0 push @{ $pri{$_->[4]} }, [ @$_[4,5,6,7] ]
222 0 for @_;
223  
224 # order by priority
225 0 for my $pri (sort { $a <=> $b } keys %pri) {
226 # order by weight
227 0 my @rr = sort { $a->[1] <=> $b->[1] } @{ delete $pri{$pri} };
228  
229 0 my $sum; $sum += $_->[1] for @rr;
230  
231 0 ▶ while (@rr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rr) 0 0 0
232 0 my $w = int rand $sum + 1;
233 0 for (0 .. $#rr) {
234 0 ▶ if (($w -= $rr[$_][1]) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($w -= $rr[$_][1]) <= 0) 0 0 0
235 0 $sum -= $rr[$_][1];
236 0 push @res, splice @rr, $_, 1, ();
237 0 last;
238 }
239 }
240 }
241 }
242  
243 0 $cb->(@res);
244 0 });
245 }
246  
247 0 ▶sub ptr($$) {
Subroutine ptr: not called CC=1 SCAR=6.9
Pod documented
248 0 my ($domain, $cb) = @_;
249  
250 0 ▶ resolver->resolve ($domain => "ptr", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 $cb->(map $_->[4], @_);
252 0 });
253 }
254  
255 0 ▶sub any($$) {
Subroutine any: not called CC=1 SCAR=6.9
Pod documented
256 0 my ($domain, $cb) = @_;
257  
258 0 resolver->resolve ($domain => "*", $cb);
259 }
260  
261 # convert textual ip address into reverse lookup form
262 0 ▶sub _munge_ptr($) {
Subroutine _munge_ptr: not called CC=5 SCAR=34.0
263 0 ▶ my $ipn = $_[0]
Branch: 1 branch
BranchTrueFalseTotal
my $ipn = $_[0] or return 0 0 0
264 or return;
265  
266 0 my $ptr;
267  
268 0 my $af = AnyEvent::Socket::address_family ($ipn);
269  
270 0 ▶ if ($af == AF_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 10) 0 0 0
271 0 $ipn = substr $ipn, 0, 16; # anticipate future expansion
272  
273 # handle v4mapped and v4compat
274 0 ▶ if ($ipn =~ s/^\x00{10}(?:\xff\xff|\x00\x00)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipn =~ s/^\x00{10}(?:\xff\xff|\x00\x00)//) { } 0 0 0
275 0 $af = AF_INET;
276 } else {
277 0 $ptr = join ".", (reverse split //, unpack "H32", $ipn), "ip6.arpa.";
278 }
279 }
280  
281 0 ▶ if ($af == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 2) 0 0 0
282 0 $ptr = join ".", (reverse unpack "C4", $ipn), "in-addr.arpa.";
283 }
284  
285 $ptr
286 0 }
287  
288 0 ▶sub reverse_lookup($$) {
Subroutine reverse_lookup: not called CC=2 SCAR=17.9
Pod documented
289 0 my ($ip, $cb) = @_;
290  
291 0 ▶ $ip = _munge_ptr AnyEvent::Socket::parse_address ($ip)
Branch: 1 branch
BranchTrueFalseTotal
$ip = &_munge_ptr(scalar &AnyEvent::Socket::parse_address($ip)) or return &$cb() 0 0 0
292 or return $cb->();
293  
294 0 ▶ resolver->resolve ($ip => "ptr", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 $cb->(map $_->[4], @_);
296 0 });
297 }
298  
299 0 ▶sub reverse_verify($$) {
Subroutine reverse_verify: not called CC=3 SCAR=24.8
Pod documented
300 0 my ($ip, $cb) = @_;
301
302 0 ▶ my $ipn = AnyEvent::Socket::parse_address ($ip)
Branch: 1 branch
BranchTrueFalseTotal
my $ipn = &AnyEvent::Socket::parse_address($ip) or return &$cb() 0 0 0
303 or return $cb->();
304  
305 0 my $af = AnyEvent::Socket::address_family ($ipn);
306  
307 0 my @res;
308 my $cnt;
309  
310 0 ▶ my $ptr = _munge_ptr $ipn
Branch: 1 branch
BranchTrueFalseTotal
my $ptr = &_munge_ptr($ipn) or return &$cb() 0 0 0
311 or return $cb->();
312  
313 0 $ip = AnyEvent::Socket::format_address ($ipn); # normalise into the same form
314  
315 0 ▶ ptr $ptr, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
316 0 for my $name (@_) {
317 0 ++$cnt;
318
319 # () around AF_INET to work around bug in 5.8
320 0 ▶ resolver->resolve ("$name." => ($af == (AF_INET) ? "a" : "aaaa"), sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
321 0 for (@_) {
322 0 ▶ push @res, $name
Branch: 1 branch
BranchTrueFalseTotal
if $_->[4] eq $ip 0 0 0
323 if $_->[4] eq $ip;
324 }
325 0 ▶ $cb->(@res) unless --$cnt;
Branch: 1 branch
BranchTrueFalseTotal
unless --$cnt 0 0 0
326 0 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$af == 2 ? : 0 0 0
327 }
328  
329 0 ▶ $cb->() unless $cnt;
Branch: 1 branch
BranchTrueFalseTotal
unless $cnt 0 0 0
330 0 };
331 }
332  
333 #################################################################################
334  
335 =back
336  
337 =head2 LOW-LEVEL DNS EN-/DECODING FUNCTIONS
338  
339 =over 4
340  
341 =item $AnyEvent::DNS::EDNS0
342  
343 This variable decides whether dns_pack automatically enables EDNS0
344 support. By default, this is disabled (C<0>), unless overridden by
345 C<$ENV{PERL_ANYEVENT_EDNS0}>, but when set to C<1>, AnyEvent::DNS will use
346 EDNS0 in all requests.
347  
348 =cut
349  
350 5 our $EDNS0 = $ENV{PERL_ANYEVENT_EDNS0}*1; # set to 1 to enable (partial) edns0
351  
352 5 our %opcode_id = (
353 query => 0,
354 iquery => 1,
355 status => 2,
356 notify => 4,
357 update => 5,
358 map +($_ => $_), 3, 6..15
359 );
360  
361 5 our %opcode_str = reverse %opcode_id;
362  
363 5 our %rcode_id = (
364 noerror => 0,
365 formerr => 1,
366 servfail => 2,
367 nxdomain => 3,
368 notimp => 4,
369 refused => 5,
370 yxdomain => 6, # Name Exists when it should not [RFC 2136]
371 yxrrset => 7, # RR Set Exists when it should not [RFC 2136]
372 nxrrset => 8, # RR Set that should exist does not [RFC 2136]
373 notauth => 9, # Server Not Authoritative for zone [RFC 2136]
374 notzone => 10, # Name not contained in zone [RFC 2136]
375 # EDNS0 16 BADVERS Bad OPT Version [RFC 2671]
376 # EDNS0 16 BADSIG TSIG Signature Failure [RFC 2845]
377 # EDNS0 17 BADKEY Key not recognized [RFC 2845]
378 # EDNS0 18 BADTIME Signature out of time window [RFC 2845]
379 # EDNS0 19 BADMODE Bad TKEY Mode [RFC 2930]
380 # EDNS0 20 BADNAME Duplicate key name [RFC 2930]
381 # EDNS0 21 BADALG Algorithm not supported [RFC 2930]
382 map +($_ => $_), 11..15
383 );
384  
385 5 our %rcode_str = reverse %rcode_id;
386  
387 5 our %type_id = (
388 a => 1,
389 ns => 2,
390 md => 3,
391 mf => 4,
392 cname => 5,
393 soa => 6,
394 mb => 7,
395 mg => 8,
396 mr => 9,
397 null => 10,
398 wks => 11,
399 ptr => 12,
400 hinfo => 13,
401 minfo => 14,
402 mx => 15,
403 txt => 16,
404 sig => 24,
405 key => 25,
406 gpos => 27, # rfc1712
407 aaaa => 28,
408 loc => 29, # rfc1876
409 srv => 33,
410 naptr => 35, # rfc2915
411 dname => 39, # rfc2672
412 opt => 41,
413 ds => 43, # rfc4034
414 sshfp => 44, # rfc4255
415 rrsig => 46, # rfc4034
416 nsec => 47, # rfc4034
417 dnskey=> 48, # rfc4034
418 smimea=> 53, # rfc8162
419 cds => 59, # rfc7344
420 cdnskey=> 60, # rfc7344
421 openpgpkey=> 61, # rfc7926
422 csync => 62, # rfc7929
423 spf => 99,
424 tkey => 249,
425 tsig => 250,
426 ixfr => 251,
427 axfr => 252,
428 mailb => 253,
429 "*" => 255,
430 uri => 256,
431 caa => 257, # rfc6844
432 );
433  
434 5 our %type_str = reverse %type_id;
435  
436 5 our %class_id = (
437 in => 1,
438 ch => 3,
439 hs => 4,
440 none => 254,
441 "*" => 255,
442 );
443  
444 5 our %class_str = reverse %class_id;
445  
446 4 ▶sub _enc_name($) {
Subroutine _enc_name: called CC=1 SCAR=0.0
447 4 pack "(C/a*)*", (split /\./, shift), ""
448 }
449  
450 5 ▶if ($] < 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.008") 0 5 5
451 # special slower 5.6 version
452 0 ▶ *_enc_name = sub ($) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
453 0 join "", map +(pack "C/a*", $_), (split /\./, shift), ""
454 0 };
455 }
456  
457 4 ▶sub _enc_qd() {
Subroutine _enc_qd: called CC=4 SCAR=16.6
458 (_enc_name $_->[0]) . pack "nn",
459 ($_->[1] > 0 ? $_->[1] : $type_id {$_->[1]}),
460 4 ▶ ($_->[3] > 0 ? $_->[2] : $class_id{$_->[2] || "in"})
Branch: 2 branches
BranchTrueFalseTotal
$_->[1] > 0 ? : 0 4 4
$_->[3] > 0 ? : 0 4 4
Condition50%1/2
$_->[2] || 'in'
l!l
40
MC/DC0%0/1
$_->[2] || 'in'
$_->[2]
Truth table50%1/2
A
A= $_->[2]
Aresult
11
00
461 }
462  
463 0 ▶sub _enc_rr() {
Subroutine _enc_rr: not called CC=1 SCAR=6.9
464 0 die "encoding of resource records is not supported";
465 }
466  
467 =item $pkt = AnyEvent::DNS::dns_pack $dns
468  
469 Packs a perl data structure into a DNS packet. Reading RFC 1035 is strongly
470 recommended, then everything will be totally clear. Or maybe not.
471  
472 Resource records are not yet encodable.
473  
474 Examples:
475  
476 # very simple request, using lots of default values:
477 { rd => 1, qd => [ [ "host.domain", "a"] ] }
478
479 # more complex example, showing how flags etc. are named:
480
481 {
482 id => 10000,
483 op => "query",
484 rc => "nxdomain",
485
486 # flags
487 qr => 1,
488 aa => 0,
489 tc => 0,
490 rd => 0,
491 ra => 0,
492 ad => 0,
493 cd => 0,
494
495 qd => [@rr], # query section
496 an => [@rr], # answer section
497 ns => [@rr], # authority section
498 ar => [@rr], # additional records section
499 }
500  
501 =cut
502  
503 4 ▶sub dns_pack($) {
Subroutine dns_pack: called CC=10 SCAR=25.9
Pod documented
504 4 my ($req) = @_;
505  
506 pack "nn nnnn a* a* a* a* a*",
507 $req->{id},
508  
509 ! !$req->{qr} * 0x8000
510 + $opcode_id{$req->{op}} * 0x0800
511 + ! !$req->{aa} * 0x0400
512 + ! !$req->{tc} * 0x0200
513 + ! !$req->{rd} * 0x0100
514 + ! !$req->{ra} * 0x0080
515 + ! !$req->{ad} * 0x0020
516 + ! !$req->{cd} * 0x0010
517 + $rcode_id{$req->{rc}} * 0x0001,
518  
519 4 ▶ scalar @{ $req->{qd} || [] },
Condition50%1/2
$req->{'qd'} || []
l!l
40
MC/DC0%0/1
$req->{'qd'} || []
$req->{'qd'}
Truth table50%1/2
A
A= $req->{'qd'}
Aresult
11
00
520 4 ▶ scalar @{ $req->{an} || [] },
Condition50%1/2
$req->{'an'} || []
l!l
04
MC/DC0%0/1
$req->{'an'} || []
$req->{'an'}
Truth table50%1/2
A
A= $req->{'an'}
Aresult
11
00
521 4 ▶ scalar @{ $req->{ns} || [] },
Condition50%1/2
$req->{'ns'} || []
l!l
04
MC/DC0%0/1
$req->{'ns'} || []
$req->{'ns'}
Truth table50%1/2
A
A= $req->{'ns'}
Aresult
11
00
522 4 ▶ $EDNS0 + scalar @{ $req->{ar} || [] }, # EDNS0 option included here
Condition50%1/2
$req->{'ar'} || []
l!l
04
MC/DC0%0/1
$req->{'ar'} || []
$req->{'ar'}
Truth table50%1/2
A
A= $req->{'ar'}
Aresult
11
00
523  
524 4 ▶ (join "", map _enc_qd, @{ $req->{qd} || [] }),
Condition50%1/2
$req->{'qd'} || []
l!l
40
MC/DC0%0/1
$req->{'qd'} || []
$req->{'qd'}
Truth table50%1/2
A
A= $req->{'qd'}
Aresult
11
00
525 4 ▶ (join "", map _enc_rr, @{ $req->{an} || [] }),
Condition50%1/2
$req->{'an'} || []
l!l
04
MC/DC0%0/1
$req->{'an'} || []
$req->{'an'}
Truth table50%1/2
A
A= $req->{'an'}
Aresult
11
00
526 4 ▶ (join "", map _enc_rr, @{ $req->{ns} || [] }),
Condition50%1/2
$req->{'ns'} || []
l!l
04
MC/DC0%0/1
$req->{'ns'} || []
$req->{'ns'}
Truth table50%1/2
A
A= $req->{'ns'}
Aresult
11
00
527 4 ▶ (join "", map _enc_rr, @{ $req->{ar} || [] }),
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::DNS::EDNS0 ? : 0 4 4
Condition50%1/2
$req->{'ar'} || []
l!l
04
MC/DC0%0/1
$req->{'ar'} || []
$req->{'ar'}
Truth table50%1/2
A
A= $req->{'ar'}
Aresult
11
00
528  
529 ($EDNS0 ? pack "C nnNn", 0, 41, MAX_PKT, 0, 0 : "") # EDNS0 option
530 }
531  
532 5 our $ofs;
533 5 our $pkt;
534  
535 # bitches
536 16 ▶sub _dec_name {
Subroutine _dec_name: called CC=6 SCAR=18.0
537 16 my @res;
538 my $redir;
539 16 my $ptr = $ofs;
540 16 my $cnt;
541  
542 16 while () {
543 48 ▶ return undef if ++$cnt >= 256; # to avoid DoS attacks
Branch: 1 branch
BranchTrueFalseTotal
if ++$cnt >= 256 0 48 48
544  
545 48 my $len = ord substr $pkt, $ptr++, 1;
546  
547 48 ▶ if ($len >= 0xc0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len >= 192) { } 8 40 48
elsif ($len) { } 24 16 40
548 8 $ptr++;
549 8 ▶ $ofs = $ptr if $ptr > $ofs;
Branch: 1 branch
BranchTrueFalseTotal
if $ptr > $AnyEvent::DNS::ofs 8 0 8
550 8 $ptr = (unpack "n", substr $pkt, $ptr - 2, 2) & 0x3fff;
551 } elsif ($len) {
552 24 push @res, substr $pkt, $ptr, $len;
553 24 $ptr += $len;
554 } else {
555 16 ▶ $ofs = $ptr if $ptr > $ofs;
Branch: 1 branch
BranchTrueFalseTotal
if $ptr > $AnyEvent::DNS::ofs 8 8 16
556 16 return join ".", @res;
557 }
558 }
559 }
560  
561 4 ▶sub _dec_qd {
Subroutine _dec_qd: called CC=3 SCAR=12.7
562 4 my $qname = _dec_name;
563 4 my ($qt, $qc) = unpack "nn", substr $pkt, $ofs; $ofs += 4;
564 4 ▶ [$qname, $type_str{$qt} || $qt, $class_str{$qc} || $qc]
Condition33%2/6
$AnyEvent::DNS::type_str{$qt} || $qt
l!l&&r!l&&!r
400
$AnyEvent::DNS::class_str{$qc} || $qc
l!l&&r!l&&!r
400
MC/DC0%0/2
$AnyEvent::DNS::type_str{$qt} || $qt
$AnyEvent::DNS::type_str{$qt} $qt
MC/DC0%0/2
$AnyEvent::DNS::class_str{$qc} || $qc
$AnyEvent::DNS::class_str{$qc} $qc
Truth table33%1/3
A || B
A= $AnyEvent::DNS::type_str{$qt}B= $qt
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $AnyEvent::DNS::class_str{$qc}B= $qc
ABresult
1X1
011
000
565 }
566  
567 our %dec_rr = (
568 0 ▶ 1 => sub { join ".", unpack "C4", $_ }, # a
Subroutine __ANON__: not called CC=1 SCAR=6.9
569 0 ▶ 2 => sub { local $ofs = $ofs - length; _dec_name }, # ns
Subroutine __ANON__: not called CC=1 SCAR=6.9
570 0 ▶ 5 => sub { local $ofs = $ofs - length; _dec_name }, # cname
Subroutine __ANON__: not called CC=1 SCAR=6.9
571 4 ▶ 6 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
572 4 local $ofs = $ofs - length;
573 4 my $mname = _dec_name;
574 4 my $rname = _dec_name;
575 4 ($mname, $rname, unpack "NNNNN", substr $pkt, $ofs)
576 }, # soa
577 0 ▶ 11 => sub { ((join ".", unpack "C4", $_), unpack "C a*", substr $_, 4) }, # wks
Subroutine __ANON__: not called CC=1 SCAR=6.9
578 0 ▶ 12 => sub { local $ofs = $ofs - length; _dec_name }, # ptr
Subroutine __ANON__: not called CC=1 SCAR=6.9
579 0 ▶ 13 => sub { unpack "C/a* C/a*", $_ }, # hinfo
Subroutine __ANON__: not called CC=1 SCAR=6.9
580 0 ▶ 15 => sub { local $ofs = $ofs + 2 - length; ((unpack "n", $_), _dec_name) }, # mx
Subroutine __ANON__: not called CC=1 SCAR=6.9
581 0 ▶ 16 => sub { unpack "(C/a*)*", $_ }, # txt
Subroutine __ANON__: not called CC=1 SCAR=6.9
582 0 ▶ 28 => sub { AnyEvent::Socket::format_ipv6 ($_) }, # aaaa
Subroutine __ANON__: not called CC=1 SCAR=6.9
583 0 ▶ 33 => sub { local $ofs = $ofs + 6 - length; ((unpack "nnn", $_), _dec_name) }, # srv
Subroutine __ANON__: not called CC=1 SCAR=6.9
584 0 ▶ 35 => sub { # naptr
Subroutine __ANON__: not called CC=1 SCAR=6.9
585 # requires perl 5.10, sorry
586 0 my ($order, $preference, $flags, $service, $regexp, $offset) = unpack "nn C/a* C/a* C/a* .", $_;
587 0 local $ofs = $ofs + $offset - length;
588 0 ($order, $preference, $flags, $service, $regexp, _dec_name)
589 },
590 0 ▶ 39 => sub { local $ofs = $ofs - length; _dec_name }, # dname
Subroutine __ANON__: not called CC=1 SCAR=6.9
591 0 ▶ 99 => sub { unpack "(C/a*)*", $_ }, # spf
Subroutine __ANON__: not called CC=1 SCAR=6.9
592 0 ▶ 257 => sub { unpack "CC/a*a*", $_ }, # caa
Subroutine __ANON__: not called CC=1 SCAR=6.9
593 5 );
594  
595 4 ▶sub _dec_rr {
Subroutine _dec_rr: called CC=4 SCAR=16.8
596 4 my $name = _dec_name;
597  
598 4 my ($rt, $rc, $ttl, $rdlen) = unpack "nn N n", substr $pkt, $ofs; $ofs += 10;
599 4 local $_ = substr $pkt, $ofs, $rdlen; $ofs += $rdlen;
600  
601 [
602 $name,
603 $type_str{$rt} || $rt,
604 $class_str{$rc} || $rc,
605 $ttl,
606 0 ▶ ($dec_rr{$rt} || sub { $_ })->(),
Subroutine __ANON__: not called CC=1 SCAR=2.3
Condition33%3/9
$AnyEvent::DNS::type_str{$rt} || $rt
l!l&&r!l&&!r
400
$AnyEvent::DNS::class_str{$rc} || $rc
l!l&&r!l&&!r
400
$AnyEvent::DNS::dec_rr{$rt} || sub { $_; }
l!l&&r!l&&!r
400
MC/DC0%0/2
$AnyEvent::DNS::type_str{$rt} || $rt
$AnyEvent::DNS::type_str{$rt} $rt
MC/DC0%0/2
$AnyEvent::DNS::class_str{$rc} || $rc
$AnyEvent::DNS::class_str{$rc} $rc
MC/DC0%0/2
$AnyEvent::DNS::dec_rr{$rt} || sub { $_; }
$AnyEvent::DNS::dec_rr{$rt} sub { $_; }
Truth table33%1/3
A || B
A= $AnyEvent::DNS::type_str{$rt}B= $rt
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $AnyEvent::DNS::class_str{$rc}B= $rc
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $AnyEvent::DNS::dec_rr{$rt}B= sub { $_; }
ABresult
1X1
011
000
607 ]
608 }
609  
610 =item $dns = AnyEvent::DNS::dns_unpack $pkt
611  
612 Unpacks a DNS packet into a perl data structure.
613  
614 Examples:
615  
616 # an unsuccessful reply
617 {
618 'qd' => [
619 [ 'ruth.plan9.de.mach.uni-karlsruhe.de', '*', 'in' ]
620 ],
621 'rc' => 'nxdomain',
622 'ar' => [],
623 'ns' => [
624 [
625 'uni-karlsruhe.de',
626 'soa',
627 'in',
628 600,
629 'netserv.rz.uni-karlsruhe.de',
630 'hostmaster.rz.uni-karlsruhe.de',
631 2008052201, 10800, 1800, 2592000, 86400
632 ]
633 ],
634 'tc' => '',
635 'ra' => 1,
636 'qr' => 1,
637 'id' => 45915,
638 'aa' => '',
639 'an' => [],
640 'rd' => 1,
641 'op' => 'query',
642 '__' => '<original dns packet>',
643 }
644
645 # a successful reply
646
647 {
648 'qd' => [ [ 'www.google.de', 'a', 'in' ] ],
649 'rc' => 0,
650 'ar' => [
651 [ 'a.l.google.com', 'a', 'in', 3600, '209.85.139.9' ],
652 [ 'b.l.google.com', 'a', 'in', 3600, '64.233.179.9' ],
653 [ 'c.l.google.com', 'a', 'in', 3600, '64.233.161.9' ],
654 ],
655 'ns' => [
656 [ 'l.google.com', 'ns', 'in', 3600, 'a.l.google.com' ],
657 [ 'l.google.com', 'ns', 'in', 3600, 'b.l.google.com' ],
658 ],
659 'tc' => '',
660 'ra' => 1,
661 'qr' => 1,
662 'id' => 64265,
663 'aa' => '',
664 'an' => [
665 [ 'www.google.de', 'cname', 'in', 3600, 'www.google.com' ],
666 [ 'www.google.com', 'cname', 'in', 3600, 'www.l.google.com' ],
667 [ 'www.l.google.com', 'a', 'in', 3600, '66.249.93.104' ],
668 [ 'www.l.google.com', 'a', 'in', 3600, '66.249.93.147' ],
669 ],
670 'rd' => 1,
671 'op' => 0,
672 '__' => '<original dns packet>',
673 }
674  
675 =cut
676  
677 4 ▶sub dns_unpack($) {
Subroutine dns_unpack: called CC=1 SCAR=0.0
Pod documented
678 4 local $pkt = shift;
679 4 my ($id, $flags, $qd, $an, $ns, $ar)
680 = unpack "nn nnnn A*", $pkt;
681  
682 4 local $ofs = 6 * 2;
683  
684 {
685 __ => $pkt,
686 id => $id,
687 qr => ! ! ($flags & 0x8000),
688 aa => ! ! ($flags & 0x0400),
689 tc => ! ! ($flags & 0x0200),
690 rd => ! ! ($flags & 0x0100),
691 ra => ! ! ($flags & 0x0080),
692 ad => ! ! ($flags & 0x0020),
693 cd => ! ! ($flags & 0x0010),
694 op => $opcode_str{($flags & 0x001e) >> 11},
695 4 rc => $rcode_str{($flags & 0x000f)},
696  
697 qd => [map _dec_qd, 1 .. $qd],
698 an => [map _dec_rr, 1 .. $an],
699 ns => [map _dec_rr, 1 .. $ns],
700 ar => [map _dec_rr, 1 .. $ar],
701 }
702 }
703  
704 #############################################################################
705  
706 =back
707  
708 =head3 Extending DNS Encoder and Decoder
709  
710 This section describes an I<experimental> method to extend the DNS encoder
711 and decoder with new opcode, rcode, class and type strings, as well as
712 resource record decoders.
713  
714 Since this is experimental, it can change, as anything can change, but
715 this interface is expe ctedc to be relatively stable and was stable during
716 the whole existance of C<AnyEvent::DNS> so far.
717  
718 Note that, since changing the decoder or encoder might break existing
719 code, you should either be sure to control for this, or only temporarily
720 change these values, e.g. like so:
721  
722 my $decoded = do {
723 local $AnyEvent::DNS::opcode_str{7} = "yxrrset";
724 AnyEvent::DNS::dns_unpack $mypkt
725 };
726  
727 =over 4
728  
729 =item %AnyEvent::DNS::opcode_id, %AnyEvent::DNS::opcode_str
730  
731 Two hashes that map lowercase opcode strings to numerical id's (For the
732 encoder), or vice versa (for the decoder). Example: add a new opcode
733 string C<notzone>.
734  
735 $AnyEvent::DNS::opcode_id{notzone} = 10;
736 $AnyEvent::DNS::opcode_str{10} = 'notzone';
737  
738 =item %AnyEvent::DNS::rcode_id, %AnyEvent::DNS::rcode_str
739  
740 Same as above, for for rcode values.
741  
742 =item %AnyEvent::DNS::class_id, %AnyEvent::DNS::class_str
743  
744 Same as above, but for resource record class names/values.
745  
746 =item %AnyEvent::DNS::type_id, %AnyEvent::DNS::type_str
747  
748 Same as above, but for resource record type names/values.
749  
750 =item %AnyEvent::DNS::dec_rr
751  
752 This hash maps resource record type values to code references. When
753 decoding, they are called with C<$_> set to the undecoded data portion and
754 C<$ofs> being the current byte offset. of the record. You should have a
755 look at the existing implementations to understand how it works in detail,
756 but here are two examples:
757  
758 Decode an A record. A records are simply four bytes with one byte per
759 address component, so the decoder simply unpacks them and joins them with
760 dots in between:
761  
762 $AnyEvent::DNS::dec_rr{1} = sub { join ".", unpack "C4", $_ };
763  
764 Decode a CNAME record, which contains a potentially compressed domain
765 name.
766  
767 package AnyEvent::DNS; # for %dec_rr, $ofsd and &_dec_name
768 $dec_rr{5} = sub { local $ofs = $ofs - length; _dec_name };
769  
770 =back
771  
772 =head2 THE AnyEvent::DNS RESOLVER CLASS
773  
774 This is the class which does the actual protocol work.
775  
776 =over 4
777  
778 =cut
779  
780 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
781 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
782 5 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
783  
784 5 our $NOW;
785  
786 =item AnyEvent::DNS::resolver
787  
788 This function creates and returns a resolver that is ready to use and
789 should mimic the default resolver for your system as good as possible. It
790 is used by AnyEvent itself as well.
791  
792 It only ever creates one resolver and returns this one on subsequent calls
793 - see C<$AnyEvent::DNS::RESOLVER>, below, for details.
794  
795 Unless you have special needs, prefer this function over creating your own
796 resolver object.
797  
798 The resolver is created with the following parameters:
799  
800 untaint enabled
801 max_outstanding $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS} (default 10)
802  
803 C<os_config> will be used for OS-specific configuration, unless
804 C<$ENV{PERL_ANYEVENT_RESOLV_CONF}> is specified, in which case that file
805 gets parsed.
806  
807 =item $AnyEvent::DNS::RESOLVER
808  
809 This variable stores the default resolver returned by
810 C<AnyEvent::DNS::resolver>, or C<undef> when the default resolver hasn't
811 been instantiated yet.
812  
813 One can provide a custom resolver (e.g. one with caching functionality)
814 by storing it in this variable, causing all subsequent resolves done via
815 C<AnyEvent::DNS::resolver> to be done via the custom one.
816  
817 =cut
818  
819 5 our $RESOLVER;
820  
821 4 ▶sub resolver() {
Subroutine resolver: called CC=4 SCAR=14.2
Pod documented
822 4 ▶ $RESOLVER || do {
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::DNS::RESOLVER or do { $AnyEvent::DNS::RESOLVER = "AnyEvent::DNS"->new("untaint", 1, "max_outstanding", $ENV{"PERL_ANYEVENT_MAX_OUTSTANDING_DNS"} * 1 || 10); $ENV{"PERL_ANYEVENT_RESOLV_CONF"} ? $AnyEvent::DNS::RESOLVER->_load_resolv_conf_file($ENV{"PERL_ANYEVENT_RESOLV_CONF"}) : $AnyEvent::DNS::RESOLVER->os_config; $AnyEvent::DNS::RESOLVER } 3 1 4
823 $RESOLVER = new AnyEvent::DNS
824 untaint => 1,
825 1 ▶ max_outstanding => $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS}*1 || 10,
Condition50%1/2
$ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'} * 1 || 10
l!l
01
MC/DC0%0/1
$ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'} * 1 || 10
$ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'} * 1
Truth table50%1/2
A
A= $ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'} * 1
Aresult
11
00
826 ;
827  
828 $ENV{PERL_ANYEVENT_RESOLV_CONF}
829 ? $RESOLVER->_load_resolv_conf_file ($ENV{PERL_ANYEVENT_RESOLV_CONF})
830 1 ▶ : $RESOLVER->os_config;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PERL_ANYEVENT_RESOLV_CONF'} ? : 0 1 1
831  
832 1 $RESOLVER
833 }
834 }
835  
836 =item $resolver = new AnyEvent::DNS key => value...
837  
838 Creates and returns a new resolver.
839  
840 The following options are supported:
841  
842 =over 4
843  
844 =item server => [...]
845  
846 A list of server addresses (default: C<v127.0.0.1> or C<::1>) in network
847 format (i.e. as returned by C<AnyEvent::Socket::parse_address> - both IPv4
848 and IPv6 are supported).
849  
850 =item timeout => [...]
851  
852 A list of timeouts to use (also determines the number of retries). To make
853 three retries with individual time-outs of 2, 5 and 5 seconds, use C<[2,
854 5, 5]>, which is also the default.
855  
856 =item search => [...]
857  
858 The default search list of suffixes to append to a domain name (default: none).
859  
860 =item ndots => $integer
861  
862 The number of dots (default: C<1>) that a name must have so that the resolver
863 tries to resolve the name without any suffixes first.
864  
865 =item max_outstanding => $integer
866  
867 Most name servers do not handle many parallel requests very well. This
868 option limits the number of outstanding requests to C<$integer>
869 (default: C<10>), that means if you request more than this many requests,
870 then the additional requests will be queued until some other requests have
871 been resolved.
872  
873 =item reuse => $seconds
874  
875 The number of seconds (default: C<300>) that a query id cannot be re-used
876 after a timeout. If there was no time-out then query ids can be reused
877 immediately.
878  
879 =item untaint => $boolean
880  
881 When true, then the resolver will automatically untaint results, and might
882 also ignore certain environment variables.
883  
884 =back
885  
886 =cut
887  
888 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
889 1 my ($class, %arg) = @_;
890  
891 1 my $self = bless {
892 server => [],
893 timeout => [2, 5, 5],
894 search => [],
895 ndots => 1,
896 max_outstanding => 10,
897 reuse => 300,
898 %arg,
899 inhibit => 0,
900 reuse_q => [],
901 }, $class;
902  
903 # search should default to gethostname's domain
904 # but perl lacks a good posix module
905  
906 # try to create an ipv4 and an ipv6 socket
907 # only fail when we cannot create either
908 1 my $got_socket;
909  
910 1 Scalar::Util::weaken (my $wself = $self);
911  
912 1 ▶ if (socket my $fh4, AF_INET , Socket::SOCK_DGRAM(), 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (socket my $fh4, 2, 2, 0) 1 0 1
913 1 ++$got_socket;
914  
915 1 AnyEvent::fh_unblock $fh4;
916 1 $self->{fh4} = $fh4;
917 4 ▶ $self->{rw4} = AE::io $fh4, 0, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
918 4 ▶ if (my $peer = recv $fh4, my $pkt, MAX_PKT, 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $peer = recv($fh4, my $pkt, 4096, 0)) 4 0 4
919 4 $wself->_recv ($pkt, $peer);
920 }
921 1 };
922 }
923  
924 1 ▶ if (AF_INET6 && socket my $fh6, AF_INET6, Socket::SOCK_DGRAM(), 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (socket my $fh6, 10, 2, 0) 1 0 1
925 1 ++$got_socket;
926  
927 1 $self->{fh6} = $fh6;
928 1 AnyEvent::fh_unblock $fh6;
929 0 ▶ $self->{rw6} = AE::io $fh6, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
930 0 ▶ if (my $peer = recv $fh6, my $pkt, MAX_PKT, 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $peer = recv($fh6, my $pkt, 4096, 0)) 0 0 0
931 0 $wself->_recv ($pkt, $peer);
932 }
933 1 };
934 }
935  
936 $got_socket
937 1 ▶ or Carp::croak "unable to create either an IPv4 or an IPv6 socket";
Branch: 1 branch
BranchTrueFalseTotal
$got_socket or Carp::croak("unable to create either an IPv4 or an IPv6 socket") 1 0 1
938  
939 1 $self->_compile;
940  
941 1 $self
942 }
943  
944 # called to start asynchronous configuration
945 2 ▶sub _config_begin {
Subroutine _config_begin: called CC=1 SCAR=0.0
946 2 ++$_[0]{inhibit};
947 }
948  
949 # called when done with async config
950 2 ▶sub _config_done {
Subroutine _config_done: called CC=1 SCAR=0.0
951 2 --$_[0]{inhibit};
952 2 $_[0]->_compile;
953 2 $_[0]->_scheduler;
954 }
955  
956 =item $resolver->parse_resolv_conf ($string)
957  
958 Parses the given string as if it were a F<resolv.conf> file. The following
959 directives are supported (but not necessarily implemented).
960  
961 C<#>- and C<;>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>,
962 C<options> (C<timeout>, C<attempts>, C<ndots>).
963  
964 Everything else is silently ignored.
965  
966 =cut
967  
968 1 ▶sub parse_resolv_conf {
Subroutine parse_resolv_conf: called CC=13 SCAR=33.1
Pod documented
969 1 my ($self, $resolvconf) = @_;
970  
971 1 $self->{server} = [];
972 1 $self->{search} = [];
973  
974 1 my $attempts;
975  
976 1 for (split /\n/, $resolvconf) {
977 10 s/\s*[;#].*$//; # not quite legal, but many people insist
978  
979 10 ▶ if (/^\s*nameserver\s+(\S+)\s*$/i) {
Branch: 5 branches
BranchTrueFalseTotal
if (/^\s*nameserver\s+(\S+)\s*$/i) { } 2 8 10
elsif (/^\s*domain\s+(\S*)\s*$/i) { } 0 8 8
elsif (/^\s*search\s+(.*?)\s*$/i) { } 1 7 8
elsif (/^\s*sortlist\s+(.*?)\s*$/i) { } 0 7 7
elsif (/^\s*options\s+(.*?)\s*$/i) { } 0 7 7
980 2 my $ip = $1;
981 2 ▶ if (my $ipn = AnyEvent::Socket::parse_address ($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ipn = &AnyEvent::Socket::parse_address($ip)) { } 2 0 2
982 2 push @{ $self->{server} }, $ipn;
983 } else {
984 0 AE::log 5 => "nameserver $ip invalid and ignored, while parsing resolver config.";
985 }
986 } elsif (/^\s*domain\s+(\S*)\s*$/i) {
987 0 $self->{search} = [$1];
988 } elsif (/^\s*search\s+(.*?)\s*$/i) {
989 1 $self->{search} = [split /\s+/, $1];
990 } elsif (/^\s*sortlist\s+(.*?)\s*$/i) {
991 # ignored, NYI
992 } elsif (/^\s*options\s+(.*?)\s*$/i) {
993 0 for (split /\s+/, $1) {
994 0 ▶ if (/^timeout:(\d+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^timeout:(\d+)$/) { } 0 0 0
elsif (/^attempts:(\d+)$/) { } 0 0 0
elsif (/^ndots:(\d+)$/) { } 0 0 0
995 0 $self->{timeout} = [$1];
996 } elsif (/^attempts:(\d+)$/) {
997 0 $attempts = $1;
998 } elsif (/^ndots:(\d+)$/) {
999 0 $self->{ndots} = $1;
1000 } else {
1001 # debug, rotate, no-check-names, inet6
1002 }
1003 }
1004 } else {
1005 # silently skip stuff we don't understand
1006 }
1007 }
1008  
1009 1 ▶ $self->{timeout} = [($self->{timeout}[0]) x $attempts]
Branch: 1 branch
BranchTrueFalseTotal
if $attempts 0 1 1
1010 if $attempts;
1011  
1012 1 $self->_compile;
1013 }
1014  
1015 1 ▶sub _load_resolv_conf_file {
Subroutine _load_resolv_conf_file: called CC=1 SCAR=0.1
1016 1 my ($self, $resolv_conf) = @_;
1017  
1018 1 $self->_config_begin;
1019  
1020 1 require AnyEvent::IO;
1021 1 ▶ AnyEvent::IO::aio_load ($resolv_conf, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
1022 1 ▶ if (my ($contents) = @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($contents) = ) { } 1 0 1
1023 1 $self->parse_resolv_conf ($contents);
1024 } else {
1025 0 AE::log 4 => "$resolv_conf: $!";
1026 }
1027  
1028 1 $self->_config_done;
1029 1 });
1030 }
1031  
1032 =item $resolver->os_config
1033  
1034 Tries so load and parse F</etc/resolv.conf> on portable operating
1035 systems. Tries various egregious hacks on windows to force the DNS servers
1036 and searchlist out of the system.
1037  
1038 This method must be called at most once before trying to resolve anything.
1039  
1040 =cut
1041  
1042 1 ▶sub os_config {
Subroutine os_config: called CC=14 SCAR=48.4
Pod documented
1043 1 my ($self) = @_;
1044  
1045 1 $self->_config_begin;
1046  
1047 1 $self->{server} = [];
1048 1 $self->{search} = [];
1049  
1050 1 ▶ if ((AnyEvent::WIN32 || $^O =~ /cygwin/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /cygwin/i) { } 0 1 1
1051 # TODO: this blocks the program, but should not, but I
1052 # am too lazy to implement and test it. need to boot windows. ugh.
1053  
1054 #no strict 'refs';
1055  
1056 # there are many options to find the current nameservers etc. on windows
1057 # all of them don't work consistently:
1058 # - the registry thing needs separate code on win32 native vs. cygwin
1059 # - the registry layout differs between windows versions
1060 # - calling windows api functions doesn't work on cygwin
1061 # - ipconfig uses locale-specific messages
1062  
1063 # we use Net::DNS::Resolver first, and if it fails, will fall back to
1064 # ipconfig parsing.
1065 0 ▶ unless (eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Net::DNS::Resolver; my $r = "Net::DNS::Resolver"->new; $r->nameservers or die; foreach my $s ($r->nameservers) { if (my $ipn = &AnyEvent::Socket::parse_address($s)) { push @{$$self{"server"};}, $ipn; } ; } ; $self->{"search"} = [$r->searchlist]; 1 } }) 0 0 0
1066 # Net::DNS::Resolver uses a LOT of ram (~10mb), but what can we do :/
1067 # (this seems mostly to be due to Win32::API).
1068 0 require Net::DNS::Resolver;
1069 0 my $r = Net::DNS::Resolver->new;
1070  
1071 0 ▶ $r->nameservers
Branch: 1 branch
BranchTrueFalseTotal
$r->nameservers or die 0 0 0
1072 or die;
1073  
1074 0 for my $s ($r->nameservers) {
1075 0 ▶ if (my $ipn = AnyEvent::Socket::parse_address ($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ipn = &AnyEvent::Socket::parse_address($s)) 0 0 0
1076 0 push @{ $self->{server} }, $ipn;
1077 }
1078 }
1079 0 $self->{search} = [$r->searchlist];
1080  
1081 0 1
1082 }) {
1083 # we use ipconfig parsing because, despite all its brokenness,
1084 # it seems quite stable in practise.
1085 # unfortunately it wants a console window.
1086 # for good measure, we append a fallback nameserver to our list.
1087  
1088 0 ▶ if (open my $fh, "ipconfig /all |") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "ipconfig /all |") 0 0 0
1089 # parsing strategy: we go through the output and look for
1090 # :-lines with DNS in them. everything in those is regarded as
1091 # either a nameserver (if it parses as an ip address), or a suffix
1092 # (all else).
1093  
1094 0 my $dns;
1095 0 local $_;
1096 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
1097 0 ▶ if (s/^\s.*\bdns\b.*://i) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^\s.*\bdns\b.*://i) { } 0 0 0
elsif (/^\S/ or /^\s[^:]{16,}: /) { } 0 0 0
1098 0 $dns = 1;
1099 ▶ } elsif (/^\S/ || /^\s[^:]{16,}: /) {
Condition0%0/3
/^\S/ or /^\s[^:]{16,}: /
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\S/ or /^\s[^:]{16,}: /
/^\S/ /^\s[^:]{16,}: /
Truth table0%0/3
A or B
A= /^\S/B= /^\s[^:]{16,}: /
ABresult
1X1
011
000
1100 0 $dns = 0;
1101 }
1102 0 ▶ if ($dns && /^\s*(\S+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dns and /^\s*(\S+)\s*$/) 0 0 0
Condition0%0/3
$dns and /^\s*(\S+)\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
$dns and /^\s*(\S+)\s*$/
$dns /^\s*(\S+)\s*$/
Truth table0%0/3
A and B
A= $dnsB= /^\s*(\S+)\s*$/
ABresult
0X0
100
111
1103 0 my $s = $1;
1104 0 $s =~ s/%\d+(?!\S)//; # get rid of ipv6 scope id
1105 0 ▶ if (my $ipn = AnyEvent::Socket::parse_address ($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ipn = &AnyEvent::Socket::parse_address($s)) { } 0 0 0
1106 0 push @{ $self->{server} }, $ipn;
1107 } else {
1108 0 push @{ $self->{search} }, $s;
1109 }
1110 }
1111 }
1112 }
1113 }
1114  
1115 # always add the fallback servers on windows
1116 0 push @{ $self->{server} }, @DNS_FALLBACK;
1117  
1118 0 $self->_config_done;
1119 } else {
1120 # try /etc/resolv.conf everywhere else
1121  
1122 1 require AnyEvent::IO;
1123 1 ▶ AnyEvent::IO::aio_stat ("/etc/resolv.conf", sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1124 1 ▶ $self->_load_resolv_conf_file ("/etc/resolv.conf")
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
1125 if @_;
1126 1 $self->_config_done;
1127 1 });
1128 }
1129 }
1130  
1131 =item $resolver->timeout ($timeout, ...)
1132  
1133 Sets the timeout values. See the C<timeout> constructor argument (and
1134 note that this method expects the timeout values themselves, not an
1135 array-reference).
1136  
1137 =cut
1138  
1139 0 ▶sub timeout {
Subroutine timeout: not called CC=1 SCAR=6.9
Pod documented
1140 0 my ($self, @timeout) = @_;
1141  
1142 0 $self->{timeout} = \@timeout;
1143 0 $self->_compile;
1144 }
1145  
1146 =item $resolver->max_outstanding ($nrequests)
1147  
1148 Sets the maximum number of outstanding requests to C<$nrequests>. See the
1149 C<max_outstanding> constructor argument.
1150  
1151 =cut
1152  
1153 0 ▶sub max_outstanding {
Subroutine max_outstanding: not called CC=1 SCAR=6.9
Pod documented
1154 0 my ($self, $max) = @_;
1155  
1156 0 $self->{max_outstanding} = $max;
1157 0 $self->_compile;
1158 }
1159  
1160 4 ▶sub _compile {
Subroutine _compile: called CC=5 SCAR=16.1
1161 4 my $self = shift;
1162  
1163 4 my %search; $self->{search} = [grep 0 < length, grep !$search{$_}++, @{ $self->{search} }];
1164 4 my %server; $self->{server} = [grep 0 < length, grep !$server{$_}++, @{ $self->{server} }];
1165  
1166 4 ▶ unless (@{ $self->{server} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"server"};}) 1 3 4
1167 # use 127.0.0.1/::1 by default, add public nameservers as fallback
1168 my $default = $AnyEvent::PROTOCOL{ipv6} > $AnyEvent::PROTOCOL{ipv4}
1169 1 ▶ ? "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1" : "\x7f\x00\x00\x01";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PROTOCOL{'ipv6'} > $AnyEvent::PROTOCOL{'ipv4'} ? : 0 1 1
1170 1 $self->{server} = [$default, @DNS_FALLBACK];
1171 }
1172  
1173 4 my @retry;
1174  
1175 4 for my $timeout (@{ $self->{timeout} }) {
1176 12 for my $server (@{ $self->{server} }) {
1177 36 push @retry, [$server, $timeout];
1178 }
1179 }
1180  
1181 4 $self->{retry} = \@retry;
1182 }
1183  
1184 4 ▶sub _feed {
Subroutine _feed: called CC=3 SCAR=11.2
1185 4 my ($self, $res) = @_;
1186  
1187 ($res) = $res =~ /^(.*)$/s
1188 4 if AnyEvent::TAINT && $self->{untaint};
1189  
1190 4 ▶ $res = dns_unpack $res
Branch: 1 branch
BranchTrueFalseTotal
$res = &dns_unpack($res) or return 4 0 4
1191 or return;
1192  
1193 4 my $id = $self->{id}{$res->{id}};
1194  
1195 4 ▶ return unless ref $id;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $id 0 4 4
1196  
1197 4 $NOW = time;
1198 4 $id->[1]->($res);
1199 }
1200  
1201 4 ▶sub _recv {
Subroutine _recv: called CC=3 SCAR=11.8
1202 4 my ($self, $pkt, $peer) = @_;
1203  
1204 # we ignore errors (often one gets port unreachable, but there is
1205 # no good way to take advantage of that.
1206  
1207 4 my ($port, $host) = AnyEvent::Socket::unpack_sockaddr ($peer);
1208  
1209 4 ▶ return unless $port == DOMAIN_PORT && grep $_ eq $host, @{ $self->{server} };
Branch: 1 branch
BranchTrueFalseTotal
unless $port == 53 and grep(($_ eq $host), @{$$self{"server"};}) 0 4 4
Condition33%1/3
$port == 53 and grep(($_ eq $host), @{$$self{"server"};})
!ll&&!rl&&r
004
MC/DC0%0/2
$port == 53 and grep(($_ eq $host), @{$$self{"server"};})
$port == 53 grep(($_ eq $host), @{$$self{"server"};})
Truth table33%1/3
A and B
A= $port == 53B= grep(($_ eq $host), @{$$self{"server"};})
ABresult
0X0
100
111
1210  
1211 4 $self->_feed ($pkt);
1212 }
1213  
1214 4 ▶sub _free_id {
Subroutine _free_id: called CC=2 SCAR=8.1
1215 4 my ($self, $id, $timeout) = @_;
1216  
1217 4 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) { } 0 4 4
1218 # we need to block the id for a while
1219 0 $self->{id}{$id} = 1;
1220 0 push @{ $self->{reuse_q} }, [$NOW + $self->{reuse}, $id];
1221 } else {
1222 # we can quickly recycle the id
1223 4 delete $self->{id}{$id};
1224 }
1225  
1226 4 --$self->{outstanding};
1227 4 $self->_scheduler;
1228 }
1229  
1230 # execute a single request, involves sending it with timeouts to multiple servers
1231 4 ▶sub _exec {
Subroutine _exec: called CC=1 SCAR=2.2
1232 4 my ($self, $req) = @_;
1233  
1234 4 my $retry; # of retries
1235 my $do_retry;
1236  
1237 4 ▶ $do_retry = sub {
Subroutine __ANON__: called CC=4 SCAR=22.0
1238 my $retry_cfg = $self->{retry}[$retry++]
1239 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $retry_cfg = $self->{"retry"}[$retry++] or do { $self->_free_id($req->[2], $retry > 1); undef $do_retry; return $req->[1]->() } 4 0 4
1240 # failure
1241 0 $self->_free_id ($req->[2], $retry > 1);
1242 0 undef $do_retry; return $req->[1]->();
1243 };
1244  
1245 4 my ($server, $timeout) = @$retry_cfg;
1246
1247 0 ▶ $self->{id}{$req->[2]} = [(AE::timer $timeout, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1248 0 $NOW = time;
1249  
1250 # timeout, try next
1251 0 ▶ &$do_retry if $do_retry;
Branch: 1 branch
BranchTrueFalseTotal
if $do_retry 0 0 0
1252 4 ▶ }), sub {
Subroutine __ANON__: called CC=2 SCAR=13.7
1253 4 my ($res) = @_;
1254  
1255 4 ▶ if ($res->{tc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'tc'}) { } 0 4 4
1256 # success, but truncated, so use tcp
1257 0 ▶ AnyEvent::Socket::tcp_connect (AnyEvent::Socket::format_address ($server), DOMAIN_PORT, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1258 0 ▶ return unless $do_retry; # some other request could have invalidated us already
Branch: 1 branch
BranchTrueFalseTotal
unless $do_retry 0 0 0
1259  
1260 0 ▶ my ($fh) = @_
Branch: 1 branch
BranchTrueFalseTotal
my($fh) = or return &$do_retry 0 0 0
1261 or return &$do_retry;
1262  
1263 0 require AnyEvent::Handle;
1264  
1265 0 my $handle; $handle = new AnyEvent::Handle
1266 fh => $fh,
1267 timeout => $timeout,
1268 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1269 0 undef $handle;
1270 0 ▶ return unless $do_retry; # some other request could have invalidated us already
Branch: 1 branch
BranchTrueFalseTotal
unless $do_retry 0 0 0
1271 # failure, try next
1272 0 &$do_retry;
1273 0 };
1274  
1275 0 $handle->push_write (pack "n/a*", $req->[0]);
1276 0 ▶ $handle->push_read (chunk => 2, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1277 0 ▶ $handle->unshift_read (chunk => (unpack "n", $_[1]), sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1278 0 undef $handle;
1279 0 $self->_feed ($_[1]);
1280 0 });
1281 0 });
1282  
1283 0 ▶ }, sub { $timeout });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1284  
1285 } else {
1286 # success
1287 4 $self->_free_id ($req->[2], $retry > 1);
1288 4 undef $do_retry; return $req->[1]->($res);
1289 }
1290 4 }];
1291
1292 4 my $sa = AnyEvent::Socket::pack_sockaddr (DOMAIN_PORT, $server);
1293  
1294 my $fh = AF_INET == AnyEvent::Socket::sockaddr_family ($sa)
1295 ? $self->{fh4} : $self->{fh6}
1296 4 ▶ or return &$do_retry;
Branch: 2 branches
BranchTrueFalseTotal
my $fh = 2 == AnyEvent::Socket::sockaddr_family($sa) ? $self->{"fh4"} : $self->{"fh6"} or return &$do_retry 4 0 4
2 == AnyEvent::Socket::sockaddr_family($sa) ? : 4 0 4
1297  
1298 4 send $fh, $req->[0], 0, $sa;
1299 4 };
1300  
1301 4 &$do_retry;
1302 }
1303  
1304 14 ▶sub _scheduler {
Subroutine _scheduler: called CC=10 SCAR=25.7
1305 14 my ($self) = @_;
1306  
1307 14 ▶ return if $self->{inhibit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inhibit"} 1 13 14
1308  
1309 #no strict 'refs';
1310  
1311 13 $NOW = time;
1312  
1313 # first clear id reuse queue
1314 0 delete $self->{id}{ (shift @{ $self->{reuse_q} })->[1] }
1315 13 ▶ while @{ $self->{reuse_q} } && $self->{reuse_q}[0][0] <= $NOW;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"reuse_q"};} and $self->{"reuse_q"}[0][0] <= $AnyEvent::DNS::NOW and do { delete $self->{"id"}{shift(@{$$self{"reuse_q"};})->[1]} } 0 13 13
Condition33%1/3
@{$self->{"reuse_q"};} and $self->{"reuse_q"}[0][0] <= $AnyEvent::DNS::NOW
!ll&&!rl&&r
1300
MC/DC0%0/2
@{$self->{"reuse_q"};} and $self->{"reuse_q"}[0][0] <= $AnyEvent::DNS::NOW
@{$self->{"reuse_q"};} $self->{"reuse_q"}[0][0] <= $AnyEvent::DNS::NOW
Truth table33%1/3
A and B
A= @{$self->{"reuse_q"};}B= $self->{"reuse_q"}[0][0] <= $AnyEvent::DNS::NOW
ABresult
0X0
100
111
1316  
1317 13 ▶ while ($self->{outstanding} < $self->{max_outstanding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"outstanding"} < $self->{"max_outstanding"}) 21 0 21
1318  
1319 21 ▶ if (@{ $self->{reuse_q} } >= 30000) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"reuse_q"};} >= 30000) 0 21 21
1320 # we ran out of ID's, wait a bit
1321 0 ▶ $self->{reuse_to} ||= AE::timer $self->{reuse_q}[0][0] - $NOW, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1322 0 delete $self->{reuse_to};
1323 0 $self->_scheduler;
1324 0 ▶ };
Condition0%0/3
$self->{"reuse_to"} ||= &AE::timer($self->{"reuse_q"}[0][0] - $AnyEvent::DNS::NOW, 0, sub { delete $self->{"reuse_to"}; $self->_scheduler; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"reuse_to"} ||= &AE::timer($self->{"reuse_q"}[0][0] - $AnyEvent::DNS::NOW, 0, sub { delete $self->{"reuse_to"}; $self->_scheduler; } )
$self->{"reuse_to"} &AE::timer($self->{"reuse_q"}[0][0] - $AnyEvent::DNS::NOW, 0, sub { delete $self->{"reuse_to"}; $self->_scheduler; } )
Truth table0%0/3
A ||= B
A= $self->{"reuse_to"}B= &AE::timer($self->{"reuse_q"}[0][0] - $AnyEvent::DNS::NOW, 0, sub { delete $self->{"reuse_to"}; $self->_scheduler; } )
ABresult
1X1
011
000
1325 0 last;
1326 }
1327  
1328 21 ▶ if (my $req = shift @{ $self->{queue} }) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $req = shift @{$$self{'queue'};}) { } 4 17 21
elsif (my $cb = shift @{$$self{'wait'};}) { } 4 13 17
1329 # found a request in the queue, execute it
1330 4 while () {
1331 4 $req->[2] = int rand 65536;
1332 4 ▶ last unless exists $self->{id}{$req->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"id"}{$req->[2]} 4 0 4
1333 }
1334  
1335 4 ++$self->{outstanding};
1336 4 $self->{id}{$req->[2]} = 1;
1337 4 substr $req->[0], 0, 2, pack "n", $req->[2];
1338  
1339 4 $self->_exec ($req);
1340  
1341 17 } elsif (my $cb = shift @{ $self->{wait} }) {
1342 # found a wait_for_slot callback
1343 4 $cb->($self);
1344  
1345 } else {
1346 # nothing to do, just exit
1347 13 last;
1348 }
1349 }
1350 }
1351  
1352 =item $resolver->request ($req, $cb->($res))
1353  
1354 This is the main low-level workhorse for sending DNS requests.
1355  
1356 This function sends a single request (a hash-ref formated as specified
1357 for C<dns_pack>) to the configured nameservers in turn until it gets a
1358 response. It handles timeouts, retries and automatically falls back to
1359 virtual circuit mode (TCP) when it receives a truncated reply. It does not
1360 handle anything else, such as the domain searchlist or relative names -
1361 use C<< ->resolve >> for that.
1362  
1363 Calls the callback with the decoded response packet if a reply was
1364 received, or no arguments in case none of the servers answered.
1365  
1366 =cut
1367  
1368 4 ▶sub request($$) {
Subroutine request: called CC=3 SCAR=11.2
Pod documented
1369 4 my ($self, $req, $cb) = @_;
1370  
1371 # _enc_name barfs on names that are too long, which is often outside
1372 # program control, so check for too long names here.
1373 4 for (@{ $req->{qd} }) {
1374 0 ▶ return AE::postpone sub { $cb->(undef) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1375 4 ▶ if 255 < length $_->[0];
Branch: 1 branch
BranchTrueFalseTotal
if 255 < length $_->[0] 0 4 4
1376 }
1377  
1378 4 push @{ $self->{queue} }, [dns_pack $req, $cb];
1379 4 $self->_scheduler;
1380 }
1381  
1382 =item $resolver->resolve ($qname, $qtype, %options, $cb->(@rr))
1383  
1384 Queries the DNS for the given domain name C<$qname> of type C<$qtype>.
1385  
1386 A C<$qtype> is either a numerical query type (e.g. C<1> for A records) or
1387 a lowercase name (you have to look at the source to see which aliases are
1388 supported, but all types from RFC 1035, C<aaaa>, C<srv>, C<spf> and a few
1389 more are known to this module). A C<$qtype> of "*" is supported and means
1390 "any" record type.
1391  
1392 The callback will be invoked with a list of matching result records or
1393 none on any error or if the name could not be found.
1394  
1395 CNAME chains (although illegal) are followed up to a length of 10.
1396  
1397 The callback will be invoked with arraryefs of the form C<[$name,
1398 $type, $class, $ttl, @data>], where C<$name> is the domain name,
1399 C<$type> a type string or number, C<$class> a class name, C<$ttl> is the
1400 remaining time-to-live and C<@data> is resource-record-dependent data, in
1401 seconds. For C<a> records, this will be the textual IPv4 addresses, for
1402 C<ns> or C<cname> records this will be a domain name, for C<txt> records
1403 these are all the strings and so on.
1404  
1405 All types mentioned in RFC 1035, C<aaaa>, C<srv>, C<naptr> and C<spf> are
1406 decoded. All resource records not known to this module will have the raw
1407 C<rdata> field as fifth array element.
1408  
1409 Note that this resolver is just a stub resolver: it requires a name server
1410 supporting recursive queries, will not do any recursive queries itself and
1411 is not secure when used against an untrusted name server.
1412  
1413 The following options are supported:
1414  
1415 =over 4
1416  
1417 =item search => [$suffix...]
1418  
1419 Use the given search list (which might be empty), by appending each one
1420 in turn to the C<$qname>. If this option is missing then the configured
1421 C<ndots> and C<search> values define its value (depending on C<ndots>, the
1422 empty suffix will be prepended or appended to that C<search> value). If
1423 the C<$qname> ends in a dot, then the searchlist will be ignored.
1424  
1425 =item accept => [$type...]
1426  
1427 Lists the acceptable result types: only result types in this set will be
1428 accepted and returned. The default includes the C<$qtype> and nothing
1429 else. If this list includes C<cname>, then CNAME-chains will not be
1430 followed (because you asked for the CNAME record).
1431  
1432 =item class => "class"
1433  
1434 Specify the query class ("in" for internet, "ch" for chaosnet and "hs" for
1435 hesiod are the only ones making sense). The default is "in", of course.
1436  
1437 =back
1438  
1439 Examples:
1440  
1441 # full example, you can paste this into perl:
1442 use Data::Dumper;
1443 use AnyEvent::DNS;
1444 AnyEvent::DNS::resolver->resolve (
1445 "google.com", "*", my $cv = AnyEvent->condvar);
1446 warn Dumper [$cv->recv];
1447  
1448 # shortened result:
1449 # [
1450 # [ 'google.com', 'soa', 'in', 3600, 'ns1.google.com', 'dns-admin.google.com',
1451 # 2008052701, 7200, 1800, 1209600, 300 ],
1452 # [
1453 # 'google.com', 'txt', 'in', 3600,
1454 # 'v=spf1 include:_netblocks.google.com ~all'
1455 # ],
1456 # [ 'google.com', 'a', 'in', 3600, '64.233.187.99' ],
1457 # [ 'google.com', 'mx', 'in', 3600, 10, 'smtp2.google.com' ],
1458 # [ 'google.com', 'ns', 'in', 3600, 'ns2.google.com' ],
1459 # ]
1460  
1461 # resolve a records:
1462 $res->resolve ("ruth.plan9.de", "a", sub { warn Dumper [@_] });
1463  
1464 # result:
1465 # [
1466 # [ 'ruth.schmorp.de', 'a', 'in', 86400, '129.13.162.95' ]
1467 # ]
1468  
1469 # resolve any records, but return only a and aaaa records:
1470 $res->resolve ("test1.laendle", "*",
1471 accept => ["a", "aaaa"],
1472 sub {
1473 warn Dumper [@_];
1474 }
1475 );
1476  
1477 # result:
1478 # [
1479 # [ 'test1.laendle', 'a', 'in', 86400, '10.0.0.255' ],
1480 # [ 'test1.laendle', 'aaaa', 'in', 60, '3ffe:1900:4545:0002:0240:0000:0000:f7e1' ]
1481 # ]
1482  
1483 =cut
1484  
1485 4 ▶sub resolve($%) {
Subroutine resolve: called CC=1 SCAR=0.9
Pod documented
1486 4 my $cb = pop;
1487 4 my ($self, $qname, $qtype, %opt) = @_;
1488  
1489 4 ▶ $self->wait_for_slot (sub {
Subroutine __ANON__: called CC=6 SCAR=23.1
1490 4 my $self = shift;
1491  
1492 my @search = $qname =~ s/\.$//
1493 ? ""
1494 : $opt{search}
1495 0 ▶ ? @{ $opt{search} }
Branch: 1 branch
BranchTrueFalseTotal
$qname =~ tr/.// >= $self->{'ndots'} ? : 0 0 0
1496 : ($qname =~ y/.//) >= $self->{ndots}
1497 0 ? ("", @{ $self->{search} })
1498 0 ▶ : (@{ $self->{search} }, "");
Branch: 2 branches
BranchTrueFalseTotal
$qname =~ s/\.$// ? : 4 0 4
$opt{'search'} ? : 0 0 0
1499  
1500 4 ▶ my $class = $opt{class} || "in";
Condition50%1/2
$opt{'class'} || 'in'
l!l
04
MC/DC0%0/1
$opt{'class'} || 'in'
$opt{'class'}
Truth table50%1/2
A
A= $opt{'class'}
Aresult
11
00
1501  
1502 my %atype = $opt{accept}
1503 0 ▶ ? map +($_ => 1), @{ $opt{accept} }
Branch: 1 branch
BranchTrueFalseTotal
$opt{'accept'} ? : 0 4 4
1504 : ($qtype => 1);
1505  
1506 # advance in searchlist
1507 4 my ($do_search, $do_req);
1508
1509 8 ▶ $do_search = sub {
Subroutine __ANON__: called CC=2 SCAR=8.7
1510 @search
1511 8 ▶ or (undef $do_search), (undef $do_req), return $cb->();
Branch: 1 branch
BranchTrueFalseTotal
@search or undef $do_search, undef $do_req, (return &$cb()) 4 4 8
1512  
1513 4 (my $name = lc "$qname." . shift @search) =~ s/\.$//;
1514 4 my $depth = 10;
1515  
1516 # advance in cname-chain
1517 4 ▶ $do_req = sub {
Subroutine __ANON__: called CC=1 SCAR=1.6
1518 $self->request ({
1519 rd => 1,
1520 qd => [[$name, $qtype, $class]],
1521 4 ▶ }, sub {
Subroutine __ANON__: called CC=9 SCAR=32.0
1522 4 ▶ my ($res) = @_
Branch: 1 branch
BranchTrueFalseTotal
my($res) = or return &$do_search() 4 0 4
1523 or return $do_search->();
1524  
1525 4 my $cname;
1526  
1527 4 while () {
1528 # results found?
1529 4 ▶ my @rr = grep $name eq lc $_->[0] && ($atype{"*"} || $atype{$_->[1]}), @{ $res->{an} };
Condition0%0/6
$name eq lc $_->[0] && ($atype{'*'} || $atype{$_->[1]})
!ll&&!rl&&r
000
$atype{"*"} or $atype{$_->[1]}
l!l&&r!l&&!r
000
MC/DC0%0/3
$name eq lc $_->[0] && ($atype{'*'} || $atype{$_->[1]})
$name eq lc $_->[0] $atype{"*"} $atype{$_->[1]}
Truth table0%0/4
A && B or C
A= $name eq lc $_->[0]B= $atype{"*"}C= $atype{$_->[1]}
ABCresult
0XX0
1000
11X1
1011
1530  
1531 4 ▶ (undef $do_search), (undef $do_req), return $cb->(@rr)
Branch: 1 branch
BranchTrueFalseTotal
if @rr 0 4 4
1532 if @rr;
1533  
1534 # see if there is a cname we can follow
1535 4 ▶ my @rr = grep $name eq lc $_->[0] && $_->[1] eq "cname", @{ $res->{an} };
Condition0%0/3
$name eq lc $_->[0] && $_->[1] eq 'cname'
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq lc $_->[0] && $_->[1] eq 'cname'
$name eq lc $_->[0] $_->[1] eq 'cname'
Truth table0%0/3
A && B
A= $name eq lc $_->[0]B= $_->[1] eq 'cname'
ABresult
0X0
100
111
1536  
1537 4 ▶ if (@rr) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rr) { } 0 4 4
elsif ($cname) { } 0 4 4
1538 0 ▶ $depth--
Branch: 1 branch
BranchTrueFalseTotal
$depth-- or return &$do_search() 0 0 0
1539 or return $do_search->(); # cname chain too long
1540  
1541 0 $cname = 1;
1542 0 $name = lc $rr[0][4];
1543  
1544 } elsif ($cname) {
1545 # follow the cname
1546 0 return $do_req->();
1547  
1548 } else {
1549 # no, not found anything
1550 4 return $do_search->();
1551 }
1552 }
1553 4 });
1554 4 };
1555  
1556 4 $do_req->();
1557 4 };
1558  
1559 4 $do_search->();
1560 4 });
1561 }
1562  
1563 =item $resolver->wait_for_slot ($cb->($resolver))
1564  
1565 Wait until a free request slot is available and call the callback with the
1566 resolver object.
1567  
1568 A request slot is used each time a request is actually sent to the
1569 nameservers: There are never more than C<max_outstanding> of them.
1570  
1571 Although you can submit more requests (they will simply be queued until
1572 a request slot becomes available), sometimes, usually for rate-limiting
1573 purposes, it is useful to instead wait for a slot before generating the
1574 request (or simply to know when the request load is low enough so one can
1575 submit requests again).
1576  
1577 This is what this method does: The callback will be called when submitting
1578 a DNS request will not result in that request being queued. The callback
1579 may or may not generate any requests in response.
1580  
1581 Note that the callback will only be invoked when the request queue is
1582 empty, so this does not play well if somebody else keeps the request queue
1583 full at all times.
1584  
1585 =cut
1586  
1587 4 ▶sub wait_for_slot {
Subroutine wait_for_slot: called CC=1 SCAR=0.0
Pod documented
1588 4 my ($self, $cb) = @_;
1589  
1590 4 push @{ $self->{wait} }, $cb;
1591 4 $self->_scheduler;
1592 }
1593  
1594 5 ▶use AnyEvent::Socket (); # circular dependency, so do not import anything and do it at the end
Subroutine BEGIN: called CC=1 SCAR=0.0
1595  
1596 =back
1597  
1598 =head1 AUTHOR
1599  
1600 Marc Lehmann <schmorp@schmorp.de>
1601 http://anyevent.schmorp.de
1602  
1603 =cut
1604  
1605 5 1
« blib/lib/AnyEvent/Log.pm ↑ Summary blib/lib/AnyEvent.pm »