Packet.pm

filter: Statementstmt 34.0% 30 / 88 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 37.5% 9 / 24 Podpod 100% 11 / 11 totaltotal 29.7% 50 / 168 CC 20 SCAR 30.4
CC
20
Cov
24%
_decode
72.0
_encode
30.0
_first_mac
20.0
CRAP 21.0
↑ Summary Server.pm »
1 package VMPS::Packet;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw[ Exporter ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.04';
7  
8 2 ▶use Net::MAC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Storable qw[ dclone ];
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 VMPS::Packet - Handle VMPS request/response packets.
14  
15 =cut
16  
17 2 our %CONSTANTS;
18 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 %CONSTANTS = (
20 VMPS_REQ_JOIN => 0x1,
21 VMPS_RESP_JOIN => 0x2,
22 VMPS_REQ_RECONFIRM => 0x3,
23 VMPS_RESP_RECONFIRM => 0x4,
24  
25 VMPS_ERROR_NONE => 0x0,
26 VMPS_ERROR_ACCESS_DENIED => 0x3,
27 VMPS_ERROR_SHUTDOWN => 0x4,
28 VMPS_ERROR_WRONG_DOMAIN => 0x5,
29  
30 VMPS_DATA_CLIENT_IP => 0xc01,
31 VMPS_DATA_PORT_NAME => 0xc02,
32 VMPS_DATA_VLAN_NAME => 0xc03,
33 VMPS_DATA_VTP_DOMAIN => 0xc04,
34 VMPS_DATA_PACKET => 0xc05,
35 VMPS_DATA_MAC_DYNAMIC => 0xc06,
36 VMPS_DATA_UNK7 => 0xc07,
37 VMPS_DATA_MAC_STATIC => 0xc08,
38 );
39 }
40  
41 2 ▶use constant \%CONSTANTS;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use constant VMPS_HEADER_SIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use constant VMPS_TLV_HLEN => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 our @EXPORT_OK = keys %CONSTANTS;
46 2 our %EXPORT_TAGS = (
47 'constants' => [keys %CONSTANTS],
48 );
49  
50 #################################################################
51  
52 0 ▶sub _decode {
Subroutine _decode: not called CC=8 SCAR=42.8
53 0 my ($this, $dgram) = @_;
54  
55 0 ▶ die "Short packet!" if length($dgram) < VMPS_HEADER_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if length $dgram < 8 0 0 0
56  
57 ## parse the header
58 0 my ($one, $type, $err, $num_rec, $seq)
59 = unpack('C4 N', substr($dgram, 0, VMPS_HEADER_SIZE, ''));
60  
61 ## sanity check
62 0 ▶ die "Packet must start with 0x01" unless ($one == 0x1);
Branch: 1 branch
BranchTrueFalseTotal
unless $one == 1 0 0 0
63 0 ▶ die "Unknown request type: $type"
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 1 or $type == 3 0 0 0
Condition0%0/3
$type == 1 or $type == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 1 or $type == 3
$type == 1 $type == 3
Truth table0%0/3
A or B
A= $type == 1B= $type == 3
ABresult
1X1
011
000
64 unless ($type == VMPS_REQ_JOIN or $type == VMPS_REQ_RECONFIRM);
65  
66 0 my %pkt_data;
67  
68 ## decode TLVs
69 0 ▶ while ($num_rec > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($num_rec > 0) 0 0 0
70 {
71 0 ▶ $dgram or die "Short packet (expecting $num_rec more records)";
Branch: 1 branch
BranchTrueFalseTotal
$dgram or die "Short packet (expecting $num_rec more records)" 0 0 0
72 0 $num_rec--;
73 0 my ($typ, $len, $val);
74  
75 0 ($typ, $len) = unpack('Nn', substr($dgram, 0, VMPS_TLV_HLEN, ''));
76  
77 0 $val = substr($dgram, 0, $len, '');
78  
79 0 $pkt_data{$typ} = $val;
80 }
81  
82 0 ▶ $dgram and die "Extra data at end of packet!";
Branch: 1 branch
BranchTrueFalseTotal
$dgram and die "Extra data at end of packet!" 0 0 0
83  
84 0 bless { TYPE => $type,
85 ERR => $err,
86 SEQ => $seq,
87 DATA => \%pkt_data }, $this;
88 }
89  
90 ###################################
91  
92 0 ▶sub _encode {
Subroutine _encode: not called CC=5 SCAR=34.0
93 0 my ($this) = @_;
94  
95 ###################################
96 ## encode packet header
97  
98 ## leading byte always 1
99 0 my $pkt = pack('C', 1);
100  
101 die "Unknown response type ($this->{TYPE})!"
102 unless ( $this->{TYPE} == VMPS_RESP_JOIN
103 0 ▶ or $this->{TYPE} == VMPS_RESP_RECONFIRM);
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"TYPE"} == 2 or $this->{"TYPE"} == 4 0 0 0
Condition0%0/3
$this->{"TYPE"} == 2 or $this->{"TYPE"} == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->{"TYPE"} == 2 or $this->{"TYPE"} == 4
$this->{"TYPE"} == 2 $this->{"TYPE"} == 4
Truth table0%0/3
A or B
A= $this->{"TYPE"} == 2B= $this->{"TYPE"} == 4
ABresult
1X1
011
000
104  
105 0 ▶ my $num_rec = $this->{ERR} == VMPS_ERROR_NONE ? 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->{'ERR'} == 0 ? : 0 0 0
106  
107 0 $pkt .= pack('C3 N', $this->{TYPE}, $this->{ERR}, $num_rec, $this->{SEQ});
108  
109 ###################################
110 ## encode contents
111  
112 0 ▶ if ($this->{ERR} == VMPS_ERROR_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"ERR"} == 0) 0 0 0
113 {
114 0 $pkt .= $this->_encode_data(VMPS_DATA_VLAN_NAME);
115 0 $pkt .= $this->_encode_data(VMPS_DATA_MAC_STATIC, $this->_first_mac());
116 }
117  
118 0 return $pkt;
119 }
120  
121 ###################################
122  
123 0 ▶sub _encode_data {
Subroutine _encode_data: not called CC=2 SCAR=17.9
124 0 my $this = shift;
125 0 my $type = shift;
126  
127 0 ▶ my $val = @_ ? $_[0] : $this->{DATA}{$type};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
128 0 my $len = length($val);
129  
130 0 return pack ('N n a*', $type, $len, $val);
131 }
132  
133 #################################################################
134 ## accessor functions
135  
136 =head1 FUNCTIONS
137  
138 =head2 client_ip()
139  
140 Return the IP address encoded in the request. NB: this may be different
141 from the source IP in the UDP header.
142  
143 =cut
144  
145 0 ▶sub client_ip { join '.', unpack('C4', shift->{DATA}{+VMPS_DATA_CLIENT_IP}) }
Subroutine client_ip: not called CC=1 SCAR=6.9
Pod documented
146  
147 =head2 port()
148  
149 The switch port name in the request.
150  
151 =cut
152  
153 0 ▶sub port { shift->{DATA}{+VMPS_DATA_PORT_NAME} }
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
154  
155 =head2 vlan()
156  
157 Return the VLAN name from the request. With arg, sets the VLAN name in
158 replies:
159  
160 $reply->vlan('your_vlan');
161  
162 =cut
163  
164 0 ▶sub vlan {
Subroutine vlan: not called CC=2 SCAR=17.9
Pod documented
165 0 my $this = shift;
166  
167 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
168 {
169 0 $this->{DATA}{+VMPS_DATA_VLAN_NAME} = $_[0];
170 }
171  
172 0 return $this->{DATA}{+VMPS_DATA_VLAN_NAME};
173 }
174  
175 =head2 domain()
176  
177 Returns the VTP domain from the request.
178  
179 =cut
180  
181 0 ▶sub domain { shift->{DATA}{+VMPS_DATA_VTP_DOMAIN} }
Subroutine domain: not called CC=1 SCAR=6.9
Pod documented
182  
183 =head2 packet()
184  
185 Returns the "first packet" that may be encoded in the request.
186  
187 =cut
188  
189 0 ▶sub packet { shift->{DATA}{+VMPS_DATA_PACKET} }
Subroutine packet: not called CC=1 SCAR=6.9
Pod documented
190  
191 =head2 mac_addr()
192  
193 Returns a L<Net::MAC> object with the MAC address from the request. This
194 searches the dynamic mac address (0xc06), then the static mac address
195 (0xc08). Returns undef if none can be found.
196  
197 =cut
198  
199 0 ▶sub mac_addr {
Subroutine mac_addr: not called CC=2 SCAR=17.9
Pod documented
200 0 my ($this) = @_;
201  
202 0 ▶ my $mac = $this->_first_mac() || return undef;
Condition0%0/2
$this->_first_mac || (return undef)
l!l
00
MC/DC0%0/1
$this->_first_mac || (return undef)
$this->_first_mac
Truth table0%0/2
A
A= $this->_first_mac
Aresult
11
00
203 0 my $str = join ':', map { sprintf("%02x", $_) } unpack ('C6', $mac);
204  
205 0 return Net::MAC->new(mac => $str);
206 }
207  
208 0 ▶sub _first_mac {
Subroutine _first_mac: not called CC=4 SCAR=30.0
209 0 my $this = shift;
210  
211 0 for my $req (VMPS_DATA_MAC_DYNAMIC, VMPS_DATA_MAC_STATIC)
212 {
213 0 ▶ return $this->{DATA}{$req} if exists $this->{DATA}{$req};
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"DATA"}{$req} 0 0 0
214 }
215  
216 ## pick the MAC out of the first packet!
217 0 ▶ if (defined $this->{DATA}{+VMPS_DATA_PACKET})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"DATA"}{3077}) 0 0 0
218 {
219 0 return substr($this->{DATA}{+VMPS_DATA_PACKET}, 6, 6);
220 }
221  
222 0 return undef;
223 }
224  
225 #################################################################
226 ## craft a response packet
227  
228 =head1 GENERATING REPLIES
229  
230 =head2 reply()
231  
232 Create a request object with the specified error code and vlan. Vlan may
233 be omitted for error codes other than VLAN_ERROR_NONE.
234  
235 my $reply = $request->reply($err_code, $vlan);
236  
237 =cut
238  
239 0 ▶sub reply {
Subroutine reply: not called CC=3 SCAR=24.8
Pod documented
240 0 my ($req, $err, $vlan) = @_;
241 0 my $rep = dclone($req);
242  
243 0 $req->{TYPE}++; # reply packet types are req type + 1
244 0 ▶ $req->{ERR} = $err || 0;
Condition0%0/2
$err || 0
l!l
00
MC/DC0%0/1
$err || 0
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
245 0 ▶ $req->{DATA}{+VMPS_DATA_VLAN_NAME} = $vlan || '';
Condition0%0/2
$vlan || ''
l!l
00
MC/DC0%0/1
$vlan || ''
$vlan
Truth table0%0/2
A
A= $vlan
Aresult
11
00
246  
247 0 return $req;
248 }
249  
250 ###################################
251  
252 =head1 GENERATING REPLIES MADE EASY
253  
254 =head2 reject
255  
256 Create a response object that rejects the request.
257  
258 my $reply = $request->reject();
259  
260 =cut
261  
262 0 ▶sub reject { shift->reply(VMPS_ERROR_ACCESS_DENIED) }
Subroutine reject: not called CC=1 SCAR=6.9
Pod documented
263  
264 ###################################
265  
266 =head2 accept
267  
268 Accept the request, assign the user to the specified VLAN name.
269  
270 my $reply = $request->accept($vlan);
271  
272 =cut
273  
274 0 ▶sub accept { shift->reply(VMPS_ERROR_NONE, shift) }
Subroutine accept: not called CC=1 SCAR=6.9
Pod documented
275  
276 ###################################
277  
278 =head2 shutdown
279  
280 Reject the request; instruct the switch to shutdown the port.
281  
282 my $reply = $request->shutdown();
283  
284 =cut
285  
286 0 ▶sub shutdown { shift->reply(VMPS_ERROR_SHUTDOWN) }
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod documented
287  
288 ###################################
289  
290 =head2 wrong_domain
291  
292 Reject the request because it is from the wrong domain.
293  
294 my $reply = $request->wrong_domain();
295  
296 =cut
297  
298 0 ▶sub wrong_domain { shift->reply(VMPS_ERROR_WRONG_DOMAIN) }
Subroutine wrong_domain: not called CC=1 SCAR=6.9
Pod documented
299  
300 #################################################################
301  
302 2 1;
↑ Summary Server.pm »