… | |
… | |
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::Util qw(AF_INET6); |
38 | use AnyEvent::Util qw(AF_INET6); |
38 | |
39 | |
… | |
… | |
648 | calls. |
649 | calls. |
649 | |
650 | |
650 | Unless you have special needs, prefer this function over creating your own |
651 | Unless you have special needs, prefer this function over creating your own |
651 | resolver object. |
652 | resolver object. |
652 | |
653 | |
|
|
654 | The resolver is created with the following parameters: |
|
|
655 | |
|
|
656 | untaint enabled |
|
|
657 | max_outstanding $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS} |
|
|
658 | |
|
|
659 | C<os_config> will be used for OS-specific configuration, unless |
|
|
660 | C<$ENV{PERL_ANYEVENT_RESOLV_CONF}> is specified, in which case that file |
|
|
661 | gets parsed. |
|
|
662 | |
653 | =cut |
663 | =cut |
654 | |
664 | |
655 | our $RESOLVER; |
665 | our $RESOLVER; |
656 | |
666 | |
657 | sub resolver() { |
667 | sub resolver() { |
658 | $RESOLVER || do { |
668 | $RESOLVER || do { |
659 | $RESOLVER = new AnyEvent::DNS untaint => 1; |
669 | $RESOLVER = new AnyEvent::DNS |
|
|
670 | untaint => 1, |
|
|
671 | exists $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS} |
|
|
672 | ? (max_outstanding => $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS}*1 || 1) : (), |
|
|
673 | ; |
|
|
674 | |
|
|
675 | exists $ENV{PERL_ANYEVENT_RESOLV_CONF} |
|
|
676 | ? length $ENV{PERL_ANYEVENT_RESOLV_CONF} && $RESOLVER->_parse_resolv_conf_file ($ENV{PERL_ANYEVENT_RESOLV_CONF}) |
660 | $RESOLVER->os_config; |
677 | : $RESOLVER->os_config; |
|
|
678 | |
661 | $RESOLVER |
679 | $RESOLVER |
662 | } |
680 | } |
663 | } |
681 | } |
664 | |
682 | |
665 | =item $resolver = new AnyEvent::DNS key => value... |
683 | =item $resolver = new AnyEvent::DNS key => value... |
… | |
… | |
767 | $self->_compile; |
785 | $self->_compile; |
768 | |
786 | |
769 | $self |
787 | $self |
770 | } |
788 | } |
771 | |
789 | |
772 | =item $resolver->parse_resolv_conv ($string) |
790 | =item $resolver->parse_resolv_conf ($string) |
773 | |
791 | |
774 | Parses the given string as if it were a F<resolv.conf> file. The following |
792 | Parses the given string as if it were a F<resolv.conf> file. The following |
775 | directives are supported (but not necessarily implemented). |
793 | directives are supported (but not necessarily implemented). |
776 | |
794 | |
777 | C<#>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>, |
795 | C<#>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>, |
… | |
… | |
824 | if $attempts; |
842 | if $attempts; |
825 | |
843 | |
826 | $self->_compile; |
844 | $self->_compile; |
827 | } |
845 | } |
828 | |
846 | |
|
|
847 | sub _parse_resolv_conf_file { |
|
|
848 | my ($self, $resolv_conf) = @_; |
|
|
849 | |
|
|
850 | open my $fh, "<:perlio", $resolv_conf |
|
|
851 | or Carp::croak "$resolv_conf: $!"; |
|
|
852 | |
|
|
853 | local $/; |
|
|
854 | $self->parse_resolv_conf (<$fh>); |
|
|
855 | } |
|
|
856 | |
829 | =item $resolver->os_config |
857 | =item $resolver->os_config |
830 | |
858 | |
831 | Tries so load and parse F</etc/resolv.conf> on portable operating |
859 | Tries so load and parse F</etc/resolv.conf> on portable operating |
832 | systems. Tries various egregious hacks on windows to force the DNS servers |
860 | systems. Tries various egregious hacks on windows to force the DNS servers |
833 | and searchlist out of the system. |
861 | and searchlist out of the system. |
… | |
… | |
838 | my ($self) = @_; |
866 | my ($self) = @_; |
839 | |
867 | |
840 | $self->{server} = []; |
868 | $self->{server} = []; |
841 | $self->{search} = []; |
869 | $self->{search} = []; |
842 | |
870 | |
843 | if (AnyEvent::WIN32 || $^O =~ /cygwin/i) { |
871 | if ((AnyEvent::WIN32 || $^O =~ /cygwin/i)) { |
844 | no strict 'refs'; |
872 | no strict 'refs'; |
845 | |
873 | |
846 | # there are many options to find the current nameservers etc. on windows |
874 | # there are many options to find the current nameservers etc. on windows |
847 | # all of them don't work consistently: |
875 | # all of them don't work consistently: |
848 | # - the registry thing needs separate code on win32 native vs. cygwin |
876 | # - the registry thing needs separate code on win32 native vs. cygwin |
… | |
… | |
882 | push @{ $self->{server} }, $DNS_FALLBACK[rand @DNS_FALLBACK]; |
910 | push @{ $self->{server} }, $DNS_FALLBACK[rand @DNS_FALLBACK]; |
883 | |
911 | |
884 | $self->_compile; |
912 | $self->_compile; |
885 | } |
913 | } |
886 | } else { |
914 | } else { |
887 | # try resolv.conf everywhere |
915 | # try resolv.conf everywhere else |
888 | |
916 | |
889 | if (open my $fh, "</etc/resolv.conf") { |
917 | $self->_parse_resolv_conf_file ("/etc/resolv.conf") |
890 | local $/; |
918 | if -e "/etc/resolv.conf"; |
891 | $self->parse_resolv_conf (<$fh>); |
|
|
892 | } |
|
|
893 | } |
919 | } |
894 | } |
920 | } |
895 | |
921 | |
896 | =item $resolver->timeout ($timeout, ...) |
922 | =item $resolver->timeout ($timeout, ...) |
897 | |
923 | |