Allow any type of query

Refactor some repeated code


git-svn-id: https://svn.perl.org/qpsmtpd/trunk@561 958fd67b-6ff1-0310-b445-bb7760255be9
This commit is contained in:
Matt Sergeant 2005-11-11 14:28:47 +00:00
parent 0a397e74a9
commit a405e64e1c
2 changed files with 34 additions and 26 deletions

View File

@ -62,7 +62,11 @@ sub new {
}
}
else {
die "Unsupported DNS query type: $options{type}";
if (!$resolver->query_type($self, $options{type}, @{$self->{hosts}})) {
$client->enable_read() if $client;
return;
}
# die "Unsupported DNS query type: $options{type}";
}
}
else {

View File

@ -92,6 +92,21 @@ sub _query {
return 1;
}
sub query_type {
my Danga::DNS::Resolver $self = shift;
my ($asker, $type, @hosts) = @_;
my $now = time();
trace(2, "[" . keys(%{$self->{id_to_asker}}) . "] trying to resolve $type: @hosts\n");
foreach my $host (@hosts) {
$self->_query($asker, $host, $type, $now) || return;
}
return 1;
}
sub query_txt {
my Danga::DNS::Resolver $self = shift;
my ($asker, @hosts) = @_;
@ -182,6 +197,15 @@ sub max_idle_time { 30 }
sub event_err { shift->close("dns socket error") }
sub event_hup { shift->close("dns socket error") }
my %type_to_host = (
PTR => 'ptrdname',
A => 'address',
AAAA => 'address',
TXT => 'txtdata',
NS => 'nsdname',
CNAME => 'cname',
);
sub event_read {
my Danga::DNS::Resolver $self = shift;
@ -200,32 +224,12 @@ sub event_read {
my $query = $qobj->{host};
my $now = time();
my @questions = $packet->question;
#print STDERR "response to ", $questions[0]->string, "\n";
foreach my $rr ($packet->answer) {
# my $q = shift @questions;
if ($rr->type eq "PTR") {
my $rdns = $rr->ptrdname;
# NB: Cached as an "A" lookup as there's no overlap and they
# go through the same query() function above
$self->{cache}{A}{$query} = $rdns;
# $self->{cache_timeout}{A}{$query} = $now + 60; # should use $rr->ttl but that would cache for too long
$self->{cache_timeout}{A}{$query} = $now + $rr->ttl;
$qobj->run_callback($rdns);
}
elsif ($rr->type eq "A") {
my $ip = $rr->address;
$self->{cache}{A}{$query} = $ip;
# $self->{cache_timeout}{A}{$query} = $now + 60; # should use $rr->ttl but that would cache for too long
$self->{cache_timeout}{A}{$query} = $now + $rr->ttl;
$qobj->run_callback($ip);
}
elsif ($rr->type eq "TXT") {
my $txt = $rr->txtdata;
$self->{cache}{TXT}{$query} = $txt;
# $self->{cache_timeout}{TXT}{$query} = $now + 60; # should use $rr->ttl but that would cache for too long
$self->{cache_timeout}{TXT}{$query} = $now + $rr->ttl;
$qobj->run_callback($txt);
if (my $host_method = $type_to_host{$rr->type}) {
my $host = $rr->$host_method;
$self->{cache}{$rr->type}{$query} = $host;
$self->{cache_timeout}{$rr->type}{$query} = $now + $rr->ttl;
$qobj->run_callback($host);
}
elsif ($rr->type eq "MX") {
my $host = $rr->exchange;