PERL   427

DNS.pm

Guest on 11th October 2021 10:09:53 AM

  1. package Net::DNS;
  2.  
  3. #
  4. # $Id: DNS.pm 1610 2017-12-15 12:25:33Z willem $
  5. #
  6. require 5.006;
  7. our $VERSION;
  8. $VERSION = '1.14';
  9. $VERSION = eval $VERSION;
  10. our $SVNVERSION = (qw$LastChangedRevision: 1610 $)[1];
  11.  
  12.  
  13. =head1 NAME
  14.  
  15. Net::DNS - Perl Interface to the Domain Name System
  16.  
  17. =head1 SYNOPSIS
  18.  
  19.     use Net::DNS;
  20.  
  21. =head1 DESCRIPTION
  22.  
  23. Net::DNS is a collection of Perl modules that act as a Domain Name System
  24. (DNS) resolver. It allows the programmer to perform DNS queries that are
  25. beyond the capabilities of "gethostbyname" and "gethostbyaddr".
  26.  
  27. The programmer should be somewhat familiar with the format of a DNS packet
  28. and its various sections. See RFC 1035 or DNS and BIND (Albitz & Liu) for
  29. details.
  30.  
  31. =cut
  32.  
  33.  
  34. use strict;
  35. use warnings;
  36. use integer;
  37.  
  38. use base qw(Exporter);
  39. our @EXPORT = qw(SEQUENTIAL UNIXTIME YYYYMMDDxx
  40.                 yxrrset nxrrset yxdomain nxdomain rr_add rr_del
  41.                 mx rr rrsort);
  42.  
  43.  
  44. local $SIG{__DIE__};
  45. require Net::DNS::Resolver;
  46. require Net::DNS::Packet;
  47. require Net::DNS::RR;
  48. require Net::DNS::Update;
  49.  
  50.  
  51. sub version { $VERSION; }
  52.  
  53.  
  54. #
  55. # rr()
  56. #
  57. # Usage:
  58. #       @rr = rr('example.com');
  59. #       @rr = rr('example.com', 'A', 'IN');
  60. #       @rr = rr($res, 'example.com' ... );
  61. #
  62. sub rr {
  63.         my ($arg1) = @_;
  64.         my $res = ref($arg1) ? shift : new Net::DNS::Resolver();
  65.  
  66.         my $ans = $res->query(@_);
  67.         my @list = $ans ? $ans->answer : ();
  68. }
  69.  
  70.  
  71. #
  72. # mx()
  73. #
  74. # Usage:
  75. #       @mx = mx('example.com');
  76. #       @mx = mx($res, 'example.com');
  77. #
  78. sub mx {
  79.         my ($arg1) = @_;
  80.         my @res = ( ref($arg1) ? shift : () );
  81.         my ( $name, @class ) = @_;
  82.  
  83.         # This construct is best read backwards.
  84.         #
  85.         # First we take the answer section of the packet.
  86.         # Then we take just the MX records from that list
  87.         # Then we sort the list by preference
  88.         # We do this into an array to force list context.
  89.         # Then we return the list.
  90.  
  91.         my @list = sort { $a->preference <=> $b->preference }
  92.                         grep $_->type eq 'MX', &rr( @res, $name, 'MX', @class );
  93.         return @list;
  94. }
  95.  
  96.  
  97. #
  98. # rrsort()
  99. #
  100. # Usage:
  101. #    @prioritysorted = rrsort( "SRV", "priority", @rr_array );
  102. #
  103. sub rrsort {
  104.         my $rrtype = uc shift;
  105.         my ( $attribute, @rr ) = @_;    ## NB: attribute is optional
  106.         ( @rr, $attribute ) = @_ if ref($attribute) =~ /^Net::DNS::RR/;
  107.  
  108.         my @extracted = grep $_->type eq $rrtype, @rr;
  109.         return @extracted unless scalar @extracted;
  110.         my $func   = "Net::DNS::RR::$rrtype"->get_rrsort_func($attribute);
  111.         my @sorted = sort $func @extracted;
  112. }
  113.  
  114.  
  115. #
  116. # Auxiliary functions to support policy-driven zone serial numbering.
  117. #
  118. #       $successor = $soa->serial(SEQUENTIAL);
  119. #       $successor = $soa->serial(UNIXTIME);
  120. #       $successor = $soa->serial(YYYYMMDDxx);
  121. #
  122.  
  123. sub SEQUENTIAL {undef}
  124.  
  125. sub UNIXTIME { return CORE::time; }
  126.  
  127. sub YYYYMMDDxx {
  128.         my ( $dd, $mm, $yy ) = (localtime)[3 .. 5];
  129.         return 1900010000 + sprintf '%d%0.2d%0.2d00', $yy, $mm, $dd;
  130. }
  131.  
  132.  
  133. #
  134. # Auxiliary functions to support dynamic update.
  135. #
  136.  
  137. sub yxrrset {
  138.         my $rr = new Net::DNS::RR(@_);
  139.         $rr->ttl(0);
  140.         $rr->class('ANY') unless $rr->rdata;
  141.         return $rr;
  142. }
  143.  
  144. sub nxrrset {
  145.         my $rr = new Net::DNS::RR(@_);
  146.         new Net::DNS::RR(
  147.                 name  => $rr->name,
  148.                 type  => $rr->type,
  149.                 class => 'NONE'
  150.                 );
  151. }
  152.  
  153. sub yxdomain {
  154.         my ( $domain, @etc ) = map split, @_;
  155.         my $rr = new Net::DNS::RR( scalar(@etc) ? @_ : ( name => $domain ) );
  156.         new Net::DNS::RR(
  157.                 name  => $rr->name,
  158.                 type  => 'ANY',
  159.                 class => 'ANY'
  160.                 );
  161. }
  162.  
  163. sub nxdomain {
  164.         my ( $domain, @etc ) = map split, @_;
  165.         my $rr = new Net::DNS::RR( scalar(@etc) ? @_ : ( name => $domain ) );
  166.         new Net::DNS::RR(
  167.                 name  => $rr->name,
  168.                 type  => 'ANY',
  169.                 class => 'NONE'
  170.                 );
  171. }
  172.  
  173. sub rr_add {
  174.         my $rr = new Net::DNS::RR(@_);
  175.         $rr->{ttl} = 86400 unless defined $rr->{ttl};
  176.         return $rr;
  177. }
  178.  
  179. sub rr_del {
  180.         my ( $domain, @etc ) = map split, @_;
  181.         my $rr = new Net::DNS::RR( scalar(@etc) ? @_ : ( name => $domain, type => 'ANY' ) );
  182.         $rr->class( $rr->rdata ? 'NONE' : 'ANY' );
  183.         $rr->ttl(0);
  184.         return $rr;
  185. }
  186.  
  187.  
  188. 1;
  189. __END__
  190.  
  191.  
  192.  
  193. =head2 Resolver Objects
  194.  
  195. A resolver object is an instance of the L<Net::DNS::Resolver> class.
  196. A program can have multiple resolver objects, each maintaining its
  197. own state information such as the nameservers to be queried, whether
  198. recursion is desired, etc.
  199.  
  200.  
  201. =head2 Packet Objects
  202.  
  203. L<Net::DNS::Resolver> queries return L<Net::DNS::Packet> objects.
  204. Packet objects have five sections:
  205.  
  206. =over 3
  207.  
  208. =item *
  209.  
  210. The header section, a L<Net::DNS::Header> object.
  211.  
  212. =item *
  213.  
  214. The question section, a list of L<Net::DNS::Question> objects.
  215.  
  216. =item *
  217.  
  218. The answer section, a list of L<Net::DNS::RR> objects.
  219.  
  220. =item *
  221.  
  222. The authority section, a list of L<Net::DNS::RR> objects.
  223.  
  224. =item *
  225.  
  226. The additional section, a list of L<Net::DNS::RR> objects.
  227.  
  228. =back
  229.  
  230. =head2 Update Objects
  231.  
  232. L<Net::DNS::Update> is a subclass of L<Net::DNS::Packet>
  233. used to create dynamic update requests.
  234.  
  235. =head2 Header Objects
  236.  
  237. L<Net::DNS::Header> objects represent the header
  238. section of a DNS packet.
  239.  
  240. =head2 Question Objects
  241.  
  242. L<Net::DNS::Question> objects represent the content of the question
  243. section of a DNS packet.
  244.  
  245. =head2 RR Objects
  246.  
  247. L<Net::DNS::RR> is the base class for DNS resource record (RR) objects
  248. in the answer, authority, and additional sections of a DNS packet.
  249.  
  250. Do not assume that RR objects will be of the type requested.
  251. The type of an RR object must be checked before calling any methods.
  252.  
  253.  
  254. =head1 METHODS
  255.  
  256. See the manual pages listed above for other class-specific methods.
  257.  
  258. =head2 version
  259.  
  260.     print Net::DNS->version, "\n";
  261.  
  262. Returns the version of Net::DNS.
  263.  
  264.  
  265. =head2 rr
  266.  
  267.     # Use a default resolver -- can not get an error string this way.
  268.     use Net::DNS;
  269.     my @rr = rr("example.com");
  270.     my @rr = rr("example.com", "A");
  271.     my @rr = rr("example.com", "A", "IN");
  272.  
  273.     # Use your own resolver object.
  274.     my $res = Net::DNS::Resolver->new;
  275.     my @rr  = rr($res, "example.com" ... );
  276.  
  277.     my ($ptr) = rr("192.0.2.1");
  278.  
  279. The C<rr()> method provides simple RR lookup for scenarios where
  280. the full flexibility of Net::DNS is not required.
  281.  
  282. Returns a list of L<Net::DNS::RR> objects for the specified name
  283. or an empty list if the query failed or no record was found.
  284.  
  285. See L</EXAMPLES> for more complete examples.
  286.  
  287.  
  288. =head2 mx
  289.  
  290.     # Use a default resolver -- can not get an error string this way.
  291.     use Net::DNS;
  292.     my @mx = mx("example.com");
  293.  
  294.     # Use your own resolver object.
  295.     my $res = Net::DNS::Resolver->new;
  296.     my @mx  = mx($res, "example.com");
  297.  
  298. Returns a list of L<Net::DNS::RR::MX> objects representing the MX
  299. records for the specified name.
  300. The list will be sorted by preference.
  301. Returns an empty list if the query failed or no MX record was found.
  302.  
  303. This method does not look up A records; it only performs MX queries.
  304.  
  305.  
  306. =head1 Dynamic DNS Update Support
  307.  
  308. The Net::DNS module provides auxiliary functions which support
  309. dynamic DNS update requests.
  310.  
  311.  
  312. =head2 yxrrset
  313.  
  314. Use this method to add an "RRset exists" prerequisite to a dynamic
  315. update packet.  There are two forms, value-independent and
  316. value-dependent:
  317.  
  318.     # RRset exists (value-independent)
  319.     $update->push(pre => yxrrset("host.example.com A"));
  320.  
  321. Meaning:  At least one RR with the specified name and type must
  322. exist.
  323.  
  324.     # RRset exists (value-dependent)
  325.     $update->push(pre => yxrrset("host.example.com A 10.1.2.3"));
  326.  
  327. Meaning:  At least one RR with the specified name and type must
  328. exist and must have matching data.
  329.  
  330. Returns a L<Net::DNS::RR> object or C<undef> if the object could not
  331. be created.
  332.  
  333. =head2 nxrrset
  334.  
  335. Use this method to add an "RRset does not exist" prerequisite to
  336. a dynamic update packet.
  337.  
  338.     $update->push(pre => nxrrset("host.example.com A"));
  339.  
  340. Meaning:  No RRs with the specified name and type can exist.
  341.  
  342. Returns a L<Net::DNS::RR> object or C<undef> if the object could not
  343. be created.
  344.  
  345. =head2 yxdomain
  346.  
  347. Use this method to add a "name is in use" prerequisite to a dynamic
  348. update packet.
  349.  
  350.     $update->push(pre => yxdomain("host.example.com"));
  351.  
  352. Meaning:  At least one RR with the specified name must exist.
  353.  
  354. Returns a L<Net::DNS::RR> object or C<undef> if the object could not
  355. be created.
  356.  
  357. =head2 nxdomain
  358.  
  359. Use this method to add a "name is not in use" prerequisite to a
  360. dynamic update packet.
  361.  
  362.     $update->push(pre => nxdomain("host.example.com"));
  363.  
  364. Meaning:  No RR with the specified name can exist.
  365.  
  366. Returns a L<Net::DNS::RR> object or C<undef> if the object could not
  367. be created.
  368.  
  369. =head2 rr_add
  370.  
  371. Use this method to add RRs to a zone.
  372.  
  373.     $update->push(update => rr_add("host.example.com A 10.1.2.3"));
  374.  
  375. Meaning:  Add this RR to the zone.
  376.  
  377. RR objects created by this method should be added to the "update"
  378. section of a dynamic update packet.  The TTL defaults to 86400
  379. seconds (24 hours) if not specified.
  380.  
  381. Returns a L<Net::DNS::RR> object or C<undef> if the object could not
  382. be created.
  383.  
  384. =head2 rr_del
  385.  
  386. Use this method to delete RRs from a zone.  There are three forms:
  387. delete all RRsets, delete an RRset, and delete a specific RR.
  388.  
  389.     # Delete all RRsets.
  390.     $update->push(update => rr_del("host.example.com"));
  391.  
  392. Meaning:  Delete all RRs having the specified name.
  393.  
  394.     # Delete an RRset.
  395.     $update->push(update => rr_del("host.example.com A"));
  396.  
  397. Meaning:  Delete all RRs having the specified name and type.
  398.  
  399.     # Delete a specific RR.
  400.     $update->push(update => rr_del("host.example.com A 10.1.2.3"));
  401.  
  402. Meaning:  Delete all RRs having the specified name, type, and data.
  403.  
  404. RR objects created by this method should be added to the "update"
  405. section of a dynamic update packet.
  406.  
  407. Returns a L<Net::DNS::RR> object or C<undef> if the object could not
  408. be created.
  409.  
  410.  
  411. =head1 Zone Serial Number Management
  412.  
  413. The Net::DNS module provides auxiliary functions which support
  414. policy-driven zone serial numbering regimes.
  415.  
  416. =head2 SEQUENTIAL
  417.  
  418.     $successor = $soa->serial( SEQUENTIAL );
  419.  
  420. The existing serial number is incremented modulo 2**32.
  421.  
  422. =head2 UNIXTIME
  423.  
  424.     $successor = $soa->serial( UNIXTIME );
  425.  
  426. The Unix time scale will be used as the basis for zone serial
  427. numbering. The serial number will be incremented if the time
  428. elapsed since the previous update is less than one second.
  429.  
  430. =head2 YYYYMMDDxx
  431.  
  432.     $successor = $soa->serial( YYYYMMDDxx );
  433.  
  434. The 32 bit value returned by the auxiliary C<YYYYMMDDxx()> function
  435. will be used as the base for the date-coded zone serial number.
  436. Serial number increments must be limited to 100 per day for the
  437. date information to remain useful.
  438.  
  439.  
  440.  
  441. =head1 Sorting of RR arrays
  442.  
  443. C<rrsort()> provides functionality to help you sort RR arrays. In most cases
  444. this will give you the answer that you want, but you can specify your
  445. own sorting method by using the C<< Net::DNS::RR::FOO->set_rrsort_func() >>
  446. class method. See L<Net::DNS::RR> for details.
  447.  
  448. =head2 rrsort
  449.  
  450.     use Net::DNS;
  451.  
  452.     my @sorted = rrsort( $rrtype, $attribute, @rr_array );
  453.  
  454. C<rrsort()> selects all RRs from the input array that are of the type defined
  455. by the first argument. Those RRs are sorted based on the attribute that is
  456. specified as second argument.
  457.  
  458. There are a number of RRs for which the sorting function is defined in the
  459. code.
  460.  
  461. For instance:
  462.  
  463.     my @prioritysorted = rrsort( "SRV", "priority", @rr_array );
  464.  
  465. returns the SRV records sorted from lowest to highest priority and for
  466. equal priorities from highest to lowest weight.
  467.  
  468. If the function does not exist then a numerical sort on the attribute
  469. value is performed.
  470.  
  471.     my @portsorted = rrsort( "SRV", "port", @rr_array );
  472.  
  473. If the attribute is not defined then either the C<default_sort()> function or
  474. "canonical sorting" (as defined by DNSSEC) will be used.
  475.  
  476. C<rrsort()> returns a sorted array containing only elements of the specified
  477. RR type.  Any other RR types are silently discarded.
  478.  
  479. C<rrsort()> returns an empty list when arguments are incorrect.
  480.  
  481.  
  482. =head1 EXAMPLES
  483.  
  484. The following brief examples illustrate some of the features of Net::DNS.
  485. The documentation for individual modules and the demo scripts included
  486. with the distribution provide more extensive examples.
  487.  
  488. See L<Net::DNS::Update> for an example of performing dynamic updates.
  489.  
  490.  
  491. =head2 Look up host addresses.
  492.  
  493.     use Net::DNS;
  494.     my $res   = Net::DNS::Resolver->new;
  495.     my $reply = $res->search("www.example.com", "A");
  496.  
  497.     if ($reply) {
  498.         foreach my $rr ($reply->answer) {
  499.             print $rr->address, "\n" if $rr->can("address");
  500.         }
  501.     } else {
  502.         warn "query failed: ", $res->errorstring, "\n";
  503.     }
  504.  
  505.  
  506. =head2 Find the nameservers for a domain.
  507.  
  508.     use Net::DNS;
  509.     my $res   = Net::DNS::Resolver->new;
  510.     my $reply = $res->query("example.com", "NS");
  511.  
  512.     if ($reply) {
  513.         foreach $rr (grep { $_->type eq "NS" } $reply->answer) {
  514.             print $rr->nsdname, "\n";
  515.         }
  516.     } else {
  517.         warn "query failed: ", $res->errorstring, "\n";
  518.     }
  519.  
  520.  
  521. =head2 Find the MX records for a domain.
  522.  
  523.     use Net::DNS;
  524.     my $name = "example.com";
  525.     my $res  = Net::DNS::Resolver->new;
  526.     my @mx   = mx($res, $name);
  527.  
  528.     if (@mx) {
  529.         foreach $rr (@mx) {
  530.             print $rr->preference, "\t", $rr->exchange, "\n";
  531.         }
  532.     } else {
  533.         warn "Can not find MX records for $name: ", $res->errorstring, "\n";
  534.     }
  535.  
  536.  
  537. =head2 Print domain SOA record in zone file format.
  538.  
  539.     use Net::DNS;
  540.     my $res   = Net::DNS::Resolver->new;
  541.     my $reply = $res->query("example.com", "SOA");
  542.  
  543.     if ($reply) {
  544.         foreach my $rr ($reply->answer) {
  545.             $rr->print;
  546.         }
  547.     } else {
  548.         warn "query failed: ", $res->errorstring, "\n";
  549.     }
  550.  
  551.  
  552. =head2 Perform a zone transfer and print all the records.
  553.  
  554.     use Net::DNS;
  555.     my $res  = Net::DNS::Resolver->new;
  556.     $res->tcp_timeout(20);
  557.     $res->nameservers("ns.example.com");
  558.  
  559.     my @zone = $res->axfr("example.com");
  560.  
  561.     foreach $rr (@zone) {
  562.         $rr->print;
  563.     }
  564.  
  565.     warn $res->errorstring if $res->errorstring;
  566.  
  567.  
  568. =head2 Perform a background query and print the reply.
  569.  
  570.     use Net::DNS;
  571.     my $res    = Net::DNS::Resolver->new;
  572.     $res->udp_timeout(10);
  573.     $res->tcp_timeout(20);
  574.     my $socket = $res->bgsend("host.example.com");
  575.  
  576.     while ( $res->bgbusy($socket) ) {
  577.         # do some work here while waiting for the answer
  578.         # ...and some more here
  579.     }
  580.  
  581.     my $packet = $res->bgread($socket);
  582.     if ($packet) {
  583.         $packet->print;
  584.     } else {
  585.         warn "query failed: ", $res->errorstring, "\n";
  586.     }
  587.  
  588.  
  589. =head1 BUGS
  590.  
  591. Net::DNS is slow.
  592.  
  593. For other items to be fixed, or if you discover a bug in this
  594. distribution please use the CPAN bug reporting system.
  595.  
  596.  
  597. =head1 COPYRIGHT
  598.  
  599. Copyright (c)1997-2000 Michael Fuhr.
  600.  
  601. Portions Copyright (c)2002,2003 Chris Reinhardt.
  602.  
  603. Portions Copyright (c)2005 Olaf Kolkman (RIPE NCC)
  604.  
  605. Portions Copyright (c)2006 Olaf Kolkman (NLnet Labs)
  606.  
  607. Portions Copyright (c)2014 Dick Franks
  608.  
  609. All rights reserved.
  610.  
  611.  
  612. =head1 LICENSE
  613.  
  614. Permission to use, copy, modify, and distribute this software and its
  615. documentation for any purpose and without fee is hereby granted, provided
  616. that the above copyright notice appear in all copies and that both that
  617. copyright notice and this permission notice appear in supporting
  618. documentation, and that the name of the author not be used in advertising
  619. or publicity pertaining to distribution of the software without specific
  620. prior written permission.
  621.  
  622. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  623. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  624. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  625. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  626. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  627. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  628. DEALINGS IN THE SOFTWARE.
  629.  
  630.  
  631. =head1 AUTHOR INFORMATION
  632.  
  633. Net::DNS is maintained at NLnet Labs (www.nlnetlabs.nl) by Willem Toorop.
  634.  
  635. Between 2005 and 2012 Net::DNS was maintained by Olaf Kolkman.
  636.  
  637. Between 2002 and 2004 Net::DNS was maintained by Chris Reinhardt.
  638.  
  639. Net::DNS was created in 1997 by Michael Fuhr.
  640.  
  641.  
  642. =head1 SEE ALSO
  643.  
  644. L<perl>, L<Net::DNS::Resolver>, L<Net::DNS::Question>, L<Net::DNS::RR>,
  645. L<Net::DNS::Packet>, L<Net::DNS::Update>,
  646. RFC1035, L<http://www.net-dns.org/>,
  647. I<DNS and BIND> by Paul Albitz & Cricket Liu
  648.  
  649. =cut

Raw Paste


Login or Register to edit or fork this paste. It's free.