DNS/Domain.pm

filter: Statementstmt 100% 123 / 123 Branchbran 100% 26 / 26 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 21 / 21 Podpod 100% 7 / 7 totaltotal 100% 188 / 188 CC 20 SCAR 16.1
CC
20
Cov
100%
new
12.0
name
5.0
fqdn
2.0
CRAP 5.0
« DNS/RR/APL.pm ↑ Summary DNS/RR/IPSECKEY.pm »
1 package Net::DNS::Domain;
2  
3 100 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 100 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 100 our $VERSION = (qw$Id: Domain.pm 2002 2025-01-07 09:57:46Z willem $)[2];
7  
8  
9 =head1 NAME
10  
11 Net::DNS::Domain - DNS domains
12  
13 =head1 SYNOPSIS
14  
15 use Net::DNS::Domain;
16  
17 $domain = Net::DNS::Domain->new('example.com');
18 $name = $domain->name;
19  
20 =head1 DESCRIPTION
21  
22 The Net::DNS::Domain module implements a class of abstract DNS
23 domain objects with associated class and instance methods.
24  
25 Each domain object instance represents a single DNS domain which
26 has a fixed identity throughout its lifetime.
27  
28 Internally, the primary representation is a (possibly empty) list
29 of ASCII domain name labels, and optional link to an origin domain
30 object topologically closer to the DNS root.
31  
32 The computational expense of Unicode character-set conversion is
33 partially mitigated by use of caches.
34  
35 =cut
36  
37  
38 100 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 100 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41  
42 100 ▶use constant ASCII => ref eval {
Subroutine BEGIN: called CC=1 SCAR=0.0
43 100 require Encode;
44 100 Encode::find_encoding('ascii');
45 100 };
46  
47 100 ▶use constant UTF8 => scalar eval { ## not UTF-EBCDIC [see Unicode TR#16 3.6]
Subroutine BEGIN: called CC=1 SCAR=0.0
48 100 Encode::encode_utf8( chr(182) ) eq pack( 'H*', 'C2B6' );
49 100 };
50  
51 100 ▶use constant LIBIDN2 => defined eval { require Net::LibIDN2 };
Subroutine BEGIN: called CC=1 SCAR=0.0
52 100 ▶use constant IDN2FLAG => LIBIDN2 ? &Net::LibIDN2::IDN2_NFC_INPUT + &Net::LibIDN2::IDN2_NONTRANSITIONAL : 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 100 ▶use constant LIBIDN => LIBIDN2 ? undef : defined eval { require Net::LibIDN };
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # perlcc: address of encoding objects must be determined at runtime
56 100 my $ascii = ASCII ? Encode::find_encoding('ascii') : undef; # Osborn's Law:
57 100 my $utf8 = UTF8 ? Encode::find_encoding('utf8') : undef; # Variables won't; constants aren't.
58  
59  
60 =head1 METHODS
61  
62 =head2 new
63  
64 $object = Net::DNS::Domain->new('example.com');
65  
66 Creates a domain object which represents the DNS domain specified
67 by the character string argument. The argument consists of a
68 sequence of labels delimited by dots.
69  
70 A character preceded by \ represents itself, without any special
71 interpretation.
72  
73 Arbitrary 8-bit codes can be represented by \ followed by exactly
74 three decimal digits.
75 Character code points are ASCII, irrespective of the character
76 coding scheme employed by the underlying platform.
77  
78 Argument string literals should be delimited by single quotes to
79 avoid escape sequences being interpreted as octal character codes
80 by the Perl compiler.
81  
82 The character string presentation format follows the conventions
83 for zone files described in RFC1035.
84  
85 Users should be aware that non-ASCII domain names will be transcoded
86 to NFC before encoding, which is an irreversible process.
87  
88 =cut
89  
90 100 my ( %escape, %unescape ); ## precalculated ASCII escape tables
91  
92 100 our $ORIGIN;
93 100 my ( $cache1, $cache2, $limit ) = ( {}, {}, 100 );
94  
95 4731 ▶sub new {
Subroutine new: called CC=12 SCAR=24.8
Pod documented
96 4731 my ( $class, $s ) = @_;
97 4731 ▶ croak 'domain identifier undefined' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 4 4727 4731
98  
99 4727 ▶ my $index = join '', $s, $class, $ORIGIN || ''; # cache key
Condition100%2/2
$Net::DNS::Domain::ORIGIN || ''
l!l
574670
MC/DC100%1/1
$Net::DNS::Domain::ORIGIN || ''
$Net::DNS::Domain::ORIGIN
Truth table100%2/2
A
A= $Net::DNS::Domain::ORIGIN
Aresult
11
00
100 4727 ▶ my $cache = $$cache1{$index} ||= $$cache2{$index}; # two layer cache
Condition100%3/3
$cache1->{$index} ||= $cache2->{$index}
l!l&&r!l&&!r
23383032086
MC/DC100%2/2
$cache1->{$index} ||= $cache2->{$index}
$cache1->{$index} $cache2->{$index}
Truth table100%3/3
A ||= B
A= $cache1->{$index}B= $cache2->{$index}
ABresult
1X1
011
000
101 4727 ▶ return $cache if defined $cache;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache 2641 2086 4727
102  
103 2086 ▶ ( $cache1, $cache2, $limit ) = ( {}, $cache1, 500 ) unless $limit--; # recycle cache
Branch: 1 branch
BranchTrueFalseTotal
unless $limit-- 4 2082 2086
104  
105 2086 my $self = bless {}, $class;
106  
107 2086 $s =~ s/\\\\/\\092/g; # disguise escaped escape
108 2086 $s =~ s/\\\./\\046/g; # disguise escaped dot
109  
110 2086 ▶ my $label = $self->{label} = ( $s eq '@' ) ? [] : [split /\056/, _encode_utf8($s)];
Branch: 1 branch
BranchTrueFalseTotal
$s eq '@' ? : 11 2075 2086
111  
112 2086 foreach (@$label) {
113 3947 ▶ croak qq(empty label in "$s") unless length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 2 3945 3947
114  
115 3945 if ( LIBIDN2 && UTF8 && /[^\000-\177]/ ) {
116 my $rc = 0;
117 $_ = Net::LibIDN2::idn2_to_ascii_8( $_, IDN2FLAG, $rc );
118 croak Net::LibIDN2::idn2_strerror($rc) unless $_;
119 }
120  
121 3945 if ( LIBIDN && UTF8 && /[^\000-\177]/ ) {
122 $_ = Net::LibIDN::idn_to_ascii( $_, 'utf-8' );
123 croak 'name contains disallowed character' unless $_;
124 }
125  
126 3945 s/\134([\060-\071]{3})/$unescape{$1}/eg; # restore numeric escapes
127 3945 s/\134([^\134])/$1/g; # restore character escapes
128 3945 s/\134(\134)/$1/g; # restore escaped escapes
129 3945 ▶ croak qq(label too long in "$s") if length > 63;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > 63 1 3944 3945
130 }
131  
132 2083 $$cache1{$index} = $self; # cache object reference
133  
134 2083 ▶ return $self if $s =~ /\.$/; # fully qualified name
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\.$/ 379 1704 2083
135 1704 ▶ $self->{origin} = $ORIGIN || return $self; # dynamically scoped $ORIGIN
Condition100%2/2
$Net::DNS::Domain::ORIGIN || (return $self)
l!l
211683
MC/DC100%1/1
$Net::DNS::Domain::ORIGIN || (return $self)
$Net::DNS::Domain::ORIGIN
Truth table100%2/2
A
A= $Net::DNS::Domain::ORIGIN
Aresult
11
00
136 21 return $self;
137 }
138  
139  
140 =head2 name
141  
142 $name = $domain->name;
143  
144 Returns the domain name as a character string corresponding to the
145 "common interpretation" to which RFC1034, 3.1, paragraph 9 alludes.
146  
147 Character escape sequences are used to represent a dot inside a
148 domain name label and the escape character itself.
149  
150 Any non-printable code point is represented using the appropriate
151 numerical escape sequence.
152  
153 =cut
154  
155 2827 ▶sub name {
Subroutine name: called CC=5 SCAR=16.1
Pod documented
156 2827 my ($self) = @_;
157  
158 2827 ▶ return $self->{name} if defined $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"name"} 1167 1660 2827
159 1660 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 193 1467 1660
160  
161 1467 my @label = shift->_wire;
162 1467 ▶ return $self->{name} = '.' unless scalar @label;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @label 27 1440 1467
163  
164 1440 for (@label) {
165 4155 s/([^\055\101-\132\141-\172\060-\071])/$escape{$1}/eg;
166 }
167  
168 1440 return $self->{name} = _decode_ascii( join chr(46), @label );
169 }
170  
171  
172 =head2 fqdn
173  
174 $fqdn = $domain->fqdn;
175  
176 Returns a character string containing the fully qualified domain
177 name, including the trailing dot.
178  
179 =cut
180  
181 1356 ▶sub fqdn {
Subroutine fqdn: called CC=2 SCAR=6.9
Pod documented
182 1356 my $name = &name;
183 1356 ▶ return $name =~ /[.]$/ ? $name : "$name."; # append trailing dot
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /[.]$/ ? : 38 1318 1356
184 }
185  
186  
187 =head2 xname
188  
189 $xname = $domain->xname;
190  
191 Interprets an extended name containing Unicode domain name labels
192 encoded as Punycode A-labels.
193  
194 If decoding is not possible, the ACE encoded name is returned.
195  
196 =cut
197  
198 2 ▶sub xname {
Subroutine xname: called CC=1 SCAR=0.0
Pod documented
199 2 my $name = &name;
200  
201 2 if ( LIBIDN2 && UTF8 && $name =~ /xn--/i ) {
202 my $self = shift;
203 return $self->{xname} if defined $self->{xname};
204 my $u8 = Net::LibIDN2::idn2_to_unicode_88($name);
205 return $self->{xname} = $u8 ? $utf8->decode($u8) : $name;
206 }
207  
208 2 if ( LIBIDN && UTF8 && $name =~ /xn--/i ) {
209 my $self = shift;
210 return $self->{xname} if defined $self->{xname};
211 return $self->{xname} = $utf8->decode( Net::LibIDN::idn_to_unicode $name, 'utf-8' );
212 }
213 2 return $name;
214 }
215  
216  
217 =head2 label
218  
219 @label = $domain->label;
220  
221 Identifies the domain by means of a list of domain labels.
222  
223 =cut
224  
225 154 ▶sub label {
Subroutine label: called CC=2 SCAR=6.9
Pod documented
226 154 my @label = shift->_wire;
227 154 for (@label) {
228 421 s/([^\055\101-\132\141-\172\060-\071])/$escape{$1}/eg;
229 421 _decode_ascii($_);
230 }
231 154 return @label;
232 }
233  
234  
235 =head2 string
236  
237 $string = $object->string;
238  
239 Returns a character string containing the fully qualified domain
240 name as it appears in a zone file.
241  
242 Characters which are recognised by RFC1035 zone file syntax are
243 represented by the appropriate escape sequence.
244  
245 =cut
246  
247 1340 ▶sub string { return &fqdn }
Subroutine string: called CC=1 SCAR=0.0
Pod documented
248  
249  
250 =head2 origin
251  
252 $create = Net::DNS::Domain->origin( $ORIGIN );
253 $result = &$create( sub{ Net::DNS::RR->new( 'mx MX 10 a' ); } );
254 $expect = Net::DNS::RR->new( "mx.$ORIGIN. MX 10 a.$ORIGIN." );
255  
256 Class method which returns a reference to a subroutine wrapper
257 which executes a given constructor in a dynamically scoped context
258 where relative names become descendents of the specified $ORIGIN.
259  
260 =cut
261  
262 334 ▶my $placebo = sub { my $constructor = shift; &$constructor; };
Subroutine __ANON__: called CC=1 SCAR=0.0
263  
264 146 ▶sub origin {
Subroutine origin: called CC=2 SCAR=6.9
Pod documented
265 146 my ( $class, $name ) = @_;
266 146 ▶ my $domain = defined $name ? __PACKAGE__->new($name) : return $placebo;
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 13 133 146
267  
268 38 ▶ return sub { # closure w.r.t. $domain
Subroutine __ANON__: called CC=1 SCAR=0.0
269 38 my $constructor = shift;
270 38 local $ORIGIN = $domain; # dynamically scoped $ORIGIN
271 38 &$constructor;
272 }
273 13 }
274  
275  
276 ########################################
277  
278 1861 ▶sub _decode_ascii { ## ASCII to perl internal encoding
Subroutine _decode_ascii: called CC=1 SCAR=0.0
279 1861 local $_ = shift;
280  
281 # partial transliteration for non-ASCII character encodings
282 tr
283 1861 [\040-\176\000-\377]
284 [ !"#$%&'()*+,\-./0-9:;<=>?@A-Z\[\\\]^_`a-z{|}~?] unless ASCII;
285  
286 1861 my $z = length($_) - length($_); # pre-5.18 taint workaround
287 1861 return ASCII ? substr( $ascii->decode($_), $z ) : $_;
288 }
289  
290  
291 2075 ▶sub _encode_utf8 { ## perl internal encoding to UTF8
Subroutine _encode_utf8: called CC=1 SCAR=0.0
292 2075 local $_ = shift;
293  
294 # partial transliteration for non-ASCII character encodings
295 tr
296 2075 [ !"#$%&'()*+,\-./0-9:;<=>?@A-Z\[\\\]^_`a-z{|}~\000-\377]
297 [\040-\176\077] unless ASCII;
298  
299 2075 my $z = length($_) - length($_); # pre-5.18 taint workaround
300 2075 return ASCII ? substr( ( UTF8 ? $utf8 : $ascii )->encode($_), $z ) : $_;
301 }
302  
303  
304 5058 ▶sub _wire {
Subroutine _wire: called CC=2 SCAR=6.9
305 5058 my $self = shift;
306  
307 5058 my $label = $self->{label};
308 5058 my $origin = $self->{origin};
309 5058 ▶ return ( @$label, $origin ? $origin->_wire : () );
Branch: 1 branch
BranchTrueFalseTotal
$origin ? : 810 4248 5058
310 }
311  
312  
313 100 %escape = eval { ## precalculated ASCII escape table
314 100 my %table = map { ( chr($_) => chr($_) ) } ( 0 .. 127 );
315  
316 100 foreach my $n ( 0 .. 32, 34, 92, 127 .. 255 ) { # \ddd
317 16400 my $codepoint = sprintf( '%03u', $n );
318  
319 # transliteration for non-ASCII character encodings
320 16400 $codepoint =~ tr [0-9] [\060-\071];
321  
322 16400 $table{pack( 'C', $n )} = pack 'C a3', 92, $codepoint;
323 }
324  
325 100 foreach my $n ( 40, 41, 46, 59 ) { # character escape
326 400 $table{chr($n)} = pack( 'C2', 92, $n );
327 }
328  
329 100 return %table;
330 };
331  
332  
333 100 %unescape = eval { ## precalculated numeric escape table
334 100 my %table;
335  
336 100 foreach my $n ( 0 .. 255 ) {
337 25600 my $key = sprintf( '%03u', $n );
338  
339 # transliteration for non-ASCII character encodings
340 25600 $key =~ tr [0-9] [\060-\071];
341  
342 25600 $table{$key} = pack 'C', $n;
343 }
344 100 $table{"\060\071\062"} = pack 'C2', 92, 92; # escaped escape
345  
346 100 return %table;
347 };
348  
349  
350 100 1;
351 __END__
« DNS/RR/APL.pm ↑ Summary DNS/RR/IPSECKEY.pm »