| 1 | |||||||||||||||||||||||||||||
| 2 | =head1 NAME | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | TinyDNS::Reader - Read TinyDNS files. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | This module allows the parsing of a TinyDNS data-file, or individual records | ||||||||||||||||||||||||||||
| 9 | taken from one. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use TinyDNS::Reader; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $tmp = TinyDNS::Reader->new( file => "./zones/example.com" ); | ||||||||||||||||||||||||||||
| 18 | my $dns = $tmp->parse(); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | foreach my $record ( @$dns ) | ||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||
| 22 | print $record . "\n"; | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | This module contains code for reading a zone-file which has been | ||||||||||||||||||||||||||||
| 29 | created for use with L<DJB's tinydns|http://cr.yp.to/djbdns/tinydns.html>. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | A zonefile may be parsed and turned into a series of L<TinyDNS::Record> objects, | ||||||||||||||||||||||||||||
| 32 | one for each valid record which is found. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | If you wish to merge multiple records, referring to the same hostname, you should also consult the documentation for the L<TinyeDNS::Reader::Merged> module. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 METHODS | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | package TinyDNS::Reader; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | use TinyDNS::Record; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | our $VERSION = '0.7.7'; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 new | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | The constructor should be given either a "C<file>" or "C<text>" parameter, | ||||||||||||||||||||||||||||
| 56 | containing the filename to parse, or the text to parse, respectively. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub new | ||||||||||||||||||||||||||||
| 61 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.8
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 10 | my ( $proto, %supplied ) = (@_); | |||||||||||||||||||||||||||
| 63 | 10 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 10 | my $self = {}; | |||||||||||||||||||||||||||
| 66 | 10 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 10 | ▶ | if ( $supplied{ 'file' } ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 0 | $self->{ 'data' } = $self->_readFile( $supplied{ 'file' } ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | elsif ( $supplied{ 'text' } ) | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 10 | $self->{ 'data' } = $supplied{ 'text' }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else | ||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 0 | die "Missing 'text' or 'file' argument."; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 10 | return $self; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =begin doc | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Read the contents of the specified file. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Invoked by the constructor if it was passed a C<file> argument. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =end doc | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub _readFile | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_readFile: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 98 | 0 | my ( $self, $file ) = (@_); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | open( my $handle, "<", $file ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | die "Failed to read $file - $!"; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $text = ""; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | while ( my $line = <$handle> ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | $text .= $line; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 0 | close($handle); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | return ($text); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 parse | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Process and return an array of L<TinyDNS::Records> from the data contained | ||||||||||||||||||||||||||||
| 118 | in the file specified by our constructor, or the scalar reference. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub parse | ||||||||||||||||||||||||||||
| 123 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=8 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 10 | my ($self) = (@_); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 10 | my $records; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 10 | foreach my $line ( split( /[\n\r]/, $self->{ 'data' } ) ) | |||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 11 | chomp($line); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Skip empty lines. | ||||||||||||||||||||||||||||
| 133 | 11 | ▶ | next if ( !$line || !length($line) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$line and length $line
MC/DC0%0/2
$line and length $line
$line
length $line
Truth table33%1/3
A and B
A= $lineB= length $line
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Strip trailing comments. | ||||||||||||||||||||||||||||
| 136 | 11 | $line =~ s/#.*$//s; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Skip empty lines. | ||||||||||||||||||||||||||||
| 139 | 11 | ▶ | next if ( !$line || !length($line) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$line and length $line
MC/DC0%0/2
$line and length $line
$line
length $line
Truth table33%1/3
A and B
A= $lineB= length $line
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # | ||||||||||||||||||||||||||||
| 142 | # Ignore "." + ":" records | ||||||||||||||||||||||||||||
| 143 | # | ||||||||||||||||||||||||||||
| 144 | 11 | ▶ | next if ( $line =~ /^\s*[:.]/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # | ||||||||||||||||||||||||||||
| 147 | # Ensure the line is lower-cased | ||||||||||||||||||||||||||||
| 148 | # | ||||||||||||||||||||||||||||
| 149 | 11 | $line = lc($line); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | # Construct a new object, and add it to the list. | ||||||||||||||||||||||||||||
| 153 | # | ||||||||||||||||||||||||||||
| 154 | 11 | my $rec = TinyDNS::Record->new($line); | |||||||||||||||||||||||||||
| 155 | 11 | ▶ | push( @$records, $rec ) if ($rec); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 10 | return ($records); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | 1; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Steve Kemp <steve@steve.org.uk> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Copyright (C) 2014-2015 Steve Kemp <steve@steve.org.uk>. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | This code was developed for an online Git-based DNS hosting solution, | ||||||||||||||||||||||||||||
| 176 | which can be found at: | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =over 8 | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =item * | ||||||||||||||||||||||||||||
| 181 | https://dns-api.com/ | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =back | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | This library is free software. You can modify and or distribute it under | ||||||||||||||||||||||||||||
| 186 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||