blib/lib/Net/Frame/Simple.pm

filter: Statementstmt 18.7% 41 / 219 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 33.3% 10 / 30 Podpod 100% 13 / 13 totaltotal 17.4% 64 / 367 SCAR 76.9
CC
57
Cov
13%
unpack
110.0
_recv
110.0
_getPadding
90.0
CRAP 2189.2
↑ Summary Build »
1 #
2 # $Id: Simple.pm,v 6683a807d2d6 2016/10/05 16:54:08 gomor $
3 #
4 package Net::Frame::Simple;
5 2 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '1.07';
8  
9 2 ▶use Class::Gomor::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 our @ISA = qw(Class::Gomor::Array Exporter);
12 2 our @EXPORT_OK = qw(
13 $NoComputeLengths
14 $NoComputeChecksums
15 );
16 2 our @AS = qw(
17 raw
18 reply
19 timestamp
20 firstLayer
21 padding
22 ref
23 truncated
24 _canMatchLayer
25 _getKey
26 _getKeyReverse
27 );
28 2 our @AA = qw(
29 layers
30 );
31 2 __PACKAGE__->cgBuildIndices;
32 2 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
33 2 __PACKAGE__->cgBuildAccessorsArray (\@AA);
34  
35 2 ▶no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use Net::Frame::Layer qw(:consts);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 ▶use Net::Frame::Layer::UDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use Net::Frame::Layer::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 2 our $NoComputeLengths = 0;
45 2 our $NoComputeChecksums = 0;
46  
47 sub _gettimeofday {
48 0 ▶ my ($sec, $usec) = gettimeofday();
Subroutine _gettimeofday: not called CC=1 SCAR=6.9
49 0 sprintf("%d.%06d", $sec, $usec);
50 }
51  
52 sub new {
53 0 ▶ my $self = shift->SUPER::new(
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
54 timestamp => _gettimeofday(),
55 firstLayer => NF_LAYER_UNKNOWN,
56 truncated => 0,
57 layers => [],
58 @_,
59 );
60  
61 0 ▶ $self->[$__raw] ? $self->unpack : $self->pack;
Branch: 1 branch
BranchTrueFalseTotal
$self->[$__raw] ? : 0 0 0
62 0 $self;
63 }
64  
65 sub newFromDump {
66 0 ▶ my $self = shift;
Subroutine newFromDump: not called CC=1 SCAR=6.9
Pod documented
67 0 my ($h) = @_;
68 $self->new(
69 timestamp => $h->{timestamp},
70 firstLayer => $h->{firstLayer},
71 raw => $h->{raw},
72 0 );
73 }
74  
75 # If there are multiple layers of the same type, the upper will be kept
76 sub _setRef {
77 0 ▶ my $self = shift;
Subroutine _setRef: not called CC=1 SCAR=6.9
78 0 my ($l) = @_;
79 0 $self->[$__ref]->{$l->layer} = $l;
80 }
81  
82 sub unpack {
83 0 ▶ my $self = shift;
Subroutine unpack: not called CC=10 SCAR=47.0
Pod documented
84  
85 0 my $encapsulate = $self->[$__firstLayer];
86  
87 0 ▶ if ($encapsulate eq NF_LAYER_UNKNOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encapsulate eq 1) 0 0 0
88 0 print("Unable to unpack frame from this layer type.\n");
89 0 return undef;
90 }
91  
92 0 my @layers;
93 0 my $n = 0;
94 0 my $raw = $self->[$__raw];
95 0 my $rawLength = length($raw);
96 0 my $oRaw = $raw;
97 0 my $prevLayer;
98 # No more than a thousand nested layers, maybe should be a parameter
99 0 for (1..1000) {
100 0 ▶ last unless $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw 0 0 0
101  
102 0 $encapsulate =~ s/[^-:\w]//g; # Fix potential code injection
103 0 my $layer = 'Net::Frame::Layer::'.$encapsulate;
104 0 eval "require $layer";
105 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
106 0 print("*** $layer module not found.\n".
107 "*** Either install it (if avail), or implement it.\n".
108 "*** You can also send the pcap file to perl\@gomor.org.\n");
109 0 ▶ if ($prevLayer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevLayer) 0 0 0
110 0 $prevLayer->nextLayer(NF_LAYER_NOT_AVAILABLE);
111 }
112 0 last;
113 }
114 0 ▶ my $l = $layer->new(raw => $raw)->unpack
Branch: 1 branch
BranchTrueFalseTotal
my $l = $layer->new("raw", $raw)->unpack or last 0 0 0
115 or last;
116  
117 0 $encapsulate = $l->encapsulate;
118 0 $raw = $l->payload;
119  
120 0 push @layers, $l;
121 # If there are multiple layers of the same type, the upper will be kept
122 0 $self->_setRef($l);
123  
124 0 ▶ last unless $encapsulate;
Branch: 1 branch
BranchTrueFalseTotal
unless $encapsulate 0 0 0
125  
126 0 ▶ if ($encapsulate eq NF_LAYER_UNKNOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encapsulate eq 1) 0 0 0
127 0 print("Unable to unpack next layer, not yet implemented in layer: ".
128 0 "$n:@{[$l->layer]}\n");
129 0 last;
130 }
131  
132 0 $prevLayer = $l;
133 0 $oRaw = $raw;
134 }
135  
136 0 ▶ if (@layers > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@layers > 0) 0 0 0
137 0 $self->[$__layers] = \@layers;
138 0 $self->_getPadding($rawLength);
139 0 $self->_searchCanGetKeyLayer;
140 0 $self->_searchCanGetKeyReverseLayer;
141 0 $self->_searchCanMatchLayer;
142 0 return $self;
143 }
144  
145 0 undef;
146 }
147  
148 sub computeLengths {
149 0 ▶ my $self = shift;
Subroutine computeLengths: not called CC=2 SCAR=17.9
Pod documented
150 0 my $layers = $self->[$__layers];
151 0 for my $l (reverse @$layers) {
152 0 $l->computeLengths($layers);
153 }
154 0 return 1;
155 }
156  
157 sub computeChecksums {
158 0 ▶ my $self = shift;
Subroutine computeChecksums: not called CC=2 SCAR=17.9
Pod documented
159 0 my $layers = $self->[$__layers];
160 0 for my $l (reverse @$layers) {
161 0 $l->computeChecksums($layers);
162 }
163 0 return 1;
164 }
165  
166 sub pack {
167 0 ▶ my $self = shift;
Subroutine pack: not called CC=8 SCAR=42.8
Pod documented
168  
169 # If there are multiple layers of the same type,
170 # the upper will be kept for the reference
171 0 $self->_setRef($_) for @{$self->[$__layers]};
172  
173 0 ▶ $self->computeLengths unless $NoComputeLengths;
Branch: 1 branch
BranchTrueFalseTotal
unless $NoComputeLengths 0 0 0
174 0 ▶ $self->computeChecksums unless $NoComputeChecksums;
Branch: 1 branch
BranchTrueFalseTotal
unless $NoComputeChecksums 0 0 0
175  
176 0 my $raw = '';
177 0 my $last;
178 0 for (@{$self->[$__layers]}) {
179 0 $raw .= $_->pack;
180 0 $last = $_;
181 }
182 0 ▶ if ($last && defined($last->payload)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and defined $last->payload) 0 0 0
Condition0%0/3
$last and defined $last->payload
!ll&&!rl&&r
000
MC/DC0%0/2
$last and defined $last->payload
$last defined $last->payload
Truth table0%0/3
A and B
A= $lastB= defined $last->payload
ABresult
0X0
100
111
183 0 $raw .= $last->payload;
184 }
185  
186 0 ▶ $raw .= $self->[$__padding] if $self->[$__padding];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[$__padding] 0 0 0
187  
188 0 $self->_searchCanGetKeyLayer;
189 0 $self->_searchCanGetKeyReverseLayer;
190 0 $self->_searchCanMatchLayer;
191  
192 0 $self->[$__raw] = $raw;
193 }
194  
195 sub _getPadding {
196 0 ▶ my $self = shift;
Subroutine _getPadding: not called CC=9 SCAR=45.0
197 0 my ($rawLength) = @_;
198  
199 0 my $last = ${$self->[$__layers]}[-1];
200  
201 # Last layer has no payload, so no padding
202 0 ▶ return if (! defined($last->payload) || ! length($last->payload));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last->payload and length $last->payload 0 0 0
Condition0%0/3
defined $last->payload and length $last->payload
!ll&&!rl&&r
000
MC/DC0%0/2
defined $last->payload and length $last->payload
defined $last->payload length $last->payload
Truth table0%0/3
A and B
A= defined $last->payloadB= length $last->payload
ABresult
0X0
100
111
203  
204 # FIX: be it available or not, we need to parse payload/padding difference
205 # So, I comment these lines for now
206 #if ($last->nextLayer eq NF_LAYER_NOT_AVAILABLE) {
207 #return;
208 #}
209  
210 0 my $tLen = 0;
211 0 for my $l (@{$self->[$__layers]}) {
212 0 ▶ if ($l->layer eq 'IPv4') {
Branch: 2 branches
BranchTrueFalseTotal
if ($l->layer eq 'IPv4') { } 0 0 0
elsif ($l->layer eq 'IPv6') { } 0 0 0
213 0 $tLen += $l->length;
214 0 last;
215 }
216 elsif ($l->layer eq 'IPv6') {
217 0 $tLen += $l->getLength;
218 0 $tLen += $l->getPayloadLength;
219 0 last;
220 }
221 0 $tLen += $l->getLength;
222 }
223  
224 # No padding
225 0 ▶ return if $rawLength == $tLen;
Branch: 1 branch
BranchTrueFalseTotal
if $rawLength == $tLen 0 0 0
226  
227 0 my $pLen = 0;
228 0 my $padding;
229 0 ▶ if ($rawLength > $tLen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rawLength > $tLen) { } 0 0 0
230 0 $pLen = $rawLength - $tLen;
231 0 $padding = substr($self->[$__raw], $tLen, $pLen);
232 0 $self->[$__padding] = $padding;
233 }
234 else {
235 0 $self->[$__truncated] = 1;
236 }
237  
238 # Now, split padding between true padding and true payload
239 0 my $payloadLength = length($last->payload);
240 0 ▶ if ($payloadLength > $pLen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payloadLength > $pLen) { } 0 0 0
241 0 my $payload = substr($last->payload, 0, ($payloadLength - $pLen));
242 0 $last->payload($payload);
243 }
244 else {
245 0 $last->payload(undef);
246 }
247 }
248  
249 sub send {
250 0 ▶ my $self = shift;
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
251 0 my ($oWrite) = @_;
252 0 $oWrite->send($self->[$__raw]);
253 }
254  
255 0 ▶sub reSend { my $self = shift; $self->send(shift()) unless $self->[$__reply] }
Subroutine reSend: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[$__reply] 0 0 0
256  
257 sub _searchCanMatchLayer {
258 0 ▶ my $self = shift;
Subroutine _searchCanMatchLayer: not called CC=3 SCAR=24.8
259 0 for my $l (reverse @{$self->[$__layers]}) {
260 0 ▶ if ($l->can('match')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->can("match")) 0 0 0
261 0 $self->[$___canMatchLayer] = $l;
262 0 last;
263 }
264 }
265 0 undef;
266 }
267  
268 sub _searchCanGetKeyLayer {
269 0 ▶ my $self = shift;
Subroutine _searchCanGetKeyLayer: not called CC=3 SCAR=24.8
270 0 for my $l (reverse @{$self->[$__layers]}) {
271 0 ▶ if ($l->can('getKey')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->can("getKey")) 0 0 0
272 0 $self->[$___getKey] = $l->getKey;
273 0 last;
274 }
275 }
276 }
277  
278 sub _searchCanGetKeyReverseLayer {
279 0 ▶ my $self = shift;
Subroutine _searchCanGetKeyReverseLayer: not called CC=3 SCAR=24.8
280 0 for my $l (reverse @{$self->[$__layers]}) {
281 0 ▶ if ($l->can('getKeyReverse')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->can("getKeyReverse")) 0 0 0
282 0 $self->[$___getKeyReverse] = $l->getKeyReverse;
283 0 last;
284 }
285 }
286 }
287  
288 sub _recv {
289 0 ▶ my $self = shift;
Subroutine _recv: not called CC=10 SCAR=47.0
290 0 my ($oDump) = @_;
291  
292 0 my $layer = $self->[$___canMatchLayer];
293  
294 0 for my $this ($oDump->getFramesFor($self)) {
295 0 ▶ next unless $this->[$__timestamp] gt $self->[$__timestamp];
Branch: 1 branch
BranchTrueFalseTotal
unless $this->[$__timestamp] gt $self->[$__timestamp] 0 0 0
296  
297 # We must put ICMPv4 before, because the other will
298 # always match for UDP.
299 0 ▶ if (exists $this->[$__ref]->{ICMPv4}
Branch: 2 branches
BranchTrueFalseTotal
if (exists $this->[$__ref]{'ICMPv4'} and exists $this->[$__ref]{'UDP'} || exists $this->[$__ref]{'TCP'}) { } 0 0 0
elsif (exists $this->[$__ref]{$layer->layer}) { } 0 0 0
Condition0%0/6
exists $this->[$__ref]{"ICMPv4"} and exists $this->[$__ref]{"UDP"} || exists $this->[$__ref]{"TCP"}
!ll&&!rl&&r
000
exists $this->[$__ref]{"UDP"} or exists $this->[$__ref]{"TCP"}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $this->[$__ref]{"ICMPv4"} and exists $this->[$__ref]{"UDP"} || exists $this->[$__ref]{"TCP"}
exists $this->[$__ref]{"ICMPv4"} exists $this->[$__ref]{"UDP"} exists $this->[$__ref]{"TCP"}
Truth table0%0/4
A and B or C
A= exists $this->[$__ref]{"ICMPv4"}B= exists $this->[$__ref]{"UDP"}C= exists $this->[$__ref]{"TCP"}
ABCresult
0XX0
1000
11X1
1011
300 && (exists $this->[$__ref]->{UDP} || exists $this->[$__ref]->{TCP})) {
301 0 ▶ if (exists $this->[$__ref]->{$layer->layer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->[$__ref]{$layer->layer}) 0 0 0
302 return $this
303 0 ▶ if $this->[$__ref]->{$layer->layer}->getKey eq $layer->getKey;
Branch: 1 branch
BranchTrueFalseTotal
if $this->[$__ref]{$layer->layer}->getKey eq $layer->getKey 0 0 0
304 }
305 }
306 elsif (exists $this->[$__ref]->{$layer->layer}) {
307 0 ▶ return $this if $layer->match($this->[$__ref]->{$layer->layer});
Branch: 1 branch
BranchTrueFalseTotal
if $layer->match($this->[$__ref]{$layer->layer}) 0 0 0
308 }
309 }
310  
311 0 undef;
312 }
313  
314 sub recv {
315 0 ▶ my $self = shift;
Subroutine recv: not called CC=4 SCAR=30.0
Pod documented
316 0 my ($oDump) = @_;
317  
318 # We already have the reply
319 0 ▶ $self->[$__reply] and return $self->[$__reply];
Branch: 1 branch
BranchTrueFalseTotal
$self->[$__reply] and return $self->[$__reply] 0 0 0
320  
321 # Is there anything waiting ?
322 0 ▶ my $h = $oDump->next or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $h = $oDump->next or return undef 0 0 0
323  
324 0 my $oSimple = Net::Frame::Simple->newFromDump($h);
325 0 $oDump->store($oSimple);
326  
327 0 ▶ if (my $reply = $self->_recv($oDump)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reply = $self->_recv($oDump)) 0 0 0
328 0 $self->cgDebugPrint(1, "Reply received");
329 0 return $self->[$__reply] = $reply;
330 }
331  
332 0 undef;
333 }
334  
335 # Needed by Net::Frame::Dump
336 0 ▶sub getKey { shift->[$___getKey] || 'all' }
Subroutine getKey: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[$___getKey] or "all" 0 0 0
337 0 ▶sub getKeyReverse { shift->[$___getKeyReverse] || 'all' }
Subroutine getKeyReverse: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->[$___getKeyReverse] or "all" 0 0 0
338  
339 sub print {
340 0 ▶ my $self = shift;
Subroutine print: not called CC=5 SCAR=34.0
Pod documented
341  
342 0 my $str = '';
343 0 my $last;
344 0 for my $l (@{$self->[$__layers]}) {
345 0 $str .= $l->print."\n";
346 0 $last = $l;
347 }
348 0 $str =~ s/\n$//s;
349  
350 # Print remaining to be decoded, if any
351 0 ▶ if ($last && $last->payload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and $last->payload) 0 0 0
Condition0%0/3
$last and $last->payload
!ll&&!rl&&r
000
MC/DC0%0/2
$last and $last->payload
$last $last->payload
Truth table0%0/3
A and B
A= $lastB= $last->payload
ABresult
0X0
100
111
352 0 $str .= "\n".$last->layer.': payload:'.CORE::unpack('H*', $last->payload);
353 }
354  
355 # Print the padding, if any
356 0 ▶ if ($self->[$__padding]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[$__padding]) 0 0 0
357 0 $str .= "\n".'Padding: '.CORE::unpack('H*', $self->[$__padding]);
358 }
359  
360 0 $str;
361 }
362  
363 sub dump {
364 0 ▶ my $self = shift;
Subroutine dump: not called CC=5 SCAR=34.0
Pod documented
365  
366 0 my $last;
367 0 my $raw = '';
368 0 for my $l (@{$self->[$__layers]}) {
369 0 $raw .= $l->raw;
370 0 $last = $l;
371 }
372  
373 0 ▶ if ($last && defined($last->payload)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and defined $last->payload) 0 0 0
Condition0%0/3
$last and defined $last->payload
!ll&&!rl&&r
000
MC/DC0%0/2
$last and defined $last->payload
$last defined $last->payload
Truth table0%0/3
A and B
A= $lastB= defined $last->payload
ABresult
0X0
100
111
374 0 $raw .= $last->payload;
375 }
376  
377 0 ▶ $raw .= $self->[$__padding] if $self->[$__padding];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[$__padding] 0 0 0
378  
379 0 CORE::unpack('H*', $raw);
380 }
381  
382 2 1;
383  
384 __END__
↑ Summary Build »