DNS/DomainName.pm

filter: Statementstmt 100% 68 / 68 Branchbran 100% 24 / 24 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 122 / 122 CC 19 SCAR 18.4
CC
19
Cov
100%
decode
11.0
encode
6.0
encode
3.0
CRAP 6.3
« DNS/Resolver/Base.pm ↑ Summary DNS/RR.pm »
1 package Net::DNS::DomainName;
2  
3 99 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 99 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 99 our $VERSION = (qw$Id: DomainName.pm 2054 2026-07-10 09:37:11Z willem $)[2];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::DomainName - DNS name representation
12  
13 =head1 SYNOPSIS
14  
15 use Net::DNS::DomainName;
16  
17 $object = Net::DNS::DomainName->new('example.com');
18 $name = $object->name;
19 $data = $object->encode;
20  
21 ( $object, $next ) = Net::DNS::DomainName->decode( \$data, $offset );
22  
23 =head1 DESCRIPTION
24  
25 The Net::DNS::DomainName module implements the concrete representation
26 of DNS domain names used within DNS packets.
27  
28 Net::DNS::DomainName defines methods for encoding and decoding wire
29 format octet strings. All other behaviour is inherited from
30 Net::DNS::Domain.
31  
32 The Net::DNS::DomainName1035 and Net::DNS::DomainName2535 packages
33 implement disjoint domain name subtypes which provide the name
34 compression and canonicalisation specified by RFC1035 and RFC2535.
35 These are necessary to meet the backward compatibility requirements
36 introduced by RFC3597.
37  
38 =cut
39  
40  
41 99 ▶use base qw(Net::DNS::Domain);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 99 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 99 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46  
47 =head1 METHODS
48  
49 =head2 new
50  
51 $object = Net::DNS::DomainName->new('example.com');
52  
53 Creates a domain name object which identifies the domain specified
54 by the character string argument.
55  
56  
57 =head2 decode
58  
59 $object = Net::DNS::DomainName->decode( \$buffer, $offset, $hash );
60  
61 ( $object, $next ) = Net::DNS::DomainName->decode( \$buffer, $offset, $hash );
62  
63 Creates a domain name object which represents the DNS domain name
64 identified by the wire-format data at the indicated offset within
65 the data buffer.
66  
67 The argument list consists of a reference to a scalar containing the
68 wire-format data and specified offset. The optional reference to a
69 hash table provides improved efficiency of decoding compressed names
70 by exploiting already cached compression pointers.
71  
72 The returned offset value indicates the start of the next item in the
73 data buffer.
74  
75 =cut
76  
77 11216 ▶sub decode {
Subroutine decode: called CC=11 SCAR=24.0
Pod documented
78 11216 my $label = [];
79 11216 my $self = bless {label => $label}, shift;
80 11216 my $buffer = shift; # reference to data buffer
81 11216 ▶ my $offset = shift || 0; # offset within buffer
Condition100%2/2
shift() || 0
l!l
11049167
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
82 11216 my $cache = shift; # caller's compression index
83 11216 my $depth = shift; # recursion depth
84 11216 $cache->{$offset} = $self; # hashed objectref by offset
85  
86 11216 my $buflen = length $$buffer;
87 11216 my $index = $offset;
88  
89 11216 ▶ while ( $index < $buflen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $buflen) 14677 15 14692
90 14677 ▶ my $header = unpack( "\@$index C", $$buffer )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1130 256 1386
Condition100%2/2
unpack("\@$index C", $$buffer) || (return wantarray ? ($self, ++$index) : $self)
l!l
132911386
MC/DC100%1/1
unpack("\@$index C", $$buffer) || (return wantarray ? ($self, ++$index) : $self)
unpack("\@$index C", $$buffer)
Truth table100%2/2
A
A= unpack("\@$index C", $$buffer)
Aresult
11
00
91 || return wantarray ? ( $self, ++$index ) : $self;
92  
93 13291 ▶ if ( $header < 0x40 ) { # non-terminal label
Branch: 2 branches
BranchTrueFalseTotal
if ($header < 64) { } 3476 9815 13291
elsif ($header < 192) { } 2 9813 9815
94 3476 push @$label, substr( $$buffer, ++$index, $header );
95 3476 $index += $header;
96  
97 } elsif ( $header < 0xC0 ) { # deprecated extended label types
98 2 croak 'unimplemented label type';
99  
100 } else { # compression pointer
101 9813 my $link = 0x3FFF & unpack( "\@$index n", $$buffer );
102 9813 ▶ croak 'corrupt compression pointer' unless $link < $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $link < $offset 2 9811 9813
103 9811 ▶ croak 'deep compression recursion' if $depth++ > 120;
Branch: 1 branch
BranchTrueFalseTotal
if $depth++ > 120 1 9810 9811
104  
105 9810 ▶ $self->{origin} = $cache->{$link} ||= __PACKAGE__->decode( $buffer, $link, $cache, $depth );
Condition100%3/3
$cache->{$link} ||= __PACKAGE__->decode($buffer, $link, $cache, $depth)
l!l&&r!l&&!r
96811254
MC/DC100%2/2
$cache->{$link} ||= __PACKAGE__->decode($buffer, $link, $cache, $depth)
$cache->{$link} __PACKAGE__->decode($buffer, $link, $cache, $depth)
Truth table100%3/3
A ||= B
A= $cache->{$link}B= __PACKAGE__->decode($buffer, $link, $cache, $depth)
ABresult
1X1
011
000
106 9806 ▶ return wantarray ? ( $self, $index + 2 ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9481 325 9806
107 }
108 }
109 15 croak 'corrupt wire-format data';
110 }
111  
112  
113 =head2 encode
114  
115 $data = $object->encode;
116  
117 Returns the wire-format representation of the domain name suitable
118 for inclusion in a DNS packet buffer.
119  
120 =cut
121  
122 142 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
123 142 return join '', map { pack 'C a*', length($_), $_ } shift->_wire, '';
124 }
125  
126  
127 =head2 canonical
128  
129 $data = $object->canonical;
130  
131 Returns the canonical wire-format representation of the domain name
132 as defined in RFC2535(8.1).
133  
134 =cut
135  
136 931 ▶sub canonical {
Subroutine canonical: called CC=2 SCAR=6.9
Pod documented
137 931 my @label = shift->_wire;
138 931 for (@label) {
139 2194 tr /\101-\132/\141-\172/;
140 }
141 931 return join '', map { pack 'C a*', length($_), $_ } @label, '';
142 }
143  
144  
145 ########################################
146  
147 package Net::DNS::DomainName1035; ## no critic ProhibitMultiplePackages
148 99 our @ISA = qw(Net::DNS::DomainName);
149  
150 =head1 Net::DNS::DomainName1035
151  
152 Net::DNS::DomainName1035 implements a subclass of domain name
153 objects which are to be encoded using the compressed wire format
154 defined in RFC1035.
155  
156 $data = $object->encode( $offset, $hash );
157  
158 The arguments are the offset within the packet data where
159 the domain name is to be stored and a reference to a hash table used
160 to index compressed names within the packet.
161  
162 Note that RFC3597 implies that only the RR types defined in RFC1035(3.3)
163 are eligible for compression of domain names occuring in RDATA.
164  
165 If the hash reference is undefined, encode() returns the lower case
166 uncompressed canonical representation defined in RFC2535(8.1).
167  
168 =cut
169  
170 1512 ▶sub encode {
Subroutine encode: called CC=6 SCAR=17.9
171 1512 my $self = shift;
172 1512 ▶ my $offset = shift || 0; # offset in data buffer
Condition100%2/2
shift() || 0
l!l
144864
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
173 1512 ▶ my $hash = shift || return $self->canonical; # hashed offset by name
Condition100%2/2
shift() || (return $self->canonical)
l!l
1408104
MC/DC100%1/1
shift() || (return $self->canonical)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
174  
175 1408 my @labels = $self->_wire;
176 1408 my $data = '';
177 1408 ▶ while (@labels) {
Branch: 1 branch
BranchTrueFalseTotal
if (@labels) 2349 850 3199
178 2349 my $name = join( '.', @labels );
179  
180 2349 ▶ return $data . pack( 'n', 0xC000 | $hash->{$name} ) if defined $hash->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{$name} 558 1791 2349
181  
182 1791 my $label = shift @labels;
183 1791 my $length = length $label;
184 1791 $data .= pack( 'C a*', $length, $label );
185  
186 1791 ▶ next unless $offset < 0x4000;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset < 16384 1033 758 1791
187 758 $hash->{$name} = $offset;
188 758 $offset += 1 + $length;
189 }
190 850 return $data .= pack 'x';
191 }
192  
193  
194 ########################################
195  
196 package Net::DNS::DomainName2535; ## no critic ProhibitMultiplePackages
197 99 our @ISA = qw(Net::DNS::DomainName);
198  
199 =head1 Net::DNS::DomainName2535
200  
201 Net::DNS::DomainName2535 implements a subclass of domain name
202 objects which are to be encoded using uncompressed wire format.
203  
204 $data = $object->encode( $offset, $hash );
205  
206 The arguments are the offset within the packet data where
207 the domain name is to be stored and a reference to a hash table used
208 to index names already encoded within the packet.
209  
210 If the hash reference is undefined, encode() returns the lower case
211 uncompressed canonical representation defined in RFC2535(8.1).
212  
213 Note that RFC3597, and latterly RFC4034, specifies that the lower case
214 canonical form is to be used for RR types defined prior to RFC3597.
215  
216 =cut
217  
218 114 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.0
219 114 my ( $self, $offset, $hash ) = @_;
220 114 ▶ return $self->canonical unless defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 58 56 114
221 56 my $name = join '.', my @labels = $self->_wire;
222 56 ▶ $hash->{$name} = $offset if $offset < 0x4000;
Branch: 1 branch
BranchTrueFalseTotal
if $offset < 16384 6 50 56
223 56 return join '', map { pack 'C a*', length($_), $_ } @labels, '';
224 }
225  
226 99 1;
227 __END__
« DNS/Resolver/Base.pm ↑ Summary DNS/RR.pm »