… | |
… | |
526 | our $RESOLVER; |
526 | our $RESOLVER; |
527 | |
527 | |
528 | sub resolver() { |
528 | sub resolver() { |
529 | $RESOLVER || do { |
529 | $RESOLVER || do { |
530 | $RESOLVER = new AnyEvent::DNS; |
530 | $RESOLVER = new AnyEvent::DNS; |
531 | $RESOLVER->load_resolv_conf; |
531 | $RESOLVER->os_config; |
532 | $RESOLVER |
532 | $RESOLVER |
533 | } |
533 | } |
534 | } |
534 | } |
535 | |
535 | |
536 | =item $resolver = new AnyEvent::DNS key => value... |
536 | =item $resolver = new AnyEvent::DNS key => value... |
… | |
… | |
611 | } |
611 | } |
612 | |
612 | |
613 | =item $resolver->parse_resolv_conv ($string) |
613 | =item $resolver->parse_resolv_conv ($string) |
614 | |
614 | |
615 | Parses the given string a sif it were a F<resolv.conf> file. The following |
615 | Parses the given string a sif it were a F<resolv.conf> file. The following |
616 | directives are supported: |
616 | directives are supported (but not neecssarily implemented). |
617 | |
617 | |
618 | C<#>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>, |
618 | C<#>-style comments, C<nameserver>, C<domain>, C<search>, C<sortlist>, |
619 | C<options> (C<timeout>, C<attempts>, C<ndots>). |
619 | C<options> (C<timeout>, C<attempts>, C<ndots>). |
620 | |
620 | |
621 | Everything else is silently ignored. |
621 | Everything else is silently ignored. |
… | |
… | |
665 | if $attempts; |
665 | if $attempts; |
666 | |
666 | |
667 | $self->_compile; |
667 | $self->_compile; |
668 | } |
668 | } |
669 | |
669 | |
670 | =item $resolver->load_resolv_conf |
670 | =item $resolver->os_config |
671 | |
671 | |
672 | Tries to load and parse F</etc/resolv.conf>. If there will ever be windows |
672 | Tries so load and parse F</etc/resolv.conf> on portable opertaing systems. Tries various |
673 | support, then this function will do the right thing under windows, too. |
673 | egregious hacks on windows to force the dns servers and searchlist out of the config. |
674 | |
674 | |
675 | =cut |
675 | =cut |
676 | |
676 | |
677 | sub load_resolv_conf { |
677 | sub os_config { |
678 | my ($self) = @_; |
678 | my ($self) = @_; |
679 | |
679 | |
|
|
680 | if ($^O =~ /mswin32|cygwin/i) { |
|
|
681 | # yeah, it suxx... lets hope DNS is DNS in all locales |
|
|
682 | |
|
|
683 | if (open my $fh, "ipconfig /all |") { |
|
|
684 | delete $self->{server}; |
|
|
685 | delete $self->{search}; |
|
|
686 | |
|
|
687 | while (<$fh>) { |
|
|
688 | # first DNS.* is suffix list |
|
|
689 | if (/^\s*DNS/) { |
|
|
690 | while (/\s+([[:alnum:].\-]+)\s*$/) { |
|
|
691 | push @{ $self->{search} }, $1; |
|
|
692 | $_ = <$fh>; |
|
|
693 | } |
|
|
694 | last; |
|
|
695 | } |
|
|
696 | } |
|
|
697 | |
|
|
698 | while (<$fh>) { |
|
|
699 | # second DNS.* is server address list |
|
|
700 | if (/^\s*DNS/) { |
|
|
701 | while (/\s+(\d+\.\d+\.\d+\.\d+)\s*$/) { |
|
|
702 | my $ip = $1; |
|
|
703 | push @{ $self->{server} }, AnyEvent::Util::socket_inet_aton $ip |
|
|
704 | if AnyEvent::Util::dotted_quad $ip; |
|
|
705 | $_ = <$fh>; |
|
|
706 | } |
|
|
707 | last; |
|
|
708 | } |
|
|
709 | } |
|
|
710 | |
|
|
711 | $self->_compile; |
|
|
712 | } |
|
|
713 | } else { |
|
|
714 | # try resolv.conf everywhere |
|
|
715 | |
680 | open my $fh, "</etc/resolv.conf" |
716 | if (open my $fh, "</etc/resolv.conf") { |
681 | or return; |
|
|
682 | |
|
|
683 | local $/; |
717 | local $/; |
684 | $self->parse_resolv_conf (<$fh>); |
718 | $self->parse_resolv_conf (<$fh>); |
|
|
719 | } |
|
|
720 | } |
685 | } |
721 | } |
686 | |
722 | |
687 | sub _compile { |
723 | sub _compile { |
688 | my $self = shift; |
724 | my $self = shift; |
689 | |
725 | |