IMP/ProtocolPinning.pm

filter: Statementstmt 95.8% 321 / 335 Branchbran 74.2% 202 / 272 Conditioncond 67.8% 76 / 112 MC/DCmcdc 43.0% 28 / 65 Subroutinesub 95.2% 20 / 21 Podpod 85.7% 6 / 7 totaltotal 80.4% 653 / 812 CC 178 SCAR 51.5
CC
178
Cov
83%
data
247.9
_compile_cfg
22.0
_match_stream
9.7
CRAP 172.4
« IMP/Cascade.pm ↑ Summary IMP/Filter.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶no if $] >= 5.017011, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Net::IMP::ProtocolPinning;
6 3 ▶use base 'Net::IMP::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use fields (
8 3 'buf', # buffered data for each direction
9 'off_buf', # start of buf[dir] relativ to input stream
10 'off_passed', # offset up to which already passed
11 'ruleset', # active rules per dir
12 'paused', # if there is active IMP_PAUSE for dir
13 # if allow_dup already matched packets are put with key md5(seed+packet)
14 # and rule number as value into matched[dir]{...}
15 'matched', # hash of already matched packets
16 'matched_seed', # random seed for matched hash (new for each analyzer)
17 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use Net::IMP; # import IMP_ constants
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Net::IMP::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Digest::MD5 'md5';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 sub INTERFACE { return ([
27 undef, # we can stream and packets, although they behave differently
28 [
29 0 ▶ IMP_PASS, # pass data unchanged
Subroutine INTERFACE: not called CC=1 SCAR=6.9
Pod undocumented
30 IMP_DENY, # deny if rule is not matched
31 # send pause/continue if last rule of dir is reached and
32 # max_unbound is undef
33 IMP_PAUSE,
34 IMP_CONTINUE,
35 ]
36 ])}
37  
38 sub _compile_cfg {
39 60 ▶ my %args = @_;
Subroutine _compile_cfg: called CC=21 SCAR=30.9
40  
41 60 my $ignore_order = delete $args{ignore_order};
42 60 my $allow_reorder = delete $args{allow_reorder};
43 60 ▶ my $r = delete $args{rules} or die "rules need to be given\n";
Branch: 1 branch
BranchTrueFalseTotal
my $r = delete $args{"rules"} or die "rules need to be given\n" 0 60 60
44 60 my $max_unbound = delete $args{max_unbound};
45  
46 60 ▶ if ($max_unbound) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_unbound) 50 10 60
47 50 ▶ die "max_unbound should be [max0,max1]\n" if @$max_unbound>2;
Branch: 1 branch
BranchTrueFalseTotal
if @$max_unbound > 2 0 50 50
48 50 for (0,1) {
49 100 ▶ defined $max_unbound->[$_] or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $max_unbound->[$_] or next 32 68 100
50 68 ▶ die "max_unbound[$_] should be number >=0\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $max_unbound->[$_] =~ /^\d+$/ 0 68 68
51 if $max_unbound->[$_] !~m{^\d+$};
52 }
53 }
54  
55 # compile $args{rules} into list of rulesets per dir
56 # $ruleset[$dir][$i] -> [r1,r2,.] | undef
57 # - [ r1,r2.. ] - these rules can match, multiple rules at a time are only
58 # possible if reorder. The rules will be tried in the given order until
59 # one matches.
60 # - undef - no data for this dir allowed at this stage. If ignore_order
61 # there can be rules for each dir at the same time, else not.
62 # When processing data it will remove completely matched rules, but
63 # put rules which might match more (e.g. data<rxlen) at the beginning.
64 # If no more rules are open inside a ruleset it will remove the ruleset
65 # and then
66 # - if there is a next ruleset for the same dir continue with it
67 # (e.g no change after removing the done ruleset)
68 # - if there is no next ruleset (e.g. all rules done or next is undef)
69 # remove any undef set from the other dir
70 # It will remove the ruleset of no more open rules are inside.
71  
72 60 my @ruleset = ([],[]);
73 60 my $lastdir;
74 60 ▶ for (my $i=0;$i<@$r;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$r) 132 60 192
75 132 my $dir = $r->[$i]{dir};
76 132 ▶ die "rule$i.dir must be 0|1\n" unless ($dir//-1 ) ~~ [0,1];
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir // -1) ~~ [0, 1] 0 132 132
Condition50%1/2
$dir // -1
l!l
1320
MC/DC0%0/1
$dir // -1
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
77 132 ▶ die "rule$i.rxlen must be >0\n" unless ($r->[$i]{rxlen}||0)>0;
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->[$i]{"rxlen"} || 0) > 0 0 132 132
Condition50%1/2
$r->[$i]{'rxlen'} || 0
l!l
1320
MC/DC0%0/1
$r->[$i]{'rxlen'} || 0
$r->[$i]{'rxlen'}
Truth table50%1/2
A
A= $r->[$i]{'rxlen'}
Aresult
11
00
78 132 my $rx = $r->[$i]{rx};
79 132 ▶ die "rule$i.rx should be regex\n" if ref($rx) ne 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rx ne "Regexp" 0 132 132
80 132 ▶ die "rule$i.rx should not match empty string\n" if '' ~~ $rx;
Branch: 1 branch
BranchTrueFalseTotal
if "" ~~ $rx 0 132 132
81  
82 132 ▶ if ( ! $ignore_order ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ignore_order) { } 76 56 132
elsif (not @{$ruleset[$dir];}) { } 48 8 56
83 # initial rule or direction change
84 76 ▶ $lastdir //= $dir ? 0:1;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 6 26 32
Condition66%2/3
$lastdir //= $dir ? 0 : 1
l!l&&r!l&&!r
44320
MC/DC0%0/2
$lastdir //= $dir ? 0 : 1
$lastdir $dir ? 0 : 1
Truth table66%2/3
A //= B
A= $lastdirB= $dir ? 0 : 1
ABresult
1X1
011
000
85 76 ▶ if ( $lastdir != $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastdir != $dir) 58 18 76
86 58 push @{ $ruleset[$dir] }, []; # new ruleset
87 58 push @{ $ruleset[$lastdir] },undef; # no more allowd
88 58 $lastdir = $dir;
89 }
90 56 } elsif ( not @{ $ruleset[$dir] } ) {
91 # initialize when ignore_order
92 48 push @{ $ruleset[$dir] },[];
93 }
94  
95 # set ruleset to this rule
96 # if allow_reorder try to add it to existing ruleset
97 132 ▶ if ( $allow_reorder
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_reorder or not @{$ruleset[$dir][-1];}) { } 120 12 132
Condition100%3/3
$allow_reorder or not @{$ruleset[$dir][-1];}
l!l&&r!l&&!r
388212
MC/DC100%2/2
$allow_reorder or not @{$ruleset[$dir][-1];}
$allow_reorder not @{$ruleset[$dir][-1];}
Truth table100%3/3
A or not(B)
A= $allow_reorderB= not @{$ruleset[$dir][-1];}
ABresult
1X1
011
000
98 94 or ! @{ $ruleset[$dir][-1] } ) {
99 120 push @{ $ruleset[$dir][-1] },$i;
100 } else {
101 12 push @{ $ruleset[$dir] },[ $i ];
102 }
103 }
104  
105 return (
106 rules => $r,
107 ruleset => \@ruleset,
108 allow_dup => $args{allow_dup},
109 60 max_unbound => $max_unbound,
110 %args,
111 );
112 }
113  
114 sub new_factory {
115 30 ▶ my $class = shift;
Subroutine new_factory: called CC=1 SCAR=0.0
Pod documented
116 30 return $class->SUPER::new_factory( _compile_cfg(@_));
117 }
118  
119 sub validate_cfg {
120 30 ▶ my ($class,%args) = @_;
Subroutine validate_cfg: called CC=2 SCAR=7.0
Pod documented
121 30 my @err;
122 30 ▶ push @err,$@ if ! eval { my @x = _compile_cfg(%args) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { my(@x) = _compile_cfg(%args) } } 0 30 30
123 30 delete @args{qw/rules max_unbound ignore_order allow_dup allow_reorder/};
124 30 push @err,$class->SUPER::validate_cfg(%args);
125 30 return @err;
126 }
127  
128 # create new analyzer object
129 sub new_analyzer {
130 41 ▶ my ($factory,%args) = @_;
Subroutine new_analyzer: called CC=2 SCAR=6.9
Pod documented
131  
132 41 my $fargs = $factory->{factory_args};
133 my Net::IMP::ProtocolPinning $self = $factory->SUPER::new_analyzer(
134 %args,
135  
136 # buffer per direction
137 buf => [ '','' ],
138  
139 # offset for buffer per direction
140 off_buf => [0,0],
141  
142 # amount of data already passed
143 off_passed => [0,0],
144  
145 # clone ruleset because we will modify it
146 ruleset => dclone($fargs->{ruleset}),
147  
148 # hash of already matched packets (per dir) if allow_dup
149 41 ▶ matched => $fargs->{allow_dup} ? [] : undef,
Branch: 1 branch
BranchTrueFalseTotal
$fargs->{'allow_dup'} ? : 8 33 41
150 # seed for hashing matched packets, gets initialized on first use
151 matched_seed => undef,
152 );
153  
154 41 return $self;
155 }
156  
157  
158 # matches buffer against rule
159 # if match impossible returns ()
160 # if no match, but might by possible if more data are added returns (0,0)
161 # if matched and data got removed because bufsize >=rxlen returns (size,size)
162 # if matched and data are still in buffer (match may be longer) returns (size,0)
163 sub _match_stream {
164 63 ▶ my ($r,$rbuf) = @_;
Subroutine _match_stream: called CC=9 SCAR=22.7
165 63 ▶ if ( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::IMP::ProtocolPinning::DEBUG) 0 63 63
166 0 my ($pkg,undef,$line) = caller;
167 debug("try match from=%s[%d] rxlen=%d rx=%s buf=%d/'%s'",
168 0 $pkg,$line, $r->{rxlen},$r->{rx},length($$rbuf),$$rbuf);
169 }
170 63 my $lbuf = length($$rbuf);
171 63 ▶ if ($r->{rxlen} <= $lbuf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'rxlen'} <= $lbuf) { } 32 31 63
172 32 ▶ if ( substr($$rbuf,0,$r->{rxlen}) =~s{\A$r->{rx}}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$rbuf, 0, $r->{"rxlen"}) =~ s/\A$r->{"rx"}//) 30 2 32
173 30 my $lm = $lbuf - length($$rbuf);
174 30 ▶ $DEBUG && debug("final match of $lm in $r->{rxlen} bytes");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug(qq[final match of $lm in $r->{"rxlen"} bytes]) 0 30 30
175 30 return ($lm,$lm) # (matched,removed=matched)
176 }
177 2 ▶ $DEBUG && debug("final failed match in $r->{rxlen} bytes");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug(qq[final failed match in $r->{"rxlen"} bytes]) 0 2 2
178 2 return; # could never match because rxlen reached
179 } else {
180 31 ▶ if ( $$rbuf =~m{\A$r->{rx}}g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rbuf =~ /\A$r->{"rx"}/g) 19 12 31
181 # might match later again and more
182 19 my $lm = pos($$rbuf);
183 19 ▶ $DEBUG && debug("preliminary match of $lm in $lbuf bytes");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("preliminary match of $lm in $lbuf bytes") 0 19 19
184 19 return ($lm,0); # (matched,removed=0)
185 }
186 12 ▶ $DEBUG && debug("preliminary failed match in $lbuf bytes");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("preliminary failed match in $lbuf bytes") 0 12 12
187 12 return (0,0); # could match if more data
188 }
189 }
190  
191 # like _match_stream but matches rx against whole packet.
192 # result can either be final (size,size) or never ()
193 sub _match_packet {
194 30 ▶ my ($r,$rbuf) = @_;
Subroutine _match_packet: called CC=3 SCAR=11.0
195 # try to match full packet
196 30 my $len = length($$rbuf);
197 30 ▶ return if $r->{rxlen} < $len; # could not match full packet
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"rxlen"} < $len 0 30 30
198 30 ▶ return $$rbuf =~m{\A$r->{rx}\Z} ? ($len,$len) : ();
Branch: 1 branch
BranchTrueFalseTotal
$$rbuf =~ /\A$r->{'rx'}\Z/ ? : 22 8 30
199 }
200  
201 sub data {
202 114 ▶ my Net::IMP::ProtocolPinning $self = shift;
Subroutine data: called CC=135 SCAR=55.1
Pod documented
203 114 my ($dir,$data,$offset,$type) = @_;
204  
205 # buf gets removed at final reply
206 114 ▶ if ( ! $self->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"buf"}) 8 106 114
207 # we gave already the final reply
208 8 ▶ $DEBUG && debug("data[$dir] after final reply");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("data[$dir] after final reply") 0 8 8
209 8 return;
210 }
211  
212 # never did IMP_PASS into future, so no offset allowed
213 106 ▶ $offset and die "no offset allowed";
Branch: 1 branch
BranchTrueFalseTotal
$offset and die "no offset allowed" 0 106 106
214  
215 106 my $rs = $self->{ruleset}[$dir]; # [r]ule[s]et
216 106 my $rules = $self->{factory_args}{rules};
217 106 ▶ my $match = $type>0 ? \&_match_packet:\&_match_stream;
Branch: 1 branch
BranchTrueFalseTotal
$type > 0 ? : 36 70 106
218  
219 106 ▶ if ($data eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "") 7 99 106
220 # eof - remove leading rule with extendable match and then
221 # check if all rules are done
222 $DEBUG && debug("eof dir=%d rules=%s", $dir,
223 7 ▶ Data::Dumper->new([$self->{ruleset}])->Indent(0)->Terse(1)->Dump);
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("eof dir=%d rules=%s", $dir, "Data::Dumper"->new([$self->{"ruleset"}])->Indent(0)->Terse(1)->Dump) 0 7 7
224  
225 7 ▶ if ( @$rs and my $match_in_progress =
Branch: 1 branch
BranchTrueFalseTotal
if (@$rs and my $match_in_progress = $self->{"off_passed"}[$dir] - $self->{"off_buf"}[$dir]) 2 5 7
Condition66%2/3
@$rs and my $match_in_progress = $self->{"off_passed"}[$dir] - $self->{"off_buf"}[$dir]
!ll&&!rl&&r
052
MC/DC50%1/2
@$rs and my $match_in_progress = $self->{"off_passed"}[$dir] - $self->{"off_buf"}[$dir]
@$rs my $match_in_progress = $self->{"off_passed"}[$dir] - $self->{"off_buf"}[$dir]
Truth table66%2/3
A and B
A= @$rsB= my $match_in_progress = $self->{"off_passed"}[$dir] - $self->{"off_buf"}[$dir]
ABresult
0X0
100
111
226 $self->{off_passed}[$dir] - $self->{off_buf}[$dir] ) {
227 # rule done
228 2 $self->{off_buf}[$dir] = $self->{off_passed}[$dir];
229 2 $self->{buf}[$dir] = '';
230 # remove matched rule
231 # don't care for duplicates, they won't come anymore
232 2 shift(@{$rs->[0]});
233 # remove ruleset if empty
234 2 ▶ if (! @{$rs->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$rs->[0];}) 2 0 2
235 2 shift(@$rs);
236 # switch to other dir if this dir is done for now
237 2 ▶ if ( ! @$rs || ! $rs->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rs and $rs->[0]) 2 0 2
Condition33%1/3
@$rs and $rs->[0]
!ll&&!rl&&r
020
MC/DC0%0/2
@$rs and $rs->[0]
@$rs $rs->[0]
Truth table33%1/3
A and B
A= @$rsB= $rs->[0]
ABresult
0X0
100
111
238 2 ▶ my $ors = $self->{ruleset}[$dir?0:1];
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 0 2 2
239 2 ▶ shift @$ors if @$ors && ! $ors->[0];
Branch: 1 branch
BranchTrueFalseTotal
if @$ors and not $ors->[0] 2 0 2
Condition33%1/3
@$ors and not $ors->[0]
!ll&&!rl&&r
002
MC/DC0%0/2
@$ors and not $ors->[0]
@$ors not $ors->[0]
Truth table33%1/3
A and not(B)
A= @$orsB= not $ors->[0]
ABresult
0X0
100
111
240 }
241 2 ▶ goto CHECK_DONE if ! @$rs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rs 0 2 2
242 }
243 }
244 # still unmatched rules but we have eof, thus no more rules
245 # can match on this dir
246 7 ▶ if ( my ($r) = grep { $_ } @$rs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($r) = grep({$_;} @$rs)) { } 5 2 7
247 5 $self->{buf} = undef;
248 5 $self->run_callback([
249 IMP_DENY,
250 $dir,
251 5 "eof on $dir but unmatched rule#@{$r}"
252 ]);
253 } else {
254 # no more rules on eof side
255 # as long as further rules on other side gets matched everything
256 # is fine
257 }
258 7 return;
259 }
260  
261 # collect maximal offset to pass, will pass in PASS_AND_RETURN
262 99 my $pass_until;
263  
264 NEXT_RULE:
265 $DEBUG && debug("next rule dir=%d rules=%s |data=%d/'%s'",
266 115 ▶ $dir,Data::Dumper->new([$self->{ruleset}])->Indent(0)->Terse(1)->Dump,
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("next rule dir=%d rules=%s |data=%d/'%s'", $dir, "Data::Dumper"->new([$self->{"ruleset"}])->Indent(0)->Terse(1)->Dump, length $data, substr($data, 0, 100)) 0 115 115
267 length($data),substr($data,0,100));
268  
269 115 ▶ if ( ! @$rs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rs) 15 100 115
270 # no (more) rules for $dir, accumulate data until all rules for other
271 # direction are completed
272 15 ▶ $self->{buf}[$dir] eq '' or die "buffer should be empty";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"buf"}[$dir] eq "" or die "buffer should be empty" 0 15 15
273  
274 # check if other side has matched already with last rule
275 15 ▶ my $odir = $dir ? 0:1;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 1 14 15
276 15 my $ors = $self->{ruleset}[$odir];
277 15 ▶ if ( @$ors == 1 and @{$ors->[0]} == 1
Branch: 1 branch
BranchTrueFalseTotal
if (@$ors == 1 and @{$ors->[0];} == 1 and $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir] > 0) 1 14 15
Condition83%5/6
@$ors == 1 and @{$ors->[0];} == 1 and $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir] > 0
!ll&&!rl&&r
1131
@$ors == 1 and @{$ors->[0];} == 1
!ll&&!rl&&r
0114
MC/DC66%2/3
@$ors == 1 and @{$ors->[0];} == 1 and $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir] > 0
@$ors == 1 @{$ors->[0];} == 1 $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir] > 0
Truth table75%3/4
A and B and C
A= @$ors == 1B= @{$ors->[0];} == 1C= $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir] > 0
ABCresult
0XX0
10X0
1100
1111
278 and $self->{off_passed}[$odir] - $self->{off_buf}[$odir] >0 ) {
279 1 shift(@$ors);
280 1 goto CHECK_DONE;
281 }
282  
283 14 $self->{off_buf}[$dir] += length($data);
284  
285 14 my $max_unbound = $self->{factory_args}{max_unbound};
286 14 ▶ $max_unbound = $max_unbound && $max_unbound->[$dir];
Condition100%3/3
$max_unbound && $max_unbound->[$dir]
!ll&&!rl&&r
284
MC/DC100%2/2
$max_unbound && $max_unbound->[$dir]
$max_unbound $max_unbound->[$dir]
Truth table100%3/3
A && B
A= $max_unboundB= $max_unbound->[$dir]
ABresult
0X0
100
111
287 14 ▶ if ( ! defined $max_unbound ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max_unbound) 7 7 14
288 7 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("buffer data for dir $dir because buffering not bound") 0 7 7
289 "buffer data for dir $dir because buffering not bound");
290 7 ▶ if ( ! $self->{paused}[$dir] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"paused"}[$dir]) 5 2 7
291 # ask data provider to stop sending data
292 5 $self->{paused}[$dir] = 1;
293 5 $self->run_callback([ IMP_PAUSE, $dir ]);
294 }
295 # if pass_until>0 we had something to pass
296 7 goto PASS_AND_RETURN;
297 }
298  
299 7 my $unbound = $self->{off_buf}[$dir] - $self->{off_passed}[$dir];
300 $DEBUG && debug("dir=%d off=%d passed=%d -> unbound=%d",
301 7 ▶ $dir,$self->{off_buf}[$dir],$self->{off_passed}[$dir],$unbound);
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("dir=%d off=%d passed=%d -> unbound=%d", $dir, $self->{"off_buf"}[$dir], $self->{"off_passed"}[$dir], $unbound) 0 7 7
302 7 ▶ if ( $unbound <= $max_unbound ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unbound <= $max_unbound) 4 3 7
303 4 ▶ $DEBUG && debug("buffer data for dir $dir because ".
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("buffer data for dir $dir because " . "unbound($unbound)<=max_unbound($max_unbound)") 0 4 4
304 "unbound($unbound)<=max_unbound($max_unbound)");
305 4 goto PASS_AND_RETURN;
306 }
307  
308 3 $self->{buf} = undef;
309 3 $self->run_callback([
310 IMP_DENY,
311 $dir,
312 "unbound buffer size=$unbound > max_unbound($max_unbound)"
313 ]);
314 3 return;
315 }
316  
317 # append new data to buf, for packet data we work directly with $data
318 100 ▶ unless ( $type > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type > 0) 70 30 100
319 70 $self->{buf}[$dir] .= $data;
320 70 $data = '';
321 }
322  
323 100 my $crs = $rs->[0]; # crs - [c]urrent [r]ule[s]et
324 100 ▶ if ( ! $crs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($crs) 9 91 100
325 # data from $dir are not allowed at this stage
326  
327 # finish a preliminary match on the other side and then try again
328 9 ▶ my $odir = $dir ? 0:1;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 6 3 9
329 9 my $ors = $self->{ruleset}[$odir];
330 9 ▶ if ( @$ors and $ors->[0] and my $omatch_in_progress
Branch: 1 branch
BranchTrueFalseTotal
if (@$ors and $ors->[0] and my $omatch_in_progress = $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir]) 5 4 9
Condition50%3/6
@$ors and $ors->[0] and my $omatch_in_progress = $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir]
!ll&&!rl&&r
045
@$ors and $ors->[0]
!ll&&!rl&&r
009
MC/DC33%1/3
@$ors and $ors->[0] and my $omatch_in_progress = $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir]
@$ors $ors->[0] my $omatch_in_progress = $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir]
Truth table50%2/4
A and B and C
A= @$orsB= $ors->[0]C= my $omatch_in_progress = $self->{"off_passed"}[$odir] - $self->{"off_buf"}[$odir]
ABCresult
0XX0
10X0
1100
1111
331 = $self->{off_passed}[$odir] - $self->{off_buf}[$odir] ) {
332 5 ▶ $DEBUG && debug("finish preliminary match on $odir");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("finish preliminary match on $odir") 0 5 5
333 5 $self->{off_buf}[$odir] = $self->{off_passed}[$odir];
334 5 substr($self->{buf}[$odir],0,$omatch_in_progress,'');
335 5 shift(@{$ors->[0]});
336 5 ▶ if ( ! @{$ors->[0]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$ors->[0];}) 5 0 5
337 5 shift(@$ors); # ruleset done
338 5 ▶ shift(@$rs) if ! @$ors or ! $ors->[0]; # switch dir
Branch: 1 branch
BranchTrueFalseTotal
unless @$ors and $ors->[0] 5 0 5
Condition33%1/3
@$ors and $ors->[0]
!ll&&!rl&&r
050
MC/DC0%0/2
@$ors and $ors->[0]
@$ors $ors->[0]
Truth table33%1/3
A and B
A= @$orsB= $ors->[0]
ABresult
0X0
100
111
339 5 ▶ goto CHECK_DONE if ! @$ors && ! @$rs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ors or @$rs 0 5 5
Condition33%1/3
@$ors or @$rs
l!l&&r!l&&!r
500
MC/DC0%0/2
@$ors or @$rs
@$ors @$rs
Truth table33%1/3
A or B
A= @$orsB= @$rs
ABresult
1X1
011
000
340 5 goto NEXT_RULE; # and try again
341 }
342 }
343  
344 # ignore if it is a duplicate packet
345 # duplicate checking is only done for packet types
346 4 ▶ if ( $type>0 and $self->{matched} and $self->{buf}[$dir] eq ''
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0 and $self->{"matched"} and $self->{"buf"}[$dir] eq "" and my $matched = $self->{"matched"}[$dir]) 0 4 4
Condition44%4/9
$type > 0 and $self->{"matched"} and $self->{"buf"}[$dir] eq "" and my $matched = $self->{"matched"}[$dir]
!ll&&!rl&&r
400
$type > 0 and $self->{"matched"} and $self->{"buf"}[$dir] eq ""
!ll&&!rl&&r
400
$type > 0 and $self->{"matched"}
!ll&&!rl&&r
220
MC/DC0%0/4
$type > 0 and $self->{"matched"} and $self->{"buf"}[$dir] eq "" and my $matched = $self->{"matched"}[$dir]
$type > 0 $self->{"matched"} $self->{"buf"}[$dir] eq "" my $matched = $self->{"matched"}[$dir]
Truth table40%2/5
A and B and C and D
A= $type > 0B= $self->{"matched"}C= $self->{"buf"}[$dir] eq ""D= my $matched = $self->{"matched"}[$dir]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
347 and my $matched = $self->{matched}[$dir] ) {
348 0 my $hpkt = md5($self->{matched_seed} . $data);
349 0 ▶ if ( defined( my $r = $matched->{$hpkt} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $r = $matched->{$hpkt})) 0 0 0
350 0 ▶ $DEBUG && debug("ignored DUP[$dir] for rule $r");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("ignored DUP[$dir] for rule $r") 0 0 0
351 $pass_until = $self->{off_passed}[$dir]
352 0 = $self->{off_buf}[$dir] += length($data);
353 0 goto PASS_AND_RETURN;
354 }
355 }
356 4 ▶ $DEBUG && debug("data[$dir] but <undef> rule -> DENY");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("data[$dir] but <undef> rule -> DENY") 0 4 4
357 4 $self->{buf} = undef;
358 $self->run_callback([ IMP_DENY, $dir, "rule#"
359 4 ▶ .( $self->{ruleset}[$dir?0:1][0][0] )." data from wrong dir $dir"
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 2 2 4
360 ]);
361 4 return;
362 }
363  
364 # if there was a last match try to extend it or to mark rule as done
365 91 ▶ if ( my $match_in_progress =
Branch: 1 branch
BranchTrueFalseTotal
if (my $match_in_progress = $self->{"off_passed"}[$dir] - $self->{"off_buf"}[$dir]) 5 86 91
366 $self->{off_passed}[$dir] - $self->{off_buf}[$dir] ) {
367 # last rule matched already
368 5 ▶ unless ( $type>0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0) { } 0 5 5
369 # try to extend match for streams
370 my ($matched,$removed) =
371 5 $match->($rules->[$crs->[0]],\$self->{buf}[$dir]);
372 5 ▶ die "expected $crs->[0] to match" if ! $matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched 0 5 5
373 5 ▶ if ( $removed ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($removed) { } 4 1 5
elsif ($matched > $match_in_progress) { } 1 0 1
374 # rule finished, probably because rxlen reached
375 4 ▶ $DEBUG && debug("completed preliminary match rule $crs->[0]");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("completed preliminary match rule $crs->[0]") 0 4 4
376 4 $self->{off_buf}[$dir] += $removed;
377 4 ▶ if ( $removed > $match_in_progress ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($removed > $match_in_progress) 3 1 4
378 $pass_until = $self->{off_passed}[$dir]
379 3 = $self->{off_buf}[$dir];
380 }
381 # no return, might match more
382  
383 } elsif ( $matched > $match_in_progress ) {
384 # keep rule open but issue extended IMP_PASS
385 1 ▶ $DEBUG && debug("extended preliminary match rule $crs->[0]");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("extended preliminary match rule $crs->[0]") 0 1 1
386 $pass_until = $self->{off_passed}[$dir]
387 1 = $self->{off_buf}[$dir]+$matched;
388 1 goto PASS_AND_RETURN; # need more data
389 } else {
390 # keep rule open waiting for more data
391 0 ▶ $DEBUG && debug("still preliminary(?) match rule $crs->[0]");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("still preliminary(?) match rule $crs->[0]") 0 0 0
392 0 goto PASS_AND_RETURN; # need more data
393 }
394  
395 } else {
396 # stream followed by packet, so rule cannot be extended
397 # remove from buf until end of last match
398 0 ▶ $DEBUG && debug("finished match rule $crs->[0] on packet $type");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("finished match rule $crs->[0] on packet $type") 0 0 0
399 0 substr($self->{buf}[$dir],0,$match_in_progress,'');
400 0 $self->{off_buf}[$dir] = $self->{off_passed}[$dir];
401 }
402  
403 # match of previously matching rule done
404 # remove it and continue with next rule if there are more data
405 4 shift(@$crs);
406 4 ▶ if (! @$crs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$crs) 4 0 4
407 4 shift(@$rs);
408 # switch to other dir if this dir is done for now
409 4 ▶ if ( ! @$rs || ! $rs->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rs and $rs->[0]) 2 2 4
Condition66%2/3
@$rs and $rs->[0]
!ll&&!rl&&r
202
MC/DC50%1/2
@$rs and $rs->[0]
@$rs $rs->[0]
Truth table66%2/3
A and B
A= @$rsB= $rs->[0]
ABresult
0X0
100
111
410 2 ▶ my $ors = $self->{ruleset}[$dir ? 0:1];
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 0 2 2
411 2 ▶ shift @$ors if @$ors && ! $ors->[0];
Branch: 1 branch
BranchTrueFalseTotal
if @$ors and not $ors->[0] 0 2 2
Condition33%1/3
@$ors and not $ors->[0]
!ll&&!rl&&r
020
MC/DC0%0/2
@$ors and not $ors->[0]
@$ors not $ors->[0]
Truth table33%1/3
A and not(B)
A= @$orsB= not $ors->[0]
ABresult
0X0
100
111
412 2 ▶ goto CHECK_DONE if ! @$ors && ! @$rs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ors or @$rs 0 2 2
Condition33%1/3
@$ors or @$rs
l!l&&r!l&&!r
200
MC/DC0%0/2
@$ors or @$rs
@$ors @$rs
Truth table33%1/3
A or B
A= @$orsB= @$rs
ABresult
1X1
011
000
413 }
414 }
415 4 ▶ if ( $type>0 or $self->{buf}[$dir] ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0 or $self->{"buf"}[$dir] ne "") 3 1 4
Condition66%2/3
$type > 0 or $self->{"buf"}[$dir] ne ""
l!l&&r!l&&!r
031
MC/DC50%1/2
$type > 0 or $self->{"buf"}[$dir] ne ""
$type > 0 $self->{"buf"}[$dir] ne ""
Truth table66%2/3
A or B
A= $type > 0B= $self->{"buf"}[$dir] ne ""
ABresult
1X1
011
000
416 # unmatched data exist in data/buf
417 3 ▶ if ( ! @$rs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rs) 1 2 3
418 # all rules done from this direction, put back all
419 # from buf to $data before calling NEXT_RULE
420 1 $data = $self->{buf}[$dir];
421 1 $self->{buf}[$dir] = '';
422 }
423 3 goto NEXT_RULE;
424 }
425 1 goto PASS_AND_RETURN; # wait for more data
426 }
427  
428 # check against current set
429 86 ▶ if ( $type>0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type > 0) { } 28 58 86
430 # packet data
431 28 ▶ if ( $self->{buf}[$dir] ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buf"}[$dir] ne "") 0 28 28
432 0 $self->run_callback([
433 IMP_DENY,
434 $dir,
435 "packet data after unmatched streaming data"
436 ]);
437 }
438 28 ▶ for( my $i=0;$i<@$crs;$i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$crs) 30 6 36
439 30 ▶ if ( my ($len) = $match->($rules->[$crs->[$i]],\$data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($len) = &$match($$rules[$crs->[$i]], \$data)) 22 8 30
440 # match
441 $pass_until = $self->{off_passed}[$dir] =
442 22 $self->{off_buf}[$dir] += $len;
443 22 ▶ if ( $self->{matched} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"matched"}) 9 13 22
444 # preserve hash of matched packet so that duplicates are
445 # detected later
446 $self->{matched}[$dir]{ md5(
447 9 ▶ ( $self->{matched_seed} //= pack("N",rand(2**32)) ).
Condition66%2/3
$self->{"matched_seed"} //= pack("N", rand "4294967296")
l!l&&r!l&&!r
630
MC/DC0%0/2
$self->{"matched_seed"} //= pack("N", rand "4294967296")
$self->{"matched_seed"} pack("N", rand "4294967296")
Truth table66%2/3
A //= B
A= $self->{"matched_seed"}B= pack("N", rand "4294967296")
ABresult
1X1
011
000
448 $data
449 )} = $crs->[$i]
450 }
451  
452 22 ▶ if (@$crs>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$crs > 1) { } 4 18 22
453 # remove rule, keep rest in ruleset
454 4 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("full match rule $crs->[$i] - remove from ruleset") 0 4 4
455 "full match rule $crs->[$i] - remove from ruleset");
456 4 splice(@$crs,$i,1);
457 } else {
458 # remove ruleset with last rule in it
459 18 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("full match rule $crs->[$i] - remove ruleset") 0 18 18
460 "full match rule $crs->[$i] - remove ruleset");
461 18 shift(@$rs);
462 # switch to other dir if this dir is done for now
463 18 ▶ if ( ! @$rs || ! $rs->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rs and $rs->[0]) 14 4 18
Condition100%3/3
@$rs and $rs->[0]
!ll&&!rl&&r
1044
MC/DC100%2/2
@$rs and $rs->[0]
@$rs $rs->[0]
Truth table100%3/3
A and B
A= @$rsB= $rs->[0]
ABresult
0X0
100
111
464 14 ▶ my $ors = $self->{ruleset}[$dir ? 0:1];
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 5 9 14
465 14 ▶ shift @$ors if @$ors && ! $ors->[0];
Branch: 1 branch
BranchTrueFalseTotal
if @$ors and not $ors->[0] 10 4 14
Condition100%3/3
@$ors and not $ors->[0]
!ll&&!rl&&r
3110
MC/DC100%2/2
@$ors and not $ors->[0]
@$ors not $ors->[0]
Truth table100%3/3
A and not(B)
A= @$orsB= not $ors->[0]
ABresult
0X0
100
111
466 }
467 }
468  
469 # pass data
470 22 ▶ goto CHECK_DONE if ! @$rs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rs 10 12 22
471 12 goto PASS_AND_RETURN; # wait for more data
472 }
473 }
474  
475 # no rule from ruleset matched, check for duplicates
476 6 ▶ if ( $self->{matched} and my $dup = $self->{matched}[$dir] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"matched"} and my $dup = $self->{"matched"}[$dir]) 5 1 6
Condition66%2/3
$self->{"matched"} and my $dup = $self->{"matched"}[$dir]
!ll&&!rl&&r
105
MC/DC50%1/2
$self->{"matched"} and my $dup = $self->{"matched"}[$dir]
$self->{"matched"} my $dup = $self->{"matched"}[$dir]
Truth table66%2/3
A and B
A= $self->{"matched"}B= my $dup = $self->{"matched"}[$dir]
ABresult
0X0
100
111
477 5 my $r = $dup->{ md5($self->{matched_seed} . $data ) };
478 5 ▶ if ( defined $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) 5 0 5
479 # matched again - pass data
480 $pass_until = $self->{off_passed}[$dir]
481 5 = $self->{off_buf}[$dir] += length($data);
482 5 ▶ $DEBUG && debug("ignore DUP[$dir] for rule $r");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("ignore DUP[$dir] for rule $r") 0 5 5
483 5 goto PASS_AND_RETURN; # wait for more data
484 }
485 }
486  
487 # no rule and no duplicates matched, must be bad data
488 1 ▶ $DEBUG && debug("no matching rule for ${type}[$dir] - deny");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("no matching rule for ${type}[$dir] - deny") 0 1 1
489 1 $self->{buf} = undef;
490 1 $self->run_callback([
491 IMP_DENY,
492 $dir,
493 "rule#@$crs did not match"
494 ]);
495 1 return;
496  
497 } else {
498 # streaming data
499 58 my $temp_fail;
500 my $final_match;
501 58 ▶ for( my $i=0;$i<@$crs;$i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$crs) 58 14 72
502 my ($len,$removed)
503 58 = $match->($rules->[$crs->[$i]],\$self->{buf}[$dir]);
504 58 ▶ if ( ! defined $len ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $len) { } 2 56 58
elsif (not $len) { } 12 44 56
505 # will never match against rule
506 2 next;
507 } elsif ( ! $len ) {
508 # note that it might match if buf gets longer but check other
509 # rules in ruleset if they match better
510 12 $temp_fail = 1;
511 12 next;
512 }
513  
514 44 ▶ if ( ! $removed and @$crs == 1 and @$rs == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $removed and @$crs == 1 and @$rs == 1) 9 35 44
Condition83%5/6
not $removed and @$crs == 1 and @$rs == 1
!ll&&!rl&&r
2699
not $removed and @$crs == 1
!ll&&!rl&&r
26018
MC/DC66%2/3
not $removed and @$crs == 1 and @$rs == 1
not $removed @$crs == 1 @$rs == 1
Truth table75%3/4
not(A) and B and C
A= not $removedB= @$crs == 1C= @$rs == 1
ABCresult
0XX0
10X0
1100
1111
515 # last rule for dir - no need to extend preliminary matches
516 # as long as max_unbound is not restrictive
517 9 my $ma = $self->{factory_args}{max_unbound};
518 9 ▶ if ( ! defined( $ma && $ma->[$dir] )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($ma && $ma->[$dir])) 2 7 9
Condition66%2/3
$ma && $ma->[$dir]
!ll&&!rl&&r
081
MC/DC50%1/2
$ma && $ma->[$dir]
$ma $ma->[$dir]
Truth table66%2/3
A && B
A= $maB= $ma->[$dir]
ABresult
0X0
100
111
519 2 $removed = $len;
520 2 substr($self->{buf}[$dir],0,$removed,'');
521 }
522 }
523  
524 # rule matched
525 44 ▶ if ( ! $removed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $removed) { } 16 28 44
526 # match might not be final, wait for more data but put rule
527 # at the beginning of ruleset if it's not already there
528 16 ▶ unshift @$crs,splice(@$crs,$i,1) if $i>0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 16 16
529  
530 # advance off_passed, but keep off_buf
531 $pass_until = $self->{off_passed}[$dir]
532 16 = $self->{off_buf}[$dir] + $len;
533  
534 # if this is was the last completely open rule we don't need
535 # to check if the matched could be extended
536 16 ▶ if (@$crs == 1 and @$rs == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$crs == 1 and @$rs == 1) 7 9 16
Condition66%2/3
@$crs == 1 and @$rs == 1
!ll&&!rl&&r
097
MC/DC50%1/2
@$crs == 1 and @$rs == 1
@$crs == 1 @$rs == 1
Truth table66%2/3
A and B
A= @$crs == 1B= @$rs == 1
ABresult
0X0
100
111
537 # last rule on this side
538 7 ▶ my $ors = $self->{ruleset}[$dir?0:1];
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 2 5 7
539 7 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not @$ors or @$ors == 1 and not $ors->[0] or @$ors == 1 and @{$ors->[0];} == 1 and $self->{"off_passed"}[$dir ? 0 : 1] - $self->{"off_buf"}[$dir ? 0 : 1] > 0) 3 4 7
Condition80%12/15
not @$ors or @$ors == 1 and not $ors->[0] or @$ors == 1 and @{$ors->[0];} == 1 and $self->{"off_passed"}[$dir ? 0 : 1] - $self->{"off_buf"}[$dir ? 0 : 1] > 0
l!l&&r!l&&!r
214
not @$ors or @$ors == 1 and not $ors->[0]
l!l&&r!l&&!r
025
@$ors == 1 and not $ors->[0]
!ll&&!rl&&r
052
@$ors == 1 and @{$ors->[0];} == 1 and $self->{"off_passed"}[$dir ? 0 : 1] - $self->{"off_buf"}[$dir ? 0 : 1] > 0
!ll&&!rl&&r
131
@$ors == 1 and @{$ors->[0];} == 1
!ll&&!rl&&r
014
MC/DC33%2/6
not @$ors or @$ors == 1 and not $ors->[0] or @$ors == 1 and @{$ors->[0];} == 1 and $self->{"off_passed"}[$dir ? 0 : 1] - $self->{"off_buf"}[$dir ? 0 : 1] > 0
not @$ors @$ors == 1 not $ors->[0] @$ors == 1 @{$ors->[0];} == 1 $self->{"off_passed"}[$dir ? 0 : 1] - $self->{"off_buf"}[$dir ? 0 : 1] > 0
Truth table30%3/10
not(A) or B and not(C) or D and E and F
A= not @$orsB= @$ors == 1C= not $ors->[0]D= @$ors == 1E= @{$ors->[0];} == 1F= $self->{"off_passed"}[$dir ? 0 : 1] - $self->{"off_buf"}[$dir ? 0 : 1] > 0
ABCDEFresult
1XXXXX1
011XXX1
00X1111
0101111
00X0XX0
00X10X0
00X1100
0100XX0
01010X0
0101100
540 # other side has no rules
541 ! @$ors
542 # other side has empty rule
543 or @$ors == 1 and ! $ors->[0]
544 # other side has single rule which matched already
545 5 ▶ or @$ors == 1 and @{ $ors->[0] } == 1 and
Branch: 2 branches
BranchTrueFalseTotal
$dir ? : 1 3 4
$dir ? : 1 3 4
546 $self->{off_passed}[$dir?0:1]
547 - $self->{off_buf}[$dir?0:1] > 0 ) {
548  
549 # we are done and there is no need to extend the match
550 3 @$ors = @$rs = ();
551 3 goto CHECK_DONE;
552 }
553 }
554  
555 } else {
556 # final match of rule
557 $pass_until = $self->{off_passed}[$dir]
558 28 = $self->{off_buf}[$dir] += $len;
559 28 ▶ if (@$crs>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$crs > 1) { } 3 25 28
560 # remove rule, keep rest in ruleset
561 3 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("full match rule $crs->[$i] - remove from ruleset") 0 3 3
562 "full match rule $crs->[$i] - remove from ruleset");
563 3 splice(@$crs,$i,1);
564 } else {
565 # remove ruleset with last rule in it
566 25 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("full match rule $crs->[$i] - remove ruleset") 0 25 25
567 "full match rule $crs->[$i] - remove ruleset");
568 25 shift(@$rs);
569 # switch to other dir if this dir is done for now
570 25 ▶ if ( ! @$rs || ! $rs->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rs and $rs->[0]) 23 2 25
Condition100%3/3
@$rs and $rs->[0]
!ll&&!rl&&r
2032
MC/DC100%2/2
@$rs and $rs->[0]
@$rs $rs->[0]
Truth table100%3/3
A and B
A= @$rsB= $rs->[0]
ABresult
0X0
100
111
571 23 ▶ my $ors = $self->{ruleset}[$dir ? 0:1];
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 14 9 23
572 23 ▶ shift @$ors if @$ors && ! $ors->[0];
Branch: 1 branch
BranchTrueFalseTotal
if @$ors and not $ors->[0] 7 16 23
Condition100%3/3
@$ors and not $ors->[0]
!ll&&!rl&&r
977
MC/DC100%2/2
@$ors and not $ors->[0]
@$ors not $ors->[0]
Truth table100%3/3
A and not(B)
A= @$orsB= not $ors->[0]
ABresult
0X0
100
111
573 23 ▶ goto CHECK_DONE if ! @$ors && ! @$rs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ors or @$rs 13 10 23
Condition66%2/3
@$ors or @$rs
l!l&&r!l&&!r
10013
MC/DC50%1/2
@$ors or @$rs
@$ors @$rs
Truth table66%2/3
A or B
A= @$orsB= @$rs
ABresult
1X1
011
000
574 }
575 }
576 15 $final_match = 1;
577 # no allow_dup for streaming
578 }
579  
580 # pass data
581 28 ▶ if ( $final_match and $self->{buf}[$dir] ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($final_match and $self->{"buf"}[$dir] ne "") 8 20 28
Condition100%3/3
$final_match and $self->{"buf"}[$dir] ne ""
!ll&&!rl&&r
1378
MC/DC100%2/2
$final_match and $self->{"buf"}[$dir] ne ""
$final_match $self->{"buf"}[$dir] ne ""
Truth table100%3/3
A and B
A= $final_matchB= $self->{"buf"}[$dir] ne ""
ABresult
0X0
100
111
582 # try to match more
583 8 $data = $self->{buf}[$dir];
584 8 $self->{buf}[$dir] = '';
585 8 goto NEXT_RULE;
586 }
587 20 ▶ goto CHECK_DONE if ! @$rs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rs 1 19 20
588 19 goto PASS_AND_RETURN;
589 }
590  
591 14 ▶ if ( ! $temp_fail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($temp_fail) 2 12 14
592 # no rule and no duplicates matched, must be bad data
593 2 ▶ $DEBUG && debug("no matching rule for ${type}[$dir] - deny");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("no matching rule for ${type}[$dir] - deny") 0 2 2
594 2 $self->{buf} = undef;
595 2 $self->run_callback([
596 IMP_DENY,
597 $dir,
598 "rule#@$crs did not match"
599 ]);
600 }
601 14 goto PASS_AND_RETURN;
602 }
603  
604 28 ▶ CHECK_DONE:
Branch: 1 branch
BranchTrueFalseTotal
if @$rs 0 28 28
605 return if @$rs; # still unmatched rules
606  
607 # pass only current data
608 28 ▶ goto PASS_AND_RETURN if @{$self->{ruleset}[ $dir ? 0:1 ] };
Branch: 2 branches
BranchTrueFalseTotal
if @{$self->{"ruleset"}[$dir ? 0 : 1];} 2 26 28
$dir ? : 18 10 28
609  
610 # rulesets for both dirs are done, pass all data
611 26 ▶ $DEBUG && debug("all rules done - pass rest");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::ProtocolPinning::DEBUG and debug("all rules done - pass rest") 0 26 26
612 26 $self->{buf} = undef;
613 26 my @rv = (
614 [ IMP_PASS,0,IMP_MAXOFFSET ],
615 [ IMP_PASS,1,IMP_MAXOFFSET ]
616 );
617 26 for(0,1) {
618 52 ▶ $self->{paused}[$_] or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"paused"}[$_] or next 47 5 52
619 5 $self->{paused}[$_] = 0;
620 5 unshift @rv, [ IMP_CONTINUE,$_ ];
621 }
622 26 $self->run_callback(@rv);
623 26 return;
624  
625 65 ▶ PASS_AND_RETURN:
Branch: 1 branch
BranchTrueFalseTotal
unless $pass_until 19 46 65
626 return if ! $pass_until;
627 46 $self->run_callback([ IMP_PASS, $dir, $pass_until ]);
628 46 return;
629 }
630  
631 # cfg2str and str2cfg are redefined because our config hash is deeper
632 # nested due to rules and max_unbound
633 sub cfg2str {
634 2 ▶ my Net::IMP::ProtocolPinning $self = shift;
Subroutine cfg2str: called CC=4 SCAR=14.0
Pod documented
635 2 my %cfg = @_;
636  
637 2 ▶ my $rules = delete $cfg{rules} or croak("no rules defined");
Branch: 1 branch
BranchTrueFalseTotal
my $rules = delete $cfg{"rules"} or croak("no rules defined") 0 2 2
638 # re-insert [[dir,rxlen,rx],... ] as dir0,rxlen0,rx0,dir1,...
639 2 ▶ for (my $i=0;$i<@$rules;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rules) 3 2 5
640 3 @cfg{ "dir$i","rxlen$i","rx$i" } = @{ $rules->[$i] }{qw( dir rxlen rx)};
641 }
642 2 ▶ if ( my $max_unbound = delete $cfg{max_unbound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $max_unbound = delete $cfg{"max_unbound"}) 2 0 2
643 # re-insert [mo0,mo1] as max_unbound0,max_unbound1
644 2 @cfg{ 'max_unbound0', 'max_unbound1' } = @$max_unbound;
645 }
646 2 return $self->SUPER::cfg2str(%cfg);
647 }
648  
649 sub str2cfg {
650 6 ▶ my Net::IMP::ProtocolPinning $self = shift;
Subroutine str2cfg: called CC=9 SCAR=22.5
Pod documented
651 6 my %cfg = $self->SUPER::str2cfg(@_);
652 6 my $rules = $cfg{rules} = [];
653 6 for ( my $i=0;1;$i++ ) {
654 15 ▶ defined( my $dir = delete $cfg{"dir$i"} ) or last;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $dir = delete $cfg{"dir$i"}) or last 6 9 15
655 9 ▶ defined( my $rxlen = delete $cfg{"rxlen$i"} )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $rxlen = delete $cfg{"rxlen$i"}) or croak("no rxlen$i defined but dir$i") 0 9 9
656 or croak("no rxlen$i defined but dir$i");
657 9 ▶ defined( my $rx = delete $cfg{"rx$i"} )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $rx = delete $cfg{"rx$i"}) or croak("no rx$i defined but dir$i") 0 9 9
658 or croak("no rx$i defined but dir$i");
659 9 ▶ $rx = eval { qr/$rx/ } or croak("invalid regex rx$i");
Branch: 1 branch
BranchTrueFalseTotal
$rx = eval { do { qr/$rx/ } } or croak("invalid regex rx$i") 0 9 9
660 9 push @$rules, { dir => $dir, rxlen => $rxlen, rx => $rx };
661  
662  
663 }
664 6 ▶ @$rules or croak("no rules defined");
Branch: 1 branch
BranchTrueFalseTotal
@$rules or croak("no rules defined") 0 6 6
665 6 my $max_unbound = $cfg{max_unbound} = [];
666 6 for (0,1) {
667 $max_unbound->[$_] = delete $cfg{"max_unbound$_"}
668 12 ▶ if exists $cfg{"max_unbound$_"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cfg{"max_unbound$_"} 12 0 12
669 }
670  
671 # sanity check
672 6 my %scfg = %cfg;
673 6 delete @scfg{qw(rules max_unbound ignore_order allow_dup allow_reorder)};
674 6 ▶ %scfg and croak("unhandled config keys: ".join(' ',sort keys %scfg));
Branch: 1 branch
BranchTrueFalseTotal
%scfg and croak('unhandled config keys: ' . join(" ", sort(keys %scfg))) 0 6 6
675  
676 6 return %cfg;
677 }
678  
679  
680 3 1;
681  
682 __END__
« IMP/Cascade.pm ↑ Summary IMP/Filter.pm »