AnyEvent
view release on metacpan or search on metacpan
lib/AnyEvent/DNS.pm view on Meta::CPAN
# @_ = ( [10, 10, 5060, "sip1.schmorp.de" ] )
=item AnyEvent::DNS::any $domain, $cb->(@rrs)
Tries to resolve the given domain and passes all resource records found
to the callback. Note that this uses a DNS C<ANY> query, which, as of RFC
8482, are officially deprecated.
=item AnyEvent::DNS::ptr $domain, $cb->(@hostnames)
Tries to make a PTR lookup on the given domain. See C<reverse_lookup>
and C<reverse_verify> if you want to resolve an IP address to a hostname
instead.
=item AnyEvent::DNS::reverse_lookup $ipv4_or_6, $cb->(@hostnames)
Tries to reverse-resolve the given IPv4 or IPv6 address (in textual form)
into its hostname(s). Handles V4MAPPED and V4COMPAT IPv6 addresses
transparently.
=item AnyEvent::DNS::reverse_verify $ipv4_or_6, $cb->(@hostnames)
The same as C<reverse_lookup>, but does forward-lookups to verify that
the resolved hostnames indeed point to the address, which makes spoofing
harder.
If you want to resolve an address into a hostname, this is the preferred
method: The DNS records could still change, but at least this function
verified that the hostname, at one point in the past, pointed at the IP
address you originally resolved.
Example:
AnyEvent::DNS::reverse_verify "2001:500:2f::f", sub { print shift };
# => f.root-servers.net
=cut
sub MAX_PKT() { 4096 } # max packet size we advertise and accept
sub DOMAIN_PORT() { 53 } # if this changes drop me a note
sub resolver ();
sub a($$) {
my ($domain, $cb) = @_;
resolver->resolve ($domain => "a", sub {
$cb->(map $_->[4], @_);
});
}
sub aaaa($$) {
my ($domain, $cb) = @_;
resolver->resolve ($domain => "aaaa", sub {
$cb->(map $_->[4], @_);
});
}
sub mx($$) {
my ($domain, $cb) = @_;
resolver->resolve ($domain => "mx", sub {
$cb->(map $_->[5], sort { $a->[4] <=> $b->[4] } @_);
});
}
sub ns($$) {
my ($domain, $cb) = @_;
resolver->resolve ($domain => "ns", sub {
$cb->(map $_->[4], @_);
});
}
sub txt($$) {
my ($domain, $cb) = @_;
resolver->resolve ($domain => "txt", sub {
$cb->(map $_->[4], @_);
});
}
sub srv($$$$) {
my ($service, $proto, $domain, $cb) = @_;
# todo, ask for any and check glue records
resolver->resolve ("_$service._$proto.$domain" => "srv", sub {
my @res;
# classify by priority
my %pri;
push @{ $pri{$_->[4]} }, [ @$_[4,5,6,7] ]
for @_;
# order by priority
for my $pri (sort { $a <=> $b } keys %pri) {
# order by weight
my @rr = sort { $a->[1] <=> $b->[1] } @{ delete $pri{$pri} };
my $sum; $sum += $_->[1] for @rr;
while (@rr) {
my $w = int rand $sum + 1;
for (0 .. $#rr) {
if (($w -= $rr[$_][1]) <= 0) {
$sum -= $rr[$_][1];
push @res, splice @rr, $_, 1, ();
last;
}
}
}
}
$cb->(@res);
});
}
sub ptr($$) {
my ($domain, $cb) = @_;
resolver->resolve ($domain => "ptr", sub {
$cb->(map $_->[4], @_);
});
}
sub any($$) {
my ($domain, $cb) = @_;
( run in 2.170 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )