Attached is a proposed patch.
Besides adding a DnsResolver option, it contains two small
extras:
- avoids an undef warning:
t/verifier.t ................. 1/104 Use of uninitialized value
$query_type in lc at .../blib/lib/Mail/DKIM/PublicKey.pm line 86.
Use of uninitialized value $query_type in concatenation (.) or string
at .../blib/lib/Mail/DKIM/PublicKey.pm line 88.
- uses Net::DNS::Resolver::send instead of Net::DNS::Resolver::query,
it makes no sense to apply a default domain in case of a non-FQDN;
also paves a path to using bgsend() for potential async queries.
diff -r -U2 Mail-DKIM-0.39-orig/lib/Mail/DKIM/AuthorDomainPolicy.pm Mail-DKIM-0.39-new/lib/Mail/DKIM/AuthorDomainPolicy.pm
--- Mail-DKIM-0.39-orig/lib/Mail/DKIM/AuthorDomainPolicy.pm 2010-01-23 18:44:52.000000000 +0100
+++ Mail-DKIM-0.39-new/lib/Mail/DKIM/AuthorDomainPolicy.pm 2012-11-06 14:17:33.220434302 +0100
@@ -42,4 +42,5 @@
Protocol => "dns",
Author => 'jsmith@example.org',
+ DnsResolver => Net::DNS::Resolver->new, # optional
);
@@ -82,5 +83,7 @@
}
- my @resp = Mail::DKIM::DNS::query($prms{Domain}, "MX");
+ my @resp = Mail::DKIM::DNS::query($prms{Domain}, "MX",
+ DnsResolver => $prms{DnsResolver} );
+
if (!@resp && $@ eq "NXDOMAIN")
{
diff -r -U2 Mail-DKIM-0.39-orig/lib/Mail/DKIM/DNS.pm Mail-DKIM-0.39-new/lib/Mail/DKIM/DNS.pm
--- Mail-DKIM-0.39-orig/lib/Mail/DKIM/DNS.pm 2010-01-23 18:44:52.000000000 +0100
+++ Mail-DKIM-0.39-new/lib/Mail/DKIM/DNS.pm 2012-11-06 14:34:06.527433369 +0100
@@ -26,8 +26,8 @@
sub query
{
- my ($domain, $type) = @_;
+ my ($domain, $type, %prms) = @_;
- my $rslv = Net::DNS::Resolver->new()
- or die "can't create DNS resolver";
+ my $rslv = $prms{DnsResolver} || Net::DNS::Resolver->new;
+ $rslv or die "can't create DNS resolver";
#
@@ -49,5 +49,5 @@
eval
{
- $resp = $rslv->query($domain, $type);
+ $resp = $rslv->send($domain, $type);
};
my $E = $@;
@@ -100,5 +100,5 @@
my $warning;
eval {
- @resp = query($domain, $type);
+ @resp = query($domain, $type, %prms);
$warning = $@;
undef $@;
diff -r -U2 Mail-DKIM-0.39-orig/lib/Mail/DKIM/Policy.pm Mail-DKIM-0.39-new/lib/Mail/DKIM/Policy.pm
--- Mail-DKIM-0.39-orig/lib/Mail/DKIM/Policy.pm 2010-01-23 18:44:52.000000000 +0100
+++ Mail-DKIM-0.39-new/lib/Mail/DKIM/Policy.pm 2012-11-06 02:05:19.407434501 +0100
@@ -98,4 +98,5 @@
$host, "TXT",
Callbacks => \%callbacks,
+ DnsResolver => $prms{DnsResolver},
);
return $waiter;
diff -r -U2 Mail-DKIM-0.39-orig/lib/Mail/DKIM/PublicKey.pm Mail-DKIM-0.39-new/lib/Mail/DKIM/PublicKey.pm
--- Mail-DKIM-0.39-orig/lib/Mail/DKIM/PublicKey.pm 2010-01-23 18:44:52.000000000 +0100
+++ Mail-DKIM-0.39-new/lib/Mail/DKIM/PublicKey.pm 2012-11-06 14:11:43.800434696 +0100
@@ -41,4 +41,5 @@
Selector => "brisbane",
Domain => "example.com",
+ DnsResolver => Net::DNS::Resolver->new, # optional
);
@@ -84,6 +85,7 @@
my ($query_type, $query_options) = split(/\//, $prms{Protocol}, 2);
- if (lc($query_type) ne "dns")
- {
+ if (!defined $query_type) {
+ die "empty or unrecognized query type\n";
+ } elsif (lc($query_type) ne "dns") {
die "unknown query type '$query_type'\n";
}
@@ -123,4 +125,5 @@
$host, "TXT",
Callbacks => \%callbacks,
+ DnsResolver => $prms{DnsResolver},
);
return $waiter;
diff -r -U2 Mail-DKIM-0.39-orig/lib/Mail/DKIM/Signature.pm Mail-DKIM-0.39-new/lib/Mail/DKIM/Signature.pm
--- Mail-DKIM-0.39-orig/lib/Mail/DKIM/Signature.pm 2010-11-14 22:05:44.000000000 +0100
+++ Mail-DKIM-0.39-new/lib/Mail/DKIM/Signature.pm 2012-11-06 15:08:22.032432356 +0100
@@ -49,4 +49,5 @@
bless $self, $class;
+ $self->{DnsResolver} = $prms{DnsResolver};
$self->version("1");
$self->algorithm($prms{'Algorithm'} || "rsa-sha1");
@@ -506,4 +507,5 @@
my $self = shift;
return if exists $self->{public_key_query};
+ my %prms = @_;
my $on_success = sub {
@@ -517,4 +519,5 @@
$self->{public_key_query} =
Mail::DKIM::PublicKey->fetch_async(
+ DnsResolver => $prms{DnsResolver},
Protocol => $self->protocol,
Selector => $self->selector,
@@ -540,5 +543,5 @@
delete $self->{public};
delete $self->{public_error};
- $self->fetch_public_key;
+ $self->fetch_public_key(@_);
}
@@ -564,5 +567,5 @@
if (not exists $self->{public_key_query})
{
- $self->fetch_public_key;
+ $self->fetch_public_key(@_);
}
diff -r -U2 Mail-DKIM-0.39-orig/lib/Mail/DKIM/Verifier.pm Mail-DKIM-0.39-new/lib/Mail/DKIM/Verifier.pm
--- Mail-DKIM-0.39-orig/lib/Mail/DKIM/Verifier.pm 2010-11-14 22:05:44.000000000 +0100
+++ Mail-DKIM-0.39-new/lib/Mail/DKIM/Verifier.pm 2012-11-06 15:05:23.737432715 +0100
@@ -96,5 +96,5 @@
my $dkim = Mail::DKIM::Verifier->new(%options);
-The only option supported at this time is:
+Options supported at this time are:
=over
@@ -105,4 +105,18 @@
is written to the referenced string or file handle.
+=item DnsResolver
+
+a DNS resolver object. When the option is missing or undefined, method
+Net::DNS::Resolver->new() will be called to provide a resolver object.
+This options allows for more flexibility in passing options to a
+DNS resolver, e.g.:
+
+ my $r;
+ $r = Net::DNS::Resolver->new(udp_timeout => 3, tcp_timeout => 3, retry => 2);
+ $r->udppacketsize(1280); # enables EDNS0, sets acceptable UDP packet size
+ $r->persistent_udp(1);
+# $r->debug(1);
+ my $dkim = Mail::DKIM::Verifier->new($r);
+
=back
@@ -159,5 +173,6 @@
my $signature = Mail::DKIM::Signature->parse($line);
$self->add_signature($signature);
- $signature->fetch_public_key;
+ $signature->fetch_public_key(
+ DnsResolver => $self->{DnsResolver} );
};
if ($@)
@@ -179,5 +194,6 @@
my $signature = Mail::DKIM::DkSignature->parse($line);
$self->add_signature($signature);
- $signature->fetch_public_key;
+ $signature->fetch_public_key(
+ DnsResolver => $self->{DnsResolver} );
};
if ($@)
@@ -435,5 +451,6 @@
eval
{
- $pkey = $signature->get_public_key;
+ $pkey = $signature->get_public_key(
+ DnsResolver => $self->{DnsResolver} );
};
if ($@)
@@ -571,6 +588,7 @@
return map {
Mail::DKIM::AuthorDomainPolicy->fetch(
- Protocol => "dns",
Author => $_,
+ Protocol => "dns",
+ DnsResolver => $self->{DnsResolver},
)
} @authors;
@@ -604,6 +622,7 @@
# fetch the policy
return Mail::DKIM::DkimPolicy->fetch(
- Protocol => "dns",
Author => $author,
+ Protocol => "dns",
+ DnsResolver => $self->{DnsResolver},
);
}
@@ -647,4 +666,5 @@
Author => $author,
Sender => $sender,
+ DnsResolver => $self->{DnsResolver},
);
}