Record.pm

filter: Statementstmt 80.3% 94 / 117 Branchbran 68.4% 26 / 38 Conditioncond 38.7% 19 / 49 MC/DCmcdc 18.5% 5 / 27 Subroutinesub 92.8% 13 / 14 Podpod 100% 10 / 10 totaltotal 65.4% 167 / 255 CC 42 SCAR 40.6
CC
42
Cov
68%
new
92.3
stringify
5.6
ttl
3.8
CRAP 58.1
↑ Summary Reader.pm »
1  
2 =head1 NAME
3  
4 TinyDNS::Record - Parse a single TinyDNS Record.
5  
6 =head1 DESCRIPTION
7  
8 This module provides an object API to a single TinyDNS record/line.
9  
10 It is not quite valid because:
11  
12 =over 8
13  
14 =item *
15 We ignore SOA records, which Amazon would handle for us.
16  
17 =item *
18 Our TXT records handling uses "T" not ":".
19  
20 =item *
21 Our SRV records are non-standard.
22  
23 =item *
24 Our MX record handling allows a name to be set without IP.
25  
26 =back
27  
28 There are probably other differences.
29  
30 =cut
31  
32 =head1 METHODS
33  
34 =cut
35  
36 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 package TinyDNS::Record;
40  
41  
42 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44  
45 #
46 # Allow our object to treated as a string.
47 #
48 3 ▶use overload '""' => 'stringify';
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50  
51  
52 =head2 new
53  
54 Constructor, which sets the type of the object.
55  
56 The constructor is expected to be passed a valid line of text which
57 describes a single record, for example C<+example.example.com:1.2.3.4:200>.
58  
59 =cut
60  
61 sub new
62 62 ▶{
Subroutine new: called CC=34 SCAR=45.3
Pod documented
63 62 my ( $proto, $line ) = (@_);
64 62 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0620
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
65  
66 62 my $self = {};
67 62 bless( $self, $class );
68  
69  
70 #
71 # Ensure we process as lower-case
72 #
73 62 $line = lc($line);
74  
75 #
76 # Record the line we were created with.
77 #
78 62 $self->{ 'input' } = $line;
79  
80 #
81 # The record-type is the first character.
82 #
83 62 my $rec = substr( $line, 0, 1 );
84  
85 #
86 # Remove the record-type from the line
87 #
88 62 $line = substr( $line, 1 );
89  
90 #
91 # Tokenize - NOTE This is ignored for TXT records,
92 # (because a TXT record used for SPF might have an embedded
93 # ":" for example.)
94 #
95 62 my @data = split( /:/, $line );
96  
97 #
98 # Nasty parsing for each record type..
99 #
100 # We should do better.
101 #
102 #
103 62 ▶ if ( ( $rec eq '+' ) || ( $rec eq '=' ) )
Branch: 8 branches
BranchTrueFalseTotal
if ($rec eq '+' or $rec eq '=') { } 25 37 62
elsif ($rec eq '_') { } 0 37 37
elsif ($rec eq '6') { } 21 16 37
elsif ($rec eq '@') { } 6 10 16
elsif ($rec eq '&') { } 2 8 10
elsif ($rec eq 'c' or $rec eq 'C') { } 2 6 8
elsif ($rec eq 't' or $rec eq 'T') { } 4 2 6
elsif ($rec eq '^') { } 2 0 2
Condition66%2/3
$rec eq "+" or $rec eq "="
l!l&&r!l&&!r
25037
MC/DC50%1/2
$rec eq "+" or $rec eq "="
$rec eq "+" $rec eq "="
Truth table66%2/3
A or B
A= $rec eq "+"B= $rec eq "="
ABresult
1X1
011
000
104 {
105  
106 # name : ipv4 : ttl
107 25 $self->{ 'type' } = "A";
108 25 $self->{ 'name' } = $data[0];
109 25 $self->{ 'value' } = $data[1];
110 25 ▶ $self->{ 'ttl' } = $data[2] || 300;
Condition50%1/2
$data[2] || 300
l!l
250
MC/DC0%0/1
$data[2] || 300
$data[2]
Truth table50%1/2
A
A= $data[2]
Aresult
11
00
111 }
112 elsif ( $rec eq '_' )
113 {
114  
115 # The long-form is:
116 # $name.$proto.$domain : $hostname : $port : $prior : $weight : $ttl
117 #
118 # The short-form is much more common and useful:
119 # $name.$proto.$domain : $hostname : $port : $ttl
120 #
121 0 $self->{ 'type' } = "SRV";
122 0 $self->{ 'name' } = "_" . $data[0];
123  
124 0 ▶ if ( scalar(@data) == 4 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @data == 4) { } 0 0 0
125 {
126 0 ▶ my $host = $data[1] || 0;
Condition0%0/2
$data[1] || 0
l!l
00
MC/DC0%0/1
$data[1] || 0
$data[1]
Truth table0%0/2
A
A= $data[1]
Aresult
11
00
127 0 ▶ my $port = $data[2] || 0;
Condition0%0/2
$data[2] || 0
l!l
00
MC/DC0%0/1
$data[2] || 0
$data[2]
Truth table0%0/2
A
A= $data[2]
Aresult
11
00
128 0 ▶ my $ttl = $data[3] || 300;
Condition0%0/2
$data[3] || 300
l!l
00
MC/DC0%0/1
$data[3] || 300
$data[3]
Truth table0%0/2
A
A= $data[3]
Aresult
11
00
129  
130 # Bogus priority + weight
131 0 $self->{ 'value' } = "1 10 $port $host";
132 0 $self->{ 'ttl' } = $ttl;
133 }
134 else
135 {
136 0 ▶ my $host = $data[1] || 0;
Condition0%0/2
$data[1] || 0
l!l
00
MC/DC0%0/1
$data[1] || 0
$data[1]
Truth table0%0/2
A
A= $data[1]
Aresult
11
00
137 0 ▶ my $port = $data[2] || 0;
Condition0%0/2
$data[2] || 0
l!l
00
MC/DC0%0/1
$data[2] || 0
$data[2]
Truth table0%0/2
A
A= $data[2]
Aresult
11
00
138 0 ▶ my $priority = $data[3] || 1;
Condition0%0/2
$data[3] || 1
l!l
00
MC/DC0%0/1
$data[3] || 1
$data[3]
Truth table0%0/2
A
A= $data[3]
Aresult
11
00
139 0 ▶ my $weight = $data[4] || 10;
Condition0%0/2
$data[4] || 10
l!l
00
MC/DC0%0/1
$data[4] || 10
$data[4]
Truth table0%0/2
A
A= $data[4]
Aresult
11
00
140 0 ▶ my $ttl = $data[5] || 300;
Condition0%0/2
$data[5] || 300
l!l
00
MC/DC0%0/1
$data[5] || 300
$data[5]
Truth table0%0/2
A
A= $data[5]
Aresult
11
00
141  
142 # Bogus priority + weight
143 0 $self->{ 'value' } = "$priority $weight $port $host";
144 0 $self->{ 'ttl' } = $ttl;
145 }
146  
147 }
148 elsif ( $rec eq '6' )
149 {
150  
151 # name : ipv6 : ttl
152 21 $self->{ 'type' } = "AAAA";
153 21 $self->{ 'name' } = $data[0];
154 21 ▶ $self->{ 'ttl' } = $data[2] || 300;
Condition50%1/2
$data[2] || 300
l!l
210
MC/DC0%0/1
$data[2] || 300
$data[2]
Truth table50%1/2
A
A= $data[2]
Aresult
11
00
155  
156 #
157 # Convert an IPv6 record of the form:
158 # "200141c8010b01010000000000000010"
159 # to the expected value:
160 # "2001:41c8:010b:0101:0000:0000:0000:0010".
161 #
162 21 my $ipv6 = $data[1];
163 21 my @tmp = ( $ipv6 =~ m/..../g );
164 21 $self->{ 'value' } = join( ":", @tmp );
165 }
166 elsif ( $rec eq '@' )
167 {
168  
169 #
170 # @name:destination:priority:TTL
171 #
172 6 ▶ if ( scalar(@data) == 4 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @data == 4) 2 4 6
173 {
174 2 $self->{ 'type' } = "MX";
175 2 $self->{ 'name' } = $data[0];
176 2 ▶ $self->{ 'priority' } = $data[2] || "15";
Condition50%1/2
$data[2] || '15'
l!l
20
MC/DC0%0/1
$data[2] || '15'
$data[2]
Truth table50%1/2
A
A= $data[2]
Aresult
11
00
177 2 ▶ $self->{ 'ttl' } = $data[3] || 300;
Condition50%1/2
$data[3] || 300
l!l
20
MC/DC0%0/1
$data[3] || 300
$data[3]
Truth table50%1/2
A
A= $data[3]
Aresult
11
00
178 2 $self->{ 'value' } = $self->{ 'priority' } . " " . $data[1];
179 }
180 6 ▶ if ( scalar(@data) == 3 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @data == 3) 4 2 6
181 {
182 #
183 # @name:destination:priority
184 #
185 4 $self->{ 'type' } = "MX";
186 4 $self->{ 'name' } = $data[0];
187 4 ▶ $self->{ 'priority' } = $data[2] || "15";
Condition50%1/2
$data[2] || '15'
l!l
40
MC/DC0%0/1
$data[2] || '15'
$data[2]
Truth table50%1/2
A
A= $data[2]
Aresult
11
00
188 4 $self->{ 'ttl' } = 300;
189 4 $self->{ 'value' } = $self->{ 'priority' } . " " . $data[1];
190 }
191 }
192 elsif ( $rec eq '&' )
193 {
194  
195 #
196 # NS
197 # &host.example.com:IGNORED:ns1.secure.net:ttl
198 #
199 2 $self->{ 'type' } = "NS";
200 2 $self->{ 'name' } = $data[0];
201 2 $self->{ 'value' } = $data[2];
202 2 ▶ $self->{ 'ttl' } = $data[3] || 300;
Condition50%1/2
$data[3] || 300
l!l
20
MC/DC0%0/1
$data[3] || 300
$data[3]
Truth table50%1/2
A
A= $data[3]
Aresult
11
00
203 }
204 ▶ elsif ( ( $rec eq 'c' ) || ( $rec eq 'C' ) )
Condition66%2/3
$rec eq "c" or $rec eq "C"
l!l&&r!l&&!r
206
MC/DC50%1/2
$rec eq "c" or $rec eq "C"
$rec eq "c" $rec eq "C"
Truth table66%2/3
A or B
A= $rec eq "c"B= $rec eq "C"
ABresult
1X1
011
000
205 {
206  
207 #
208 # name : dest : [ttl]
209 #
210 2 $self->{ 'type' } = "CNAME";
211 2 $self->{ 'name' } = $data[0];
212 2 $self->{ 'value' } = $data[1];
213 2 ▶ $self->{ 'ttl' } = $data[2] || 300;
Condition50%1/2
$data[2] || 300
l!l
20
MC/DC0%0/1
$data[2] || 300
$data[2]
Truth table50%1/2
A
A= $data[2]
Aresult
11
00
214 }
215 ▶ elsif ( ( $rec eq 't' ) || ( $rec eq 'T' ) )
Condition66%2/3
$rec eq "t" or $rec eq "T"
l!l&&r!l&&!r
402
MC/DC50%1/2
$rec eq "t" or $rec eq "T"
$rec eq "t" $rec eq "T"
Truth table66%2/3
A or B
A= $rec eq "t"B= $rec eq "T"
ABresult
1X1
011
000
216 {
217  
218 #
219 # name : "data " : [TTL]
220 #
221 4 ▶ if ( $line =~ /([^:]+):"([^"]+)":*([0-9]*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /([^:]+):"([^"]+)":*([0-9]*)$/) { } 4 0 4
222 {
223 4 $self->{ 'type' } = "TXT";
224 4 $self->{ 'name' } = $1;
225 4 $self->{ 'value' } = "\"$2\"";
226 4 ▶ $self->{ 'ttl' } = $3 || 3600;
Condition100%2/2
$3 || 3600
l!l
31
MC/DC100%1/1
$3 || 3600
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
227 }
228 else
229 {
230 0 die "Invalid TXT record - $line\n";
231 }
232 }
233 elsif ( $rec eq '^' )
234 {
235  
236 #
237 # ptr : "rdns " : [TTL]
238 #
239 2 $self->{ 'type' } = "PTR";
240 2 $self->{ 'name' } = $data[0];
241 2 $self->{ 'value' } = $data[1];
242 2 ▶ $self->{ 'ttl' } = $data[2] || 300;
Condition100%2/2
$data[2] || 300
l!l
11
MC/DC100%1/1
$data[2] || 300
$data[2]
Truth table100%2/2
A
A= $data[2]
Aresult
11
00
243 }
244 else
245 {
246 0 carp "Unknown record type [$rec]: $line";
247 0 return;
248 }
249 62 return $self;
250  
251 }
252  
253  
254 =head2 input
255  
256 Return the text that this record was created with.
257  
258 =cut
259  
260 sub input
261 0 ▶{
Subroutine input: not called CC=1 SCAR=6.9
Pod documented
262 0 my ($self) = (@_);
263  
264 0 return ( $self->{ 'input' } );
265 }
266  
267  
268 =head2 valid
269  
270 Is this record valid? Return 0 or 1 as appropriate.
271  
272 =cut
273  
274 sub valid
275 61 ▶{
Subroutine valid: called CC=2 SCAR=7.2
Pod documented
276 61 my ($self) = (@_);
277  
278 61 ▶ return ( $self->{ 'type' } ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'type'} ? : 61 0 61
279 }
280  
281  
282 =head2 type
283  
284 Return the type this record has, such as "A", "AAAA", "NS", etc.
285  
286 =cut
287  
288 sub type
289 144 ▶{
Subroutine type: called CC=1 SCAR=0.0
Pod documented
290 144 my ($self) = (@_);
291  
292 144 return ( $self->{ 'type' } );
293 }
294  
295  
296 =head2 ttl
297  
298 Return the TTL of this recrd.
299  
300 If no TTL was explicitly specified we default to 300 seconds, or five minutes.
301  
302 =cut
303  
304 sub ttl
305 139 ▶{
Subroutine ttl: called CC=3 SCAR=13.3
Pod documented
306 139 my ($self) = (@_);
307  
308 139 ▶ if ( $self->{ 'ttl' } &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ttl"} and $self->{"ttl"} =~ /^([0-9]+)$/) 139 0 139
Condition33%1/3
$self->{"ttl"} and $self->{"ttl"} =~ /^([0-9]+)$/
!ll&&!rl&&r
00139
MC/DC0%0/2
$self->{"ttl"} and $self->{"ttl"} =~ /^([0-9]+)$/
$self->{"ttl"} $self->{"ttl"} =~ /^([0-9]+)$/
Truth table33%1/3
A and B
A= $self->{"ttl"}B= $self->{"ttl"} =~ /^([0-9]+)$/
ABresult
0X0
100
111
309 $self->{ 'ttl' } =~ /^([0-9]+)$/ )
310 {
311 139 return $self->{ 'ttl' };
312 }
313  
314 0 return 300;
315 }
316  
317  
318 =head2 name
319  
320 Get the name of this record.
321  
322 =cut
323  
324 sub name
325 144 ▶{
Subroutine name: called CC=1 SCAR=0.0
Pod documented
326 144 my ($self) = (@_);
327 144 return ( $self->{ 'name' } );
328 }
329  
330  
331 =head2 value
332  
333 Get the value of this record.
334  
335 =cut
336  
337 sub value
338 148 ▶{
Subroutine value: called CC=1 SCAR=0.0
Pod documented
339 148 my ($self) = (@_);
340  
341 148 return ( $self->{ 'value' } );
342 }
343  
344  
345 =head2 add
346  
347 Add a new value to the existing record.
348  
349 This is used by the L<TinyDNS::Reader::Merged> module.
350  
351  
352 =cut
353  
354 sub add
355 1 ▶{
Subroutine add: called CC=2 SCAR=7.3
Pod documented
356 1 my ( $self, $addition ) = (@_);
357  
358 1 my $value = $self->{ 'value' };
359 1 ▶ if ( ref \$value eq "SCALAR" )
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$value eq 'SCALAR') { } 1 0 1
360 {
361 1 my $x;
362 1 push( @$x, $value );
363 1 push( @$x, $addition );
364 1 $self->{ 'value' } = $x;
365 }
366 else
367 {
368 0 push( @$value, $addition );
369 0 $self->{ 'value' } = $value;
370 }
371 }
372  
373  
374 =head2 stringify
375  
376 Convert the record to a string, suitable for printing.
377  
378 =cut
379  
380 sub stringify
381 62 ▶{
Subroutine stringify: called CC=5 SCAR=17.2
Pod documented
382 62 my ($self) = (@_);
383 62 my $txt = "";
384  
385 62 ▶ $txt .= ( "Type " . $self->type() . "\n" ) if ( $self->type() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->type 62 0 62
386 62 ▶ $txt .= ( " Name:" . $self->name() . "\n" ) if ( $self->name() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->name 62 0 62
387 62 ▶ $txt .= ( " Value:" . $self->value() . "\n" ) if ( $self->value() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->value 62 0 62
388 62 ▶ $txt .= ( " TTL:" . $self->ttl() . "\n" ) if ( $self->ttl() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->ttl 62 0 62
389  
390 }
391  
392  
393 =head2 hash
394  
395 Return a consistent hash of the record.
396  
397 =cut
398  
399 sub hash
400 10 ▶{
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
401 10 my ($self) = (@_);
402  
403 10 my $hash;
404 10 $hash .= $self->type();
405 10 $hash .= $self->name();
406 10 $hash .= $self->value();
407 10 $hash .= $self->ttl();
408  
409 10 return ($hash);
410 }
411  
412 3 1;
413  
414  
415 =head1 AUTHOR
416  
417 Steve Kemp <steve@steve.org.uk>
418  
419 =cut
420  
421 =head1 COPYRIGHT AND LICENSE
422  
423 Copyright (C) 2014-2015 Steve Kemp <steve@steve.org.uk>.
424  
425 This code was developed for an online Git-based DNS hosting solution,
426 which can be found at:
427  
428 =over 8
429  
430 =item *
431 https://dns-api.com/
432  
433 =back
434  
435 This library is free software. You can modify and or distribute it under
436 the same terms as Perl itself.
437  
438 =cut
↑ Summary Reader.pm »