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

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 119 CC 8 SCAR 25.8
CC
8
Cov
0%
new
42.0
from_bytes
12.0
msg_type
2.0
CRAP 13.2
1 #!/bin/false
2 # ABSTRACT: DHCPv6 packet base class
3 # PODNAME: Net::DHCPv6::Packet
4 use strictures 2;
5  
6 package Net::DHCPv6::Packet;
7 $Net::DHCPv6::Packet::VERSION = '0.002';
8 use Carp qw( croak );
9 use Net::DHCPv6::Constants;
10 use Net::DHCPv6::OptionList;
11 use Net::DHCPv6::Packet::Relay;
12 use Net::DHCPv6::X::BadMessage;
13 use namespace::clean;
14  
15 my $TX_ID_MAX = 0xFFFFFF; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
16 my $TX_ID_BYTES = 3; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
17 my $HDR_SIZE = 4; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
18  
19 sub new {
20 my ( $class, @argv ) = @_;
21 if ( @argv == 1 ) {
22 return $class->from_bytes( $argv[0] );
23 }
24 croak 'Packet->new: no arguments' unless @argv;
25 my %args = @argv;
26 croak 'Packet->new: msg_type is required' unless defined $args{msg_type};
27 croak 'Packet->new: transaction_id is required' unless defined $args{transaction_id};
28 croak 'transaction_id must fit in 24 bits'
29 if $args{transaction_id} < 0 || $args{transaction_id} > $TX_ID_MAX;
30 $args{options} = $args{options} // Net::DHCPv6::OptionList->new;
31 my $self = {
32 msg_type => $args{msg_type},
33 transaction_id => $args{transaction_id},
34 options => $args{options},
35 };
36 return bless $self, $class;
37 }
38  
39 sub msg_type { return shift->{msg_type} }
40 sub transaction_id { return shift->{transaction_id} }
41 sub options { return shift->{options} }
42  
43 sub add_option {
44 my ( $self, $option ) = @_;
45 return $self->{options}->add_option( $option );
46 }
47  
48 sub get_option {
49 my ( $self, $code ) = @_;
50 return $self->{options}->get_option( $code );
51 }
52  
53 sub as_bytes {
54 my $self = shift;
55 my $tid = substr( pack( 'N', $self->{transaction_id} ), 1, $TX_ID_BYTES );
56 my $opts = $self->{options}->as_bytes;
57 return pack( 'C', $self->{msg_type} ) . $tid . $opts;
58 }
59  
60 sub from_bytes {
61 my ( $class, $bytes ) = @_;
62 Net::DHCPv6::X::BadMessage->throw( message => 'Empty packet data' )
63 if !defined $bytes || CORE::length( $bytes ) < $HDR_SIZE;
64 my $msg_type = unpack( 'C', substr( $bytes, 0, 1 ) );
65  
66 if ( $msg_type == $RELAY_FORW || $msg_type == $RELAY_REPLY ) {
67 return Net::DHCPv6::Packet::Relay->from_bytes( $bytes );
68 }
69  
70 my $tid = unpack( 'N', chr( 0 ) . substr( $bytes, 1, $TX_ID_BYTES ) );
71 my $opts_bytes = substr( $bytes, $HDR_SIZE );
72  
73 my $subclass = $Net::DHCPv6::Packet::MESSAGE_CLASS{$msg_type} || $class;
74 my $opts = Net::DHCPv6::OptionList->from_bytes( $opts_bytes );
75 my $packet = bless {
76 msg_type => $msg_type,
77 transaction_id => $tid,
78 options => $opts,
79 }, $subclass;
80 return $packet;
81 }
82  
83 sub type {
84 my $self = shift;
85 return $Net::DHCPv6::Constants::REV_MESSAGE_TYPE{ $self->{msg_type} };
86 }
87  
88 sub msg_type_name { return shift->type }
89  
90 our %MESSAGE_CLASS;
91  
92 1;
93  
94 __END__