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