blib/lib/Net/DHCPv6/DUID.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 285 CC 26 SCAR 28.1
CC
26
Cov
0%
try_from_bytes
56.0
new
30.0
as_bytes
30.0
CRAP 16.6
1 #!/bin/false
2 # ABSTRACT: DUID parse/emit and helper constructors
3 # PODNAME: Net::DHCPv6::DUID
4 use strictures 2;
5  
6 package Net::DHCPv6::DUID;
7 $Net::DHCPv6::DUID::VERSION = '0.002';
8 use Carp qw( croak );
9 use Net::DHCPv6::Constants;
10 use Net::DHCPv6::X::BadDUID;
11 use namespace::clean;
12 my $EMPTY = q();
13 my $N_LEN = 4; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
14 my $LLT_HDR_SIZE = 6; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
15  
16 sub new {
17 my ( $class, %args ) = @_;
18 my $type = $args{duid_type} // croak 'Net::DHCPv6::DUID->new: duid_type is required';
19 my $self = { duid_type => $type, identifier => $args{identifier} // $EMPTY };
20 if ( $type == $DUID_LLT ) { _new_llt( $self, \%args ) }
21 if ( $type == $DUID_EN ) { _new_en( $self, \%args ) }
22 if ( $type == $DUID_LL ) { _new_ll( $self, \%args ) }
23 if ( $type == $DUID_UUID ) { _new_uuid( $self, \%args ) }
24 return bless $self, $class;
25 }
26  
27 sub _new_llt {
28 my ( $self, $arg ) = @_;
29 croak 'DUID-LLT requires link_layer_type, time, and identifier'
30 unless defined $arg->{link_layer_type}
31 && defined $arg->{time}
32 && defined $arg->{identifier};
33 $self->{link_layer_type} = $arg->{link_layer_type};
34 $self->{time} = $arg->{time};
35 $self->{identifier} = $arg->{identifier};
36 return;
37 }
38  
39 sub _new_en {
40 my ( $self, $arg ) = @_;
41 croak 'DUID-EN requires enterprise_number and identifier'
42 unless defined $arg->{enterprise_number}
43 && defined $arg->{identifier};
44 $self->{enterprise_number} = $arg->{enterprise_number};
45 $self->{identifier} = $arg->{identifier};
46 return;
47 }
48  
49 sub _new_ll {
50 my ( $self, $arg ) = @_;
51 croak 'DUID-LL requires link_layer_type and identifier'
52 unless defined $arg->{link_layer_type}
53 && defined $arg->{identifier};
54 $self->{link_layer_type} = $arg->{link_layer_type};
55 $self->{identifier} = $arg->{identifier};
56 return;
57 }
58  
59 sub _new_uuid {
60 my ( $self, $arg ) = @_;
61 croak 'DUID-UUID requires identifier (16 bytes)'
62 unless defined $arg->{identifier};
63 croak 'DUID-UUID identifier must be 16 bytes'
64 unless CORE::length( $arg->{identifier} ) == $IPV6_ADDR_LEN;
65 $self->{identifier} = $arg->{identifier};
66 return;
67 }
68  
69 sub duid_type { return shift->{duid_type} }
70 sub link_layer_type { return shift->{link_layer_type} }
71 sub time { return shift->{time} }
72 sub enterprise_number { return shift->{enterprise_number} }
73 sub identifier { return shift->{identifier} }
74  
75 my %DUID_LENGTH_BASE = (
76 $DUID_LLT => 2 + 2 + $N_LEN,
77 $DUID_EN => 2 + $N_LEN,
78 $DUID_LL => 2 + 2,
79 $DUID_UUID => 2,
80 );
81  
82 sub length {
83 my $self = shift;
84 my $type = $self->{duid_type};
85 my $id = $self->{identifier} // $EMPTY;
86 return ( $DUID_LENGTH_BASE{$type} // 2 ) + CORE::length( $id );
87 }
88  
89 sub as_bytes {
90 my $self = shift;
91 my $type = $self->{duid_type};
92 my $id = $self->{identifier} // $EMPTY;
93 my $buf = pack( 'n', $type );
94  
95 if ( $type == $DUID_LLT ) {
96 $buf .= pack( 'n', $self->{link_layer_type} );
97 $buf .= pack( 'N', $self->{time} );
98 $buf .= $id;
99 return $buf;
100 }
101 if ( $type == $DUID_EN ) {
102 $buf .= pack( 'N', $self->{enterprise_number} );
103 $buf .= $id;
104 return $buf;
105 }
106 if ( $type == $DUID_LL ) {
107 $buf .= pack( 'n', $self->{link_layer_type} );
108 $buf .= $id;
109 return $buf;
110 }
111 if ( $type == $DUID_UUID ) {
112 $buf .= $id;
113 return $buf;
114 }
115 $buf .= $id;
116 return $buf;
117 }
118  
119 sub as_string {
120 my $self = shift;
121 my $bytes = $self->as_bytes;
122 my $tname = $Net::DHCPv6::Constants::REV_DUID_TYPE{ $self->{duid_type} }
123 || sprintf( 'TYPE%d', $self->{duid_type} );
124 return sprintf( '%s:%s', $tname, unpack( 'H*', $bytes ) );
125 }
126  
127 sub _try_llt {
128 my ( $class, $rest, $rest_len ) = @_;
129 my $partial = bless( { duid_type => $DUID_LLT }, $class );
130 my $error;
131 if ( $rest_len >= 2 ) {
132 $partial->{link_layer_type} = unpack( 'n', $rest );
133 if ( $rest_len >= $LLT_HDR_SIZE ) {
134 $partial->{time} = unpack( 'x2 N', $rest );
135 $partial->{identifier} = substr( $rest, $LLT_HDR_SIZE );
136 }
137 else {
138 $error = "Need $LLT_HDR_SIZE bytes for DUID-LLT hwtype+time, got $rest_len";
139 }
140 }
141 else {
142 $error = "Need at least 2 bytes for DUID-LLT hwtype, got $rest_len";
143 }
144 return ( $partial, $error );
145 }
146  
147 sub _try_en {
148 my ( $class, $rest, $rest_len ) = @_;
149 my $partial = bless( { duid_type => $DUID_EN }, $class );
150 my $error;
151 if ( $rest_len >= $N_LEN ) {
152 $partial->{enterprise_number} = unpack( 'N', $rest );
153 $partial->{identifier} = substr( $rest, $N_LEN );
154 }
155 else {
156 $error = "Need $N_LEN bytes for DUID-EN enterprise_number, got $rest_len";
157 }
158 return ( $partial, $error );
159 }
160  
161 sub _try_ll {
162 my ( $class, $rest, $rest_len ) = @_;
163 my $partial = bless( { duid_type => $DUID_LL }, $class );
164 my $error;
165 if ( $rest_len >= 2 ) {
166 $partial->{link_layer_type} = unpack( 'n', $rest );
167 $partial->{identifier} = substr( $rest, 2 );
168 }
169 else {
170 $error = "Need at least 2 bytes for DUID-LL hwtype, got $rest_len";
171 }
172 return ( $partial, $error );
173 }
174  
175 sub _try_uuid {
176 my ( $class, $rest, $rest_len ) = @_;
177 my $partial = bless( { duid_type => $DUID_UUID }, $class );
178 my $error;
179 if ( $rest_len >= $IPV6_ADDR_LEN ) {
180 $partial->{identifier} = substr( $rest, 0, $IPV6_ADDR_LEN );
181 }
182 else {
183 $error = "Need $IPV6_ADDR_LEN bytes for DUID-UUID, got $rest_len";
184 }
185 return ( $partial, $error );
186 }
187  
188 sub try_from_bytes {
189 my ( $class, $bytes ) = @_;
190 return ( undef, 'No data provided' ) unless defined $bytes;
191  
192 my $len = CORE::length( $bytes );
193 return ( undef, 'Need at least 2 bytes for DUID type' ) if $len < 2;
194  
195 my $type = unpack( 'n', substr( $bytes, 0, 2 ) );
196 my $rest = substr( $bytes, 2 );
197 my $rest_len = CORE::length( $rest );
198  
199 if ( $type == $DUID_LLT ) { return _try_llt( $class, $rest, $rest_len ) }
200 if ( $type == $DUID_EN ) { return _try_en( $class, $rest, $rest_len ) }
201 if ( $type == $DUID_LL ) { return _try_ll( $class, $rest, $rest_len ) }
202 if ( $type == $DUID_UUID ) { return _try_uuid( $class, $rest, $rest_len ) }
203  
204 return (
205 bless(
206 {
207 duid_type => $type,
208 identifier => $rest,
209 },
210 $class
211 ),
212 undef
213 );
214 }
215  
216 sub from_bytes {
217 my ( $class, $bytes ) = @_;
218 my ( $duid, $error ) = $class->try_from_bytes( $bytes );
219 Net::DHCPv6::X::BadDUID->throw( message => $error ) if $error;
220 return $duid;
221 }
222  
223 sub new_llt {
224 my ( $class, $link_layer_type, $time, $mac_bytes ) = @_;
225 return $class->new(
226 duid_type => $DUID_LLT,
227 link_layer_type => $link_layer_type,
228 time => $time,
229 identifier => $mac_bytes,
230 );
231 }
232  
233 sub new_en {
234 my ( $class, $enterprise_number, $identifier ) = @_;
235 return $class->new(
236 duid_type => $DUID_EN,
237 enterprise_number => $enterprise_number,
238 identifier => $identifier,
239 );
240 }
241  
242 sub new_ll {
243 my ( $class, $link_layer_type, $mac_bytes ) = @_;
244 return $class->new(
245 duid_type => $DUID_LL,
246 link_layer_type => $link_layer_type,
247 identifier => $mac_bytes,
248 );
249 }
250  
251 sub new_uuid {
252 my ( $class, $uuid_bytes ) = @_;
253 return $class->new(
254 duid_type => $DUID_UUID,
255 identifier => $uuid_bytes,
256 );
257 }
258  
259 1;
260  
261 __END__