… | |
… | |
35 | |
35 | |
36 | use AnyEvent (); |
36 | use AnyEvent (); |
37 | use AnyEvent::Handle (); |
37 | use AnyEvent::Handle (); |
38 | use AnyEvent::Util qw(AF_INET6); |
38 | use AnyEvent::Util qw(AF_INET6); |
39 | |
39 | |
40 | our $VERSION = 4.13; |
40 | our $VERSION = 4.45; |
41 | |
41 | |
42 | our @DNS_FALLBACK = (v208.67.220.220, v208.67.222.222); |
42 | our @DNS_FALLBACK = (v208.67.220.220, v208.67.222.222); |
43 | |
43 | |
44 | =item AnyEvent::DNS::a $domain, $cb->(@addrs) |
44 | =item AnyEvent::DNS::a $domain, $cb->(@addrs) |
45 | |
45 | |
… | |
… | |
170 | my %pri; |
170 | my %pri; |
171 | push @{ $pri{$_->[3]} }, [ @$_[3,4,5,6] ] |
171 | push @{ $pri{$_->[3]} }, [ @$_[3,4,5,6] ] |
172 | for @_; |
172 | for @_; |
173 | |
173 | |
174 | # order by priority |
174 | # order by priority |
175 | for my $pri (sort { $a->[0] <=> $b->[0] } keys %pri) { |
175 | for my $pri (sort { $a <=> $b } keys %pri) { |
176 | # order by weight |
176 | # order by weight |
177 | my @rr = sort { $a->[1] <=> $b->[1] } @{ delete $pri{$pri} }; |
177 | my @rr = sort { $a->[1] <=> $b->[1] } @{ delete $pri{$pri} }; |
178 | |
178 | |
179 | my $sum; $sum += $_->[1] for @rr; |
179 | my $sum; $sum += $_->[1] for @rr; |
180 | |
180 | |
… | |
… | |
295 | C<$ENV{PERL_ANYEVENT_EDNS0}>, but when set to C<1>, AnyEvent::DNS will use |
295 | C<$ENV{PERL_ANYEVENT_EDNS0}>, but when set to C<1>, AnyEvent::DNS will use |
296 | EDNS0 in all requests. |
296 | EDNS0 in all requests. |
297 | |
297 | |
298 | =cut |
298 | =cut |
299 | |
299 | |
300 | our $EDNS0 = $ENV{PERL_ANYEVENT_EDNS0} * 1; # set to 1 to enable (partial) edns0 |
300 | our $EDNS0 = $ENV{PERL_ANYEVENT_EDNS0}*1; # set to 1 to enable (partial) edns0 |
301 | |
301 | |
302 | our %opcode_id = ( |
302 | our %opcode_id = ( |
303 | query => 0, |
303 | query => 0, |
304 | iquery => 1, |
304 | iquery => 1, |
305 | status => 2, |
305 | status => 2, |
… | |
… | |
352 | mx => 15, |
352 | mx => 15, |
353 | txt => 16, |
353 | txt => 16, |
354 | aaaa => 28, |
354 | aaaa => 28, |
355 | srv => 33, |
355 | srv => 33, |
356 | naptr => 35, # rfc2915 |
356 | naptr => 35, # rfc2915 |
|
|
357 | dname => 39, # rfc2672 |
357 | opt => 41, |
358 | opt => 41, |
358 | spf => 99, |
359 | spf => 99, |
359 | tkey => 249, |
360 | tkey => 249, |
360 | tsig => 250, |
361 | tsig => 250, |
361 | ixfr => 251, |
362 | ixfr => 251, |
… | |
… | |
506 | 15 => sub { local $ofs = $ofs + 2 - length; ((unpack "n", $_), _dec_name) }, # mx |
507 | 15 => sub { local $ofs = $ofs + 2 - length; ((unpack "n", $_), _dec_name) }, # mx |
507 | 16 => sub { unpack "(C/a*)*", $_ }, # txt |
508 | 16 => sub { unpack "(C/a*)*", $_ }, # txt |
508 | 28 => sub { AnyEvent::Socket::format_address ($_) }, # aaaa |
509 | 28 => sub { AnyEvent::Socket::format_address ($_) }, # aaaa |
509 | 33 => sub { local $ofs = $ofs + 6 - length; ((unpack "nnn", $_), _dec_name) }, # srv |
510 | 33 => sub { local $ofs = $ofs + 6 - length; ((unpack "nnn", $_), _dec_name) }, # srv |
510 | 35 => sub { # naptr |
511 | 35 => sub { # naptr |
|
|
512 | # requires perl 5.10, sorry |
511 | my ($order, $preference, $flags, $service, $regexp, $offset) = unpack "nn C/a* C/a* C/a* .", $_; |
513 | my ($order, $preference, $flags, $service, $regexp, $offset) = unpack "nn C/a* C/a* C/a* .", $_; |
512 | local $ofs = $ofs + $offset - length; |
514 | local $ofs = $ofs + $offset - length; |
513 | ($order, $preference, $flags, $service, $regexp, _dec_name) |
515 | ($order, $preference, $flags, $service, $regexp, _dec_name) |
514 | }, |
516 | }, |
|
|
517 | 39 => sub { local $ofs = $ofs - length; _dec_name }, # dname |
515 | 99 => sub { unpack "(C/a*)*", $_ }, # spf |
518 | 99 => sub { unpack "(C/a*)*", $_ }, # spf |
516 | ); |
519 | ); |
517 | |
520 | |
518 | sub _dec_rr { |
521 | sub _dec_rr { |
519 | my $name = _dec_name; |
522 | my $name = _dec_name; |
… | |
… | |
652 | |
655 | |
653 | our $RESOLVER; |
656 | our $RESOLVER; |
654 | |
657 | |
655 | sub resolver() { |
658 | sub resolver() { |
656 | $RESOLVER || do { |
659 | $RESOLVER || do { |
657 | $RESOLVER = new AnyEvent::DNS; |
660 | $RESOLVER = new AnyEvent::DNS untaint => 1; |
658 | $RESOLVER->os_config; |
661 | $RESOLVER->os_config; |
659 | $RESOLVER |
662 | $RESOLVER |
660 | } |
663 | } |
661 | } |
664 | } |
662 | |
665 | |
… | |
… | |
700 | =item reuse => $seconds |
703 | =item reuse => $seconds |
701 | |
704 | |
702 | The number of seconds (default: C<300>) that a query id cannot be re-used |
705 | The number of seconds (default: C<300>) that a query id cannot be re-used |
703 | after a timeout. If there was no time-out then query ids can be reused |
706 | after a timeout. If there was no time-out then query ids can be reused |
704 | immediately. |
707 | immediately. |
|
|
708 | |
|
|
709 | =item untaint => $boolean |
|
|
710 | |
|
|
711 | When true, then the resolver will automatically untaint results, and might |
|
|
712 | also ignore certain environment variables. |
705 | |
713 | |
706 | =back |
714 | =back |
707 | |
715 | |
708 | =cut |
716 | =cut |
709 | |
717 | |
… | |
… | |
819 | $self->_compile; |
827 | $self->_compile; |
820 | } |
828 | } |
821 | |
829 | |
822 | =item $resolver->os_config |
830 | =item $resolver->os_config |
823 | |
831 | |
824 | Tries so load and parse F</etc/resolv.conf> on portable operating systems. Tries various |
832 | Tries so load and parse F</etc/resolv.conf> on portable operating |
825 | egregious hacks on windows to force the DNS servers and searchlist out of the system. |
833 | systems. Tries various egregious hacks on windows to force the DNS servers |
|
|
834 | and searchlist out of the system. |
826 | |
835 | |
827 | =cut |
836 | =cut |
828 | |
837 | |
829 | sub os_config { |
838 | sub os_config { |
830 | my ($self) = @_; |
839 | my ($self) = @_; |
… | |
… | |
936 | } |
945 | } |
937 | |
946 | |
938 | sub _feed { |
947 | sub _feed { |
939 | my ($self, $res) = @_; |
948 | my ($self, $res) = @_; |
940 | |
949 | |
|
|
950 | ($res) = $res =~ /^(.*)$/s |
|
|
951 | if AnyEvent::TAINT && $self->{untaint}; |
|
|
952 | |
941 | $res = dns_unpack $res |
953 | $res = dns_unpack $res |
942 | or return; |
954 | or return; |
943 | |
955 | |
944 | my $id = $self->{id}{$res->{id}}; |
956 | my $id = $self->{id}{$res->{id}}; |
945 | |
957 | |
… | |
… | |
997 | |
1009 | |
998 | $self->{id}{$req->[2]} = [AnyEvent->timer (after => $timeout, cb => sub { |
1010 | $self->{id}{$req->[2]} = [AnyEvent->timer (after => $timeout, cb => sub { |
999 | $NOW = time; |
1011 | $NOW = time; |
1000 | |
1012 | |
1001 | # timeout, try next |
1013 | # timeout, try next |
1002 | &$do_retry; |
1014 | &$do_retry if $do_retry; |
1003 | }), sub { |
1015 | }), sub { |
1004 | my ($res) = @_; |
1016 | my ($res) = @_; |
1005 | |
1017 | |
1006 | if ($res->{tc}) { |
1018 | if ($res->{tc}) { |
1007 | # success, but truncated, so use tcp |
1019 | # success, but truncated, so use tcp |
… | |
… | |
1115 | |
1127 | |
1116 | push @{ $self->{queue} }, [dns_pack $req, $cb]; |
1128 | push @{ $self->{queue} }, [dns_pack $req, $cb]; |
1117 | $self->_scheduler; |
1129 | $self->_scheduler; |
1118 | } |
1130 | } |
1119 | |
1131 | |
1120 | =item $resolver->resolve ($qname, $qtype, %options, $cb->($rcode, @rr)) |
1132 | =item $resolver->resolve ($qname, $qtype, %options, $cb->(@rr)) |
1121 | |
1133 | |
1122 | Queries the DNS for the given domain name C<$qname> of type C<$qtype>. |
1134 | Queries the DNS for the given domain name C<$qname> of type C<$qtype>. |
1123 | |
1135 | |
1124 | A C<$qtype> is either a numerical query type (e.g. C<1> for A records) or |
1136 | A C<$qtype> is either a numerical query type (e.g. C<1> for A records) or |
1125 | a lowercase name (you have to look at the source to see which aliases are |
1137 | a lowercase name (you have to look at the source to see which aliases are |
… | |
… | |
1130 | The callback will be invoked with a list of matching result records or |
1142 | The callback will be invoked with a list of matching result records or |
1131 | none on any error or if the name could not be found. |
1143 | none on any error or if the name could not be found. |
1132 | |
1144 | |
1133 | CNAME chains (although illegal) are followed up to a length of 10. |
1145 | CNAME chains (although illegal) are followed up to a length of 10. |
1134 | |
1146 | |
1135 | The callback will be invoked with an result code in string form (noerror, |
1147 | The callback will be invoked with arraryefs of the form C<[$name, $type, |
1136 | formerr, servfail, nxdomain, notimp, refused and so on), or numerical |
1148 | $class, @data>], where C<$name> is the domain name, C<$type> a type string |
1137 | form if the result code is not supported. The remaining arguments are |
1149 | or number, C<$class> a class name and @data is resource-record-dependent |
1138 | arraryefs of the form C<[$name, $type, $class, @data>], where C<$name> is |
1150 | data. For C<a> records, this will be the textual IPv4 addresses, for C<ns> |
1139 | the domain name, C<$type> a type string or number, C<$class> a class name |
1151 | or C<cname> records this will be a domain name, for C<txt> records these |
1140 | and @data is resource-record-dependent data. For C<a> records, this will |
1152 | are all the strings and so on. |
1141 | be the textual IPv4 addresses, for C<ns> or C<cname> records this will be |
|
|
1142 | a domain name, for C<txt> records these are all the strings and so on. |
|
|
1143 | |
1153 | |
1144 | All types mentioned in RFC 1035, C<aaaa>, C<srv>, C<naptr> and C<spf> are |
1154 | All types mentioned in RFC 1035, C<aaaa>, C<srv>, C<naptr> and C<spf> are |
1145 | decoded. All resource records not known to this module will have |
1155 | decoded. All resource records not known to this module will have |
1146 | the raw C<rdata> field as fourth entry. |
1156 | the raw C<rdata> field as fourth entry. |
1147 | |
1157 | |