File Coverage

blib/lib/Net/Frame/Layer/CDP/Address.pm
Criterion Covered Total %
statement 52 71 73.2
branch 8 32 25.0
condition n/a
subroutine 13 16 81.2
pod 8 8 100.0
total 81 127 63.7


line stmt bran cond sub pod time code
1             #
2             # $Id: Address.pm 1640 2009-11-09 17:58:27Z VinsWorldcom $
3             #
4             package Net::Frame::Layer::CDP::Address;
5 19     19   7408 use strict; use warnings;
  19     19   32  
  19         645  
  19         92  
  19         31  
  19         599  
6              
7 19     19   1329 use Net::Frame::Layer qw(:consts :subs);
  19         83875  
  19         3876  
8 19     19   101 use Exporter;
  19         33  
  19         2137  
9             our @ISA = qw(Net::Frame::Layer Exporter);
10              
11             our %EXPORT_TAGS = (
12             consts => [qw(
13             NF_CDP_ADDRESS_PROTOCOL_TYPE_NLPID
14             NF_CDP_ADDRESS_PROTOCOL_TYPE_8022
15             NF_CDP_ADDRESS_PROTOCOL_IP
16             NF_CDP_ADDRESS_PROTOCOL_IPv6
17             )],
18             );
19             our @EXPORT_OK = (
20             @{$EXPORT_TAGS{consts}},
21             );
22              
23 19     19   105 use constant NF_CDP_ADDRESS_PROTOCOL_TYPE_NLPID => 1;
  19         32  
  19         1129  
24 19     19   102 use constant NF_CDP_ADDRESS_PROTOCOL_TYPE_8022 => 2;
  19         82  
  19         1056  
25 19     19   94 use constant NF_CDP_ADDRESS_PROTOCOL_IP => CORE::pack 'H*', 'cc';
  19         31  
  19         980  
26 19     19   207 use constant NF_CDP_ADDRESS_PROTOCOL_IPv6 => CORE::pack 'H*', 'aaaa0300000086dd';
  19         28  
  19         18266  
27              
28             our @AS = qw(
29             protocolType
30             protocolLength
31             protocol
32             addressLength
33             address
34             );
35             __PACKAGE__->cgBuildIndices;
36             __PACKAGE__->cgBuildAccessorsScalar(\@AS);
37              
38             #no strict 'vars';
39              
40             sub new {
41             shift->SUPER::new(
42 3     3 1 490 protocolType => NF_CDP_ADDRESS_PROTOCOL_TYPE_NLPID,
43             protocolLength => 1,
44             protocol => NF_CDP_ADDRESS_PROTOCOL_IP,
45             addressLength => 4,
46             address => '127.0.0.1',
47             @_,
48             );
49             }
50              
51             sub ipv4Address {
52 0     0 1 0 return new(@_)
53             }
54              
55             sub ipv6Address {
56             shift->SUPER::new(
57 2     2 1 463 protocolType => NF_CDP_ADDRESS_PROTOCOL_TYPE_8022,
58             protocolLength => 8,
59             protocol => NF_CDP_ADDRESS_PROTOCOL_IPv6,
60             addressLength => 16,
61             address => '::1',
62             @_,
63             );
64             }
65              
66             sub getLength {
67 0     0 1 0 my $self = shift;
68              
69 0         0 my $length = 4;
70 0         0 $length += length($self->protocol);
71 0         0 $length += $self->addressLength;
72              
73 0         0 return $length
74             }
75              
76             sub pack {
77 1     1 1 302 my $self = shift;
78              
79 1 50       4 my $raw = $self->SUPER::pack('CC',
80             $self->protocolType,
81             $self->protocolLength
82             ) or return;
83              
84 1         57 my $protocolLength = $self->protocolLength;
85 1 50       15 $raw .= $self->SUPER::pack("a$protocolLength n",
86             $self->protocol,
87             $self->addressLength
88             ) or return;
89              
90 1         31 my $addressLength = $self->addressLength;
91 1 50       12 if ($self->protocol eq NF_CDP_ADDRESS_PROTOCOL_IP) {
    0          
92 1 50       15 $raw .= $self->SUPER::pack('a4',
93             inetAton($self->address)
94             ) or return;
95             } elsif ($self->protocol eq NF_CDP_ADDRESS_PROTOCOL_IPv6) {
96 0 0       0 $raw .= $self->SUPER::pack('a16',
97             inet6Aton($self->address)
98             ) or return;
99             } else {
100 0 0       0 $raw .= $self->SUPER::pack("a$addressLength",
101             $self->address
102             ) or return;
103             }
104              
105 1         44 return $self->raw($raw);
106             }
107              
108             sub unpack {
109 1     1 1 21 my $self = shift;
110              
111 1 50       6 my ($protocolType, $protocolLength, $tail) =
112             $self->SUPER::unpack('CC a*', $self->raw)
113             or return;
114              
115 1         36 $self->protocolType($protocolType);
116 1         14 $self->protocolLength($protocolLength);
117              
118 1         10 my ($protocol, $addressLength);
119 1 50       19 ($protocol, $addressLength, $tail) =
120             $self->SUPER::unpack("a$protocolLength n a*", $tail)
121             or return;
122              
123 1         22 $self->protocol($protocol);
124 1         14 $self->addressLength($addressLength);
125              
126 1         10 my ($address, $payload);
127 1 50       4 if ($self->protocol eq NF_CDP_ADDRESS_PROTOCOL_IP) {
    0          
128 1 50       16 ($address, $payload) =
129             $self->SUPER::unpack('a4 a*', $tail)
130             or return;
131 1         16 $self->address(inetNtoa($address));
132             } elsif ($self->protocol eq NF_CDP_ADDRESS_PROTOCOL_IPv6) {
133 0 0       0 ($address, $payload) =
134             $self->SUPER::unpack('a16 a*', $tail)
135             or return;
136 0         0 $self->address(inet6Ntoa($address));
137             } else {
138 0 0       0 ($address, $payload) =
139             $self->SUPER::unpack("a$addressLength a*", $tail)
140             or return;
141 0         0 $self->address($address);
142             }
143              
144 1         23 $self->payload($payload);
145              
146 1         11 return $self;
147             }
148              
149             sub computeLengths {
150 0     0 1 0 my $self = shift;
151              
152 0         0 $self->protocolLength(length($self->protocol));
153              
154 0 0       0 if ($self->protocol eq NF_CDP_ADDRESS_PROTOCOL_IP) {
    0          
155 0         0 $self->addressLength(4)
156             } elsif ($self->protocol eq NF_CDP_ADDRESS_PROTOCOL_IPv6) {
157 0         0 $self->addressLength(16)
158             }else {
159 0         0 return;
160             }
161              
162 0         0 return 1;
163             }
164              
165             sub print {
166 9     9 1 17 my $self = shift;
167              
168 9         33 my $l = $self->layer;
169 9         81 my $buf = sprintf
170             "$l: protocolType:%d protocolLength:%d protocol:0x%s\n".
171             "$l: addressLength:%d address:%s",
172             $self->protocolType, $self->protocolLength, (CORE::unpack 'H*', $self->protocol),
173             $self->addressLength, $self->address;
174              
175 9         824 return $buf;
176             }
177              
178             1;
179              
180             __END__