1Net::DNS::Resolver(3) User Contributed Perl DocumentationNet::DNS::Resolver(3)
2
3
4

NAME

6       Net::DNS::Resolver - DNS resolver class
7

SYNOPSIS

9           use Net::DNS;
10
11           $resolver = new Net::DNS::Resolver();
12
13           # Perform a lookup, using the searchlist if appropriate.
14           $reply = $resolver->search( 'example.com' );
15
16           # Perform a lookup, without the searchlist
17           $reply = $resolver->query( 'example.com', 'MX' );
18
19           # Perform a lookup, without pre or post-processing
20           $reply = $resolver->send( 'example.com', 'MX', 'IN' );
21
22           # Send a prebuilt query packet
23           $query = new Net::DNS::Packet( ... );
24           $reply = $resolver->send( $query );
25

DESCRIPTION

27       Instances of the Net::DNS::Resolver class represent resolver objects.
28       A program may have multiple resolver objects, each maintaining its own
29       state information such as the nameservers to be queried, whether
30       recursion is desired, etc.
31

METHODS

33   new
34           # Use the default configuration
35           $resolver = new Net::DNS::Resolver();
36
37           # Use my own configuration file
38           $resolver = new Net::DNS::Resolver( config_file => '/my/dns.conf' );
39
40           # Set options in the constructor
41           $resolver = new Net::DNS::Resolver(
42               nameservers => [ '2001:DB8::1', 'ns.example.com' ],
43               recurse     => 0,
44               debug       => 1
45               );
46
47       Returns a resolver object.  If no arguments are supplied, "new()"
48       returns an object having the default configuration.
49
50       On Unix and Linux systems, the default values are read from the
51       following files, in the order indicated:
52
53           /etc/resolv.conf, $HOME/.resolv.conf, ./.resolv.conf
54
55       The following keywords are recognised in resolver configuration files:
56
57           nameserver address
58
59           IP address of a name server that the resolver should query.
60
61           domain localdomain
62
63           The domain suffix to be appended to a short non-absolute name.
64
65           search domain ...
66
67           A space-separated list of domains in the desired search path.
68
69           options option:value ...
70
71           A space-separated list of key:value items.
72
73       Except for /etc/resolv.conf, files will only be read if owned by the
74       effective userid running the program.  In addition, several environment
75       variables may contain configuration information; see "ENVIRONMENT".
76
77       Note that the domain and searchlist keywords are mutually exclusive.
78       If both are present, the resulting behaviour is unspecified.  If
79       neither is present, the domain is determined from the local hostname.
80
81       On Windows systems, an attempt is made to determine the system defaults
82       using the registry.  Systems with many dynamically configured network
83       interfaces may confuse Net::DNS.
84
85           # Use my own configuration file
86           $resolver = new Net::DNS::Resolver( config_file => '/my/dns.conf' );
87
88       You can include a configuration file of your own when creating a
89       resolver object.  This is supported on both Unix and Windows.
90
91       If a custom configuration file is specified at first instantiation, all
92       other configuration files and environment variables are ignored.
93
94           # Set options in the constructor
95           $resolver = new Net::DNS::Resolver(
96               nameservers => [ '2001:DB8::1', 'ns.example.com' ],
97               recurse     => 0
98               );
99
100       Explicit arguments to "new()" override the corresponding configuration
101       variables.  The argument list consists of a sequence of (name=>value)
102       pairs, each interpreted as an invocation of the corresponding method.
103
104   print
105           $resolver->print;
106
107       Prints the resolver state on the standard output.
108
109   query
110           $packet = $resolver->query( 'mailhost' );
111           $packet = $resolver->query( 'mailhost.example.com' );
112           $packet = $resolver->query( '2001:DB8::1' );
113           $packet = $resolver->query( 'example.com', 'MX' );
114           $packet = $resolver->query( 'annotation.example.com', 'TXT', 'IN' );
115
116       Performs a DNS query for the given name; the search list is not
117       applied.  If "defnames" is true, the default domain will be appended to
118       unqualified names.
119
120       The record type and class can be omitted; they default to A and IN.  If
121       the name looks like an IP address (IPv4 or IPv6), then a query within
122       in-addr.arpa or ip6.arpa will be performed.
123
124       Returns a Net::DNS::Packet object, or "undef" if no answers were found.
125       The reason for failure may be determined using "errorstring()".
126
127       If you need to examine the response packet, whether it contains any
128       answers or not, use the "send()" method instead.
129
130   search
131           $packet = $resolver->search( 'mailhost' );
132           $packet = $resolver->search( 'mailhost.example.com' );
133           $packet = $resolver->search( '2001:DB8::1' );
134           $packet = $resolver->search( 'example.com', 'MX' );
135           $packet = $resolver->search( 'annotation.example.com', 'TXT', 'IN' );
136
137       Performs a DNS query for the given name, applying the searchlist if
138       appropriate.  The search algorithm is as follows:
139
140       If the name contains one or more non-terminal dots, perform an initial
141       query using the unmodified name.
142
143       If the number of dots is less than "ndots", and there is no terminal
144       dot, try appending each suffix in the search list.
145
146       The record type and class can be omitted; they default to A and IN.  If
147       the name looks like an IP address (IPv4 or IPv6), then a query within
148       in-addr.arpa or ip6.arpa will be performed.
149
150       Returns a Net::DNS::Packet object, or "undef" if no answers were found.
151       The reason for failure may be determined using "errorstring()".
152
153       If you need to examine the response packet, whether it contains any
154       answers or not, use the "send()" method instead.
155
156   send
157           $packet = $resolver->send( $query );
158
159           $packet = $resolver->send( 'mailhost.example.com' );
160           $packet = $resolver->send( '2001:DB8::1' );
161           $packet = $resolver->send( 'example.com', 'MX' );
162           $packet = $resolver->send( 'annotation.example.com', 'TXT', 'IN' );
163
164       Performs a DNS query for the given name.  Neither the searchlist nor
165       the default domain will be appended.
166
167       The argument list can be either a pre-built query Net::DNS::Packet or a
168       list of strings.  The record type and class can be omitted; they
169       default to A and IN.  If the name looks like an IP address (IPv4 or
170       IPv6), then a query within in-addr.arpa or ip6.arpa will be performed.
171
172       Returns a Net::DNS::Packet object whether there were any answers or
173       not.  Use "$packet->header->ancount" or "$packet->answer" to find out
174       if there were any records in the answer section.  Returns "undef" if no
175       response was received.
176
177   axfr
178           @zone = $resolver->axfr();
179           @zone = $resolver->axfr( 'example.com' );
180           @zone = $resolver->axfr( 'example.com', 'IN' );
181
182           $iterator = $resolver->axfr();
183           $iterator = $resolver->axfr( 'example.com' );
184           $iterator = $resolver->axfr( 'example.com', 'IN' );
185
186           $rr = $iterator->();
187
188       Performs a zone transfer using the resolver nameservers list, attempted
189       in the order listed.
190
191       If the zone is omitted, it defaults to the first zone listed in the
192       resolver search list.
193
194       If the class is omitted, it defaults to IN.
195
196       When called in list context, "axfr()" returns a list of Net::DNS::RR
197       objects.  The redundant SOA record that terminates the zone transfer is
198       not returned to the caller.
199
200       In deferrence to RFC1035(6.3), a complete zone transfer is expected to
201       return all records in the zone or nothing at all.  When no resource
202       records are returned by "axfr()", the reason for failure may be
203       determined using "errorstring()".
204
205       Here is an example that uses a timeout and TSIG verification:
206
207           $resolver->tcp_timeout( 10 );
208           $resolver->tsig( 'Khmac-sha1.example.+161+24053.private' );
209           @zone = $resolver->axfr( 'example.com' );
210
211           foreach $rr (@zone) {
212               $rr->print;
213           }
214
215       When called in scalar context, "axfr()" returns an iterator object.
216       Each invocation of the iterator returns a single Net::DNS::RR or
217       "undef" when the zone is exhausted.
218
219       An exception is raised if the zone transfer can not be completed.
220
221       The redundant SOA record that terminates the zone transfer is not
222       returned to the caller.
223
224       Here is the example above, implemented using an iterator:
225
226           $resolver->tcp_timeout( 10 );
227           $resolver->tsig( 'Khmac-sha1.example.+161+24053.private' );
228           $iterator = $resolver->axfr( 'example.com' );
229
230           while ( $rr = $iterator->() ) {
231               $rr->print;
232           }
233
234   bgsend
235           $handle = $resolver->bgsend( $packet ) || die $resolver->errorstring;
236
237           $handle = $resolver->bgsend( 'mailhost.example.com' );
238           $handle = $resolver->bgsend( '2001:DB8::1' );
239           $handle = $resolver->bgsend( 'example.com', 'MX' );
240           $handle = $resolver->bgsend( 'annotation.example.com', 'TXT', 'IN' );
241
242       Performs a background DNS query for the given name and returns
243       immediately without waiting for the response. The program can then
244       perform other tasks while awaiting the response from the nameserver.
245
246       The argument list can be either a Net::DNS::Packet object or a list of
247       strings.  The record type and class can be omitted; they default to A
248       and IN.  If the name looks like an IP address (IPv4 or IPv6), then a
249       query within in-addr.arpa or ip6.arpa will be performed.
250
251       Returns an opaque handle which is passed to subsequent invocations of
252       the "bgbusy()" and "bgread()" methods.  Errors are indicated by
253       returning "undef" in which case the reason for failure may be
254       determined using "errorstring()".
255
256       The response Net::DNS::Packet object is obtained by calling "bgread()".
257
258       BEWARE: Programs should make no assumptions about the nature of the
259       handles returned by "bgsend()" which should be used strictly as
260       described here.
261
262   bgread
263           $handle = $resolver->bgsend( 'www.example.com' );
264           $packet = $resolver->bgread($handle);
265
266       Reads the response following a background query.  The argument is the
267       handle returned by "bgsend()".
268
269       Returns a Net::DNS::Packet object or "undef" if no response was
270       received before the timeout interval expired.
271
272   bgbusy
273           $handle = $resolver->bgsend( 'foo.example.com' );
274
275           while ($resolver->bgbusy($handle)) {
276               ...
277           }
278
279           $packet = $resolver->bgread($handle);
280
281       Returns true while awaiting the response or for the transaction to time
282       out.  The argument is the handle returned by "bgsend()".
283
284       Truncated UDP packets will be retried transparently using TCP while
285       continuing to assert busy to the caller.
286
287   bgisready
288           until ($resolver->bgisready($handle)) {
289               ...
290           }
291
292       "bgisready()" is the logical complement of "bgbusy()" which is retained
293       for backward compatibility.
294
295   debug
296           print 'debug flag: ', $resolver->debug, "\n";
297           $resolver->debug(1);
298
299       Get or set the debug flag.  If set, calls to "search()", "query()", and
300       "send()" will print debugging information on the standard output.  The
301       default is false.
302
303   defnames
304           print 'defnames flag: ', $resolver->defnames, "\n";
305           $resolver->defnames(0);
306
307       Get or set the defnames flag.  If true, calls to "query()" will append
308       the default domain to resolve names that are not fully qualified.  The
309       default is true.
310
311   dnsrch
312           print 'dnsrch flag: ', $resolver->dnsrch, "\n";
313           $resolver->dnsrch(0);
314
315       Get or set the dnsrch flag.  If true, calls to "search()" will apply
316       the search list to resolve names that are not fully qualified.  The
317       default is true.
318
319   domain
320           $domain = $resolver->domain;
321           $resolver->domain( 'domain.example' );
322
323       Gets or sets the resolver default domain.
324
325   igntc
326           print 'igntc flag: ', $resolver->igntc, "\n";
327           $resolver->igntc(1);
328
329       Get or set the igntc flag.  If true, truncated packets will be ignored.
330       If false, the query will be retried using TCP.  The default is false.
331
332   nameserver, nameservers
333           @nameservers = $resolver->nameservers();
334           $resolver->nameservers( '2001:DB8::1', '192.0.2.1' );
335           $resolver->nameservers( 'ns.domain.example.' );
336
337       Gets or sets the nameservers to be queried.
338
339       Also see the IPv6 transport notes below
340
341   persistent_tcp
342           print 'Persistent TCP flag: ', $resolver->persistent_tcp, "\n";
343           $resolver->persistent_tcp(1);
344
345       Get or set the persistent TCP setting.  If true, Net::DNS will keep a
346       TCP socket open for each host:port to which it connects.  This is
347       useful if you are using TCP and need to make a lot of queries or
348       updates to the same nameserver.
349
350       The default is false unless you are running a SOCKSified Perl, in which
351       case the default is true.
352
353   persistent_udp
354           print 'Persistent UDP flag: ', $resolver->persistent_udp, "\n";
355           $resolver->persistent_udp(1);
356
357       Get or set the persistent UDP setting.  If true, a Net::DNS resolver
358       will use the same UDP socket for all queries within each address
359       family.
360
361       This avoids the cost of creating and tearing down UDP sockets, but also
362       defeats source port randomisation.
363
364   port
365           print 'sending queries to port ', $resolver->port, "\n";
366           $resolver->port(9732);
367
368       Gets or sets the port to which queries are sent.  Convenient for
369       nameserver testing using a non-standard port.  The default is port 53.
370
371   recurse
372           print 'recursion flag: ', $resolver->recurse, "\n";
373           $resolver->recurse(0);
374
375       Get or set the recursion flag.  If true, this will direct nameservers
376       to perform a recursive query.  The default is true.
377
378   retrans
379           print 'retrans interval: ', $resolver->retrans, "\n";
380           $resolver->retrans(3);
381
382       Get or set the retransmission interval The default is 5 seconds.
383
384   retry
385           print 'number of tries: ', $resolver->retry, "\n";
386           $resolver->retry(2);
387
388       Get or set the number of times to try the query.  The default is 4.
389
390   searchlist
391           @searchlist = $resolver->searchlist;
392           $resolver->searchlist( 'a.example', 'b.example', 'c.example' );
393
394       Gets or sets the resolver search list.
395
396   srcaddr
397           $resolver->srcaddr('2001::DB8::1');
398
399       Sets the source address from which queries are sent.  Convenient for
400       forcing queries from a specific interface on a multi-homed host.  The
401       default is to use any local address.
402
403   srcport
404           $resolver->srcport(5353);
405
406       Sets the port from which queries are sent.  The default is 0, meaning
407       any port.
408
409   tcp_timeout
410           print 'TCP timeout: ', $resolver->tcp_timeout, "\n";
411           $resolver->tcp_timeout(10);
412
413       Get or set the TCP timeout in seconds.  The default is 120 seconds (2
414       minutes).
415
416   udp_timeout
417           print 'UDP timeout: ', $resolver->udp_timeout, "\n";
418           $resolver->udp_timeout(10);
419
420       Get or set the bgsend() UDP timeout in seconds.  The default is 30
421       seconds.
422
423   udppacketsize
424           print "udppacketsize: ", $resolver->udppacketsize, "\n";
425           $resolver->udppacketsize(2048);
426
427       Get or set the UDP packet size.  If set to a value not less than the
428       default DNS packet size, an EDNS extension will be added indicating
429       support for large UDP datagrams.
430
431   usevc
432           print 'usevc flag: ', $resolver->usevc, "\n";
433           $resolver->usevc(1);
434
435       Get or set the usevc flag.  If true, queries will be performed using
436       virtual circuits (TCP) instead of datagrams (UDP).  The default is
437       false.
438
439   replyfrom
440           print 'last response was from: ', $resolver->replyfrom, "\n";
441
442       Returns the IP address from which the most recent packet was received
443       in response to a query.
444
445   errorstring
446           print 'query status: ', $resolver->errorstring, "\n";
447
448       Returns a string containing error information from the most recent DNS
449       protocol interaction.  "errorstring()" is meaningful only when
450       interrogated immediately after the corresponding method call.
451
452   dnssec
453           print "dnssec flag: ", $resolver->dnssec, "\n";
454           $resolver->dnssec(0);
455
456       The dnssec flag causes the resolver to transmit DNSSEC queries and to
457       add a EDNS0 record as required by RFC2671 and RFC3225.  The actions of,
458       and response from, the remote nameserver is determined by the settings
459       of the AD and CD flags.
460
461       Calling the "dnssec()" method with a non-zero value will also set the
462       UDP packet size to the default value of 2048. If that is too small or
463       too big for your environment, you should call the "udppacketsize()"
464       method immediately after.
465
466          $resolver->dnssec(1);                # DNSSEC using default packetsize
467          $resolver->udppacketsize(1250);      # lower the UDP packet size
468
469       A fatal exception will be raised if the "dnssec()" method is called but
470       the Net::DNS::SEC library has not been installed.
471
472   adflag
473           $resolver->dnssec(1);
474           $resolver->adflag(1);
475           print "authentication desired flag: ", $resolver->adflag, "\n";
476
477       Gets or sets the AD bit for dnssec queries.  This bit indicates that
478       the caller is interested in the returned AD (authentic data) bit but
479       does not require any dnssec RRs to be included in the response.  The
480       default value is false.
481
482   cdflag
483           $resolver->dnssec(1);
484           $resolver->cdflag(1);
485           print "checking disabled flag: ", $resolver->cdflag, "\n";
486
487       Gets or sets the CD bit for dnssec queries.  This bit indicates that
488       authentication by upstream nameservers should be suppressed.  Any
489       dnssec RRs required to execute the authentication procedure should be
490       included in the response.  The default value is false.
491
492   tsig
493           $resolver->tsig( $tsig );
494
495           $resolver->tsig( 'Khmac-sha1.example.+161+24053.private' );
496
497           $resolver->tsig( 'Khmac-sha1.example.+161+24053.key' );
498
499           $resolver->tsig( 'Khmac-sha1.example.+161+24053.key',
500                       fudge => 60
501                       );
502
503           $resolver->tsig( $key_name, $key );
504
505           $resolver->tsig( undef );
506
507       Set the TSIG record used to automatically sign outgoing queries, zone
508       transfers and updates. Automatic signing is disabled if called with
509       undefined arguments.
510
511       The default resolver behaviour is not to sign any packets.  You must
512       call this method to set the key if you would like the resolver to sign
513       and verify packets automatically.
514
515       Packets can also be signed manually; see the Net::DNS::Packet and
516       Net::DNS::Update manual pages for examples.  TSIG records in manually-
517       signed packets take precedence over those that the resolver would add
518       automatically.
519

