| 1 | package Net::DNS; | ||||||||||
| 2 | |||||||||||
| 3 | 172 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 172 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 86 | our $VERSION; | |||||||||
| 7 | 86 | $VERSION = '1.58'; | |||||||||
| 8 | 86 | $VERSION = eval {$VERSION}; | |||||||||
| 9 | 86 | our $SVNVERSION = (qw$Id: DNS.pm 2068 2026-11-20 12:27:22Z willem $)[2]; | |||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Net::DNS - Perl Interface to the Domain Name System | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | use Net::DNS; | ||||||||||
| 19 | my $resolver = Net::DNS::Resolver->new(...); | ||||||||||
| 20 | my $response = $resolver->send(...); | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | Net::DNS is a collection of Perl modules that act as a Domain Name System | ||||||||||
| 25 | (DNS) resolver. It allows the programmer to perform DNS queries that are | ||||||||||
| 26 | beyond the capabilities of C<gethostbyname()> and C<gethostbyaddr()>. | ||||||||||
| 27 | |||||||||||
| 28 | The programmer should be familiar with the structure of a DNS packet | ||||||||||
| 29 | and the zone file presentation format described in RFC1035. | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | 172 | ▶ | use integer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 172 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 86 | our @EXPORT = qw(SEQUENTIAL UNIXTIME YYYYMMDDxx | |||||||||
| 38 | yxrrset nxrrset yxdomain nxdomain rr_add rr_del | ||||||||||
| 39 | mx rr rrsort); | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | 86 | local $SIG{__DIE__}; | |||||||||
| 43 | 86 | require Net::DNS::Resolver; | |||||||||
| 44 | 86 | require Net::DNS::Packet; | |||||||||
| 45 | 86 | require Net::DNS::RR; | |||||||||
| 46 | 86 | require Net::DNS::Update; | |||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | sub version { return $VERSION; } | ||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | # | ||||||||||
| 53 | # rr() | ||||||||||
| 54 | # | ||||||||||
| 55 | # Usage: | ||||||||||
| 56 | # @rr = rr('example.com'); | ||||||||||
| 57 | # @rr = rr('example.com', 'A', 'IN'); | ||||||||||
| 58 | # @rr = rr($res, 'example.com' ... ); | ||||||||||
| 59 | # | ||||||||||
| 60 | 7 | ▶ | sub rr { | ||||||||
|
Subroutine
rr: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 61 | 7 | my @arg = @_; | |||||||||
| 62 | 7 | ▶ | my $res = ( ref( $arg[0] ) ? shift @arg : Net::DNS::Resolver->new() ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 7 | my $reply = $res->query(@arg); | |||||||||
| 65 | 7 | ▶ | my @list = $reply ? $reply->answer : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 7 | return @list; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | # | ||||||||||
| 71 | # mx() | ||||||||||
| 72 | # | ||||||||||
| 73 | # Usage: | ||||||||||
| 74 | # @mx = mx('example.com'); | ||||||||||
| 75 | # @mx = mx($res, 'example.com'); | ||||||||||
| 76 | # | ||||||||||
| 77 | 4 | ▶ | sub mx { | ||||||||
|
Subroutine
mx: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 78 | 4 | my @arg = @_; | |||||||||
| 79 | 4 | ▶ | my @res = ( ref( $arg[0] ) ? shift @arg : () ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 4 | my ( $name, @class ) = @arg; | |||||||||
| 81 | |||||||||||
| 82 | # This construct is best read backwards. | ||||||||||
| 83 | # | ||||||||||
| 84 | # First we take the answer section of the packet. | ||||||||||
| 85 | # Then we take just the MX records from that list | ||||||||||
| 86 | # Then we sort the list by preference | ||||||||||
| 87 | # We do this into an array to force list context. | ||||||||||
| 88 | # Then we return the list. | ||||||||||
| 89 | |||||||||||
| 90 | 3 | my @list = sort { $a->preference <=> $b->preference } | |||||||||
| 91 | 4 | grep { $_->type eq 'MX' } &rr( @res, $name, 'MX', @class ); | |||||||||
| 92 | 4 | return @list; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | # | ||||||||||
| 97 | # rrsort() | ||||||||||
| 98 | # | ||||||||||
| 99 | # Usage: | ||||||||||
| 100 | # @prioritysorted = rrsort( "SRV", "priority", @rr_array ); | ||||||||||
| 101 | # | ||||||||||
| 102 | 12 | ▶ | sub rrsort { | ||||||||
|
Subroutine
rrsort: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 103 | 12 | my @arg = @_; | |||||||||
| 104 | 12 | my $rrtype = uc shift @arg; | |||||||||
| 105 | 12 | my ( $attribute, @rr ) = @arg; ## NB: attribute is optional | |||||||||
| 106 | 12 | ▶ | ( @rr, $attribute ) = @arg if ref($attribute) =~ /^Net::DNS::RR/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | |||||||||||
| 108 | 12 | my @extracted = grep { $_->type eq $rrtype } @rr; | |||||||||
| 109 | 12 | ▶ | return @extracted unless scalar @extracted; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 10 | my $func = "Net::DNS::RR::$rrtype"->get_rrsort_func($attribute); | |||||||||
| 111 | 10 | my @sorted = sort $func @extracted; | |||||||||
| 112 | 10 | return @sorted; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | # | ||||||||||
| 117 | # Auxiliary functions to support policy-driven zone serial numbering. | ||||||||||
| 118 | # | ||||||||||
| 119 | # $successor = $soa->serial(SEQUENTIAL); | ||||||||||
| 120 | # $successor = $soa->serial(UNIXTIME); | ||||||||||
| 121 | # $successor = $soa->serial(YYYYMMDDxx); | ||||||||||
| 122 | # | ||||||||||
| 123 | |||||||||||
| 124 | 3 | ▶ | sub SEQUENTIAL { return (undef) } | ||||||||
|
Subroutine
SEQUENTIAL: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 125 | |||||||||||
| 126 | 1 | ▶ | sub UNIXTIME { return CORE::time; } | ||||||||
|
Subroutine
UNIXTIME: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 127 | |||||||||||
| 128 | 2 | ▶ | sub YYYYMMDDxx { | ||||||||
|
Subroutine
YYYYMMDDxx: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 129 | 2 | my ( $dd, $mm, $yy ) = (localtime)[3 .. 5]; | |||||||||
| 130 | 2 | return 1900010000 + sprintf '%d%0.2d%0.2d00', $yy, $mm, $dd; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | # | ||||||||||
| 135 | # Auxiliary functions to support dynamic update. | ||||||||||
| 136 | # | ||||||||||
| 137 | |||||||||||
| 138 | 5 | ▶ | sub yxrrset { | ||||||||
|
Subroutine
yxrrset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 139 | 5 | my @arg = @_; | |||||||||
| 140 | 5 | my $rr = Net::DNS::RR->new(@arg); | |||||||||
| 141 | 5 | $rr->ttl(0); | |||||||||
| 142 | 5 | ▶ | $rr->class('ANY') unless $rr->rdata; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 5 | return $rr; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 2 | ▶ | sub nxrrset { | ||||||||
|
Subroutine
nxrrset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 147 | 2 | my @arg = @_; | |||||||||
| 148 | 2 | my $rr = Net::DNS::RR->new(@arg); | |||||||||
| 149 | 2 | return Net::DNS::RR->new( | |||||||||
| 150 | name => $rr->name, | ||||||||||
| 151 | type => $rr->type, | ||||||||||
| 152 | class => 'NONE' | ||||||||||
| 153 | ); | ||||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | 2 | ▶ | sub yxdomain { | ||||||||
|
Subroutine
yxdomain: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 157 | 2 | my @arg = @_; | |||||||||
| 158 | 2 | my ( $domain, @etc ) = map {split} @arg; | |||||||||
| 159 | 2 | ▶ | my $rr = Net::DNS::RR->new( scalar(@etc) ? @arg : ( name => $domain ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 2 | return Net::DNS::RR->new( | |||||||||
| 161 | name => $rr->name, | ||||||||||
| 162 | type => 'ANY', | ||||||||||
| 163 | class => 'ANY' | ||||||||||
| 164 | ); | ||||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 2 | ▶ | sub nxdomain { | ||||||||
|
Subroutine
nxdomain: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 168 | 2 | my @arg = @_; | |||||||||
| 169 | 2 | my ( $domain, @etc ) = map {split} @arg; | |||||||||
| 170 | 2 | ▶ | my $rr = Net::DNS::RR->new( scalar(@etc) ? @arg : ( name => $domain ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | 2 | return Net::DNS::RR->new( | |||||||||
| 172 | name => $rr->name, | ||||||||||
| 173 | type => 'ANY', | ||||||||||
| 174 | class => 'NONE' | ||||||||||
| 175 | ); | ||||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | 4 | ▶ | sub rr_add { | ||||||||
|
Subroutine
rr_add: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 179 | 4 | my @arg = @_; | |||||||||
| 180 | 4 | my $rr = Net::DNS::RR->new(@arg); | |||||||||
| 181 | 4 | ▶ | $rr->{ttl} = 86400 unless defined $rr->{ttl}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 182 | 4 | return $rr; | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | 3 | ▶ | sub rr_del { | ||||||||
|
Subroutine
rr_del: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 186 | 3 | my @arg = @_; | |||||||||
| 187 | 3 | my ( $domain, @etc ) = map {split} @arg; | |||||||||
| 188 | 3 | ▶ | my $rr = Net::DNS::RR->new( scalar(@etc) ? @arg : ( name => $domain, type => 'ANY' ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 3 | ▶ | $rr->class( $rr->rdata ? 'NONE' : 'ANY' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 3 | $rr->ttl(0); | |||||||||
| 191 | 3 | return $rr; | |||||||||
| 192 | } | ||||||||||
| 193 | |||||||||||
| 194 | |||||||||||
| 195 | 86 | 1; | |||||||||
| 196 | __END__ | ||||||||||