table of contents
AnyEvent::DNS(3pm) | User Contributed Perl Documentation | AnyEvent::DNS(3pm) |
NAME¶
AnyEvent::DNS - fully asynchronous DNS resolutionSYNOPSIS¶
use AnyEvent::DNS; my $cv = AnyEvent->condvar; AnyEvent::DNS::a "www.google.de", $cv; # ... later my @addrs = $cv->recv;
DESCRIPTION¶
This module offers both a number of DNS convenience functions as well as a fully asynchronous and high-performance pure-perl stub resolver. The stub resolver supports DNS over IPv4 and IPv6, UDP and TCP, optional EDNS0 support for up to 4kiB datagrams and automatically falls back to virtual circuit mode for large responses.CONVENIENCE FUNCTIONS¶
- AnyEvent::DNS::a $domain, $cb->(@addrs)
- Tries to resolve the given domain to IPv4 address(es).
- AnyEvent::DNS::aaaa $domain, $cb->(@addrs)
- Tries to resolve the given domain to IPv6 address(es).
- AnyEvent::DNS::mx $domain, $cb->(@hostnames)
- Tries to resolve the given domain into a sorted (lower preference value first) list of domain names.
- AnyEvent::DNS::ns $domain, $cb->(@hostnames)
- Tries to resolve the given domain name into a list of name servers.
- AnyEvent::DNS::txt $domain, $cb->(@hostnames)
- Tries to resolve the given domain name into a list of text
records. Only the first text string per record will be returned. If you
want all strings, you need to call the resolver manually:
resolver->resolve ($domain => "txt", sub { for my $record (@_) { my (undef, undef, undef, @txt) = @$record; # strings now in @txt } });
- AnyEvent::DNS::srv $service, $proto, $domain, $cb->(@srv_rr)
- Tries to resolve the given service, protocol and domain
name into a list of service records.
AnyEvent::DNS::srv "sip", "udp", "schmorp.de", sub { ... # @_ = ( [10, 10, 5060, "sip1.schmorp.de" ] )
- AnyEvent::DNS::any $domain, $cb->(@rrs)
- Tries to resolve the given domain and passes all resource records found to the callback.
- AnyEvent::DNS::ptr $domain, $cb->(@hostnames)
- Tries to make a PTR lookup on the given domain. See "reverse_lookup" and "reverse_verify" if you want to resolve an IP address to a hostname instead.
- 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.
- AnyEvent::DNS::reverse_verify $ipv4_or_6, $cb->(@hostnames)
- The same as "reverse_lookup", but does
forward-lookups to verify that the resolved hostnames indeed point to the
address, which makes spoofing harder.
AnyEvent::DNS::reverse_verify "2001:500:2f::f", sub { print shift }; # => f.root-servers.net
LOW-LEVEL DNS EN-/DECODING FUNCTIONS¶
- $AnyEvent::DNS::EDNS0
- This variable decides whether dns_pack automatically enables EDNS0 support. By default, this is disabled (0), unless overridden by $ENV{PERL_ANYEVENT_EDNS0}, but when set to 1, AnyEvent::DNS will use EDNS0 in all requests.
- $pkt = AnyEvent::DNS::dns_pack $dns
- Packs a perl data structure into a DNS packet. Reading RFC
1035 is strongly recommended, then everything will be totally clear. Or
maybe not.
# very simple request, using lots of default values: { rd => 1, qd => [ [ "host.domain", "a"] ] } # more complex example, showing how flags etc. are named: { id => 10000, op => "query", rc => "nxdomain", # flags qr => 1, aa => 0, tc => 0, rd => 0, ra => 0, ad => 0, cd => 0, qd => [@rr], # query section an => [@rr], # answer section ns => [@rr], # authority section ar => [@rr], # additional records section }
- $dns = AnyEvent::DNS::dns_unpack $pkt
- Unpacks a DNS packet into a perl data structure.
# an unsuccessful reply { 'qd' => [ [ 'ruth.plan9.de.mach.uni-karlsruhe.de', '*', 'in' ] ], 'rc' => 'nxdomain', 'ar' => [], 'ns' => [ [ 'uni-karlsruhe.de', 'soa', 'in', 600, 'netserv.rz.uni-karlsruhe.de', 'hostmaster.rz.uni-karlsruhe.de', 2008052201, 10800, 1800, 2592000, 86400 ] ], 'tc' => '', 'ra' => 1, 'qr' => 1, 'id' => 45915, 'aa' => '', 'an' => [], 'rd' => 1, 'op' => 'query' } # a successful reply { 'qd' => [ [ 'www.google.de', 'a', 'in' ] ], 'rc' => 0, 'ar' => [ [ 'a.l.google.com', 'a', 'in', 3600, '209.85.139.9' ], [ 'b.l.google.com', 'a', 'in', 3600, '64.233.179.9' ], [ 'c.l.google.com', 'a', 'in', 3600, '64.233.161.9' ], ], 'ns' => [ [ 'l.google.com', 'ns', 'in', 3600, 'a.l.google.com' ], [ 'l.google.com', 'ns', 'in', 3600, 'b.l.google.com' ], ], 'tc' => '', 'ra' => 1, 'qr' => 1, 'id' => 64265, 'aa' => '', 'an' => [ [ 'www.google.de', 'cname', 'in', 3600, 'www.google.com' ], [ 'www.google.com', 'cname', 'in', 3600, 'www.l.google.com' ], [ 'www.l.google.com', 'a', 'in', 3600, '66.249.93.104' ], [ 'www.l.google.com', 'a', 'in', 3600, '66.249.93.147' ], ], 'rd' => 1, 'op' => 0 }
THE AnyEvent::DNS RESOLVER CLASS¶
This is the class which does the actual protocol work.- AnyEvent::DNS::resolver
- This function creates and returns a resolver that is ready
to use and should mimic the default resolver for your system as good as
possible. It is used by AnyEvent itself as well.
untaint enabled max_outstanding $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS}
- $AnyEvent::DNS::RESOLVER
- This variable stores the default resolver returned by
"AnyEvent::DNS::resolver", or "undef" when the default
resolver hasn't been instantiated yet.
- $resolver = new AnyEvent::DNS key => value...
- Creates and returns a new resolver.
- server => [...]
- A list of server addresses (default: "v127.0.0.1" or "::1") in network format (i.e. as returned by "AnyEvent::Socket::parse_address" - both IPv4 and IPv6 are supported).
- timeout => [...]
- A list of timeouts to use (also determines the number of retries). To make three retries with individual time-outs of 2, 5 and 5 seconds, use "[2, 5, 5]", which is also the default.
- search => [...]
- The default search list of suffixes to append to a domain name (default: none).
- ndots => $integer
- The number of dots (default: 1) that a name must have so that the resolver tries to resolve the name without any suffixes first.
- max_outstanding => $integer
- Most name servers do not handle many parallel requests very well. This option limits the number of outstanding requests to $integer (default: 10), that means if you request more than this many requests, then the additional requests will be queued until some other requests have been resolved.
- reuse => $seconds
- The number of seconds (default: 300) that a query id cannot be re-used after a timeout. If there was no time-out then query ids can be reused immediately.
- untaint => $boolean
- When true, then the resolver will automatically untaint results, and might also ignore certain environment variables.
- $resolver->parse_resolv_conf ($string)
- Parses the given string as if it were a resolv.conf
file. The following directives are supported (but not necessarily
implemented).
- $resolver->os_config
- Tries so load and parse /etc/resolv.conf on portable
operating systems. Tries various egregious hacks on windows to force the
DNS servers and searchlist out of the system.
- $resolver->timeout ($timeout, ...)
- Sets the timeout values. See the "timeout" constructor argument (and note that this method expects the timeout values themselves, not an array-reference).
- $resolver->max_outstanding ($nrequests)
- Sets the maximum number of outstanding requests to $nrequests. See the "max_outstanding" constructor argument.
- $resolver->request ($req, $cb->($res))
- This is the main low-level workhorse for sending DNS
requests.
- $resolver->resolve ($qname, $qtype, %options, $cb->(@rr))
- Queries the DNS for the given domain name $qname of type
$qtype.
- search => [$suffix...]
- Use the given search list (which might be empty), by appending each one in turn to the $qname. If this option is missing then the configured "ndots" and "search" values define its value (depending on "ndots", the empty suffix will be prepended or appended to that "search" value). If the $qname ends in a dot, then the searchlist will be ignored.
- accept => [$type...]
- Lists the acceptable result types: only result types in this set will be accepted and returned. The default includes the $qtype and nothing else. If this list includes "cname", then CNAME-chains will not be followed (because you asked for the CNAME record).
- class => "class"
- Specify the query class ("in" for internet, "ch" for chaosnet and "hs" for hesiod are the only ones making sense). The default is "in", of course.
# full example, you can paste this into perl: use Data::Dumper; use AnyEvent::DNS; AnyEvent::DNS::resolver->resolve ( "google.com", "*", my $cv = AnyEvent->condvar); warn Dumper [$cv->recv]; # shortened result: # [ # [ 'google.com', 'soa', 'in', 3600, 'ns1.google.com', 'dns-admin.google.com', # 2008052701, 7200, 1800, 1209600, 300 ], # [ # 'google.com', 'txt', 'in', 3600, # 'v=spf1 include:_netblocks.google.com ~all' # ], # [ 'google.com', 'a', 'in', 3600, '64.233.187.99' ], # [ 'google.com', 'mx', 'in', 3600, 10, 'smtp2.google.com' ], # [ 'google.com', 'ns', 'in', 3600, 'ns2.google.com' ], # ] # resolve a records: $res->resolve ("ruth.plan9.de", "a", sub { warn Dumper [@_] }); # result: # [ # [ 'ruth.schmorp.de', 'a', 'in', 86400, '129.13.162.95' ] # ] # resolve any records, but return only a and aaaa records: $res->resolve ("test1.laendle", "*", accept => ["a", "aaaa"], sub { warn Dumper [@_]; } ); # result: # [ # [ 'test1.laendle', 'a', 'in', 86400, '10.0.0.255' ], # [ 'test1.laendle', 'aaaa', 'in', 60, '3ffe:1900:4545:0002:0240:0000:0000:f7e1' ] # ]
- $resolver->wait_for_slot ($cb->($resolver))
- Wait until a free request slot is available and call the
callback with the resolver object.
AUTHOR¶
Marc Lehmann <schmorp@schmorp.de> http://anyevent.schmorp.de
2012-04-13 | perl v5.14.2 |