ENVIRONMENT

521       The following environment variables can also be used to configure the
522       resolver:
523
524   RES_NAMESERVERS
525           # Bourne Shell
526           RES_NAMESERVERS="2001:DB8::1 192.0.2.1"
527           export RES_NAMESERVERS
528
529           # C Shell
530           setenv RES_NAMESERVERS "2001:DB8::1 192.0.2.1"
531
532       A space-separated list of nameservers to query.
533
534   RES_SEARCHLIST
535           # Bourne Shell
536           RES_SEARCHLIST="a.example.com b.example.com c.example.com"
537           export RES_SEARCHLIST
538
539           # C Shell
540           setenv RES_SEARCHLIST "a.example.com b.example.com c.example.com"
541
542       A space-separated list of domains to put in the search list.
543
544   LOCALDOMAIN
545           # Bourne Shell
546           LOCALDOMAIN=example.com
547           export LOCALDOMAIN
548
549           # C Shell
550           setenv LOCALDOMAIN example.com
551
552       The default domain.
553
554   RES_OPTIONS
555           # Bourne Shell
556           RES_OPTIONS="retrans:3 retry:2 inet6"
557           export RES_OPTIONS
558
559           # C Shell
560           setenv RES_OPTIONS "retrans:3 retry:2 inet6"
561
562       A space-separated list of resolver options to set.  Options that take
563       values are specified as "option:value".
564

