DNS/RR/SVCB.pm

filter: Statementstmt 100% 216 / 216 Branchbran 100% 86 / 86 Conditioncond 92.8% 13 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 38 / 38 Podpod 50.0% 6 / 12 totaltotal 97.5% 365 / 374 CC 52 SCAR 15.5
CC
52
Cov
100%
_post_parse
10.0
_SvcParam
9.0
_format_rdata
8.0
CRAP 4.7
« DNS/RR/IPSECKEY.pm ↑ Summary DNS/RR/DELEG.pm »
1 package Net::DNS::RR::SVCB;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = (qw$Id: SVCB.pm 2043 2026-01-14 13:35:59Z willem $)[2];
6  
7 2 ▶use base qw(Net::DNS::RR);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Net::DNS::RR::SVCB - DNS SVCB resource record
13  
14 =cut
15  
16 2 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Net::DNS::DomainName;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Net::DNS::RR::A;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Net::DNS::RR::AAAA;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Net::DNS::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 2 my %keybyname = (
26 mandatory => 'key0', # RFC9460(8)
27 alpn => 'key1', # RFC9460(7.1)
28 'no-default-alpn' => 'key2', # RFC9460(7.1)
29 port => 'key3', # RFC9460(7.2)
30 ipv4hint => 'key4', # RFC9460(7.3)
31 ech => 'key5', # RFC9460
32 ipv6hint => 'key6', # RFC9460(7.3)
33 dohpath => 'key7', # RFC9461
34 ohttp => 'key8', # RFC9540(4)
35 'tls-supported-groups' => 'key9',
36 );
37  
38  
39 14 ▶sub _decode_rdata { ## decode rdata from wire-format octet string
Subroutine _decode_rdata: called CC=4 SCAR=13.9
40 14 my ( $self, $data, $offset ) = @_;
41  
42 14 my $limit = $self->{rdlength};
43 14 my $rdata = $self->{rdata} = substr $$data, $offset, $limit;
44 14 $self->{SvcPriority} = unpack 'n', $rdata;
45 14 ( $self->{TargetName}, $offset ) = Net::DNS::DomainName->decode( \$rdata, 2 );
46  
47 14 my $params = $self->{SvcParams} = [];
48 14 ▶ while ( ( my $start = $offset + 4 ) <= $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $start = $offset + 4) <= $limit) 12 12 24
49 12 my ( $key, $size ) = unpack( "\@$offset n2", $rdata );
50 12 ▶ last if ( $offset = $start + $size ) > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if ($offset = $start + $size) > $limit 2 10 12
51 10 push @$params, ( $key, substr $rdata, $start, $size );
52 }
53 14 ▶ die $self->type . ': corrupt RDATA' unless $offset == $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset == $limit 2 12 14
54 12 return;
55 }
56  
57  
58 8 ▶sub _encode_rdata { ## encode rdata as wire-format octet string
Subroutine _encode_rdata: called CC=4 SCAR=13.9
59 8 my $self = shift;
60  
61 8 ▶ return $self->{rdata} if $self->{rdata};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rdata"} 3 5 8
62 5 my @packed = pack 'n a*', $self->{SvcPriority}, $self->{TargetName}->encode;
63 5 ▶ my $params = $self->{SvcParams} || [];
Condition100%2/2
$self->{'SvcParams'} || []
l!l
14
MC/DC100%1/1
$self->{'SvcParams'} || []
$self->{'SvcParams'}
Truth table100%2/2
A
A= $self->{'SvcParams'}
Aresult
11
00
64 5 my @params = @$params;
65 5 ▶ while (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 1 5 6
66 1 my $key = shift @params;
67 1 my $val = shift @params;
68 1 push @packed, pack( 'n2a*', $key, length($val), $val );
69 }
70 5 return join '', @packed;
71 }
72  
73  
74 25 ▶sub _format_rdata { ## format rdata portion of RR string.
Subroutine _format_rdata: called CC=8 SCAR=20.8
75 25 my $self = shift;
76  
77 25 my @rdata = unpack 'H4', pack 'n', $self->{SvcPriority};
78  
79 25 my $encode = $self->{TargetName}->encode();
80 25 my $length = 2 + length $encode;
81 25 my @target = grep {length} split /(\S{32})/, unpack 'H*', $encode;
82 25 my $target = substr $self->{TargetName}->string, 0, 40;
83 25 ▶ push @rdata, join '', shift(@target), "\t; $target\n" unless $target eq '.';
Branch: 1 branch
BranchTrueFalseTotal
unless $target eq "." 19 6 25
84 25 push @rdata, @target;
85  
86 25 ▶ my $params = $self->{SvcParams} || [];
Condition100%2/2
$self->{'SvcParams'} || []
l!l
196
MC/DC100%1/1
$self->{'SvcParams'} || []
$self->{'SvcParams'}
Truth table100%2/2
A
A= $self->{'SvcParams'}
Aresult
11
00
87 25 my @params = @$params;
88 25 ▶ while (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 30 25 55
89 30 my $key = shift @params;
90 30 my $val = shift @params;
91 30 push @rdata, "\n", unpack 'H4H4', pack( 'n2', $key, length $val );
92 30 my @hex = grep {length} split /(\S{32})/, unpack 'H*', $val;
93 30 ▶ push @rdata, shift @hex if @hex;
Branch: 1 branch
BranchTrueFalseTotal
if @hex 23 7 30
94 30 ▶ push @rdata, "\t; key$key\n" unless $key < 16;
Branch: 1 branch
BranchTrueFalseTotal
unless $key < 16 9 21 30
95 30 push @rdata, @hex;
96 30 $length += 4 + length $val;
97 }
98 25 ▶ if ( $self->{rdata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rdata"}) 5 20 25
99 5 ▶ if ( my $corrupt = substr $self->{rdata}, $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $corrupt = substr($self->{"rdata"}, $length)) 2 3 5
100 2 my ( $hex, @hex ) = grep {length} split /(\S{32})/, unpack 'H*', $corrupt;
101 2 push @rdata, "\n$hex\t; corrupt RDATA\n", @hex;
102 2 $length += length $corrupt;
103 }
104 }
105 25 return ( "\\# $length", @rdata );
106 }
107  
108  
109 44 ▶sub _parse_rdata { ## populate RR from rdata in argument list
Subroutine _parse_rdata: called CC=6 SCAR=17.9
110 44 my ( $self, @argument ) = @_;
111  
112 44 $self->svcpriority( shift @argument );
113 44 $self->targetname( shift @argument );
114  
115 44 ▶ while ( local $_ = shift @argument ) {
Branch: 1 branch
BranchTrueFalseTotal
if (local $_ = shift @argument) 55 31 86
116 55 m/^([^=]+)(=?)(.*)$/;
117 55 my $key = $1;
118 55 ▶ my $val = length($3) ? $3 : $2 ? shift @argument : '';
Branch: 2 branches
BranchTrueFalseTotal
length $3 ? : 38 17 55
$2 ? : 4 13 17
119 55 ▶ if (/^key\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^key\d+/) { } 12 43 55
120 12 $self->$key($val);
121 } else {
122 43 local $_ = $val;
123 43 ▶ die <<'RIP' if /\\092[,\\]/;
Branch: 1 branch
BranchTrueFalseTotal
if /\\092[,\\]/ 2 41 43
124 SVCB: Please use standard RFC1035 escapes
125 RFC9460 double-escape insanity not implemented
126 RIP
127 41 s/^"([^"]*)"$/$1/s; # strip enclosing quotes
128 41 s/\\,/\\044/g; # disguise (RFC1035) escaped comma
129 41 $self->$key( split /,/ );
130 }
131 }
132 31 return;
133 }
134  
135  
136 43 ▶sub _post_parse { ## parser post processing
Subroutine _post_parse: called CC=10 SCAR=23.0
137 43 my $self = shift;
138  
139 43 ▶ my $paramref = $self->{SvcParams} || [];
Condition100%2/2
$self->{'SvcParams'} || []
l!l
349
MC/DC100%1/1
$self->{'SvcParams'} || []
$self->{'SvcParams'}
Truth table100%2/2
A
A= $self->{'SvcParams'}
Aresult
11
00
140 43 ▶ my %svcparam = scalar(@$paramref) ? @$paramref : return;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$paramref ? : 31 12 43
141  
142 31 $self->key0(undef); # ruse to force sorting of SvcParams
143 31 ▶ if ( defined $svcparam{0} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $svcparam{0}) 7 24 31
144 7 my %unique;
145 7 foreach ( grep { !$unique{$_}++ } unpack 'n*', $svcparam{0} ) {
146 9 ▶ die( $self->type . qq[: unexpected "key0" in mandatory list] ) if $unique{0};
Branch: 1 branch
BranchTrueFalseTotal
if $unique{0} 1 8 9
147 8 ▶ die( $self->type . qq[: duplicate "key$_" in mandatory list] ) if --$unique{$_};
Branch: 1 branch
BranchTrueFalseTotal
if --$unique{$_} 1 7 8
148 7 ▶ die( $self->type . qq[: mandatory "key$_" not present] ) unless defined $svcparam{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $svcparam{$_} 1 6 7
149 }
150 4 $self->mandatory( keys %unique ); # restore mandatory key list
151 }
152 28 ▶ die( $self->type . qq[: expected alpn="..." not present] ) if defined( $svcparam{2} ) && !$svcparam{1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $svcparam{2} and not $svcparam{1} 2 26 28
Condition100%3/3
defined $svcparam{2} and not $svcparam{1}
!ll&&!rl&&r
2422
MC/DC100%2/2
defined $svcparam{2} and not $svcparam{1}
defined $svcparam{2} not $svcparam{1}
Truth table100%3/3
A and not(B)
A= defined $svcparam{2}B= not $svcparam{1}
ABresult
0X0
100
111
153 26 return;
154 }
155  
156  
157 2 ▶sub _defaults { ## specify RR attribute default values
Subroutine _defaults: called CC=1 SCAR=0.0
158 2 my $self = shift;
159  
160 2 $self->_parse_rdata(qw(0 .));
161 2 return;
162 }
163  
164  
165 50 ▶sub svcpriority {
Subroutine svcpriority: called CC=2 SCAR=6.9
Pod undocumented
166 50 my ( $self, @value ) = @_; # uncoverable pod
167 50 for (@value) { $self->{SvcPriority} = 0 + $_ }
168 50 return $self->{SvcPriority};
169 }
170  
171  
172 51 ▶sub targetname {
Subroutine targetname: called CC=5 SCAR=16.1
Pod undocumented
173 51 my ( $self, @value ) = @_; # uncoverable pod
174 51 ▶ $self->{TargetName} = Net::DNS::DomainName->new(@value) if @value;
Branch: 1 branch
BranchTrueFalseTotal
if @value 45 6 51
175 51 ▶ my $target = $self->{TargetName} ? $self->{TargetName}->name : return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TargetName'} ? : 49 2 51
176 49 ▶ return $target unless $target eq '.';
Branch: 1 branch
BranchTrueFalseTotal
unless $target eq "." 33 16 49
177 16 ▶ return $self->{SvcPriority} ? $self->owner : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'SvcPriority'} ? : 14 2 16
178 }
179  
180  
181 13 ▶sub mandatory { ## mandatory=key1,port,...
Subroutine mandatory: called CC=3 SCAR=11.0
Pod documented
182 13 my ( $self, @value ) = @_;
183 13 ▶ my @list = map { $keybyname{lc $_} || $_ } @value;
Condition66%2/3
$keybyname{lc $_} || $_
l!l&&r!l&&!r
3120
MC/DC0%0/2
$keybyname{lc $_} || $_
$keybyname{lc $_} $_
Truth table66%2/3
A || B
A= $keybyname{lc $_}B= $_
ABresult
1X1
011
000
184 13 ▶ my @keys = map { /(\d+)$/ ? $1 : die( $self->type . qq[: unexpected "$_"] ) } @list;
Branch: 1 branch
BranchTrueFalseTotal
/(\d+)$/ ? : 14 1 15
185 12 return $self->_SvcParam( 0, _integer16( sort { $a <=> $b } @keys ) );
186 }
187  
188 5 ▶sub alpn { ## alpn=h3,h2,...
Subroutine alpn: called CC=1 SCAR=0.0
Pod documented
189 5 my ( $self, @value ) = @_;
190 5 return $self->_SvcParam( 1, _string(@value) );
191 }
192  
193 6 ▶sub no_default_alpn { ## no-default-alpn (Boolean)
Subroutine no_default_alpn: called CC=2 SCAR=6.9
Pod undocumented
194 6 my ( $self, @value ) = @_; # uncoverable pod
195 6 ▶ return $self->_SvcParam(2) if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 2 4 6
196 4 return $self->_SvcParam( 2, _boolean(@value) );
197 }
198  
199 8 ▶sub port { ## port=1234
Subroutine port: called CC=1 SCAR=0.0
Pod documented
200 8 my ( $self, @value ) = @_;
201 8 return $self->_SvcParam( 3, map { _integer16($_) } @value );
202 }
203  
204 4 ▶sub ipv4hint { ## ipv4hint=192.0.2.1,...
Subroutine ipv4hint: called CC=1 SCAR=0.0
Pod documented
205 4 my ( $self, @value ) = @_;
206 4 return $self->_SvcParam( 4, _address4(@value) );
207 }
208  
209 5 ▶sub ech { ## ech=base64
Subroutine ech: called CC=1 SCAR=0.0
Pod documented
210 5 my ( $self, @value ) = @_;
211 5 return $self->_SvcParam( 5, map { _base64($_) } @value );
212 }
213  
214 6 ▶sub ipv6hint { ## ipv6hint=2001:DB8::1,...
Subroutine ipv6hint: called CC=1 SCAR=0.0
Pod documented
215 6 my ( $self, @value ) = @_;
216 6 return $self->_SvcParam( 6, _address6(@value) );
217 }
218  
219 3 ▶sub dohpath { ## dohpath=/dns-query{?dns}
Subroutine dohpath: called CC=1 SCAR=0.0
Pod undocumented
220 3 my ( $self, @value ) = @_; # uncoverable pod
221 3 return $self->_SvcParam( 7, map { substr _string($_), 1 } @value );
222 }
223  
224 4 ▶sub ohttp { ## ohttp
Subroutine ohttp: called CC=2 SCAR=6.9
Pod undocumented
225 4 my ( $self, @value ) = @_; # uncoverable pod
226 4 ▶ return $self->_SvcParam(8) if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 3 4
227 3 return $self->_SvcParam( 8, _boolean(@value) );
228 }
229  
230 3 ▶sub tls_supported_groups { ## tls_supported_groups=29,23
Subroutine tls_supported_groups: called CC=1 SCAR=0.0
Pod undocumented
231 3 my ( $self, @value ) = @_; # uncoverable pod
232 3 return $self->_SvcParam( 9, _integer16(@value) );
233 }
234  
235  
236 ########################################
237  
238  
239 43 ▶sub _concatenate { ## concatenate octet string(s)
Subroutine _concatenate: called CC=2 SCAR=6.9
240 43 my @arg = @_;
241 43 ▶ return scalar(@arg) ? join( '', @arg ) : @arg;
Branch: 1 branch
BranchTrueFalseTotal
scalar @arg ? : 34 9 43
242 }
243  
244 7 ▶sub _boolean {
Subroutine _boolean: called CC=3 SCAR=11.0
245 7 my @arg = @_;
246 7 ▶ return '' unless scalar @arg;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @arg 4 3 7
247 3 ▶ return map { $_ ? '' : undef } @arg;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 2 1 3
248 }
249  
250 7 ▶sub _string {
Subroutine _string: called CC=1 SCAR=0.0
251 7 my @arg = @_;
252 7 my @val = map { split /,/ } @arg;
253 7 return _concatenate( map { Net::DNS::Text->new($_)->encode() } @val );
254 }
255  
256 4 ▶sub _base64 {
Subroutine _base64: called CC=1 SCAR=0.0
257 4 my @arg = @_;
258 4 return _concatenate( map { MIME::Base64::decode($_) } @arg );
259 }
260  
261 22 ▶sub _integer16 {
Subroutine _integer16: called CC=1 SCAR=0.0
262 22 my @arg = @_;
263 22 my @val = map { split /,/ } @arg;
264 22 return _concatenate( map { pack( 'n', $_ ) } @val );
265 }
266  
267 4 ▶sub _address4 {
Subroutine _address4: called CC=1 SCAR=0.0
268 4 my @arg = @_;
269 4 my @val = map { split /,/ } @arg;
270 4 return _concatenate( map { Net::DNS::RR::A::address( {}, $_ ) } @val );
271 }
272  
273 6 ▶sub _address6 {
Subroutine _address6: called CC=1 SCAR=0.0
274 6 my @arg = @_;
275 6 my @val = map { split /,/ } @arg;
276 6 return _concatenate( map { Net::DNS::RR::AAAA::address( {}, $_ ) } @val );
277 }
278  
279  
280 57 ▶sub AUTOLOAD { ## Dynamic constructor/accessor methods
Subroutine AUTOLOAD: called CC=5 SCAR=16.1
281 57 my ( $self, @argument ) = @_;
282  
283 57 our $AUTOLOAD;
284 57 my ($method) = reverse split /::/, $AUTOLOAD;
285 57 my $canonical = lc($method);
286 57 $canonical =~ s/-/_/g;
287 57 ▶ if ( $self->can($canonical) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($canonical)) 4 53 57
288 2 ▶ no strict 'refs'; ## no critic ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
289 4 ▶ *{$AUTOLOAD} = sub { shift->$canonical(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
290 4 return $self->$canonical(@argument);
291 }
292  
293 53 my $super = "SUPER::$method";
294 53 ▶ return $self->$super(@argument) unless $method =~ /^key[0]*(\d+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^key[0]*(\d+)$/i 1 52 53
295 52 my $key = $1;
296  
297 52 ▶ return $self->_SvcParam($key) unless @argument;
Branch: 1 branch
BranchTrueFalseTotal
unless @argument 9 43 52
298 43 my $first = shift @argument;
299 43 ▶ my $value = defined $first ? Net::DNS::Text->new($first)->raw : $first;
Branch: 1 branch
BranchTrueFalseTotal
defined $first ? : 12 31 43
300 43 return $self->_SvcParam( $key, $value, @argument );
301 }
302  
303  
304 108 ▶sub _SvcParam {
Subroutine _SvcParam: called CC=9 SCAR=22.0
305 108 my ( $self, $key, @argument ) = @_;
306  
307 108 ▶ my $paramsref = $self->{SvcParams} || [];
Condition100%2/2
$self->{'SvcParams'} || []
l!l
6444
MC/DC100%1/1
$self->{'SvcParams'} || []
$self->{'SvcParams'}
Truth table100%2/2
A
A= $self->{'SvcParams'}
Aresult
11
00
308 108 my %svcparams = @$paramsref;
309  
310 108 ▶ if ( scalar @argument ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @argument) { } 82 26 108
311 82 my $arg = shift @argument; # keyNN($value);
312 82 ▶ delete $svcparams{$key} unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 32 50 82
313 82 ▶ die( $self->type . qq[: duplicate SvcParam "key$key"] ) if defined $svcparams{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $svcparams{$key} 1 81 82
314 81 ▶ die( $self->type . qq[: invalid SvcParam "key$key"] ) if $key > 65534;
Branch: 1 branch
BranchTrueFalseTotal
if $key > 65534 1 80 81
315 80 ▶ die( $self->type . qq[: unexpected "key$key" value] ) if scalar @argument;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @argument 2 78 80
316 78 delete $self->{rdata};
317 78 ▶ $svcparams{$key} = $arg if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 46 32 78
318 78 $self->{SvcParams} = [map { ( $_, $svcparams{$_} ) } sort { $a <=> $b } keys %svcparams];
319 } else {
320 26 ▶ die( $self->type . qq[: no value specified for "key$key"] ) unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 6 20 26
321 }
322  
323 98 return $svcparams{$key};
324 }
325  
326 ########################################
327  
328  
329 2 1;
330 __END__
« DNS/RR/IPSECKEY.pm ↑ Summary DNS/RR/DELEG.pm »