1Net::DNS::Resolver(3) User Contributed Perl DocumentationNet::DNS::Resolver(3)
2
3
4
6 Net::DNS::Resolver - DNS resolver class
7
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
27 Instances of the Net::DNS::Resolver class represent resolver objects.
28 A program can 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
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 => [ '10.1.1.128', '10.1.2.128' ],
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 => [ '10.1.1.128', '10.1.2.128' ],
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( '192.0.2.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, and the number of dots is less than
118 "ndots", the default domain will be appended unless name is absolute.
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( '192.0.2.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 Unless the number of dots is less than "ndots", perform an initial
141 query using the unmodified name.
142
143 If "dnsrch" is true and the name has no terminal dot, try appending
144 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->query( '192.0.2.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( '192.0.2.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 answer from a background query. The argument is the handle
267 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( '192.0.2.1', '192.0.2.2', '2001:DB8::3' );
335
336 Gets or sets the nameservers to be queried.
337
338 Also see the IPv6 transport notes below
339
340 persistent_tcp
341 print 'Persistent TCP flag: ', $resolver->persistent_tcp, "\n";
342 $resolver->persistent_tcp(1);
343
344 Get or set the persistent TCP setting. If true, Net::DNS will keep a
345 TCP socket open for each host:port to which it connects. This is
346 useful if you are using TCP and need to make a lot of queries or
347 updates to the same nameserver.
348
349 The default is false unless you are running a SOCKSified Perl, in which
350 case the default is true.
351
352 persistent_udp
353 print 'Persistent UDP flag: ', $resolver->persistent_udp, "\n";
354 $resolver->persistent_udp(1);
355
356 Get or set the persistent UDP setting. If true, a Net::DNS resolver
357 will use the same UDP socket for all queries within each address
358 family.
359
360 This avoids the cost of creating and tearing down UDP sockets, but also
361 defeats source port randomisation.
362
363 port
364 print 'sending queries to port ', $resolver->port, "\n";
365 $resolver->port(9732);
366
367 Gets or sets the port to which queries are sent. Convenient for
368 nameserver testing using a non-standard port. The default is port 53.
369
370 recurse
371 print 'recursion flag: ', $resolver->recurse, "\n";
372 $resolver->recurse(0);
373
374 Get or set the recursion flag. If true, this will direct nameservers
375 to perform a recursive query. The default is true.
376
377 retrans
378 print 'retrans interval: ', $resolver->retrans, "\n";
379 $resolver->retrans(3);
380
381 Get or set the retransmission interval The default is 5 seconds.
382
383 retry
384 print 'number of tries: ', $resolver->retry, "\n";
385 $resolver->retry(2);
386
387 Get or set the number of times to try the query. The default is 4.
388
389 searchlist
390 @searchlist = $resolver->searchlist;
391 $resolver->searchlist( 'a.example', 'b.example', 'c.example' );
392
393 Gets or sets the resolver search list.
394
395 srcaddr
396 $resolver->srcaddr('192.0.2.1');
397
398 Sets the source address from which queries are sent. Convenient for
399 forcing queries from a specific interface on a multi-homed host. The
400 default is to use any local address.
401
402 srcport
403 $resolver->srcport(5353);
404
405 Sets the port from which queries are sent. The default is 0, meaning
406 any port.
407
408 tcp_timeout
409 print 'TCP timeout: ', $resolver->tcp_timeout, "\n";
410 $resolver->tcp_timeout(10);
411
412 Get or set the TCP timeout in seconds. The default is 120 seconds (2
413 minutes).
414
415 udp_timeout
416 print 'UDP timeout: ', $resolver->udp_timeout, "\n";
417 $resolver->udp_timeout(10);
418
419 Get or set the bgsend() UDP timeout in seconds. The default is 30
420 seconds.
421
422 udppacketsize
423 print "udppacketsize: ", $resolver->udppacketsize, "\n";
424 $resolver->udppacketsize(2048);
425
426 Get or set the UDP packet size. If set to a value not less than the
427 default DNS packet size, an EDNS extension will be added indicating
428 support for large UDP datagrams.
429
430 usevc
431 print 'usevc flag: ', $resolver->usevc, "\n";
432 $resolver->usevc(1);
433
434 Get or set the usevc flag. If true, queries will be performed using
435 virtual circuits (TCP) instead of datagrams (UDP). The default is
436 false.
437
438 answerfrom
439 print 'last answer was from: ', $resolver->answerfrom, "\n";
440
441 Returns the IP address from which the most recent packet was received
442 in response to a query.
443
444 errorstring
445 print 'query status: ', $resolver->errorstring, "\n";
446
447 Returns a string containing error information from the most recent DNS
448 protocol interaction. "errorstring()" is meaningful only when
449 interrogated immediately after the corresponding method call.
450
451 dnssec
452 print "dnssec flag: ", $resolver->dnssec, "\n";
453 $resolver->dnssec(0);
454
455 The dnssec flag causes the resolver to transmit DNSSEC queries and to
456 add a EDNS0 record as required by RFC2671 and RFC3225. The actions of,
457 and response from, the remote nameserver is determined by the settings
458 of the AD and CD flags.
459
460 Calling the "dnssec()" method with a non-zero value will also set the
461 UDP packet size to the default value of 2048. If that is too small or
462 too big for your environment, you should call the "udppacketsize()"
463 method immediately after.
464
465 $resolver->dnssec(1); # DNSSEC using default packetsize
466 $resolver->udppacketsize(1250); # lower the UDP packet size
467
468 A fatal exception will be raised if the "dnssec()" method is called but
469 the Net::DNS::SEC library has not been installed.
470
471 adflag
472 $resolver->dnssec(1);
473 $resolver->adflag(1);
474 print "authentication desired flag: ", $resolver->adflag, "\n";
475
476 Gets or sets the AD bit for dnssec queries. This bit indicates that
477 the caller is interested in the returned AD (authentic data) bit but
478 does not require any dnssec RRs to be included in the response. The
479 default value is false.
480
481 cdflag
482 $resolver->dnssec(1);
483 $resolver->cdflag(1);
484 print "checking disabled flag: ", $resolver->cdflag, "\n";
485
486 Gets or sets the CD bit for dnssec queries. This bit indicates that
487 authentication by upstream nameservers should be suppressed. Any
488 dnssec RRs required to execute the authentication procedure should be
489 included in the response. The default value is false.
490
491 tsig
492 $resolver->tsig( $tsig );
493
494 $resolver->tsig( 'Khmac-sha1.example.+161+24053.private' );
495
496 $resolver->tsig( 'Khmac-sha1.example.+161+24053.key' );
497
498 $resolver->tsig( 'Khmac-sha1.example.+161+24053.key',
499 fudge => 60
500 );
501
502 $resolver->tsig( $key_name, $key );
503
504 $resolver->tsig( undef );
505
506 Set the TSIG record used to automatically sign outgoing queries, zone
507 transfers and updates. Automatic signing is disabled if called with
508 undefined arguments.
509
510 The default resolver behaviour is not to sign any packets. You must
511 call this method to set the key if you would like the resolver to sign
512 and verify packets automatically.
513
514 Packets can also be signed manually; see the Net::DNS::Packet and
515 Net::DNS::Update manual pages for examples. TSIG records in manually-
516 signed packets take precedence over those that the resolver would add
517 automatically.
518
520 The following environment variables can also be used to configure the
521 resolver:
522
523 RES_NAMESERVERS
524 # Bourne Shell
525 RES_NAMESERVERS="192.0.2.1 192.0.2.2 2001:DB8::3"
526 export RES_NAMESERVERS
527
528 # C Shell
529 setenv RES_NAMESERVERS "192.0.2.1 192.0.2.2 2001:DB8::3"
530
531 A space-separated list of nameservers to query.
532
533 RES_SEARCHLIST
534 # Bourne Shell
535 RES_SEARCHLIST="a.example.com b.example.com c.example.com"
536 export RES_SEARCHLIST
537
538 # C Shell
539 setenv RES_SEARCHLIST "a.example.com b.example.com c.example.com"
540
541 A space-separated list of domains to put in the search list.
542
543 LOCALDOMAIN
544 # Bourne Shell
545 LOCALDOMAIN=example.com
546 export LOCALDOMAIN
547
548 # C Shell
549 setenv LOCALDOMAIN example.com
550
551 The default domain.
552
553 RES_OPTIONS
554 # Bourne Shell
555 RES_OPTIONS="retrans:3 retry:2 inet6"
556 export RES_OPTIONS
557
558 # C Shell
559 setenv RES_OPTIONS "retrans:3 retry:2 inet6"
560
561 A space-separated list of resolver options to set. Options that take
562 values are specified as "option:value".
563
565 The Net::DNS::Resolver library will enable IPv6 transport if the
566 IO::Socket::IP library package is available.
567
568 The "force_v4()", "force_v6()", "prefer_v4()", and "prefer_v6()"
569 methods with non-zero argument may be used to configure transport
570 selection.
571
572 The behaviour of the "nameserver()" method illustrates the transport
573 selection mechanism. If, for example, IPv6 is not available or IPv4
574 transport has been forced, the "nameserver()" method will only return
575 IPv4 addresses:
576
577 $resolver->nameservers( '192.0.2.1', '192.0.2.2', '2001:DB8::3' );
578 $resolver->force_v4(1);
579 print join ' ', $resolver->nameservers();
580
581 will print
582
583 192.0.2.1 192.0.2.2
584
586 Net::DNS::Resolver is actually an empty subclass. At compile time a
587 super class is chosen based on the current platform. A side benefit of
588 this allows for easy modification of the methods in Net::DNS::Resolver.
589 You can simply add a method to the namespace!
590
591 For example, if we wanted to cache lookups:
592
593 package Net::DNS::Resolver;
594
595 my %cache;
596
597 sub search {
598 $self = shift;
599
600 $cache{"@_"} ||= $self->SUPER::search(@_);
601 }
602
604 Copyright (c)1997-2000 Michael Fuhr.
605
606 Portions Copyright (c)2002-2004 Chris Reinhardt.
607
608 Portions Copyright (c)2005 Olaf M. Kolkman, NLnet Labs.
609
610 Portions Copyright (c)2014,2015 Dick Franks.
611
612 All rights reserved.
613
615 Permission to use, copy, modify, and distribute this software and its
616 documentation for any purpose and without fee is hereby granted,
617 provided that the above copyright notice appear in all copies and that
618 both that copyright notice and this permission notice appear in
619 supporting documentation, and that the name of the author not be used
620 in advertising or publicity pertaining to distribution of the software
621 without specific prior written permission.
622
623 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
624 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
625 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
626 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
627 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
628 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
629 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
630
632 perl, Net::DNS, Net::DNS::Packet, Net::DNS::Update, Net::DNS::Header,
633 Net::DNS::Question, Net::DNS::RR, resolver(5), RFC 1034, RFC 1035
634
635
636
637perl v5.26.3 2018-02-09 Net::DNS::Resolver(3)