1AnyEvent::DNS(3)      User Contributed Perl Documentation     AnyEvent::DNS(3)
2
3
4

NAME

6       AnyEvent::DNS - fully asynchronous DNS resolution
7

SYNOPSIS

9          use AnyEvent::DNS;
10
11          my $cv = AnyEvent->condvar;
12          AnyEvent::DNS::a "www.google.de", $cv;
13          # ... later
14          my @addrs = $cv->recv;
15

DESCRIPTION

17       This module offers both a number of DNS convenience functions as well
18       as a fully asynchronous and high-performance pure-perl stub resolver.
19
20       The stub resolver supports DNS over IPv4 and IPv6, UDP and TCP,
21       optional EDNS0 support for up to 4kiB datagrams and automatically falls
22       back to virtual circuit mode for large responses.
23
24   CONVENIENCE FUNCTIONS
25       AnyEvent::DNS::a $domain, $cb->(@addrs)
26           Tries to resolve the given domain to IPv4 address(es).
27
28       AnyEvent::DNS::aaaa $domain, $cb->(@addrs)
29           Tries to resolve the given domain to IPv6 address(es).
30
31       AnyEvent::DNS::mx $domain, $cb->(@hostnames)
32           Tries to resolve the given domain into a sorted (lower preference
33           value first) list of domain names.
34
35       AnyEvent::DNS::ns $domain, $cb->(@hostnames)
36           Tries to resolve the given domain name into a list of name servers.
37
38       AnyEvent::DNS::txt $domain, $cb->(@hostnames)
39           Tries to resolve the given domain name into a list of text records.
40           Only the first text string per record will be returned. If you want
41           all strings, you need to call the resolver manually:
42
43              resolver->resolve ($domain => "txt", sub {
44                 for my $record (@_) {
45                    my (undef, undef, undef, @txt) = @$record;
46                    # strings now in @txt
47                 }
48              });
49
50       AnyEvent::DNS::srv $service, $proto, $domain, $cb->(@srv_rr)
51           Tries to resolve the given service, protocol and domain name into a
52           list of service records.
53
54           Each $srv_rr is an array reference with the following contents:
55           "[$priority, $weight, $transport, $target]".
56
57           They will be sorted with lowest priority first, then randomly
58           distributed by weight as per RFC 2782.
59
60           Example:
61
62              AnyEvent::DNS::srv "sip", "udp", "schmorp.de", sub { ...
63              # @_ = ( [10, 10, 5060, "sip1.schmorp.de" ] )
64
65       AnyEvent::DNS::any $domain, $cb->(@rrs)
66           Tries to resolve the given domain and passes all resource records
67           found to the callback.
68
69       AnyEvent::DNS::ptr $domain, $cb->(@hostnames)
70           Tries to make a PTR lookup on the given domain. See
71           "reverse_lookup" and "reverse_verify" if you want to resolve an IP
72           address to a hostname instead.
73
74       AnyEvent::DNS::reverse_lookup $ipv4_or_6, $cb->(@hostnames)
75           Tries to reverse-resolve the given IPv4 or IPv6 address (in textual
76           form) into its hostname(s). Handles V4MAPPED and V4COMPAT IPv6
77           addresses transparently.
78
79       AnyEvent::DNS::reverse_verify $ipv4_or_6, $cb->(@hostnames)
80           The same as "reverse_lookup", but does forward-lookups to verify
81           that the resolved hostnames indeed point to the address, which
82           makes spoofing harder.
83
84           If you want to resolve an address into a hostname, this is the
85           preferred method: The DNS records could still change, but at least
86           this function verified that the hostname, at one point in the past,
87           pointed at the IP address you originally resolved.
88
89           Example:
90
91              AnyEvent::DNS::reverse_verify "2001:500:2f::f", sub { print shift };
92              # => f.root-servers.net
93
94   LOW-LEVEL DNS EN-/DECODING FUNCTIONS
95       $AnyEvent::DNS::EDNS0
96           This variable decides whether dns_pack automatically enables EDNS0
97           support. By default, this is disabled (0), unless overridden by
98           $ENV{PERL_ANYEVENT_EDNS0}, but when set to 1, AnyEvent::DNS will
99           use EDNS0 in all requests.
100
101       $pkt = AnyEvent::DNS::dns_pack $dns
102           Packs a perl data structure into a DNS packet. Reading RFC 1035 is
103           strongly recommended, then everything will be totally clear. Or
104           maybe not.
105
106           Resource records are not yet encodable.
107
108           Examples:
109
110              # very simple request, using lots of default values:
111              { rd => 1, qd => [ [ "host.domain", "a"] ] }
112
113              # more complex example, showing how flags etc. are named:
114
115              {
116                 id => 10000,
117                 op => "query",
118                 rc => "nxdomain",
119
120                 # flags
121                 qr => 1,
122                 aa => 0,
123                 tc => 0,
124                 rd => 0,
125                 ra => 0,
126                 ad => 0,
127                 cd => 0,
128
129                 qd => [@rr], # query section
130                 an => [@rr], # answer section
131                 ns => [@rr], # authority section
132                 ar => [@rr], # additional records section
133              }
134
135       $dns = AnyEvent::DNS::dns_unpack $pkt
136           Unpacks a DNS packet into a perl data structure.
137
138           Examples:
139
140              # an unsuccessful reply
141              {
142                'qd' => [
143                          [ 'ruth.plan9.de.mach.uni-karlsruhe.de', '*', 'in' ]
144                        ],
145                'rc' => 'nxdomain',
146                'ar' => [],
147                'ns' => [
148                          [
149                            'uni-karlsruhe.de',
150                            'soa',
151                            'in',
152                            600,
153                            'netserv.rz.uni-karlsruhe.de',
154                            'hostmaster.rz.uni-karlsruhe.de',
155                            2008052201, 10800, 1800, 2592000, 86400
156                          ]
157                        ],
158                'tc' => '',
159                'ra' => 1,
160                'qr' => 1,
161                'id' => 45915,
162                'aa' => '',
163                'an' => [],
164                'rd' => 1,
165                'op' => 'query'
166              }
167
168              # a successful reply
169
170              {
171                'qd' => [ [ 'www.google.de', 'a', 'in' ] ],
172                'rc' => 0,
173                'ar' => [
174                          [ 'a.l.google.com', 'a', 'in', 3600, '209.85.139.9' ],
175                          [ 'b.l.google.com', 'a', 'in', 3600, '64.233.179.9' ],
176                          [ 'c.l.google.com', 'a', 'in', 3600, '64.233.161.9' ],
177                        ],
178                'ns' => [
179                          [ 'l.google.com', 'ns', 'in', 3600, 'a.l.google.com' ],
180                          [ 'l.google.com', 'ns', 'in', 3600, 'b.l.google.com' ],
181                        ],
182                'tc' => '',
183                'ra' => 1,
184                'qr' => 1,
185                'id' => 64265,
186                'aa' => '',
187                'an' => [
188                          [ 'www.google.de', 'cname', 'in', 3600, 'www.google.com' ],
189                          [ 'www.google.com', 'cname', 'in', 3600, 'www.l.google.com' ],
190                          [ 'www.l.google.com', 'a', 'in', 3600, '66.249.93.104' ],
191                          [ 'www.l.google.com', 'a', 'in', 3600, '66.249.93.147' ],
192                        ],
193                'rd' => 1,
194                'op' => 0
195              }
196
197   THE AnyEvent::DNS RESOLVER CLASS
198       This is the class which does the actual protocol work.
199
200       AnyEvent::DNS::resolver
201           This function creates and returns a resolver that is ready to use
202           and should mimic the default resolver for your system as good as
203           possible. It is used by AnyEvent itself as well.
204
205           It only ever creates one resolver and returns this one on
206           subsequent calls - see $AnyEvent::DNS::RESOLVER, below, for
207           details.
208
209           Unless you have special needs, prefer this function over creating
210           your own resolver object.
211
212           The resolver is created with the following parameters:
213
214              untaint          enabled
215              max_outstanding  $ENV{PERL_ANYEVENT_MAX_OUTSTANDING_DNS} (default 10)
216
217           "os_config" will be used for OS-specific configuration, unless
218           $ENV{PERL_ANYEVENT_RESOLV_CONF} is specified, in which case that
219           file gets parsed.
220
221       $AnyEvent::DNS::RESOLVER
222           This variable stores the default resolver returned by
223           "AnyEvent::DNS::resolver", or "undef" when the default resolver
224           hasn't been instantiated yet.
225
226           One can provide a custom resolver (e.g. one with caching
227           functionality) by storing it in this variable, causing all
228           subsequent resolves done via "AnyEvent::DNS::resolver" to be done
229           via the custom one.
230
231       $resolver = new AnyEvent::DNS key => value...
232           Creates and returns a new resolver.
233
234           The following options are supported:
235
236           server => [...]
237               A list of server addresses (default: "v127.0.0.1" or "::1") in
238               network format (i.e. as returned by
239               "AnyEvent::Socket::parse_address" - both IPv4 and IPv6 are
240               supported).
241
242           timeout => [...]
243               A list of timeouts to use (also determines the number of
244               retries). To make three retries with individual time-outs of 2,
245               5 and 5 seconds, use "[2, 5, 5]", which is also the default.
246
247           search => [...]
248               The default search list of suffixes to append to a domain name
249               (default: none).
250
251           ndots => $integer
252               The number of dots (default: 1) that a name must have so that
253               the resolver tries to resolve the name without any suffixes
254               first.
255
256           max_outstanding => $integer
257               Most name servers do not handle many parallel requests very
258               well. This option limits the number of outstanding requests to
259               $integer (default: 10), that means if you request more than
260               this many requests, then the additional requests will be queued
261               until some other requests have been resolved.
262
263           reuse => $seconds
264               The number of seconds (default: 300) that a query id cannot be
265               re-used after a timeout. If there was no time-out then query
266               ids can be reused immediately.
267
268           untaint => $boolean
269               When true, then the resolver will automatically untaint
270               results, and might also ignore certain environment variables.
271
272       $resolver->parse_resolv_conf ($string)
273           Parses the given string as if it were a resolv.conf file. The
274           following directives are supported (but not necessarily
275           implemented).
276
277           "#"- and ";"-style comments, "nameserver", "domain", "search",
278           "sortlist", "options" ("timeout", "attempts", "ndots").
279
280           Everything else is silently ignored.
281
282       $resolver->os_config
283           Tries so load and parse /etc/resolv.conf on portable operating
284           systems. Tries various egregious hacks on windows to force the DNS
285           servers and searchlist out of the system.
286
287           This method must be called at most once before trying to resolve
288           anything.
289
290       $resolver->timeout ($timeout, ...)
291           Sets the timeout values. See the "timeout" constructor argument
292           (and note that this method expects the timeout values themselves,
293           not an array-reference).
294
295       $resolver->max_outstanding ($nrequests)
296           Sets the maximum number of outstanding requests to $nrequests. See
297           the "max_outstanding" constructor argument.
298
299       $resolver->request ($req, $cb->($res))
300           This is the main low-level workhorse for sending DNS requests.
301
302           This function sends a single request (a hash-ref formated as
303           specified for "dns_pack") to the configured nameservers in turn
304           until it gets a response. It handles timeouts, retries and
305           automatically falls back to virtual circuit mode (TCP) when it
306           receives a truncated reply. It does not handle anything else, such
307           as the domain searchlist or relative names - use "->resolve" for
308           that.
309
310           Calls the callback with the decoded response packet if a reply was
311           received, or no arguments in case none of the servers answered.
312
313       $resolver->resolve ($qname, $qtype, %options, $cb->(@rr))
314           Queries the DNS for the given domain name $qname of type $qtype.
315
316           A $qtype is either a numerical query type (e.g. 1 for A records) or
317           a lowercase name (you have to look at the source to see which
318           aliases are supported, but all types from RFC 1035, "aaaa", "srv",
319           "spf" and a few more are known to this module). A $qtype of "*" is
320           supported and means "any" record type.
321
322           The callback will be invoked with a list of matching result records
323           or none on any error or if the name could not be found.
324
325           CNAME chains (although illegal) are followed up to a length of 10.
326
327           The callback will be invoked with arraryefs of the form "[$name,
328           $type, $class, $ttl, @data"], where $name is the domain name, $type
329           a type string or number, $class a class name, $ttl is the remaining
330           time-to-live and @data is resource-record-dependent data, in
331           seconds. For "a" records, this will be the textual IPv4 addresses,
332           for "ns" or "cname" records this will be a domain name, for "txt"
333           records these are all the strings and so on.
334
335           All types mentioned in RFC 1035, "aaaa", "srv", "naptr" and "spf"
336           are decoded. All resource records not known to this module will
337           have the raw "rdata" field as fifth array element.
338
339           Note that this resolver is just a stub resolver: it requires a name
340           server supporting recursive queries, will not do any recursive
341           queries itself and is not secure when used against an untrusted
342           name server.
343
344           The following options are supported:
345
346           search => [$suffix...]
347               Use the given search list (which might be empty), by appending
348               each one in turn to the $qname. If this option is missing then
349               the configured "ndots" and "search" values define its value
350               (depending on "ndots", the empty suffix will be prepended or
351               appended to that "search" value). If the $qname ends in a dot,
352               then the searchlist will be ignored.
353
354           accept => [$type...]
355               Lists the acceptable result types: only result types in this
356               set will be accepted and returned. The default includes the
357               $qtype and nothing else. If this list includes "cname", then
358               CNAME-chains will not be followed (because you asked for the
359               CNAME record).
360
361           class => "class"
362               Specify the query class ("in" for internet, "ch" for chaosnet
363               and "hs" for hesiod are the only ones making sense). The
364               default is "in", of course.
365
366           Examples:
367
368              # full example, you can paste this into perl:
369              use Data::Dumper;
370              use AnyEvent::DNS;
371              AnyEvent::DNS::resolver->resolve (
372                 "google.com", "*", my $cv = AnyEvent->condvar);
373              warn Dumper [$cv->recv];
374
375              # shortened result:
376              # [
377              #   [ 'google.com', 'soa', 'in', 3600, 'ns1.google.com', 'dns-admin.google.com',
378              #     2008052701, 7200, 1800, 1209600, 300 ],
379              #   [
380              #     'google.com', 'txt', 'in', 3600,
381              #     'v=spf1 include:_netblocks.google.com ~all'
382              #   ],
383              #   [ 'google.com', 'a', 'in', 3600, '64.233.187.99' ],
384              #   [ 'google.com', 'mx', 'in', 3600, 10, 'smtp2.google.com' ],
385              #   [ 'google.com', 'ns', 'in', 3600, 'ns2.google.com' ],
386              # ]
387
388              # resolve a records:
389              $res->resolve ("ruth.plan9.de", "a", sub { warn Dumper [@_] });
390
391              # result:
392              # [
393              #   [ 'ruth.schmorp.de', 'a', 'in', 86400, '129.13.162.95' ]
394              # ]
395
396              # resolve any records, but return only a and aaaa records:
397              $res->resolve ("test1.laendle", "*",
398                 accept => ["a", "aaaa"],
399                 sub {
400                    warn Dumper [@_];
401                 }
402              );
403
404              # result:
405              # [
406              #   [ 'test1.laendle', 'a', 'in', 86400, '10.0.0.255' ],
407              #   [ 'test1.laendle', 'aaaa', 'in', 60, '3ffe:1900:4545:0002:0240:0000:0000:f7e1' ]
408              # ]
409
410       $resolver->wait_for_slot ($cb->($resolver))
411           Wait until a free request slot is available and call the callback
412           with the resolver object.
413
414           A request slot is used each time a request is actually sent to the
415           nameservers: There are never more than "max_outstanding" of them.
416
417           Although you can submit more requests (they will simply be queued
418           until a request slot becomes available), sometimes, usually for
419           rate-limiting purposes, it is useful to instead wait for a slot
420           before generating the request (or simply to know when the request
421           load is low enough so one can submit requests again).
422
423           This is what this method does: The callback will be called when
424           submitting a DNS request will not result in that request being
425           queued. The callback may or may not generate any requests in
426           response.
427
428           Note that the callback will only be invoked when the request queue
429           is empty, so this does not play well if somebody else keeps the
430           request queue full at all times.
431

AUTHOR

433          Marc Lehmann <schmorp@schmorp.de>
434          http://anyevent.schmorp.de
435
436
437
438perl v5.28.0                      2017-06-10                  AnyEvent::DNS(3)
Impressum