http://www.perlmonks.org?node_id=11160586

gtech99 has asked for the wisdom of the Perl Monks concerning the following question:

I want to do a recursive dns lookup using Net::DNS::Resolver::Recurse , akin to doing dig +trace

Great, the module's description even gives you a code example on how to do that.

However, the module doesn't seem to try more than one NS in case of failure. I know I can do a loop where I can specify myself the NSs but how can I do that with this module ? I am testing one domain that has two NS listed. One answers properly, the other returns REFUSED.

How can I get it to retry on the other server ?

Replies are listed 'Best First'.
Re: Net::DNS::Resolver::Recurse issue
by parv (Parson) on Jul 14, 2024 at 06:33 UTC

    What is the short runnable code that you had tried?

      I might add that indeed, the domain is misconfigured from multiple points of view.

      1. Glue is missing

      2. The non-glued NS is refusing queries

      However, from what I read in the module's code, it should still work around this and circle back to the working NS

      Right now my code works only if it falls by chance on the working NS

      This is code from the module's source, I actually see it print "recover missing glue for .." in debug mode

      sub _recurse { my ( $self, $query, $apex ) = @_; $self->_diag("using cached nameservers for $apex"); my $cache = $self->{persistent}->{$apex}; my @nslist = keys %$cache; my @glue = grep { $$cache{$_} } @nslist; my @noglue = grep { !$$cache{$_} } @nslist; my $reply; foreach my $ns ( @glue, @noglue ) { if ( my $iplist = $$cache{$ns} ) { $self->nameservers(@$iplist); } else { $self->_diag("recover missing glue for $ns"); next if substr( lc($ns), -length($apex) ) eq $apex; my @ip = $self->nameservers($ns); $$cache{$ns} = \@ip; } $query->header->id(undef); last if $reply = $self->SUPER::send($query); $$cache{$ns} = undef; # park non-responder } $self->_callback($reply); return unless $reply; my $zone = $self->_referral($reply) || return $reply; die '_recurse exceeded depth limit' if $self->{recurse_depth}++ > +50; my $qname = lc( ( $query->question )[0]->qname ); my $suffix = substr( $qname, -length($zone) ); return $zone eq $suffix ? $self->_recurse( $query, $zone ) : undef +; }
      Oops, sorry. Really basic stuff. This fails on domains that have listed two nameservers, but only one provides actual answers because the other one is offline or misconfigured.
      #!/usr/local/bin/perl use Net::DNS::Resolver::Recurse; my $res = Net::DNS::Resolver::Recurse->new; my $coderef = sub { my $packet = shift; printf ";; Received %d bytes from %s\n\n", $packet->answersize, $packet->answerfrom; }; $res->callback($coderef); $res->debug(0); my $reply = $res->query("test.com", "MX"); if ($reply) { foreach $rr (grep { $_->type eq "MX" } $reply->answer) { print $rr->exchange, $rr->preference, "\n"; } } else { warn "query failed: ", $res->errorstring, "\n"; }