ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/DNS.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/DNS.pm (file contents):
Revision 1.101 by root, Mon Jul 6 21:47:14 2009 UTC vs.
Revision 1.102 by root, Mon Jul 6 23:32:49 2009 UTC

29package AnyEvent::DNS; 29package AnyEvent::DNS;
30 30
31no warnings; 31no warnings;
32use strict; 32use strict;
33 33
34use Carp ();
34use Socket qw(AF_INET SOCK_DGRAM SOCK_STREAM); 35use Socket qw(AF_INET SOCK_DGRAM SOCK_STREAM);
35 36
36use AnyEvent (); 37use AnyEvent ();
37use AnyEvent::Util qw(AF_INET6); 38use AnyEvent::Util qw(AF_INET6);
38 39
648calls. 649calls.
649 650
650Unless you have special needs, prefer this function over creating your own 651Unless you have special needs, prefer this function over creating your own
651resolver object. 652resolver object.
652 653
654The resolver is created with the following parameters:
655
656 untaint enabled
657 max_outstanding $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS}
658
659C<os_config> will be used for OS-specific configuration, unless
660C<$ENV{PERL_ANYEVENT_RESOLV_CONF}> is specified, in which case that file
661gets parsed.
662
653=cut 663=cut
654 664
655our $RESOLVER; 665our $RESOLVER;
656 666
657sub resolver() { 667sub 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
774Parses the given string as if it were a F<resolv.conf> file. The following 792Parses the given string as if it were a F<resolv.conf> file. The following
775directives are supported (but not necessarily implemented). 793directives are supported (but not necessarily implemented).
776 794
777C<#>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>, 795C<#>-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
847sub _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
831Tries so load and parse F</etc/resolv.conf> on portable operating 859Tries so load and parse F</etc/resolv.conf> on portable operating
832systems. Tries various egregious hacks on windows to force the DNS servers 860systems. Tries various egregious hacks on windows to force the DNS servers
833and searchlist out of the system. 861and 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines