… | |
… | |
29 | package AnyEvent::DNS; |
29 | package AnyEvent::DNS; |
30 | |
30 | |
31 | no warnings; |
31 | no warnings; |
32 | use strict; |
32 | use strict; |
33 | |
33 | |
|
|
34 | use Carp (); |
34 | use Socket qw(AF_INET SOCK_DGRAM SOCK_STREAM); |
35 | use Socket qw(AF_INET SOCK_DGRAM SOCK_STREAM); |
35 | |
36 | |
36 | use AnyEvent (); |
37 | use AnyEvent (); |
37 | use AnyEvent::Handle (); |
|
|
38 | use AnyEvent::Util qw(AF_INET6); |
38 | use AnyEvent::Util qw(AF_INET6); |
39 | |
39 | |
40 | our $VERSION = 4.3; |
40 | our $VERSION = 4.83; |
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 | |
… | |
… | |
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, |
… | |
… | |
376 | |
377 | |
377 | our %class_str = reverse %class_id; |
378 | our %class_str = reverse %class_id; |
378 | |
379 | |
379 | sub _enc_name($) { |
380 | sub _enc_name($) { |
380 | pack "(C/a*)*", (split /\./, shift), "" |
381 | pack "(C/a*)*", (split /\./, shift), "" |
|
|
382 | } |
|
|
383 | |
|
|
384 | if ($[ < 5.008) { |
|
|
385 | # special slower 5.6 version |
|
|
386 | *_enc_name = sub { |
|
|
387 | join "", map +(pack "C/a*", $_), (split /\./, shift), "" |
|
|
388 | }; |
381 | } |
389 | } |
382 | |
390 | |
383 | sub _enc_qd() { |
391 | sub _enc_qd() { |
384 | (_enc_name $_->[0]) . pack "nn", |
392 | (_enc_name $_->[0]) . pack "nn", |
385 | ($_->[1] > 0 ? $_->[1] : $type_id {$_->[1]}), |
393 | ($_->[1] > 0 ? $_->[1] : $type_id {$_->[1]}), |
… | |
… | |
503 | 11 => sub { ((join ".", unpack "C4", $_), unpack "C a*", substr $_, 4) }, # wks |
511 | 11 => sub { ((join ".", unpack "C4", $_), unpack "C a*", substr $_, 4) }, # wks |
504 | 12 => sub { local $ofs = $ofs - length; _dec_name }, # ptr |
512 | 12 => sub { local $ofs = $ofs - length; _dec_name }, # ptr |
505 | 13 => sub { unpack "C/a* C/a*", $_ }, # hinfo |
513 | 13 => sub { unpack "C/a* C/a*", $_ }, # hinfo |
506 | 15 => sub { local $ofs = $ofs + 2 - length; ((unpack "n", $_), _dec_name) }, # mx |
514 | 15 => sub { local $ofs = $ofs + 2 - length; ((unpack "n", $_), _dec_name) }, # mx |
507 | 16 => sub { unpack "(C/a*)*", $_ }, # txt |
515 | 16 => sub { unpack "(C/a*)*", $_ }, # txt |
508 | 28 => sub { AnyEvent::Socket::format_address ($_) }, # aaaa |
516 | 28 => sub { AnyEvent::Socket::format_ipv6 ($_) }, # aaaa |
509 | 33 => sub { local $ofs = $ofs + 6 - length; ((unpack "nnn", $_), _dec_name) }, # srv |
517 | 33 => sub { local $ofs = $ofs + 6 - length; ((unpack "nnn", $_), _dec_name) }, # srv |
510 | 35 => sub { # naptr |
518 | 35 => sub { # naptr |
511 | # requires perl 5.10, sorry |
519 | # requires perl 5.10, sorry |
512 | my ($order, $preference, $flags, $service, $regexp, $offset) = unpack "nn C/a* C/a* C/a* .", $_; |
520 | my ($order, $preference, $flags, $service, $regexp, $offset) = unpack "nn C/a* C/a* C/a* .", $_; |
513 | local $ofs = $ofs + $offset - length; |
521 | local $ofs = $ofs + $offset - length; |
514 | ($order, $preference, $flags, $service, $regexp, _dec_name) |
522 | ($order, $preference, $flags, $service, $regexp, _dec_name) |
515 | }, |
523 | }, |
|
|
524 | 39 => sub { local $ofs = $ofs - length; _dec_name }, # dname |
516 | 99 => sub { unpack "(C/a*)*", $_ }, # spf |
525 | 99 => sub { unpack "(C/a*)*", $_ }, # spf |
517 | ); |
526 | ); |
518 | |
527 | |
519 | sub _dec_rr { |
528 | sub _dec_rr { |
520 | my $name = _dec_name; |
529 | my $name = _dec_name; |
… | |
… | |
647 | calls. |
656 | calls. |
648 | |
657 | |
649 | Unless you have special needs, prefer this function over creating your own |
658 | Unless you have special needs, prefer this function over creating your own |
650 | resolver object. |
659 | resolver object. |
651 | |
660 | |
|
|
661 | The resolver is created with the following parameters: |
|
|
662 | |
|
|
663 | untaint enabled |
|
|
664 | max_outstanding $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS} |
|
|
665 | |
|
|
666 | C<os_config> will be used for OS-specific configuration, unless |
|
|
667 | C<$ENV{PERL_ANYEVENT_RESOLV_CONF}> is specified, in which case that file |
|
|
668 | gets parsed. |
|
|
669 | |
652 | =cut |
670 | =cut |
653 | |
671 | |
654 | our $RESOLVER; |
672 | our $RESOLVER; |
655 | |
673 | |
656 | sub resolver() { |
674 | sub resolver() { |
657 | $RESOLVER || do { |
675 | $RESOLVER || do { |
658 | $RESOLVER = new AnyEvent::DNS; |
676 | $RESOLVER = new AnyEvent::DNS |
|
|
677 | untaint => 1, |
|
|
678 | exists $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS} |
|
|
679 | ? (max_outstanding => $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS}*1 || 1) : (), |
|
|
680 | ; |
|
|
681 | |
|
|
682 | exists $ENV{PERL_ANYEVENT_RESOLV_CONF} |
|
|
683 | ? length $ENV{PERL_ANYEVENT_RESOLV_CONF} && $RESOLVER->_parse_resolv_conf_file ($ENV{PERL_ANYEVENT_RESOLV_CONF}) |
659 | $RESOLVER->os_config; |
684 | : $RESOLVER->os_config; |
|
|
685 | |
660 | $RESOLVER |
686 | $RESOLVER |
661 | } |
687 | } |
662 | } |
688 | } |
663 | |
689 | |
664 | =item $resolver = new AnyEvent::DNS key => value... |
690 | =item $resolver = new AnyEvent::DNS key => value... |
… | |
… | |
701 | =item reuse => $seconds |
727 | =item reuse => $seconds |
702 | |
728 | |
703 | The number of seconds (default: C<300>) that a query id cannot be re-used |
729 | The number of seconds (default: C<300>) that a query id cannot be re-used |
704 | after a timeout. If there was no time-out then query ids can be reused |
730 | after a timeout. If there was no time-out then query ids can be reused |
705 | immediately. |
731 | immediately. |
|
|
732 | |
|
|
733 | =item untaint => $boolean |
|
|
734 | |
|
|
735 | When true, then the resolver will automatically untaint results, and might |
|
|
736 | also ignore certain environment variables. |
706 | |
737 | |
707 | =back |
738 | =back |
708 | |
739 | |
709 | =cut |
740 | =cut |
710 | |
741 | |
… | |
… | |
761 | $self->_compile; |
792 | $self->_compile; |
762 | |
793 | |
763 | $self |
794 | $self |
764 | } |
795 | } |
765 | |
796 | |
766 | =item $resolver->parse_resolv_conv ($string) |
797 | =item $resolver->parse_resolv_conf ($string) |
767 | |
798 | |
768 | Parses the given string as if it were a F<resolv.conf> file. The following |
799 | Parses the given string as if it were a F<resolv.conf> file. The following |
769 | directives are supported (but not necessarily implemented). |
800 | directives are supported (but not necessarily implemented). |
770 | |
801 | |
771 | C<#>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>, |
802 | C<#>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>, |
… | |
… | |
818 | if $attempts; |
849 | if $attempts; |
819 | |
850 | |
820 | $self->_compile; |
851 | $self->_compile; |
821 | } |
852 | } |
822 | |
853 | |
|
|
854 | sub _parse_resolv_conf_file { |
|
|
855 | my ($self, $resolv_conf) = @_; |
|
|
856 | |
|
|
857 | open my $fh, "<", $resolv_conf |
|
|
858 | or Carp::croak "$resolv_conf: $!"; |
|
|
859 | |
|
|
860 | local $/; |
|
|
861 | $self->parse_resolv_conf (<$fh>); |
|
|
862 | } |
|
|
863 | |
823 | =item $resolver->os_config |
864 | =item $resolver->os_config |
824 | |
865 | |
825 | Tries so load and parse F</etc/resolv.conf> on portable operating systems. Tries various |
866 | Tries so load and parse F</etc/resolv.conf> on portable operating |
826 | egregious hacks on windows to force the DNS servers and searchlist out of the system. |
867 | systems. Tries various egregious hacks on windows to force the DNS servers |
|
|
868 | and searchlist out of the system. |
827 | |
869 | |
828 | =cut |
870 | =cut |
829 | |
871 | |
830 | sub os_config { |
872 | sub os_config { |
831 | my ($self) = @_; |
873 | my ($self) = @_; |
832 | |
874 | |
833 | $self->{server} = []; |
875 | $self->{server} = []; |
834 | $self->{search} = []; |
876 | $self->{search} = []; |
835 | |
877 | |
836 | if (AnyEvent::WIN32 || $^O =~ /cygwin/i) { |
878 | if ((AnyEvent::WIN32 || $^O =~ /cygwin/i)) { |
837 | no strict 'refs'; |
879 | no strict 'refs'; |
838 | |
880 | |
839 | # there are many options to find the current nameservers etc. on windows |
881 | # there are many options to find the current nameservers etc. on windows |
840 | # all of them don't work consistently: |
882 | # all of them don't work consistently: |
841 | # - the registry thing needs separate code on win32 native vs. cygwin |
883 | # - the registry thing needs separate code on win32 native vs. cygwin |
… | |
… | |
875 | push @{ $self->{server} }, $DNS_FALLBACK[rand @DNS_FALLBACK]; |
917 | push @{ $self->{server} }, $DNS_FALLBACK[rand @DNS_FALLBACK]; |
876 | |
918 | |
877 | $self->_compile; |
919 | $self->_compile; |
878 | } |
920 | } |
879 | } else { |
921 | } else { |
880 | # try resolv.conf everywhere |
922 | # try resolv.conf everywhere else |
881 | |
923 | |
882 | if (open my $fh, "</etc/resolv.conf") { |
924 | $self->_parse_resolv_conf_file ("/etc/resolv.conf") |
883 | local $/; |
925 | if -e "/etc/resolv.conf"; |
884 | $self->parse_resolv_conf (<$fh>); |
|
|
885 | } |
|
|
886 | } |
926 | } |
887 | } |
927 | } |
888 | |
928 | |
889 | =item $resolver->timeout ($timeout, ...) |
929 | =item $resolver->timeout ($timeout, ...) |
890 | |
930 | |
… | |
… | |
936 | $self->{retry} = \@retry; |
976 | $self->{retry} = \@retry; |
937 | } |
977 | } |
938 | |
978 | |
939 | sub _feed { |
979 | sub _feed { |
940 | my ($self, $res) = @_; |
980 | my ($self, $res) = @_; |
|
|
981 | |
|
|
982 | ($res) = $res =~ /^(.*)$/s |
|
|
983 | if AnyEvent::TAINT && $self->{untaint}; |
941 | |
984 | |
942 | $res = dns_unpack $res |
985 | $res = dns_unpack $res |
943 | or return; |
986 | or return; |
944 | |
987 | |
945 | my $id = $self->{id}{$res->{id}}; |
988 | my $id = $self->{id}{$res->{id}}; |
… | |
… | |
1009 | AnyEvent::Socket::tcp_connect (AnyEvent::Socket::format_address ($server), DOMAIN_PORT, sub { |
1052 | AnyEvent::Socket::tcp_connect (AnyEvent::Socket::format_address ($server), DOMAIN_PORT, sub { |
1010 | return unless $do_retry; # some other request could have invalidated us already |
1053 | return unless $do_retry; # some other request could have invalidated us already |
1011 | |
1054 | |
1012 | my ($fh) = @_ |
1055 | my ($fh) = @_ |
1013 | or return &$do_retry; |
1056 | or return &$do_retry; |
|
|
1057 | |
|
|
1058 | require AnyEvent::Handle; |
1014 | |
1059 | |
1015 | my $handle; $handle = new AnyEvent::Handle |
1060 | my $handle; $handle = new AnyEvent::Handle |
1016 | fh => $fh, |
1061 | fh => $fh, |
1017 | timeout => $timeout, |
1062 | timeout => $timeout, |
1018 | on_error => sub { |
1063 | on_error => sub { |
… | |
… | |
1039 | } |
1084 | } |
1040 | }]; |
1085 | }]; |
1041 | |
1086 | |
1042 | my $sa = AnyEvent::Socket::pack_sockaddr (DOMAIN_PORT, $server); |
1087 | my $sa = AnyEvent::Socket::pack_sockaddr (DOMAIN_PORT, $server); |
1043 | |
1088 | |
1044 | my $fh = AF_INET == Socket::sockaddr_family ($sa) |
1089 | my $fh = AF_INET == AnyEvent::Socket::sockaddr_family ($sa) |
1045 | ? $self->{fh4} : $self->{fh6} |
1090 | ? $self->{fh4} : $self->{fh6} |
1046 | or return &$do_retry; |
1091 | or return &$do_retry; |
1047 | |
1092 | |
1048 | send $fh, $req->[0], 0, $sa; |
1093 | send $fh, $req->[0], 0, $sa; |
1049 | }; |
1094 | }; |
… | |
… | |
1116 | |
1161 | |
1117 | push @{ $self->{queue} }, [dns_pack $req, $cb]; |
1162 | push @{ $self->{queue} }, [dns_pack $req, $cb]; |
1118 | $self->_scheduler; |
1163 | $self->_scheduler; |
1119 | } |
1164 | } |
1120 | |
1165 | |
1121 | =item $resolver->resolve ($qname, $qtype, %options, $cb->($rcode, @rr)) |
1166 | =item $resolver->resolve ($qname, $qtype, %options, $cb->(@rr)) |
1122 | |
1167 | |
1123 | Queries the DNS for the given domain name C<$qname> of type C<$qtype>. |
1168 | Queries the DNS for the given domain name C<$qname> of type C<$qtype>. |
1124 | |
1169 | |
1125 | A C<$qtype> is either a numerical query type (e.g. C<1> for A records) or |
1170 | A C<$qtype> is either a numerical query type (e.g. C<1> for A records) or |
1126 | a lowercase name (you have to look at the source to see which aliases are |
1171 | a lowercase name (you have to look at the source to see which aliases are |
… | |
… | |
1131 | The callback will be invoked with a list of matching result records or |
1176 | The callback will be invoked with a list of matching result records or |
1132 | none on any error or if the name could not be found. |
1177 | none on any error or if the name could not be found. |
1133 | |
1178 | |
1134 | CNAME chains (although illegal) are followed up to a length of 10. |
1179 | CNAME chains (although illegal) are followed up to a length of 10. |
1135 | |
1180 | |
1136 | The callback will be invoked with an result code in string form (noerror, |
1181 | The callback will be invoked with arraryefs of the form C<[$name, $type, |
1137 | formerr, servfail, nxdomain, notimp, refused and so on), or numerical |
1182 | $class, @data>], where C<$name> is the domain name, C<$type> a type string |
1138 | form if the result code is not supported. The remaining arguments are |
1183 | or number, C<$class> a class name and @data is resource-record-dependent |
1139 | arraryefs of the form C<[$name, $type, $class, @data>], where C<$name> is |
1184 | data. For C<a> records, this will be the textual IPv4 addresses, for C<ns> |
1140 | the domain name, C<$type> a type string or number, C<$class> a class name |
1185 | or C<cname> records this will be a domain name, for C<txt> records these |
1141 | and @data is resource-record-dependent data. For C<a> records, this will |
1186 | are all the strings and so on. |
1142 | be the textual IPv4 addresses, for C<ns> or C<cname> records this will be |
|
|
1143 | a domain name, for C<txt> records these are all the strings and so on. |
|
|
1144 | |
1187 | |
1145 | All types mentioned in RFC 1035, C<aaaa>, C<srv>, C<naptr> and C<spf> are |
1188 | All types mentioned in RFC 1035, C<aaaa>, C<srv>, C<naptr> and C<spf> are |
1146 | decoded. All resource records not known to this module will have |
1189 | decoded. All resource records not known to this module will have |
1147 | the raw C<rdata> field as fourth entry. |
1190 | the raw C<rdata> field as fourth entry. |
1148 | |
1191 | |
… | |
… | |
1274 | if (@rr) { |
1317 | if (@rr) { |
1275 | $depth-- |
1318 | $depth-- |
1276 | or return $do_search->(); # cname chain too long |
1319 | or return $do_search->(); # cname chain too long |
1277 | |
1320 | |
1278 | $cname = 1; |
1321 | $cname = 1; |
1279 | $name = $rr[0][3]; |
1322 | $name = lc $rr[0][3]; |
1280 | |
1323 | |
1281 | } elsif ($cname) { |
1324 | } elsif ($cname) { |
1282 | # follow the cname |
1325 | # follow the cname |
1283 | return $do_req->(); |
1326 | return $do_req->(); |
1284 | |
1327 | |