1Net::DNS(3) User Contributed Perl Documentation Net::DNS(3)
2
3
4
6 Net::DNS - Perl Interface to the Domain Name System
7
9 use Net::DNS;
10
12 Net::DNS is a collection of Perl modules that act as a Domain Name
13 System (DNS) resolver. It allows the programmer to perform DNS queries
14 that are beyond the capabilities of "gethostbyname" and
15 "gethostbyaddr".
16
17 The programmer should be somewhat familiar with the format of a DNS
18 packet and its various sections. See RFC 1035 or DNS and BIND (Albitz &
19 Liu) for details.
20
21 Resolver Objects
22 A resolver object is an instance of the Net::DNS::Resolver class. A
23 program can have multiple resolver objects, each maintaining its own
24 state information such as the nameservers to be queried, whether
25 recursion is desired, etc.
26
27 Packet Objects
28 Net::DNS::Resolver queries return Net::DNS::Packet objects. Packet
29 objects have five sections:
30
31 · The header section, a Net::DNS::Header object.
32
33 · The question section, a list of Net::DNS::Question objects.
34
35 · The answer section, a list of Net::DNS::RR objects.
36
37 · The authority section, a list of Net::DNS::RR objects.
38
39 · The additional section, a list of Net::DNS::RR objects.
40
41 Update Objects
42 Net::DNS::Update is a subclass of Net::DNS::Packet used to create
43 dynamic update requests.
44
45 Header Objects
46 Net::DNS::Header objects represent the header section of a DNS packet.
47
48 Question Objects
49 Net::DNS::Question objects represent the content of the question
50 section of a DNS packet.
51
52 RR Objects
53 Net::DNS::RR is the base class for DNS resource record (RR) objects in
54 the answer, authority, and additional sections of a DNS packet.
55
56 Do not assume that RR objects will be of the type requested. The type
57 of an RR object must be checked before calling any methods.
58
60 See the manual pages listed above for other class-specific methods.
61
62 version
63 print Net::DNS->version, "\n";
64
65 Returns the version of Net::DNS.
66
67 rr
68 # Use a default resolver -- can not get an error string this way.
69 use Net::DNS;
70 my @rr = rr("example.com");
71 my @rr = rr("example.com", "A");
72 my @rr = rr("example.com", "A", "IN");
73
74 # Use your own resolver object.
75 my $res = Net::DNS::Resolver->new;
76 my @rr = rr($res, "example.com" ... );
77
78 my ($ptr) = rr("192.0.2.1");
79
80 The "rr()" method provides simple RR lookup for scenarios where the
81 full flexibility of Net::DNS is not required.
82
83 Returns a list of Net::DNS::RR objects for the specified name or an
84 empty list if the query failed or no record was found.
85
86 See "EXAMPLES" for more complete examples.
87
88 mx
89 # Use a default resolver -- can not get an error string this way.
90 use Net::DNS;
91 my @mx = mx("example.com");
92
93 # Use your own resolver object.
94 my $res = Net::DNS::Resolver->new;
95 my @mx = mx($res, "example.com");
96
97 Returns a list of Net::DNS::RR::MX objects representing the MX records
98 for the specified name. The list will be sorted by preference.
99 Returns an empty list if the query failed or no MX record was found.
100
101 This method does not look up A records; it only performs MX queries.
102
104 The Net::DNS module provides auxiliary functions which support dynamic
105 DNS update requests.
106
107 yxrrset
108 Use this method to add an "RRset exists" prerequisite to a dynamic
109 update packet. There are two forms, value-independent and value-
110 dependent:
111
112 # RRset exists (value-independent)
113 $update->push(pre => yxrrset("host.example.com A"));
114
115 Meaning: At least one RR with the specified name and type must exist.
116
117 # RRset exists (value-dependent)
118 $update->push(pre => yxrrset("host.example.com A 10.1.2.3"));
119
120 Meaning: At least one RR with the specified name and type must exist
121 and must have matching data.
122
123 Returns a Net::DNS::RR object or "undef" if the object could not be
124 created.
125
126 nxrrset
127 Use this method to add an "RRset does not exist" prerequisite to a
128 dynamic update packet.
129
130 $update->push(pre => nxrrset("host.example.com A"));
131
132 Meaning: No RRs with the specified name and type can exist.
133
134 Returns a Net::DNS::RR object or "undef" if the object could not be
135 created.
136
137 yxdomain
138 Use this method to add a "name is in use" prerequisite to a dynamic
139 update packet.
140
141 $update->push(pre => yxdomain("host.example.com"));
142
143 Meaning: At least one RR with the specified name must exist.
144
145 Returns a Net::DNS::RR object or "undef" if the object could not be
146 created.
147
148 nxdomain
149 Use this method to add a "name is not in use" prerequisite to a dynamic
150 update packet.
151
152 $update->push(pre => nxdomain("host.example.com"));
153
154 Meaning: No RR with the specified name can exist.
155
156 Returns a Net::DNS::RR object or "undef" if the object could not be
157 created.
158
159 rr_add
160 Use this method to add RRs to a zone.
161
162 $update->push(update => rr_add("host.example.com A 10.1.2.3"));
163
164 Meaning: Add this RR to the zone.
165
166 RR objects created by this method should be added to the "update"
167 section of a dynamic update packet. The TTL defaults to 86400 seconds
168 (24 hours) if not specified.
169
170 Returns a Net::DNS::RR object or "undef" if the object could not be
171 created.
172
173 rr_del
174 Use this method to delete RRs from a zone. There are three forms:
175 delete all RRsets, delete an RRset, and delete a specific RR.
176
177 # Delete all RRsets.
178 $update->push(update => rr_del("host.example.com"));
179
180 Meaning: Delete all RRs having the specified name.
181
182 # Delete an RRset.
183 $update->push(update => rr_del("host.example.com A"));
184
185 Meaning: Delete all RRs having the specified name and type.
186
187 # Delete a specific RR.
188 $update->push(update => rr_del("host.example.com A 10.1.2.3"));
189
190 Meaning: Delete all RRs having the specified name, type, and data.
191
192 RR objects created by this method should be added to the "update"
193 section of a dynamic update packet.
194
195 Returns a Net::DNS::RR object or "undef" if the object could not be
196 created.
197
199 The Net::DNS module provides auxiliary functions which support policy-
200 driven zone serial numbering regimes.
201
202 SEQUENTIAL
203 $successor = $soa->serial( SEQUENTIAL );
204
205 The existing serial number is incremented modulo 2**32.
206
207 UNIXTIME
208 $successor = $soa->serial( UNIXTIME );
209
210 The Unix time scale will be used as the basis for zone serial
211 numbering. The serial number will be incremented if the time elapsed
212 since the previous update is less than one second.
213
214 YYYYMMDDxx
215 $successor = $soa->serial( YYYYMMDDxx );
216
217 The 32 bit value returned by the auxiliary "YYYYMMDDxx()" function will
218 be used as the base for the date-coded zone serial number. Serial
219 number increments must be limited to 100 per day for the date
220 information to remain useful.
221
223 "rrsort()" provides functionality to help you sort RR arrays. In most
224 cases this will give you the answer that you want, but you can specify
225 your own sorting method by using the
226 "Net::DNS::RR::FOO->set_rrsort_func()" class method. See Net::DNS::RR
227 for details.
228
229 rrsort
230 use Net::DNS;
231
232 my @sorted = rrsort( $rrtype, $attribute, @rr_array );
233
234 "rrsort()" selects all RRs from the input array that are of the type
235 defined by the first argument. Those RRs are sorted based on the
236 attribute that is specified as second argument.
237
238 There are a number of RRs for which the sorting function is defined in
239 the code.
240
241 For instance:
242
243 my @prioritysorted = rrsort( "SRV", "priority", @rr_array );
244
245 returns the SRV records sorted from lowest to highest priority and for
246 equal priorities from highest to lowest weight.
247
248 If the function does not exist then a numerical sort on the attribute
249 value is performed.
250
251 my @portsorted = rrsort( "SRV", "port", @rr_array );
252
253 If the attribute is not defined then either the "default_sort()"
254 function or "canonical sorting" (as defined by DNSSEC) will be used.
255
256 "rrsort()" returns a sorted array containing only elements of the
257 specified RR type. Any other RR types are silently discarded.
258
259 "rrsort()" returns an empty list when arguments are incorrect.
260
262 The following brief examples illustrate some of the features of
263 Net::DNS. The documentation for individual modules and the demo
264 scripts included with the distribution provide more extensive examples.
265
266 See Net::DNS::Update for an example of performing dynamic updates.
267
268 Look up host addresses.
269 use Net::DNS;
270 my $res = Net::DNS::Resolver->new;
271 my $reply = $res->search("www.example.com", "A");
272
273 if ($reply) {
274 foreach my $rr ($reply->answer) {
275 print $rr->address, "\n" if $rr->can("address");
276 }
277 } else {
278 warn "query failed: ", $res->errorstring, "\n";
279 }
280
281 Find the nameservers for a domain.
282 use Net::DNS;
283 my $res = Net::DNS::Resolver->new;
284 my $reply = $res->query("example.com", "NS");
285
286 if ($reply) {
287 foreach $rr (grep { $_->type eq "NS" } $reply->answer) {
288 print $rr->nsdname, "\n";
289 }
290 } else {
291 warn "query failed: ", $res->errorstring, "\n";
292 }
293
294 Find the MX records for a domain.
295 use Net::DNS;
296 my $name = "example.com";
297 my $res = Net::DNS::Resolver->new;
298 my @mx = mx($res, $name);
299
300 if (@mx) {
301 foreach $rr (@mx) {
302 print $rr->preference, "\t", $rr->exchange, "\n";
303 }
304 } else {
305 warn "Can not find MX records for $name: ", $res->errorstring, "\n";
306 }
307
308 Print domain SOA record in zone file format.
309 use Net::DNS;
310 my $res = Net::DNS::Resolver->new;
311 my $reply = $res->query("example.com", "SOA");
312
313 if ($reply) {
314 foreach my $rr ($reply->answer) {
315 $rr->print;
316 }
317 } else {
318 warn "query failed: ", $res->errorstring, "\n";
319 }
320
321 Perform a zone transfer and print all the records.
322 use Net::DNS;
323 my $res = Net::DNS::Resolver->new;
324 $res->tcp_timeout(20);
325 $res->nameservers("ns.example.com");
326
327 my @zone = $res->axfr("example.com");
328
329 foreach $rr (@zone) {
330 $rr->print;
331 }
332
333 warn $res->errorstring if $res->errorstring;
334
335 Perform a background query and print the reply.
336 use Net::DNS;
337 my $res = Net::DNS::Resolver->new;
338 $res->udp_timeout(10);
339 $res->tcp_timeout(20);
340 my $socket = $res->bgsend("host.example.com");
341
342 while ( $res->bgbusy($socket) ) {
343 # do some work here while waiting for the answer
344 # ...and some more here
345 }
346
347 my $packet = $res->bgread($socket);
348 if ($packet) {
349 $packet->print;
350 } else {
351 warn "query failed: ", $res->errorstring, "\n";
352 }
353
355 Net::DNS is slow.
356
357 For other items to be fixed, or if you discover a bug in this
358 distribution please use the CPAN bug reporting system.
359
361 Copyright (c)1997-2000 Michael Fuhr.
362
363 Portions Copyright (c)2002,2003 Chris Reinhardt.
364
365 Portions Copyright (c)2005 Olaf Kolkman (RIPE NCC)
366
367 Portions Copyright (c)2006 Olaf Kolkman (NLnet Labs)
368
369 Portions Copyright (c)2014 Dick Franks
370
371 All rights reserved.
372
374 Permission to use, copy, modify, and distribute this software and its
375 documentation for any purpose and without fee is hereby granted,
376 provided that the above copyright notice appear in all copies and that
377 both that copyright notice and this permission notice appear in
378 supporting documentation, and that the name of the author not be used
379 in advertising or publicity pertaining to distribution of the software
380 without specific prior written permission.
381
382 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
383 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
384 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
385 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
386 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
387 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
388 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
389
391 Net::DNS is maintained at NLnet Labs (www.nlnetlabs.nl) by Willem
392 Toorop.
393
394 Between 2005 and 2012 Net::DNS was maintained by Olaf Kolkman.
395
396 Between 2002 and 2004 Net::DNS was maintained by Chris Reinhardt.
397
398 Net::DNS was created in 1997 by Michael Fuhr.
399
401 perl, Net::DNS::Resolver, Net::DNS::Question, Net::DNS::RR,
402 Net::DNS::Packet, Net::DNS::Update, RFC1035, <http://www.net-dns.org/>,
403 DNS and BIND by Paul Albitz & Cricket Liu
404
405
406
407perl v5.26.3 2018-02-09 Net::DNS(3)