blib/lib/Net/DHCPv6/Option/SipServerD.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 150 CC 12 SCAR 35.1
CC
12
Cov
0%
_read_labels_at
90.0
_encode_domain
6.0
_decode_domains
6.0
CRAP 33.4
1 #!/bin/false
2 # ABSTRACT: SIP Server Domain Name option (code 21) -- list of domain names
3 # PODNAME: Net::DHCPv6::Option::SipServerD
4 use strictures 2;
5  
6 package Net::DHCPv6::Option::SipServerD;
7 $Net::DHCPv6::Option::SipServerD::VERSION = '0.002';
8 use Net::DHCPv6::OptionList;
9 use Net::DHCPv6::Option;
10 use Net::DHCPv6::Constants;
11 use Net::DHCPv6::X::Truncated;
12 use Net::DHCPv6::X::BadOption;
13 use parent 'Net::DHCPv6::Option';
14 use Ref::Util qw( is_plain_arrayref );
15 use namespace::clean;
16 my $EMPTY = q();
17 my $MAX_PTR_DEPTH = 255; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
18  
19 sub _encode_domain {
20 my ( $domain ) = @_;
21 return chr( 0 ) unless defined $domain && CORE::length( $domain );
22 my @labels = split m/[.]/, $domain;
23 return join( $EMPTY, map { pack( 'C', CORE::length ) . $_ } @labels ) . chr( 0 );
24 }
25  
26 sub _read_labels_at {
27 my ( $payload, $offset_ref, $len, $depth ) = @_;
28 $depth //= 0;
29 my @labels;
30 while ( ${$offset_ref} < $len ) {
31 my $llen = unpack( 'C', substr( $payload, ${$offset_ref}, 1 ) );
32 if ( $llen == 0 ) {
33 ++${$offset_ref};
34 last;
35 }
36 if ( ( $llen & $DN_COMPRESS_MASK ) == $DN_COMPRESS_MASK ) { ## no critic (Bangs::ProhibitBitwiseOperators ValuesAndExpressions::ProhibitMagicNumbers)
37 if ( $Net::DHCPv6::Option::FOLLOW_COMPRESSION ) {
38 Net::DHCPv6::X::Truncated->throw( message => 'Truncated compression pointer' )
39 if ${$offset_ref} + 2 > $len;
40 my $ptr =
41 ( ( $llen & $DN_LABEL_MASK ) << 8 ) | unpack( 'C', substr( $payload, ${$offset_ref} + 1, 1 ) ); ## no critic (Bangs::ProhibitBitwiseOperators ValuesAndExpressions::ProhibitMagicNumbers)
42 Net::DHCPv6::X::BadOption->throw( message => 'Compression pointer out of range' )
43 if $ptr >= $len;
44 Net::DHCPv6::X::BadOption->throw( message => 'Compression pointer depth exceeded' )
45 if $depth > $MAX_PTR_DEPTH;
46 ${$offset_ref} += 2;
47 my $ptr_ref = \$ptr;
48 push @labels, _read_labels_at( $payload, $ptr_ref, $len, $depth + 1 );
49 last;
50 }
51 Net::DHCPv6::X::BadOption->throw( message => 'Compression pointer in domain name' );
52 }
53 Net::DHCPv6::X::BadOption->throw( message => 'Invalid domain label length' ) if $llen > $DN_LABEL_MASK;
54 ++${$offset_ref};
55 Net::DHCPv6::X::Truncated->throw( message => 'Truncated domain label' )
56 if ${$offset_ref} + $llen > $len;
57 push @labels, substr( $payload, ${$offset_ref}, $llen );
58 ${$offset_ref} += $llen;
59 }
60 return @labels;
61 }
62  
63 sub _decode_domains {
64 my ( $payload ) = @_;
65 my @domain_list;
66 my $offset = 0;
67 my $len = CORE::length( $payload );
68 while ( $offset < $len ) {
69 my @labels = _read_labels_at( $payload, \$offset, $len );
70 push @domain_list, @labels ? join( '.', @labels ) : $EMPTY;
71 }
72 return \@domain_list;
73 }
74  
75 sub new {
76 my ( $class, %args ) = @_;
77 my $domains = $args{domains} // $args{data} // [];
78 $domains = [$domains] unless is_plain_arrayref( $domains );
79 $args{code} = $OPTION_SIP_SERVER_D;
80 $args{data} = join( $EMPTY, map { _encode_domain( $_ ) } @{$domains} );
81 my $self = $class->SUPER::new( %args );
82 $self->{domains} = $domains;
83 return bless $self, $class;
84 }
85  
86 sub domains { return shift->{domains} }
87  
88 sub from_bytes_inner {
89 my ( $class, $code, $payload ) = @_;
90 my $domains = _decode_domains( $payload );
91 return $class->new( domains => $domains );
92 }
93  
94 $Net::DHCPv6::OptionList::OPTION_CLASS{$OPTION_SIP_SERVER_D} = __PACKAGE__;
95 1;
96  
97 __END__