IPv4 TRANSPORT

566       The "force_v4()", "force_v6()", "prefer_v4()", and "prefer_v6()"
567       methods with non-zero argument may be used to configure transport
568       selection.
569
570       The behaviour of the "nameserver()" method illustrates the transport
571       selection mechanism.  If, for example, IPv4 transport has been forced,
572       the "nameserver()" method will only return IPv4 addresses:
573
574           $resolver->nameservers( '192.0.2.1', '192.0.2.2', '2001:DB8::3' );
575           $resolver->force_v4(1);
576           print join ' ', $resolver->nameservers();
577
578       will print
579
580           192.0.2.1 192.0.2.2
581

CUSTOMISED RESOLVERS

583       Net::DNS::Resolver is actually an empty subclass.  At compile time a
584       super class is chosen based on the current platform.  A side benefit of
585       this allows for easy modification of the methods in Net::DNS::Resolver.
586       You can simply add a method to the namespace!
587
588       For example, if we wanted to cache lookups:
589
590           package Net::DNS::Resolver;
591
592           my %cache;
593
594           sub search {
595               $self = shift;
596
597               $cache{"@_"} ||= $self->SUPER::search(@_);
598           }
599
601       Copyright (c)1997-2000 Michael Fuhr.
602
603       Portions Copyright (c)2002-2004 Chris Reinhardt.
604
605       Portions Copyright (c)2005 Olaf M. Kolkman, NLnet Labs.
606
607       Portions Copyright (c)2014,2015 Dick Franks.
608
609       All rights reserved.
610

LICENSE

612       Permission to use, copy, modify, and distribute this software and its
613       documentation for any purpose and without fee is hereby granted,
614       provided that the above copyright notice appear in all copies and that
615       both that copyright notice and this permission notice appear in
616       supporting documentation, and that the name of the author not be used
617       in advertising or publicity pertaining to distribution of the software
618       without specific prior written permission.
619
620       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
621       OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
622       MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
623       IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
624       CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
625       TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
626       SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
627

SEE ALSO

629       perl, Net::DNS, Net::DNS::Packet, Net::DNS::Update, Net::DNS::Header,
630       Net::DNS::Question, Net::DNS::RR, resolver(5), RFC 1034, RFC 1035
631
632
633
634perl v5.30.0                      2019-07-26             Net::DNS::Resolver(3)
Impressum