DNS/ZoneFile.pm

filter: Statementstmt 100% 251 / 251 Branchbran 100% 108 / 108 Conditioncond 100% 35 / 35 MC/DCmcdc 100% 21 / 21 Subroutinesub 100% 35 / 35 Podpod 100% 8 / 8 totaltotal 100% 458 / 458 CC 70 SCAR 21.5
CC
70
Cov
100%
_getline
23.0
_format
8.0
_getRR
8.0
CRAP 8.6
« DNS/Resolver/MSWin32.pm ↑ Summary DNS/Question.pm »
1 package Net::DNS::ZoneFile;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 our $VERSION = (qw$Id: ZoneFile.pm 2002 2025-01-07 09:57:46Z willem $)[2];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::ZoneFile - DNS zone file
12  
13 =head1 SYNOPSIS
14  
15 use Net::DNS::ZoneFile;
16  
17 $zonefile = Net::DNS::ZoneFile->new( 'named.example' );
18  
19 while ( $rr = $zonefile->read ) {
20 $rr->print;
21 }
22  
23 @zone = $zonefile->read;
24  
25  
26 =head1 DESCRIPTION
27  
28 Each Net::DNS::ZoneFile object instance represents a zone file
29 together with any subordinate files introduced by the $INCLUDE
30 directive. Zone file syntax is defined by RFC1035.
31  
32 A program may have multiple zone file objects, each maintaining
33 its own independent parser state information.
34  
35 The parser supports both the $TTL directive defined by RFC2308
36 and the BIND $GENERATE syntax extension.
37  
38 All RRs in a zone file must have the same class, which may be
39 specified for the first RR encountered and is then propagated
40 automatically to all subsequent records.
41  
42 =cut
43  
44  
45 12 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 12 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 12 our @EXPORT = qw(parse read readfh);
50  
51 12 ▶use constant UTF8 => scalar eval { ## not UTF-EBCDIC [see Unicode TR#16 3.6]
Subroutine BEGIN: called CC=1 SCAR=0.0
52 12 require Encode;
53 12 Encode::encode_utf8( chr(182) ) eq pack( 'H*', 'C2B6' );
54 12 };
55  
56 12 require IO::File;
57 12 require PerlIO;
58 12 require Net::DNS::Domain;
59 12 require Net::DNS::RR;
60  
61  
62 =head1 METHODS
63  
64  
65 =head2 new
66  
67 $zonefile = Net::DNS::ZoneFile->new( 'filename', ['example.com'] );
68  
69 $handle = IO::File->new( 'filename', '<:encoding(ISO8859-7)' );
70 $zonefile = Net::DNS::ZoneFile->new( $handle, ['example.com'] );
71  
72 The new() constructor returns a Net::DNS::ZoneFile object which
73 represents the zone file specified in the argument list.
74  
75 The specified file or file handle is open for reading and closed when
76 exhausted or all references to the ZoneFile object cease to exist.
77  
78 The optional second argument specifies $ORIGIN for the zone file.
79  
80 Zone files are presumed to be UTF-8 encoded where that is supported.
81  
82 Alternative character encodings may be specified indirectly by creating
83 a file handle with the desired encoding layer, which is then passed as
84 an argument to new(). The specified encoding is propagated to files
85 introduced by $INCLUDE directives.
86  
87 =cut
88  
89 67 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
90 67 my ( $class, $filename, $origin ) = @_;
91 67 my $self = bless {fileopen => {}}, $class;
92  
93 67 $self->_origin($origin);
94  
95 67 ▶ if ( ref($filename) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename) 16 51 67
96 16 $self->{filehandle} = $self->{filename} = $filename;
97 16 ▶ return $self if ref($filename) =~ /IO::File|FileHandle|GLOB|Text/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($filename) =~ /IO::File|FileHandle|GLOB|Text/ 15 1 16
98 1 croak 'argument not a file handle';
99 }
100  
101 51 ▶ croak 'filename argument undefined' unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 1 50 51
102 50 my $discipline = UTF8 ? '<:encoding(UTF-8)' : '<';
103 50 ▶ $self->{filehandle} = IO::File->new( $filename, $discipline ) or croak "$filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"filehandle"} = "IO::File"->new($filename, $discipline) or croak("${filename}: $!") 46 4 50
104 46 $self->{fileopen}->{$filename}++;
105 46 $self->{filename} = $filename;
106 46 return $self;
107 }
108  
109  
110 =head2 read
111  
112 $rr = $zonefile->read;
113 @rr = $zonefile->read;
114  
115 When invoked in scalar context, read() returns a Net::DNS::RR object
116 representing the next resource record encountered in the zone file,
117 or undefined if end of data has been reached.
118  
119 When invoked in list context, read() returns the list of Net::DNS::RR
120 objects in the order that they appear in the zone file.
121  
122 Comments and blank lines are silently disregarded.
123  
124 $INCLUDE, $ORIGIN, $TTL and $GENERATE directives are processed
125 transparently.
126  
127 =cut
128  
129 101 ▶sub read {
Subroutine read: called CC=6 SCAR=17.9
Pod documented
130 101 my ($self) = @_;
131  
132 101 ▶ return &_read unless ref $self; # compatibility interface
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 5 96 101
133  
134 96 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 9 87 96
135 9 my @zone; # return entire zone
136 9 eval {
137 9 local $SIG{__DIE__};
138 9 ▶ while ( my $rr = $self->_getRR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rr = $self->_getRR) 183 8 191
139 183 push( @zone, $rr );
140 }
141 };
142 9 ▶ croak join ' ', $@, ' file', $self->name, 'line', $self->line, "\n " if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 8 9
143 8 return @zone;
144 }
145  
146 87 my $rr = eval {
147 87 local $SIG{__DIE__};
148 87 $self->_getRR; # return single RR
149 };
150 87 ▶ croak join ' ', $@, ' file', $self->name, 'line', $self->line, "\n " if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 26 61 87
151 61 return $rr;
152 }
153  
154  
155 =head2 name
156  
157 $filename = $zonefile->name;
158  
159 Returns the name of the current zone file.
160 Embedded $INCLUDE directives will cause this to differ from the
161 filename argument supplied when the object was created.
162  
163 =cut
164  
165 40 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
166 40 return shift->{filename};
167 }
168  
169  
170 =head2 line
171  
172 $line = $zonefile->line;
173  
174 Returns the number of the last line read from the current zone file.
175  
176 =cut
177  
178 91 ▶sub line {
Subroutine line: called CC=2 SCAR=6.9
Pod documented
179 91 my $self = shift;
180 91 ▶ return $self->{eom} if defined $self->{eom};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"eom"} 2 89 91
181 89 return $self->{filehandle}->input_line_number;
182 }
183  
184  
185 =head2 origin
186  
187 $origin = $zonefile->origin;
188  
189 Returns the fully qualified name of the current origin within the
190 zone file.
191  
192 =cut
193  
194 5 ▶sub origin {
Subroutine origin: called CC=1 SCAR=0.0
Pod documented
195 5 my $context = shift->{context};
196 5 ▶ return &$context( sub { Net::DNS::Domain->new('@') } )->string;
Subroutine __ANON__: called CC=1 SCAR=0.0
197 }
198  
199  
200 =head2 ttl
201  
202 $ttl = $zonefile->ttl;
203  
204 Returns the default TTL as specified by the $TTL directive.
205  
206 =cut
207  
208 2 ▶sub ttl {
Subroutine ttl: called CC=1 SCAR=0.0
Pod documented
209 2 return shift->{TTL};
210 }
211  
212  
213 =head1 COMPATIBILITY WITH Net::DNS::ZoneFile 1.04
214  
215 Applications which depended on the defunct Net::DNS::ZoneFile 1.04
216 CPAN distribution will continue to operate with minimal change using
217 the compatibility interface described below.
218 New application code should use the object-oriented interface.
219  
220 use Net::DNS::ZoneFile;
221  
222 $listref = Net::DNS::ZoneFile->read( $filename );
223 $listref = Net::DNS::ZoneFile->read( $filename, $include_dir );
224  
225 $listref = Net::DNS::ZoneFile->readfh( $filehandle );
226 $listref = Net::DNS::ZoneFile->readfh( $filehandle, $include_dir );
227  
228 $listref = Net::DNS::ZoneFile->parse( $string );
229 $listref = Net::DNS::ZoneFile->parse( $string, $include_dir );
230 $listref = Net::DNS::ZoneFile->parse( \$string );
231 $listref = Net::DNS::ZoneFile->parse( \$string, $include_dir );
232  
233 $_->print for @$listref;
234  
235 The optional second argument specifies the default path for filenames.
236 The current working directory is used by default.
237  
238 Although not available in the original implementation, the RR list can
239 be obtained directly by calling any of these methods in list context.
240  
241 @rr = Net::DNS::ZoneFile->read( $filename, $include_dir );
242  
243 The partial result is returned if an error is encountered by the parser.
244  
245  
246 =head2 read
247  
248 $listref = Net::DNS::ZoneFile->read( $filename );
249 $listref = Net::DNS::ZoneFile->read( $filename, $include_dir );
250  
251 read() parses the contents of the specified file
252 and returns a reference to the list of Net::DNS::RR objects.
253 The return value is undefined if an error is encountered by the parser.
254  
255 =cut
256  
257 12 our $include_dir; ## dynamically scoped
258  
259 18 ▶sub _filename { ## rebase unqualified filename
Subroutine _filename: called CC=5 SCAR=16.1
260 18 my $name = shift;
261 18 ▶ return $name if ref($name); ## file handle
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 7 11 18
262 11 ▶ return $name unless $include_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::DNS::ZoneFile::include_dir 8 3 11
263 3 require File::Spec;
264 3 ▶ return $name if File::Spec->file_name_is_absolute($name);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($name) 1 2 3
265 2 ▶ return $name if -f $name; ## file in current directory
Branch: 1 branch
BranchTrueFalseTotal
if -f $name 1 1 2
266 1 return File::Spec->catfile( $include_dir, $name );
267 }
268  
269  
270 12 ▶sub _read {
Subroutine _read: called CC=7 SCAR=19.5
271 12 my ($arg1) = @_;
272 12 ▶ shift if !ref($arg1) && $arg1 eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $arg1 and $arg1 eq __PACKAGE__ 4 8 12
Condition100%3/3
not ref $arg1 and $arg1 eq __PACKAGE__
!ll&&!rl&&r
714
MC/DC100%2/2
not ref $arg1 and $arg1 eq __PACKAGE__
not ref $arg1 $arg1 eq __PACKAGE__
Truth table100%3/3
not(A) and B
A= not ref $arg1B= $arg1 eq __PACKAGE__
ABresult
0X0
100
111
273 12 my $filename = shift;
274 12 local $include_dir = shift;
275  
276 12 my $zonefile = Net::DNS::ZoneFile->new( _filename($filename) );
277 10 my @zone;
278 10 eval {
279 10 local $SIG{__DIE__};
280 10 my $rr;
281 10 ▶ push( @zone, $rr ) while $rr = $zonefile->_getRR;
Branch: 1 branch
BranchTrueFalseTotal
$rr = $zonefile->_getRR and do { push @zone, $rr } 24 8 32
282 };
283 10 ▶ return wantarray ? @zone : \@zone unless $@;
Branch: 2 branches
BranchTrueFalseTotal
unless $@ 8 2 10
wantarray ? : 4 4 8
284 2 carp $@;
285 2 ▶ return wantarray ? @zone : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
286 }
287  
288  
289 {
290  
291 package Net::DNS::ZoneFile::Text; ## no critic ProhibitMultiplePackages
292  
293 12 ▶ use overload ( '<>' => 'readline' );
Subroutine BEGIN: called CC=1 SCAR=0.0
294  
295 7 ▶ sub new {
Subroutine new: called CC=2 SCAR=6.9
296 7 my ( $class, $data ) = @_;
297 7 my $self = bless {}, $class;
298 7 ▶ $self->{data} = [split /\n/, ref($data) ? $$data : $data];
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 1 6 7
299 7 return $self;
300 }
301  
302 40 ▶ sub readline {
Subroutine readline: called CC=1 SCAR=0.0
303 40 my $self = shift;
304 40 $self->{line}++;
305 40 return shift( @{$self->{data}} );
306 }
307  
308 5 ▶ sub close {
Subroutine close: called CC=1 SCAR=0.0
309 5 shift->{data} = [];
310 5 return 1;
311 }
312  
313 5 ▶ sub input_line_number {
Subroutine input_line_number: called CC=1 SCAR=0.0
314 5 return shift->{line};
315 }
316  
317 }
318  
319  
320 =head2 readfh
321  
322 $listref = Net::DNS::ZoneFile->readfh( $filehandle );
323 $listref = Net::DNS::ZoneFile->readfh( $filehandle, $include_dir );
324  
325 readfh() parses data from the specified file handle
326 and returns a reference to the list of Net::DNS::RR objects.
327 The return value is undefined if an error is encountered by the parser.
328  
329 =cut
330  
331 12 ▶sub readfh {
Subroutine readfh: called CC=1 SCAR=0.0
Pod documented
332 7 return &_read;
333 }
334  
335  
336 =head2 parse
337  
338 $listref = Net::DNS::ZoneFile->parse( $string );
339 $listref = Net::DNS::ZoneFile->parse( $string, $include_dir );
340 $listref = Net::DNS::ZoneFile->parse( \$string );
341 $listref = Net::DNS::ZoneFile->parse( \$string, $include_dir );
342  
343 parse() interprets the text in the argument string
344 and returns a reference to the list of Net::DNS::RR objects.
345 The return value is undefined if an error is encountered by the parser.
346  
347 =cut
348  
349 7 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
350 7 my ($arg1) = @_;
351 7 ▶ shift if $arg1 eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $arg1 eq __PACKAGE__ 6 1 7
352 7 my $string = shift;
353 7 my @include = grep {defined} shift;
354 7 return &readfh( Net::DNS::ZoneFile::Text->new($string), @include );
355 }
356  
357  
358 ########################################
359  
360  
361 {
362  
363 package Net::DNS::ZoneFile::Generator; ## no critic ProhibitMultiplePackages
364  
365 12 ▶ use overload ( '<>' => 'readline' );
Subroutine BEGIN: called CC=1 SCAR=0.0
366  
367 12 ▶ sub new {
Subroutine new: called CC=6 SCAR=17.9
368 12 my ( $class, $range, $template, $line ) = @_;
369 12 my $self = bless {}, $class;
370  
371 12 my ( $bound, $step ) = split m#[/]#, $range; # initial iterator state
372 12 my ( $first, $last ) = split m#[-]#, $bound;
373 12 ▶ $first ||= 0;
Condition100%2/2
$first ||= 0
l!l
111
MC/DC100%1/1
$first ||= 0
$first
Truth table100%2/2
A
A= $first
Aresult
11
00
374 12 ▶ $last ||= $first;
Condition100%3/3
$last ||= $first
l!l&&r!l&&!r
921
MC/DC100%2/2
$last ||= $first
$last $first
Truth table100%3/3
A ||= B
A= $lastB= $first
ABresult
1X1
011
000
375 12 ▶ $step ||= 1; # coerce step to match range
Condition100%2/2
$step ||= 1
l!l
210
MC/DC100%1/1
$step ||= 1
$step
Truth table100%2/2
A
A= $step
Aresult
11
00
376 12 ▶ $step = ( $last < $first ) ? -abs($step) : abs($step);
Branch: 1 branch
BranchTrueFalseTotal
$last < $first ? : 1 11 12
377 12 $self->{count} = int( ( $last - $first ) / $step ) + 1;
378  
379 12 for ($template) {
380 12 s/\\\$/\\036/g; # disguise escaped dollar
381 12 s/\$\$/\\036/g; # disguise escaped dollar
382 12 s/^"(.*)"$/$1/s; # unwrap BIND's quoted template
383 12 @{$self}{qw(instant step template line)} = ( $first, $step, $_, $line );
384 }
385 12 return $self;
386 }
387  
388 27 ▶ sub readline {
Subroutine readline: called CC=3 SCAR=11.0
389 27 my $self = shift;
390 27 ▶ return unless $self->{count}-- > 0; # EOF
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"count"}-- > 0 11 16 27
391  
392 16 my $instant = $self->{instant}; # update iterator state
393 16 $self->{instant} += $self->{step};
394  
395 16 local $_ = $self->{template}; # copy template
396 16 ▶ while (/\$\{(.*)\}/) { # interpolate ${...}
Branch: 1 branch
BranchTrueFalseTotal
if (/\$\{(.*)\}/) 10 15 25
397 10 my $s = _format( $instant, split /\,/, $1 );
398 9 s/\$\{$1\}/$s/eg;
399 }
400  
401 15 s/\$/$instant/eg; # interpolate $
402 15 s/\\036/\$/g; # reinstate escaped $
403 15 return $_;
404 }
405  
406 11 ▶ sub close {
Subroutine close: called CC=1 SCAR=0.0
407 11 shift->{count} = 0; # suppress iterator
408 11 return 1;
409 }
410  
411 12 ▶ sub input_line_number {
Subroutine input_line_number: called CC=1 SCAR=0.0
412 12 return shift->{line}; # fixed: identifies $GENERATE
413 }
414  
415  
416 10 ▶ sub _format { ## convert $GENERATE iteration number to specified format
Subroutine _format: called CC=8 SCAR=20.8
417 10 my $number = shift; # per ISC BIND 9.7
418 10 ▶ my $offset = shift || 0;
Condition100%2/2
shift() || 0
l!l
19
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
419 10 ▶ my $length = shift || 0;
Condition100%2/2
shift() || 0
l!l
46
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
420 10 ▶ my $format = shift || 'd';
Condition100%2/2
shift() || 'd'
l!l
91
MC/DC100%1/1
shift() || 'd'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
421  
422 10 my $value = $number + $offset;
423 10 ▶ my $digit = $length || 1;
Condition100%2/2
$length || 1
l!l
46
MC/DC100%1/1
$length || 1
$length
Truth table100%2/2
A
A= $length
Aresult
11
00
424 10 ▶ return substr sprintf( "%01.$digit$format", $value ), -$length if $format =~ /[doxX]/;
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /[doxX]/ 7 3 10
425  
426 3 my $nibble = join( '.', split //, sprintf ".%32.32lx", $value );
427 3 ▶ return reverse lc( substr $nibble, -$length ) if $format =~ /[n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /[n]/ 1 2 3
428 2 ▶ return reverse uc( substr $nibble, -$length ) if $format =~ /[N]/;
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /[N]/ 1 1 2
429 1 die "unknown $format format";
430 }
431  
432 }
433  
434  
435 12 ▶sub _generate { ## expand $GENERATE into input stream
Subroutine _generate: called CC=1 SCAR=0.0
436 12 my ( $self, $range, $template ) = @_;
437  
438 12 my $handle = Net::DNS::ZoneFile::Generator->new( $range, $template, $self->line );
439  
440 12 $self->{parent} = bless {%$self}, ref($self); # save state, create link
441 12 delete $self->{latest}; # forget current domain name
442 12 return $self->{filehandle} = $handle;
443 }
444  
445  
446 12 my $LEX_REGEX = q/("[^"]*"|"[^"]*$)|;[^\n]*|([()])|[ \t\n\r\f]+/;
447  
448 407 ▶sub _getline { ## get line from current source
Subroutine _getline: called CC=23 SCAR=31.4
449 407 my $self = shift;
450  
451 407 my $fh = $self->{filehandle};
452 407 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 555 45 600
453 555 ▶ next if /^\s*;/; # discard comment line
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*;/ 88 467 555
454 467 ▶ next unless /\S/; # discard blank line
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 79 388 467
455  
456 388 ▶ if (/["(]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/["(]/) 59 329 388
457 59 s/\\\\/\\092/g; # disguise escaped escape
458 59 s/\\"/\\034/g; # disguise escaped quote
459 59 s/\\\(/\\040/g; # disguise escaped bracket
460 59 s/\\\)/\\041/g; # disguise escaped bracket
461 59 s/\\;/\\059/g; # disguise escaped semicolon
462 59 ▶ my @token = grep { defined && length } split /(^\s)|$LEX_REGEX/o;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
61699244
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
463  
464 59 ▶ while ( $token[-1] =~ /^"[^"]*$/ ) { # multiline quoted string
Branch: 1 branch
BranchTrueFalseTotal
if ($token[-1] =~ /^"[^"]*$/) 2 59 61
465 2 $_ = pop(@token) . <$fh>; # reparse fragments
466 2 s/\\\\/\\092/g; # disguise escaped escape
467 2 s/\\"/\\034/g; # disguise escaped quote
468 2 s/\\\(/\\040/g; # disguise escaped bracket
469 2 s/\\\)/\\041/g; # disguise escaped bracket
470 2 s/\\;/\\059/g; # disguise escaped semicolon
471 2 ▶ push @token, grep { defined && length } split /$LEX_REGEX/o;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
443
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
472 2 $_ = join ' ', @token; # reconstitute RR string
473 }
474  
475 59 ▶ if ( grep { $_ eq '(' } @token ) { # concatenate multiline RR
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "(";} @token) 24 35 59
476 24 ▶ until ( grep { $_ eq ')' } @token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq ")";} @token) 84 24 108
477 84 $_ = pop(@token) . <$fh>;
478 84 s/\\\\/\\092/g; # disguise escaped escape
479 84 s/\\"/\\034/g; # disguise escaped quote
480 84 s/\\\(/\\040/g; # disguise escaped bracket
481 84 s/\\\)/\\041/g; # disguise escaped bracket
482 84 s/\\;/\\059/g; # disguise escaped semicolon
483 84 ▶ push @token, grep { defined && length } split /$LEX_REGEX/o;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
61032391
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
484 84 ▶ chomp $token[-1] unless $token[-1] =~ /^"[^"]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $token[-1] =~ /^"[^"]*$/ 83 1 84
485 }
486 24 $_ = join ' ', @token; # reconstitute RR string
487 }
488 }
489  
490 388 ▶ return $_ unless /^[\$]/; # RR string
Branch: 1 branch
BranchTrueFalseTotal
unless /^[\$]/ 352 36 388
491  
492 36 ▶ my @token = grep { defined && length } split /$LEX_REGEX/o;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
1281100
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
493 36 ▶ if (/^\$INCLUDE/) { # directive
Branch: 4 branches
BranchTrueFalseTotal
if (/^\$INCLUDE/) { } 7 29 36
elsif (/^\$GENERATE/) { } 13 16 29
elsif (/^\$ORIGIN/) { } 9 7 16
elsif (/^\$TTL/) { } 4 3 7
494 7 my ( $keyword, @argument ) = @token;
495 7 ▶ die '$INCLUDE incomplete' unless @argument;
Branch: 1 branch
BranchTrueFalseTotal
unless @argument 1 6 7
496 6 $fh = $self->_include(@argument);
497  
498 } elsif (/^\$GENERATE/) { # directive
499 13 my ( $keyword, $range, @template ) = @token;
500 13 ▶ die '$GENERATE incomplete' unless @template;
Branch: 1 branch
BranchTrueFalseTotal
unless @template 1 12 13
501 12 $fh = $self->_generate( $range, "@template" );
502  
503 } elsif (/^\$ORIGIN/) { # directive
504 9 my ( $keyword, $origin ) = @token;
505 9 ▶ die '$ORIGIN incomplete' unless defined $origin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $origin 1 8 9
506 8 $self->_origin($origin);
507  
508 } elsif (/^\$TTL/) { # directive
509 4 my ( $keyword, $ttl ) = @token;
510 4 ▶ die '$TTL incomplete' unless defined $ttl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ttl 1 3 4
511 3 $self->{TTL} = Net::DNS::RR::ttl( {}, $ttl );
512  
513 } else { # unrecognised
514 3 my ($keyword) = @token;
515 3 die qq[unknown "$keyword" directive];
516 }
517 }
518  
519 45 $self->{eom} = $self->line; # end of file
520 45 $fh->close();
521 45 ▶ my $link = $self->{parent} || return; # end of zone
Condition100%2/2
$self->{'parent'} || (return)
l!l
1431
MC/DC100%1/1
$self->{'parent'} || (return)
$self->{'parent'}
Truth table100%2/2
A
A= $self->{'parent'}
Aresult
11
00
522 14 %$self = %$link; # end $INCLUDE
523 14 return $self->_getline; # resume input
524 }
525  
526  
527 313 ▶sub _getRR { ## get RR from current source
Subroutine _getRR: called CC=8 SCAR=20.8
528 313 my $self = shift;
529  
530 313 local $_;
531 313 ▶ $self->_getline || return; # line already in $_
Branch: 1 branch
BranchTrueFalseTotal
$self->_getline or return 287 16 303
532  
533 287 my $noname = s/^\s/\@\t/; # placeholder for empty RR name
534  
535 # construct RR object with context specific dynamically scoped $ORIGIN
536 287 my $context = $self->{context};
537 287 ▶ my $rr = &$context( sub { Net::DNS::RR->_new_string($_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
538  
539 268 my $latest = $self->{latest}; # overwrite placeholder
540 268 ▶ $rr->{owner} = $latest->{owner} if $noname && $latest;
Branch: 1 branch
BranchTrueFalseTotal
if $noname and $latest 15 253 268
Condition100%3/3
$noname and $latest
!ll&&!rl&&r
252115
MC/DC100%2/2
$noname and $latest
$noname $latest
Truth table100%3/3
A and B
A= $nonameB= $latest
ABresult
0X0
100
111
541  
542 268 ▶ $self->{class} = $rr->class unless $self->{class}; # propagate RR class
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"class"} 34 234 268
543 268 $rr->class( $self->{class} );
544  
545 268 ▶ unless ( defined $self->{TTL} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"TTL"}) 243 25 268
546 243 ▶ $self->{TTL} = $rr->minimum if $rr->type eq 'SOA'; # default TTL
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type eq "SOA" 5 238 243
547 }
548 268 ▶ $rr->{ttl} = $self->{TTL} unless defined $rr->{ttl};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rr->{"ttl"} 111 157 268
549  
550 268 return $self->{latest} = $rr;
551 }
552  
553  
554 6 ▶sub _include { ## open $INCLUDE file
Subroutine _include: called CC=4 SCAR=13.9
555 6 my ( $self, $include, $origin ) = @_;
556  
557 6 my $filename = _filename($include);
558 6 ▶ die qq(\$INCLUDE $filename: Unexpected recursion) if $self->{fileopen}->{$filename}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fileopen"}{$filename}++ 1 5 6
559  
560 5 my $discipline = join( ':', '<', PerlIO::get_layers $self->{filehandle} );
561 5 ▶ my $filehandle = IO::File->new( $filename, $discipline ) or die qq(\$INCLUDE $filename: $!);
Branch: 1 branch
BranchTrueFalseTotal
my $filehandle = "IO::File"->new($filename, $discipline) or die "\$INCLUDE ${filename}: $!" 4 1 5
562  
563 4 $self->{parent} = bless {%$self}, ref($self); # save state, create link
564 4 delete $self->{latest}; # forget current domain name
565 4 ▶ $self->_origin($origin) if $origin;
Branch: 1 branch
BranchTrueFalseTotal
if $origin 1 3 4
566 4 $self->{filename} = $filename;
567 4 return $self->{filehandle} = $filehandle;
568 }
569  
570  
571 76 ▶sub _origin { ## change $ORIGIN (scope: current file)
Subroutine _origin: called CC=2 SCAR=6.9
572 76 my ( $self, $name ) = @_;
573 76 my $context = $self->{context};
574 76 ▶ $context = Net::DNS::Domain->origin(undef) unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 67 9 76
575 76 ▶ $self->{context} = &$context( sub { Net::DNS::Domain->origin($name) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
576 76 delete $self->{latest}; # forget previous owner
577 76 return;
578 }
579  
580  
581 12 1;
582 __END__
« DNS/Resolver/MSWin32.pm ↑ Summary DNS/Question.pm »