DNS/RR.pm

filter: Statementstmt 100% 307 / 307 Branchbran 100% 146 / 146 Conditioncond 100% 42 / 42 MC/DCmcdc 100% 23 / 23 Subroutinesub 100% 50 / 50 Podpod 100% 19 / 21 totaltotal 100% 587 / 589 CC 97 SCAR 18.1
CC
97
Cov
100%
_new_string
18.0
_new_hash
10.0
string
8.0
CRAP 6.1
« DNS/DomainName.pm ↑ Summary DNS/RR/AMTRELAY.pm »
1 package Net::DNS::RR;
2  
3 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 96 our $VERSION = (qw$Id: RR.pm 2037 2025-08-18 14:39:32Z willem $)[2];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::RR - DNS resource record base class
12  
13 =head1 SYNOPSIS
14  
15 use Net::DNS;
16  
17 $rr = Net::DNS::RR->new('example.com IN AAAA 2001:DB8::1');
18  
19 $rr = Net::DNS::RR->new(
20 owner => 'example.com',
21 type => 'AAAA',
22 address => '2001:DB8::1'
23 );
24  
25  
26 =head1 DESCRIPTION
27  
28 Net::DNS::RR is the base class for DNS Resource Record (RR) objects.
29 See also the manual pages for each specific RR type.
30  
31 =cut
32  
33  
34 96 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 96 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 96 ▶use constant LIB => grep { $_ ne '.' } grep { !ref($_) } @INC;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 96 ▶use Net::DNS::Parameters qw(%classbyname :class :type);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 96 ▶use Net::DNS::DomainName;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43 =head1 METHODS
44  
45 B<WARNING!!!> Do not assume the RR objects you receive from a query
46 are of a particular type. You must always check the object type
47 before calling any of its methods. If you call an unknown method,
48 you will get an error message and execution will be terminated.
49  
50 =cut
51  
52 sub new {
53 1318 ▶ my ( $class, @list ) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
54 1318 my $rr = eval {
55 1318 local $SIG{__DIE__};
56 1318 ▶ scalar @list > 1 ? &_new_hash : &_new_string;
Branch: 1 branch
BranchTrueFalseTotal
scalar @list > 1 ? : 585 733 1318
57 };
58 1318 ▶ return $rr if $rr;
Branch: 1 branch
BranchTrueFalseTotal
if $rr 1294 24 1318
59 24 ▶ my @param = map { defined($_) ? split /\s+/ : 'undef' } @list;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 29 1 30
60 24 my $stmnt = substr "$class->new( @param )", 0, 80;
61 24 croak "${@}in $stmnt\n";
62 }
63  
64  
65 =head2 new (from string)
66  
67 $aaaa = Net::DNS::RR->new('host.example.com. 86400 AAAA 2001:DB8::1');
68 $mx = Net::DNS::RR->new('example.com. 7200 MX 10 mailhost.example.com.');
69 $cname = Net::DNS::RR->new('www.example.com 300 IN CNAME host.example.com');
70 $txt = Net::DNS::RR->new('txt.example.com 3600 HS TXT "text data"');
71  
72 Returns an object of the appropriate RR type, or a L<Net::DNS::RR> object
73 if the type is not implemented. The attribute values are extracted from the
74 string passed by the user. The syntax of the argument string follows the
75 RFC1035 specification for zone files, and is compatible with the result
76 returned by the string method.
77  
78 The owner and RR type are required; all other information is optional.
79 Omitting the optional fields is useful for creating the empty RDATA
80 sections required for certain dynamic update operations.
81 See the L<Net::DNS::Update> manual page for additional examples.
82  
83 All names are interpreted as fully qualified domain names.
84 The trailing dot (.) is optional.
85  
86 =cut
87  
88 96 my $PARSE_REGEX = q/("[^"]*")|;[^\n]*|[ \t\n\r\f()]+/; # NB: *not* \s (matches Unicode white space)
89  
90 sub _new_string {
91 1020 ▶ my ( $base, $string ) = @_;
Subroutine _new_string: called CC=18 SCAR=28.9
92 1020 ▶ die 'argument absent or undefined' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 1019 1020
93 1019 ▶ die 'non-scalar argument' if ref $string;
Branch: 1 branch
BranchTrueFalseTotal
if ref $string 2 1017 1019
94  
95 # parse into quoted strings, contiguous non-whitespace and (discarded) comments
96 1017 local $_ = $string;
97 1017 s/\\\\/\\092/g; # disguise escaped escape
98 1017 s/\\"/\\034/g; # disguise escaped quote
99 1017 s/\\\(/\\040/g; # disguise escaped bracket
100 1017 s/\\\)/\\041/g; # disguise escaped bracket
101 1017 s/\\;/\\059/g; # disguise escaped semicolon
102 1017 ▶ my ( $owner, @token ) = grep { defined && length } split /$PARSE_REGEX/o;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 5161 4100 9261
103  
104 1017 ▶ die 'unable to parse RR string' unless scalar @token;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @token 1 1016 1017
105 1016 my $t1 = $token[0];
106 1016 my $t2 = $token[1];
107  
108 1016 my ( $ttl, $class );
109 1016 ▶ if ( not defined $t2 ) { # <owner> <type>
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $t2) { } 70 946 1016
elsif ($t1 =~ /^\d/) { } 495 451 946
elsif ($Net::DNS::RR::classbyname{uc $t1} or $t1 =~ /^CLASS\d/i) { } 53 398 451
110 70 ▶ @token = ('ANY') if $classbyname{uc $t1}; # <owner> <class>
Branch: 1 branch
BranchTrueFalseTotal
if $Net::DNS::RR::classbyname{uc $t1} 1 69 70
111 } elsif ( $t1 =~ /^\d/ ) {
112 495 $ttl = shift @token; # <owner> <ttl> [<class>] <type>
113 495 ▶ $class = shift @token if $classbyname{uc $t2} || $t2 =~ /^CLASS\d/i;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::DNS::RR::classbyname{uc $t2} or $t2 =~ /^CLASS\d/i 483 12 495
Condition100%3/3
$Net::DNS::RR::classbyname{uc $t2} or $t2 =~ /^CLASS\d/i
l!l&&r!l&&!r
480312
MC/DC100%2/2
$Net::DNS::RR::classbyname{uc $t2} or $t2 =~ /^CLASS\d/i
$Net::DNS::RR::classbyname{uc $t2} $t2 =~ /^CLASS\d/i
Truth table100%3/3
A or B
A= $Net::DNS::RR::classbyname{uc $t2}B= $t2 =~ /^CLASS\d/i
ABresult
1X1
011
000
114 ▶ } elsif ( $classbyname{uc $t1} || $t1 =~ /^CLASS\d/i ) {
Condition100%3/3
$Net::DNS::RR::classbyname{uc $t1} or $t1 =~ /^CLASS\d/i
l!l&&r!l&&!r
458398
MC/DC100%2/2
$Net::DNS::RR::classbyname{uc $t1} or $t1 =~ /^CLASS\d/i
$Net::DNS::RR::classbyname{uc $t1} $t1 =~ /^CLASS\d/i
Truth table100%3/3
A or B
A= $Net::DNS::RR::classbyname{uc $t1}B= $t1 =~ /^CLASS\d/i
ABresult
1X1
011
000
115 53 $class = shift @token; # <owner> <class> [<ttl>] <type>
116 53 ▶ $ttl = shift @token if $t2 =~ /^\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $t2 =~ /^\d/ 8 45 53
117 }
118  
119 1016 my $type = shift(@token);
120 1016 my $populated = scalar @token;
121  
122 1016 my $self = $base->_subclass( $type, $populated ); # create RR object
123 1015 $self->owner($owner);
124 1015 &class( $self, $class ); # specify CLASS
125 1015 &ttl( $self, $ttl ); # specify TTL
126  
127 1015 ▶ return $self unless $populated; # empty RR
Branch: 1 branch
BranchTrueFalseTotal
unless $populated 89 926 1015
128  
129 926 ▶ if ( $#token && $token[0] =~ /^[\\]?#$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#token and $token[0] =~ /^[\\]?#$/) { } 29 897 926
Condition100%3/3
$#token and $token[0] =~ /^[\\]?#$/
!ll&&!rl&&r
66023729
MC/DC100%2/2
$#token and $token[0] =~ /^[\\]?#$/
$#token $token[0] =~ /^[\\]?#$/
Truth table100%3/3
A and B
A= $#tokenB= $token[0] =~ /^[\\]?#$/
ABresult
0X0
100
111
130 29 shift @token; # RFC3597 hexadecimal format
131 29 ▶ my $rdlen = shift(@token) || 0;
Condition100%2/2
shift @token || 0
l!l
236
MC/DC100%1/1
shift @token || 0
shift @token
Truth table100%2/2
A
A= shift @token
Aresult
11
00
132 29 my $rdata = pack 'H*', join( '', @token );
133 29 ▶ die 'length and hexadecimal data inconsistent' unless $rdlen == length $rdata;
Branch: 1 branch
BranchTrueFalseTotal
unless $rdlen == length $rdata 3 26 29
134 26 $self->rdata($rdata); # unpack RDATA
135 } else {
136 897 $self->_parse_rdata(@token); # parse arguments
137 }
138  
139 901 $self->_post_parse();
140 890 return $self;
141 }
142  
143  
144 =head2 new (from hash)
145  
146 $rr = Net::DNS::RR->new(%hash);
147  
148 $rr = Net::DNS::RR->new(
149 owner => 'host.example.com',
150 ttl => 86400,
151 class => 'IN',
152 type => 'AAAA',
153 address => '2001:DB8::1'
154 );
155
156 $rr = Net::DNS::RR->new(
157 owner => 'txt.example.com',
158 type => 'TXT',
159 txtdata => [ 'one', 'two' ]
160 );
161  
162 Returns an object of the appropriate RR type, or a L<Net::DNS::RR> object
163 if the type is not implemented. Consult the relevant manual pages for the
164 usage of type specific attributes.
165  
166 The owner and RR type are required; all other information is optional.
167 Omitting optional attributes is useful for creating the empty RDATA
168 sections required for certain dynamic update operations.
169  
170 =cut
171  
172 96 my @core = qw(owner name type class ttl rdlength);
173  
174 sub _new_hash {
175 585 ▶ my $base = shift;
Subroutine _new_hash: called CC=10 SCAR=23.0
176  
177 585 my %argument = ( owner => '.', type => 'NULL' );
178 585 my @attribute;
179 585 ▶ while ( my $key = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = shift()) 1371 585 1956
180 1371 push @attribute, $key;
181 1371 $argument{lc $key} = shift;
182 }
183  
184 585 my ( $owner, $name, $type, $class, $ttl ) = delete @argument{@core};
185  
186 585 my $self = $base->_subclass( $type, scalar(%argument) );
187 585 ▶ $self->owner( $name ? $name : $owner );
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 258 327 585
188 585 ▶ $self->class($class) if defined $class; # optional CLASS
Branch: 1 branch
BranchTrueFalseTotal
if defined $class 23 562 585
189 585 ▶ $self->ttl($ttl) if defined $ttl; # optional TTL
Branch: 1 branch
BranchTrueFalseTotal
if defined $ttl 16 569 585
190  
191 585 eval {
192 585 foreach my $attribute (@attribute) {
193 1371 my $value = $argument{lc $attribute};
194 1371 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 898 473 1371
195 473 ▶ $self->$attribute( ref($value) eq 'ARRAY' ? @$value : $value );
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 472 473
196 }
197 };
198 585 ▶ die ref($self) eq __PACKAGE__ ? "type $type not implemented" : () if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 2 583 585
ref $self eq __PACKAGE__ ? : 1 1 2
199  
200 583 $self->_post_parse();
201 583 return $self;
202 }
203  
204  
205 =head2 decode
206  
207 ( $rr, $next ) = Net::DNS::RR->decode( \$data, $offset, @opaque );
208  
209 Decodes a DNS resource record at the specified location within a
210 DNS packet.
211  
212 The argument list consists of a reference to the buffer containing
213 the packet data and offset indicating where resource record begins.
214 Any remaining arguments are passed as opaque data to subordinate
215 decoders and do not form part of the published interface.
216  
217 Returns a C<Net::DNS::RR> object and the offset of the next record
218 in the packet.
219  
220 An exception is raised if the data buffer contains insufficient or
221 corrupt data.
222  
223 =cut
224  
225 96 ▶use constant RRFIXEDSZ => length pack 'n2 N n', (0) x 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
226  
227 sub decode {
228 9955 ▶ my ( $base, @argument ) = @_;
Subroutine decode: called CC=7 SCAR=19.5
Pod documented
229  
230 9955 my ( $owner, $fixed ) = Net::DNS::DomainName1035->decode(@argument);
231 9954 my $index = $fixed + RRFIXEDSZ;
232 9954 my ( $data, $offset, @opaque ) = @argument;
233 9954 ▶ die 'corrupt wire-format data' if length $$data < $index;
Branch: 1 branch
BranchTrueFalseTotal
if length $$data < $index 1 9953 9954
234 9953 my $self = $base->_subclass( unpack "\@$fixed n", $$data );
235 9953 $self->{owner} = $owner;
236 9953 @{$self}{qw(class ttl rdlength)} = unpack "\@$fixed x2 n N n", $$data;
237  
238 9953 my $next = $index + $self->{rdlength};
239 9953 ▶ die 'corrupt wire-format data' if length $$data < $next;
Branch: 1 branch
BranchTrueFalseTotal
if length $$data < $next 1 9952 9953
240  
241 9952 ▶ if ( $next > $index or $self->type eq 'OPT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $index or $self->type eq "OPT") 9951 1 9952
Condition100%3/3
$next > $index or $self->type eq "OPT"
l!l&&r!l&&!r
9899521
MC/DC100%2/2
$next > $index or $self->type eq "OPT"
$next > $index $self->type eq "OPT"
Truth table100%3/3
A or B
A= $next > $indexB= $self->type eq "OPT"
ABresult
1X1
011
000
242 9951 local $self->{offset} = $offset;
243 9951 eval { $self->_decode_rdata( $data, $index, @opaque ) };
244 9951 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 7 9944 9951
245 }
246  
247 9952 ▶ return wantarray ? ( $self, $next ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9876 76 9952
248 }
249  
250  
251 =head2 encode
252  
253 $data = $rr->encode( $offset, @opaque );
254  
255 Returns the C<Net::DNS::RR> in binary format suitable for inclusion
256 in a DNS packet buffer.
257  
258 The offset indicates the intended location within the packet data
259 where the C<Net::DNS::RR> is to be stored.
260  
261 Any remaining arguments are opaque data which are passed intact to
262 subordinate encoders.
263  
264 =cut
265  
266 sub encode {
267 1145 ▶ my ( $self, $offset, @opaque ) = @_;
Subroutine encode: called CC=5 SCAR=16.1
Pod documented
268 1145 ▶ ( $offset, @opaque ) = ( 0x4000, {} ) unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 327 818 1145
269  
270 1145 my $owner = $self->{owner}->encode( $offset, @opaque );
271 1145 my ( $type, $class, $ttl ) = @{$self}{qw(type class ttl)};
272 1145 ▶ my $rdata = $self->_empty ? '' : $self->_encode_rdata( $offset + length($owner) + RRFIXEDSZ, @opaque );
Branch: 1 branch
BranchTrueFalseTotal
$self->_empty ? : 63 1082 1145
273 1145 ▶ return pack 'a* n2 N n a*', $owner, $type, $class || 1, $ttl || 0, length $rdata, $rdata;
Condition100%4/4
$class || 1
l!l
869276
$ttl || 0
l!l
171128
MC/DC100%1/1
$class || 1
$class
MC/DC100%1/1
$ttl || 0
$ttl
Truth table100%2/2
A
A= $class
Aresult
11
00
Truth table100%2/2
A
A= $ttl
Aresult
11
00
274 }
275  
276  
277 =head2 canonical
278  
279 $data = $rr->canonical;
280  
281 Returns the C<Net::DNS::RR> in canonical binary format suitable for
282 DNSSEC signature validation.
283  
284 The absence of the associative array argument signals to subordinate
285 encoders that the canonical uncompressed form of embedded domain
286 names is to be used.
287  
288 =cut
289  
290 sub canonical {
291 305 ▶ my $self = shift;
Subroutine canonical: called CC=4 SCAR=13.9
Pod documented
292  
293 305 my $owner = $self->{owner}->canonical;
294 305 my ( $type, $class, $ttl ) = @{$self}{qw(type class ttl)};
295 305 ▶ my $rdata = $self->_empty ? '' : $self->_encode_rdata( length($owner) + RRFIXEDSZ );
Branch: 1 branch
BranchTrueFalseTotal
$self->_empty ? : 29 276 305
296 305 ▶ return pack 'a* n2 N n a*', $owner, $type, $class || 1, $ttl || 0, length $rdata, $rdata;
Condition100%4/4
$class || 1
l!l
27926
$ttl || 0
l!l
67238
MC/DC100%1/1
$class || 1
$class
MC/DC100%1/1
$ttl || 0
$ttl
Truth table100%2/2
A
A= $class
Aresult
11
00
Truth table100%2/2
A
A= $ttl
Aresult
11
00
297 }
298  
299  
300 =head2 print
301  
302 $rr->print;
303  
304 Prints the resource record to the currently selected output filehandle.
305 Calls the string method to get the formatted RR representation.
306  
307 =cut
308  
309 sub print {
310 27 ▶ print shift->string, "\n";
Subroutine print: called CC=1 SCAR=0.0
Pod documented
311 27 return;
312 }
313  
314  
315 =head2 string
316  
317 print $rr->string, "\n";
318  
319 Returns a string representation of the RR using the master file format
320 mandated by RFC1035.
321 All domain names are fully qualified with trailing dot.
322 This differs from RR attribute methods, which omit the trailing dot.
323  
324 =cut
325  
326 sub string {
327 1118 ▶ my $self = shift;
Subroutine string: called CC=8 SCAR=20.8
Pod documented
328  
329 1118 my $owner = $self->{owner}->string;
330 1118 ▶ my @ttl = defined $self->{ttl} ? $self->{ttl} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'ttl'} ? : 838 280 1118
331 1118 ▶ my @class = defined $self->{class} ? $self->class : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'class'} ? : 859 259 1118
332 1118 my @core = ( $owner, @ttl, @class, $self->type );
333  
334 1118 local $SIG{__DIE__};
335 1118 my $empty = $self->_empty;
336 1118 ▶ my @rdata = $empty ? () : eval { $self->_format_rdata };
Branch: 1 branch
BranchTrueFalseTotal
$empty ? : 25 1093 1118
337 1118 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1117 1118
338  
339 1118 ▶ my $tab = length($owner) < 72 ? "\t" : ' ';
Branch: 1 branch
BranchTrueFalseTotal
length $owner < 72 ? : 1112 6 1118
340 1118 my @line = _wrap( join( $tab, @core, '(' ), @rdata, ')' );
341  
342 1118 my $last = pop(@line); # last or only line
343 1118 ▶ $last = join $tab, @core, "@rdata" unless scalar(@line);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @line 272 846 1118
344  
345 1118 ▶ $self->_annotation('no data') if $empty;
Branch: 1 branch
BranchTrueFalseTotal
if $empty 25 1093 1118
346 1118 return join "\n\t", @line, _wrap( $last, $self->_annotation );
347 }
348  
349  
350 =head2 plain
351  
352 $plain = $rr->plain;
353  
354 Returns a simplified single-line representation of the RR.
355 This facilitates interaction with programs like nsupdate
356 which have rudimentary parsers.
357  
358 =cut
359  
360 sub plain {
361 8 ▶ return join ' ', shift->token;
Subroutine plain: called CC=1 SCAR=0.0
Pod documented
362 }
363  
364  
365 =head2 token
366  
367 @token = $rr->token;
368  
369 Returns a token list representation of the RR zone file string.
370  
371 =cut
372  
373 sub token {
374 7 ▶ my $self = shift;
Subroutine token: called CC=5 SCAR=16.1
Pod documented
375  
376 7 ▶ my @ttl = defined $self->{ttl} ? $self->{ttl} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'ttl'} ? : 1 6 7
377 7 ▶ my @class = defined $self->{class} ? $self->class : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'class'} ? : 1 6 7
378 7 my @core = ( $self->{owner}->string, @ttl, @class, $self->type );
379  
380 # parse into quoted strings, contiguous non-whitespace and (discarded) comments
381 7 ▶ local $_ = $self->_empty ? '' : join( ' ', $self->_format_rdata );
Branch: 1 branch
BranchTrueFalseTotal
$self->_empty ? : 4 3 7
382 7 s/\\\\/\\092/g; # disguise escaped escape
383 7 s/\\"/\\034/g; # disguise escaped quote
384 7 s/\\\(/\\040/g; # disguise escaped bracket
385 7 s/\\\)/\\041/g; # disguise escaped bracket
386 7 s/\\;/\\059/g; # disguise escaped semicolon
387 7 ▶ return ( @core, grep { defined && length } split /$PARSE_REGEX/o );
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 19 16 35
388 }
389  
390  
391 =head2 generic
392  
393 $generic = $rr->generic;
394  
395 Returns the generic RR representation defined in RFC3597. This facilitates
396 creation of zone files containing RRs unrecognised by outdated nameservers
397 and provisioning software.
398  
399 =cut
400  
401 sub generic {
402 8 ▶ my $self = shift;
Subroutine generic: called CC=4 SCAR=13.9
Pod documented
403  
404 8 ▶ my @ttl = defined $self->{ttl} ? $self->{ttl} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'ttl'} ? : 2 6 8
405 8 ▶ my @class = defined $self->{class} ? "CLASS$self->{class}" : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'class'} ? : 2 6 8
406 8 my @core = ( $self->{owner}->string, @ttl, @class, "TYPE$self->{type}" );
407 8 my $data = $self->rdata;
408 8 my @data = ( '\\#', length($data), split /(\S{32})/, unpack 'H*', $data );
409 8 my @line = _wrap( "@core (", @data, ')' );
410 8 ▶ return join "\n\t", @line if scalar(@line) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @line > 1 1 7 8
411 7 return join ' ', @core, @data;
412 }
413  
414  
415 =head2 owner name
416  
417 $name = $rr->owner;
418  
419 Returns the owner name of the record.
420  
421 =cut
422  
423 sub owner {
424 2302 ▶ my ( $self, @name ) = @_;
Subroutine owner: called CC=3 SCAR=11.0
Pod documented
425 2302 for (@name) { $self->{owner} = Net::DNS::DomainName1035->new($_) }
426 2302 ▶ return defined wantarray ? $self->{owner}->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 689 1613 2302
427 }
428  
429 424 ▶sub name { return &owner; } ## historical
Subroutine name: called CC=1 SCAR=0.0
Pod documented
430  
431  
432 =head2 type
433  
434 $type = $rr->type;
435  
436 Returns the record type.
437  
438 =cut
439  
440 sub type {
441 2180 ▶ my ( $self, @value ) = @_;
Subroutine type: called CC=2 SCAR=6.9
Pod documented
442 2180 for (@value) { croak 'not possible to change RR->type' }
443 2179 return typebyval( $self->{type} );
444 }
445  
446  
447 =head2 class
448  
449 $class = $rr->class;
450  
451 Resource record class.
452  
453 =cut
454  
455 sub class {
456 2527 ▶ my ( $self, $class ) = @_;
Subroutine class: called CC=3 SCAR=11.0
Pod documented
457 2527 ▶ return $self->{class} = classbyname($class) if defined $class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $class 967 1560 2527
458 1560 ▶ return defined $self->{class} ? classbyval( $self->{class} ) : 'IN';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'class'} ? : 1053 507 1560
459 }
460  
461  
462 =head2 ttl
463  
464 $ttl = $rr->ttl;
465 $ttl = $rr->ttl(3600);
466  
467 Resource record time to live in seconds.
468  
469 =cut
470  
471 # The following time units are recognised, but are not part of the
472 # published API. These are required for parsing BIND zone files but
473 # should not be used in other contexts.
474 96 my %unit = ( W => 604800, D => 86400, H => 3600, M => 60, S => 1 );
475  
476 sub ttl {
477 1129 ▶ my ( $self, $time ) = @_;
Subroutine ttl: called CC=5 SCAR=16.1
Pod documented
478  
479 1129 ▶ return $self->{ttl} || 0 unless defined $time; # avoid defining rr->{ttl}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 533 596 1129
Condition100%2/2
$self->{'ttl'} || 0
l!l
11522
MC/DC100%1/1
$self->{'ttl'} || 0
$self->{'ttl'}
Truth table100%2/2
A
A= $self->{'ttl'}
Aresult
11
00
480  
481 596 my $ttl = 0;
482 596 my %time = reverse split /(\D)\D*/, $time . 'S';
483 596 ▶ while ( my ( $u, $t ) = each %time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %time) 596 595 1191
484 596 ▶ my $scale = $unit{uc $u} || die qq(bad time: $t$u);
Condition100%2/2
$unit{uc $u} || die("bad time: $t$u")
l!l
5951
MC/DC100%1/1
$unit{uc $u} || die("bad time: $t$u")
$unit{uc $u}
Truth table100%2/2
A
A= $unit{uc $u}
Aresult
11
00
485 595 $ttl += $t * $scale;
486 }
487 595 return $self->{ttl} = $ttl;
488 }
489  
490  
491 ################################################################################
492 ##
493 ## Default implementation for unknown RR type
494 ##
495 ################################################################################
496  
497 sub _decode_rdata { ## decode rdata from wire-format octet string
498 7 ▶ my ( $self, $data, $offset ) = @_;
Subroutine _decode_rdata: called CC=1 SCAR=0.0
499 7 return $self->{rdata} = substr $$data, $offset, $self->{rdlength};
500 }
501  
502  
503 sub _encode_rdata { ## encode rdata as wire-format octet string
504 12 ▶ return shift->{rdata};
Subroutine _encode_rdata: called CC=1 SCAR=0.0
505 }
506  
507  
508 sub _format_rdata { ## format rdata portion of RR string
509 7 ▶ my $rdata = shift->rdata; # RFC3597 unknown RR format
Subroutine _format_rdata: called CC=1 SCAR=0.0
510 7 return ( '\\#', length($rdata), split /(\S{32})/, unpack 'H*', $rdata );
511 }
512  
513  
514 sub _parse_rdata { ## parse RR attributes in argument list
515 2 ▶ my $self = shift;
Subroutine _parse_rdata: called CC=2 SCAR=6.9
516 2 ▶ die join ' ', 'type', $self->type, 'not implemented' if ref($self) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq __PACKAGE__ 1 1 2
517 1 die join ' ', 'no zone file representation defined for', $self->type;
518 }
519  
520  
521 ▶sub _post_parse { } ## parser post processing
Subroutine _post_parse: called CC=1 SCAR=0.0
522  
523  
524 ▶sub _defaults { } ## set attribute default values
Subroutine _defaults: called CC=1 SCAR=0.0
525  
526  
527 sub dump { ## print internal data structure
528 2 ▶ my @data = @_; # uncoverable pod
Subroutine dump: called CC=4 SCAR=13.9
Pod undocumented
529 2 require Data::Dumper;
530 2 ▶ local $Data::Dumper::Maxdepth = $Data::Dumper::Maxdepth || 6;
Condition100%2/2
$Data::Dumper::Maxdepth || 6
l!l
11
MC/DC100%1/1
$Data::Dumper::Maxdepth || 6
$Data::Dumper::Maxdepth
Truth table100%2/2
A
A= $Data::Dumper::Maxdepth
Aresult
11
00
531 2 ▶ local $Data::Dumper::Sortkeys = $Data::Dumper::Sortkeys || 1;
Condition100%2/2
$Data::Dumper::Sortkeys || 1
l!l
11
MC/DC100%1/1
$Data::Dumper::Sortkeys || 1
$Data::Dumper::Sortkeys
Truth table100%2/2
A
A= $Data::Dumper::Sortkeys
Aresult
11
00
532 2 ▶ local $Data::Dumper::Useqq = $Data::Dumper::Useqq || 1;
Condition100%2/2
$Data::Dumper::Useqq || 1
l!l
11
MC/DC100%1/1
$Data::Dumper::Useqq || 1
$Data::Dumper::Useqq
Truth table100%2/2
A
A= $Data::Dumper::Useqq
Aresult
11
00
533 2 return print Data::Dumper::Dumper(@data);
534 }
535  
536 sub rdatastr { ## historical RR subtype method
537 2 ▶ my $self = shift; # uncoverable pod
Subroutine rdatastr: called CC=1 SCAR=0.0
Pod undocumented
538 2 $self->_deprecate('prefer $rr->rdstring()');
539 2 return $self->rdstring;
540 }
541  
542  
543 =head2 rdata
544  
545 $rr = Net::DNS::RR->new( type => NULL, rdata => 'arbitrary' );
546  
547 Resource record data section when viewed as opaque octets.
548  
549 =cut
550  
551 sub rdata {
552 173 ▶ my $self = shift;
Subroutine rdata: called CC=5 SCAR=16.1
Pod documented
553  
554 173 ▶ return $self->_empty ? '' : eval { $self->_encode_rdata( 0x4000, {} ) } unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 139 34 173
$self->_empty ? : 23 116 139
555  
556 34 ▶ my $data = shift || '';
Condition100%2/2
shift() || ''
l!l
268
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
557 34 ▶ $self->_decode_rdata( \$data, 0 ) if ( $self->{rdlength} = length $data );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rdlength"} = length $data 26 8 34
558 32 return;
559 }
560  
561  
562 =head2 rdstring
563  
564 $rdstring = $rr->rdstring;
565  
566 Returns a string representation of the RR-specific data.
567  
568 =cut
569  
570 sub rdstring {
571 88 ▶ my $self = shift;
Subroutine rdstring: called CC=3 SCAR=11.0
Pod documented
572 88 local $SIG{__DIE__};
573  
574 88 ▶ my @rdata = $self->_empty ? () : eval { $self->_format_rdata };
Branch: 1 branch
BranchTrueFalseTotal
$self->_empty ? : 19 69 88
575 88 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 87 88
576  
577 88 return join "\n\t", _wrap(@rdata);
578 }
579  
580  
581 =head2 rdlength
582  
583 $rdlength = $rr->rdlength;
584  
585 Returns the uncompressed length of the encoded RR-specific data.
586  
587 =cut
588  
589 sub rdlength {
590 7 ▶ return length shift->rdata;
Subroutine rdlength: called CC=1 SCAR=0.0
Pod documented
591 }
592  
593  
594 ###################################################################################
595  
596 =head1 Sorting of RR arrays
597  
598 Sorting of RR arrays is done by Net::DNS::rrsort(), see documentation
599 for L<Net::DNS>. This package provides class methods to set the
600 comparator function used for a particular RR based on its attributes.
601  
602  
603 =head2 set_rrsort_func
604  
605 my $function = sub { ## numerically ascending order
606 $Net::DNS::a->{'preference'} <=> $Net::DNS::b->{'preference'};
607 };
608  
609 Net::DNS::RR::MX->set_rrsort_func( 'preference', $function );
610  
611 Net::DNS::RR::MX->set_rrsort_func( 'default_sort', $function );
612  
613 set_rrsort_func() must be called as a class method. The first argument is
614 the attribute name on which the sorting is to take place. If you specify
615 "default_sort" then that is the sort algorithm that will be used when
616 get_rrsort_func() is called without an RR attribute as argument.
617  
618 The second argument is a reference to a comparator function that uses the
619 global variables $a and $b in the Net::DNS package. During sorting, the
620 variables $a and $b will contain references to objects of the class whose
621 set_rrsort_func() was called. The above sorting function will only be
622 applied to Net::DNS::RR::MX objects.
623  
624 The above example is the sorting function implemented in MX.
625  
626 =cut
627  
628 96 our %rrsortfunct;
629  
630 sub set_rrsort_func {
631 42 ▶ my $class = shift;
Subroutine set_rrsort_func: called CC=1 SCAR=0.0
Pod documented
632 42 my $attribute = shift;
633 42 my $function = shift;
634  
635 42 my ($type) = $class =~ m/::([^:]+)$/;
636 42 $rrsortfunct{$type}{$attribute} = $function;
637 42 return;
638 }
639  
640  
641 =head2 get_rrsort_func
642  
643 $function = Net::DNS::RR::MX->get_rrsort_func('preference');
644 $function = Net::DNS::RR::MX->get_rrsort_func();
645  
646 get_rrsort_func() returns a reference to the comparator function.
647  
648 =cut
649  
650 30 ▶my $default = sub { return $Net::DNS::a->canonical() cmp $Net::DNS::b->canonical(); };
Subroutine __ANON__: called CC=1 SCAR=0.0
651  
652 sub get_rrsort_func {
653 13 ▶ my $class = shift;
Subroutine get_rrsort_func: called CC=3 SCAR=11.0
Pod documented
654 13 ▶ my $attribute = shift || 'default_sort';
Condition100%2/2
shift() || 'default_sort'
l!l
67
MC/DC100%1/1
shift() || 'default_sort'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
655  
656 13 my ($type) = $class =~ m/::([^:]+)$/;
657  
658 13 ▶ return $rrsortfunct{$type}{$attribute} || return $default;
Condition100%2/2
$Net::DNS::RR::rrsortfunct{$type}{$attribute} || (return $default)
l!l
85
MC/DC100%1/1
$Net::DNS::RR::rrsortfunct{$type}{$attribute} || (return $default)
$Net::DNS::RR::rrsortfunct{$type}{$attribute}
Truth table100%2/2
A
A= $Net::DNS::RR::rrsortfunct{$type}{$attribute}
Aresult
11
00
659 }
660  
661  
662 ################################################################################
663 #
664 # Net::DNS::RR->_subclass($rrname)
665 # Net::DNS::RR->_subclass($rrname, $default)
666 #
667 # Create a new object blessed into appropriate RR subclass, after
668 # loading the subclass module (if necessary). A subclass with no
669 # corresponding module will be regarded as unknown and blessed
670 # into the RR base class.
671 #
672 # The optional second argument indicates that default values are
673 # to be copied into the newly created object.
674  
675 96 our %_MINIMAL = ( 255 => bless ['type' => 255], __PACKAGE__ );
676 96 our %_LOADED = %_MINIMAL;
677  
678 sub _subclass {
679 11554 ▶ my ( $class, $rrname, $default ) = @_;
Subroutine _subclass: called CC=6 SCAR=17.9
680  
681 11554 ▶ unless ( $_LOADED{$rrname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Net::DNS::RR::_LOADED{$rrname}) 184 11370 11554
682 184 my $rrtype = typebyname($rrname);
683  
684 183 ▶ unless ( $_LOADED{$rrtype} ) { # load once only
Branch: 1 branch
BranchTrueFalseTotal
unless ($Net::DNS::RR::_LOADED{$rrtype}) 173 10 183
685 173 local @INC = LIB;
686  
687 173 my $identifier = typebyval($rrtype);
688 173 $identifier =~ s/\W/_/g; # kosher Perl identifier
689  
690 173 my $subclass = join '::', __PACKAGE__, $identifier;
691  
692 173 ▶ unless ( eval "require $subclass" ) { ## no critic ProhibitStringyEval
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $subclass") 1 172 173
693 1 my $perl = Net::DNS::Parameters::_typespec("$rrtype.RRTYPE");
694 1 $subclass = join '::', __PACKAGE__, "TYPE$rrtype";
695 push @INC, sub { # see perldoc -f require
696 1 ▶ my @line = split /\n/, $perl;
Subroutine __ANON__: called CC=1 SCAR=0.0
697 1 ▶ return ( sub { defined( $_ = shift @line ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
698 1 };
699 1 eval "require $subclass"; ## no critic ProhibitStringyEval
700 }
701  
702 173 ▶ $subclass = __PACKAGE__ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 172 173
703  
704 # cache pre-built minimal and populated default object images
705 173 my @base = ( 'type' => $rrtype );
706 173 $_MINIMAL{$rrtype} = bless [@base], $subclass;
707  
708 173 my $object = bless {@base}, $subclass;
709 173 $object->_defaults;
710 173 $_LOADED{$rrtype} = bless [%$object], $subclass;
711 }
712  
713 183 $_MINIMAL{$rrname} = $_MINIMAL{$rrtype};
714 183 $_LOADED{$rrname} = $_LOADED{$rrtype};
715 }
716  
717 11553 ▶ my $prebuilt = $default ? $_LOADED{$rrname} : $_MINIMAL{$rrname};
Branch: 1 branch
BranchTrueFalseTotal
$default ? : 1222 10331 11553
718 11553 return bless {@$prebuilt}, ref($prebuilt); # create object
719 }
720  
721  
722 sub _annotation {
723 1177 ▶ my ( $self, @note ) = @_;
Subroutine _annotation: called CC=4 SCAR=13.9
724 1177 ▶ push @{$self->{annotation}}, "\t; @note" if scalar @note;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @note 59 1118 1177
725 1177 ▶ return wantarray ? @{delete( $self->{annotation} ) || []} : undef;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 1118 59 1177
delete $self->{"annotation"} or [] 1061 57 1118
726 }
727  
728  
729 96 my %warned;
730  
731 sub _deprecate {
732 9 ▶ my ( undef, @note ) = @_;
Subroutine _deprecate: called CC=2 SCAR=6.9
733 9 ▶ carp "deprecated method; @note" unless $warned{"@note"}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $warned{join $", @note}++ 5 4 9
734 9 return;
735 }
736  
737  
738 96 my %ignore = map { ( $_ => 1 ) } @core, 'annotation', '#';
739  
740 sub _empty {
741 2802 ▶ my $self = shift;
Subroutine _empty: called CC=2 SCAR=6.9
742 2802 ▶ return not( $self->{'#'} ||= scalar grep { !$ignore{$_} } keys %$self );
Condition100%2/2
$self->{"#"} ||= scalar grep({not $ignore{$_};} keys %$self)
l!l
14321370
MC/DC100%1/1
$self->{"#"} ||= scalar grep({not $ignore{$_};} keys %$self)
$self->{"#"}
Truth table100%2/2
A
A= $self->{"#"}
Aresult
11
00
743 }
744  
745  
746 sub _wrap {
747 2367 ▶ my @text = @_;
Subroutine _wrap: called CC=7 SCAR=19.5
748 2367 my $cols = 80;
749 2367 my $coln = 0;
750  
751 2367 my ( @line, @fill );
752 2367 foreach (@text) {
753 5627 ▶ $coln += ( length || next ) + 1;
Condition100%2/2
length $_ || (next)
l!l
559136
MC/DC100%1/1
length $_ || (next)
length $_
Truth table100%2/2
A
A= length $_
Aresult
11
00
754 5591 ▶ if ( $coln > $cols ) { # start new line
Branch: 1 branch
BranchTrueFalseTotal
if ($coln > $cols) 1779 3812 5591
755 1779 ▶ push( @line, join ' ', @fill ) if @fill;
Branch: 1 branch
BranchTrueFalseTotal
if @fill 1768 11 1779
756 1779 $coln = length;
757 1779 @fill = ();
758 }
759 5591 ▶ $coln = $cols if chomp; # force line break
Branch: 1 branch
BranchTrueFalseTotal
if chomp $_ 91 5500 5591
760 5591 ▶ push( @fill, $_ ) if length;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ 5541 50 5591
761 }
762 2367 return ( @line, join ' ', @fill );
763 }
764  
765  
766 ################################################################################
767  
768 ▶sub DESTROY { } ## Avoid tickling AUTOLOAD (in cleanup)
Subroutine DESTROY: called CC=1 SCAR=0.0
769  
770 ## no critic
771 sub AUTOLOAD { ## Default method
772 11 ▶ my ($self) = @_;
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
773  
774 96 ▶ no strict 'refs'; ## no critic ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
775 11 our $AUTOLOAD;
776 11 my ($method) = reverse split /::/, $AUTOLOAD;
777  
778 11 my $canonical = lc($method); ## tolerate mixed-case attribute name
779 11 ▶ if ( $self->can($canonical) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($canonical)) 6 5 11
780 6 ▶ *{$AUTOLOAD} = sub { shift->$canonical(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
781 6 return &$AUTOLOAD;
782 }
783  
784 5 my $oref = ref($self);
785 5 ▶ *{$AUTOLOAD} = sub { }; ## suppress deep recursion
Subroutine __ANON__: called CC=1 SCAR=0.0
786 5 ▶ croak qq[$self has no class method "$method"] unless $oref;
Branch: 1 branch
BranchTrueFalseTotal
unless $oref 1 4 5
787  
788 4 my $string = $self->string;
789 4 my @object = grep { defined($_) } $oref, $oref->VERSION;
790 4 my $module = join '::', __PACKAGE__, $self->type;
791 4 ▶ eval("require $module") if $oref eq __PACKAGE__; ## no critic ProhibitStringyEval
Branch: 1 branch
BranchTrueFalseTotal
if $oref eq __PACKAGE__ 1 3 4
792  
793 4 @_ = (<<"END");
794 *** FATAL PROGRAM ERROR!! Unknown instance method "$method"
795 *** which the program has attempted to call for the object:
796 ***
797 $string
798 ***
799 *** THIS IS A BUG IN THE CALLING SOFTWARE, which incorrectly assumes
800 *** that the object would be of a particular type. The type of an
801 *** object should be checked before calling any of its methods.
802 ***
803 @object
804 $@
805 END
806 4 goto &Carp::confess;
807 }
808  
809  
810 96 1;
811 __END__
« DNS/DomainName.pm ↑ Summary DNS/RR/AMTRELAY.pm »