PcapWriter/TCP.pm

filter: Statementstmt 80.2% 77 / 96 Branchbran 54.8% 45 / 82 Conditioncond 55.0% 11 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 8 totaltotal 63.0% 147 / 233 CC 50 SCAR 30.3
CC
50
Cov
67%
write_with_flags
31.8
close
26.1
shutdown
20.0
CRAP 20.7
↑ Summary PcapWriter/IP.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::PcapWriter::TCP;
5 7 ▶use fields qw(flow writer l2prefix pktmpl last_timestamp connected);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Net::PcapWriter::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Socket qw(AF_INET IPPROTO_TCP);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
11 2 my ($class,$writer,$src,$sport,$dst,$dport) = @_;
12 2 my $self = fields::new($class);
13 $self->{flow} = [
14 # src, dst, sport, dport, state, sn
15 # state = 0bFfSs: acked [F]in|send [f]in|acked [S]yn|send [s]yn
16 # sn gets initialized on sending SYN
17 2 [ $src,$dst,$sport,$dport,0, undef ],
18 [ $dst,$src,$dport,$sport,0, undef ],
19 ];
20 2 $self->{writer} = $writer;
21 2 $self->{last_timestamp} = undef;
22 2 $self->{l2prefix} = $self->{writer}->layer2prefix($src);
23 $self->{pktmpl} = [
24 2 ip_packet( undef, $src, $dst, IPPROTO_TCP, 16),
25 ip_packet( undef, $dst, $src, IPPROTO_TCP, 16),
26 ];
27 2 return $self;
28 }
29  
30 16 ▶sub write_with_flags {
Subroutine write_with_flags: called CC=26 SCAR=34.6
Pod undocumented
31 16 my ($self,$dir,$data,$flags,$timestamp) = @_;
32 16 ▶ $flags ||= {};
Condition100%2/2
$flags ||= {}
l!l
124
MC/DC100%1/1
$flags ||= {}
$flags
Truth table100%2/2
A
A= $flags
Aresult
11
00
33 16 my $flow = $self->{flow}[$dir];
34  
35 16 ▶ if ($flags->{syn} and ($flow->[4] & 0b0001) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{"syn"} and ($flow->[4] & 1) == 0) 4 12 16
Condition66%2/3
$flags->{"syn"} and ($flow->[4] & 1) == 0
!ll&&!rl&&r
1204
MC/DC50%1/2
$flags->{"syn"} and ($flow->[4] & 1) == 0
$flags->{"syn"} ($flow->[4] & 1) == 0
Truth table66%2/3
A and B
A= $flags->{"syn"}B= ($flow->[4] & 1) == 0
ABresult
0X0
100
111
36 4 $flow->[4] |= 0b0001;
37 4 ▶ $flow->[5] ||= rand(2**32);
Condition33%1/3
$flow->[5] ||= rand "4294967296"
l!l&&r!l&&!r
040
MC/DC0%0/2
$flow->[5] ||= rand "4294967296"
$flow->[5] rand "4294967296"
Truth table33%1/3
A ||= B
A= $flow->[5]B= rand "4294967296"
ABresult
1X1
011
000
38 }
39 16 my $sn = $flow->[5];
40  
41 16 ▶ if ($flags->{rst}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{"rst"}) 0 16 16
42 # consider closed
43 0 $flow->[4] |= 0b1100;
44 0 ▶ $self->{flow}[$dir?0:1][4] |= 0b1100;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 0 0 0
45 }
46 16 ▶ if ($flags->{fin}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{"fin"}) 4 12 16
47 4 ▶ if (($flow->[4] & 0b0100) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($flow->[4] & 4) == 0) 4 0 4
48 4 $flow->[4] |= 0b0100;
49 4 $flow->[5]++
50 }
51 }
52 16 ▶ if ($flags->{ack}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{"ack"}) 8 8 16
53 8 ▶ my $oflow = $self->{flow}[$dir?0:1];
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 4 4 8
54 8 ▶ $flow->[4] |= 0b0010 if $oflow->[4] & 0b0001; # ACK the SYN
Branch: 1 branch
BranchTrueFalseTotal
if $oflow->[4] & 1 8 0 8
55 8 ▶ $flow->[4] |= 0b1000 if $oflow->[4] & 0b0100; # ACK the FIN
Branch: 1 branch
BranchTrueFalseTotal
if $oflow->[4] & 4 4 4 8
56 }
57  
58 16 ▶ return if ! defined $data; # only update state
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 16 16
59  
60 16 ▶ my $ack = $self->{flow}[$dir?0:1][5];
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 6 10 16
61 16 ▶ $flags->{ack} = 1 if defined $ack;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ack 14 2 16
62  
63 16 my $f = 0;
64 16 ▶ $f |= 0b000100 if $flags->{rst};
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"rst"} 0 16 16
65 16 ▶ $f |= 0b001000 if $flags->{psh};
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"psh"} 0 16 16
66 16 ▶ $f |= 0b010000 if $flags->{ack};
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"ack"} 14 2 16
67 16 ▶ $f |= 0b100000 if $flags->{urg};
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"urg"} 0 16 16
68 16 ▶ $f |= 0b000001 if $flags->{fin};
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"fin"} 4 12 16
69 16 ▶ if ( $flags->{syn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{"syn"}) 4 12 16
70 4 $f |= 0b000010;
71 4 $sn = ($sn-1) % 2**32;
72 }
73  
74 16 ▶ if (defined $flags->{_seq}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flags->{"_seq"}) 0 16 16
75 0 $sn = ($sn + $flags->{_seq}) % 2**32; # seq=-1 for keep-alive
76 }
77  
78 my $tcp = pack("nnNNCCnnna*",
79 $flow->[2],$flow->[3], # sport,dport
80 $sn, # sn
81 $ack||0, # ack
82 0x50, # size of TCP header >> 4
83 $f, # flags
84 $flags->{window} || 2**15, # window
85 0, # checksum computed later
86 16 ▶ $flags->{urg}||0, # urg pointer
Condition66%4/6
$ack || 0
l!l
142
$flags->{'window'} || 32768.0
l!l
016
$flags->{'urg'} || 0
l!l
016
MC/DC100%1/1
$ack || 0
$ack
MC/DC0%0/1
$flags->{'window'} || 32768.0
$flags->{'window'}
MC/DC0%0/1
$flags->{'urg'} || 0
$flags->{'urg'}
Truth table100%2/2
A
A= $ack
Aresult
11
00
Truth table50%1/2
A
A= $flags->{'window'}
Aresult
11
00
Truth table50%1/2
A
A= $flags->{'urg'}
Aresult
11
00
87 $data # payload
88 );
89  
90 16 $flow->[5] = (
91 $flow->[5]
92 + length($data)
93 ) % 2**32;
94 16 $self->{last_timestamp} = $timestamp;
95 $self->{writer}->packet(
96 16 $self->{l2prefix} . $self->{pktmpl}[$dir]($tcp),
97 $timestamp
98 );
99 }
100  
101 4 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod undocumented
102 4 my ($self,$dir,$data,$timestamp) = @_;
103 4 ▶ _connect($self,$timestamp) if ! $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connected"} 2 2 4
104 4 write_with_flags($self,$dir,$data,undef,$timestamp);
105 }
106  
107 0 ▶sub keepalive_probe {
Subroutine keepalive_probe: not called CC=2 SCAR=17.9
Pod undocumented
108 0 my ($self,$dir,$timestamp) = @_;
109 0 ▶ die "not connected" if ! $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connected"} 0 0 0
110 0 write_with_flags($self,$dir,'',{ _seq => -1 },$timestamp);
111 }
112  
113 2 ▶sub _connect {
Subroutine _connect: called CC=8 SCAR=24.0
114 2 my ($self,$timestamp) = @_;
115 2 my $flow = $self->{flow};
116 2 ▶ goto done if ($flow->[1][4] & 0b11) == 0b11
Branch: 1 branch
BranchTrueFalseTotal
if ($flow->[1][4] & 3) == 3 and ($flow->[0][4] & 3) == 3 0 2 2
Condition33%1/3
($flow->[1][4] & 3) == 3 and ($flow->[0][4] & 3) == 3
!ll&&!rl&&r
200
MC/DC0%0/2
($flow->[1][4] & 3) == 3 and ($flow->[0][4] & 3) == 3
($flow->[1][4] & 3) == 3 ($flow->[0][4] & 3) == 3
Truth table33%1/3
A and B
A= ($flow->[1][4] & 3) == 3B= ($flow->[0][4] & 3) == 3
ABresult
0X0
100
111
117 && ($flow->[0][4] & 0b11) == 0b11;
118  
119 # client: SYN
120 2 ▶ write_with_flags($self,0,'',{ syn => 1 },$timestamp)
Branch: 1 branch
BranchTrueFalseTotal
if ($flow->[0][4] & 1) == 0 2 0 2
121 if ($flow->[0][4] & 0b01) == 0;
122  
123 # server: SYN+ACK
124 2 ▶ write_with_flags($self,1,'',{
Branch: 3 branches
BranchTrueFalseTotal
if ($flow->[1][4] & 3) != 3 2 0 2
($flow->[1][4] & 1) == 0 ? : 2 0 2
($flow->[1][4] & 2) == 0 ? : 2 0 2
125 ($flow->[1][4] & 0b01) == 0 ? ( syn => 1 ):(),
126 ($flow->[1][4] & 0b10) == 0 ? ( ack => 1 ):(),
127 },$timestamp) if ($flow->[1][4] & 0b11) != 0b11;
128  
129 # client: ACK
130 2 ▶ write_with_flags($self,0,'',{ ack => 1 },$timestamp)
Branch: 1 branch
BranchTrueFalseTotal
if ($flow->[0][4] & 2) == 0 2 0 2
131 if ($flow->[0][4] & 0b10) == 0;
132  
133 done:
134 2 $self->{connected} = 1;
135 }
136  
137 0 ▶sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod undocumented
138 0 my ($self,$timestamp) = @_;
139 0 ▶ _connect($self,$timestamp) if ! $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"connected"} 0 0 0
140 }
141  
142 0 ▶sub shutdown {
Subroutine shutdown: not called CC=4 SCAR=30.0
Pod undocumented
143 0 my ($self,$dir,$timestamp) = @_;
144 0 ▶ if (($self->{flow}[$dir][4] & 0b0100) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"flow"}[$dir][4] & 4) == 0) 0 0 0
145 0 ▶ _connect($self,$timestamp) if ! $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connected"} 0 0 0
146 0 write_with_flags($self,$dir,'',{ fin => 1 },$timestamp);
147 0 ▶ write_with_flags($self,$dir ? 0:1,'',{ ack => 1 },$timestamp);
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 0 0 0
148 }
149 }
150  
151 2 ▶sub close {
Subroutine close: called CC=11 SCAR=32.6
Pod undocumented
152 2 my ($self,$dir,$type,$timestamp) = @_;
153 2 my $flow = $self->{flow};
154  
155 2 ▶ if (!defined $type or $type eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $type or $type eq '') { } 0 2 2
elsif ($type eq 'fin') { } 2 0 2
elsif ($type eq 'rst') { } 0 0 0
Condition33%1/3
not defined $type or $type eq ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $type or $type eq ""
not defined $type $type eq ""
Truth table33%1/3
not(A) or B
A= not defined $typeB= $type eq ""
ABresult
1X1
011
000
156 # simulate close only - don't write any packets
157 0 $flow->[0][4] |= 0b1100;
158 0 $flow->[1][4] |= 0b1100;
159  
160 } elsif ($type eq 'fin') {
161 # $dir: FIN
162 2 ▶ write_with_flags($self,$dir,'',{ fin => 1 },$timestamp)
Branch: 1 branch
BranchTrueFalseTotal
if ($flow->[$dir][4] & 4) == 0 2 0 2
163 if ($flow->[$dir][4] & 0b0100) == 0;
164  
165 # $odir: FIN+ACK
166 2 ▶ my $odir = $dir?0:1;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 0 2 2
167 2 ▶ write_with_flags($self,$odir,'',{
Branch: 3 branches
BranchTrueFalseTotal
if ($flow->[$odir][4] & 12) != 12 2 0 2
($flow->[$odir][4] & 4) == 0 ? : 2 0 2
($flow->[$odir][4] & 8) == 0 ? : 2 0 2
168 ($flow->[$odir][4] & 0b0100) == 0 ? ( fin => 1 ):(),
169 ($flow->[$odir][4] & 0b1000) == 0 ? ( ack => 1 ):(),
170 },$timestamp) if ($flow->[$odir][4] & 0b1100) != 0b1100;
171  
172 # $dir: ACK
173 2 ▶ write_with_flags($self,$dir,'',{ ack => 1 },$timestamp)
Branch: 1 branch
BranchTrueFalseTotal
if ($flow->[$dir][4] & 8) == 0 2 0 2
174 if ($flow->[$dir][4] & 0b1000) == 0;
175  
176 } elsif ($type eq 'rst') {
177 # single RST and then connection is closed
178 0 write_with_flags($self,$dir,'',{ rst => 1 },$timestamp);
179  
180 } else {
181 0 die "only fin|rst|undef are allowed with close"
182 }
183 }
184  
185 0 ▶sub ack {
Subroutine ack: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my ($self,$dir,$timestamp) = @_;
187 0 write_with_flags($self,$dir,'',{ ack => 1 },$timestamp);
188 }
189  
190 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.1
191 2 my $self = shift;
192 2 ▶ $self->{writer} or return; # happens in global destruction
Branch: 1 branch
BranchTrueFalseTotal
$self->{"writer"} or return 2 0 2
193 2 &close($self,0,'fin',$self->{last_timestamp});
194 }
195  
196  
197 7 1;
198  
199  
↑ Summary PcapWriter/IP.pm »