cppcoder
cppcoder

Reputation: 23105

DNS Resolver result from multiple nameservers

I am passing two name servers to the Net::DNS::Resolver constructor but I am getting only one result back.

How should I change the code to receive result from all the name servers?

sub resolve_dns()
{
    my $dns = $_[0];
    my $res   = Net::DNS::Resolver->new(

                nameservers => [qw(24.116.197.232 114.130.11.67 )],
                recurse     => 0,
                debug       => 1,
                tcp_timeout => 3
                );
    my $query = $res->search($dns);

    if ($query) {
        foreach my $rr ($query->answer) {
            next unless $rr->type eq "A";
            print $rr->address, "\n";
        }
    } else {
        warn "query failed: ", $res->errorstring, "\n";
    }
}

Upvotes: 1

Views: 800

Answers (1)

Borodin
Borodin

Reputation: 126722

I presume the DNS servers after the first are there for fallback purposes and only a single reply will ever be returned.

The best way seems to be to manipulate the Net::DNS::Resolver server list and explicitly make a request to each of them.

This example code demonstrates the principle

sub resolve_dns {

  my $address = shift;

  my $res = Net::DNS::Resolver->new
    recurse     => 0,
    debug       => 1,
    tcp_timeout => 3,
  );

  for my $ns (qw( 24.116.197.232  114.130.11.67 )) {

    $res->nameservers($ns);

    my $reply = $res->send($address);

    if ($reply) {
      my @type_a = grep $_->type eq 'A', $reply->answer;
      print $_->address, "\n" for @type_a;
    }
    else {
      warn sprintf "Query to %s failed: %s\n", $ns, $res->errorstring;
    }
  }

}

Upvotes: 3

Related Questions