File Coverage

blib/lib/Net/Frame/Layer/CDP/Addresses.pm
Criterion Covered Total %
statement 39 55 70.9
branch 3 8 37.5
condition 1 6 16.6
subroutine 9 11 81.8
pod 6 6 100.0
total 58 86 67.4


line stmt bran cond sub pod time code
1             #
2             # $Id: Addresses.pm 1640 2013-03-28 17:58:27Z VinsWorldcom $
3             #
4             package Net::Frame::Layer::CDP::Addresses;
5 19     19   938 use strict; use warnings;
  19     19   33  
  19         609  
  19         93  
  19         33  
  19         505  
6              
7 19     19   89 use Net::Frame::Layer qw(:consts :subs);
  19         31  
  19         5336  
8             our @ISA = qw(Net::Frame::Layer);
9              
10             our @AS = qw(
11             type
12             length
13             numAddresses
14             );
15             our @AA = qw(
16             addresses
17             );
18             __PACKAGE__->cgBuildIndices;
19             __PACKAGE__->cgBuildAccessorsScalar(\@AS);
20             __PACKAGE__->cgBuildAccessorsArray (\@AA);
21              
22             #no strict 'vars';
23              
24 19     19   673 use Net::Frame::Layer::CDP::Constants qw(:consts);
  19         42  
  19         16001  
25              
26             sub new {
27             shift->SUPER::new(
28 2     2 1 724 type => NF_CDP_TYPE_ADDRESSES,
29             length => 8,
30             numAddresses => 0,
31             addresses => [],
32             @_,
33             );
34             }
35              
36             sub getLength {
37 0     0 1 0 my $self = shift;
38              
39 0         0 my $length = 8;
40 0         0 $length += $_->getLength for $self->addresses;
41              
42 0         0 return $length
43             }
44              
45             sub pack {
46 1     1 1 262 my $self = shift;
47              
48 1 50       5 my $raw = $self->SUPER::pack('nnN',
49             $self->type,
50             $self->length,
51             $self->numAddresses
52             ) or return;
53              
54 1         63 for ($self->addresses) {
55 0         0 $raw .= $_->pack;
56             }
57              
58 1         24 return $self->raw($raw);
59             }
60              
61             sub _unpackAddresses {
62 1     1   2 my $self = shift;
63 1         2 my ($payload) = @_;
64              
65 1         2 my @addressesList;
66 1   33     9 while (defined($payload) && length($payload)) {
67 0         0 my $addr = Net::Frame::Layer::CDP::Address->new(raw => $payload)->unpack;
68 0         0 push @addressesList, $addr;
69 0         0 $payload = $addr->payload;
70 0         0 $addr->payload(undef);
71             }
72 1         9 $self->addresses(\@addressesList);
73 1         11 return $payload;
74             }
75              
76             sub unpack {
77 1     1 1 16 my $self = shift;
78              
79 1 50       5 my ($type, $length, $numAddresses, $tail) =
80             $self->SUPER::unpack('nnN a*', $self->raw)
81             or return;
82              
83 1         33 $self->type($type);
84 1         18 $self->length($length);
85 1         12 $self->numAddresses($numAddresses);
86              
87 1         10 my $valLen = $length - 8; # 4 + TLValue(4) then addresses array
88 1 50       8 my ($addresses, $payload) =
89             $self->SUPER::unpack("a$valLen a*", $tail)
90             or return;
91              
92 1         15 $self->_unpackAddresses($addresses);
93              
94 1         7 $self->payload($payload);
95              
96 1         11 return $self;
97             }
98              
99             sub computeLengths {
100 0     0 1 0 my $self = shift;
101              
102 0         0 my $length = 8;
103 0         0 $length += $_->getLength for $self->addresses;
104 0         0 $self->length($length);
105              
106             # Calculate numAddresses from addresses array items
107 0 0 0     0 if (scalar($self->addresses) && ($self->numAddresses == 0)) {
108 0         0 $self->numAddresses(scalar($self->addresses))
109             }
110              
111 0         0 return 1;
112             }
113              
114             sub print {
115 3     3 1 11 my $self = shift;
116              
117 3         12 my $l = $self->layer;
118 3         35 my $buf = sprintf
119             "$l: type:0x%04x length:%d numAddresses:%d",
120             $self->type, $self->length, $self->numAddresses;
121              
122 3         98 for ($self->addresses) {
123 4         34 $buf .= "\n" . $_->print;
124             }
125              
126 3         493 return $buf;
127             }
128              
129             1;
130              
131             __END__