IMP/SessionLog.pm

filter: Statementstmt 45.9% 28 / 61 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 69.2% 9 / 13 Podpod 75.0% 3 / 4 totaltotal 38.8% 40 / 103 CC 11 SCAR 29.2
CC
11
Cov
34%
validate_cfg
42.0
new_analyzer
30.0
data
6.0
CRAP 18.5
« IMP.pm ↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::IMP::SessionLog;
5 1 ▶use base 'Net::IMP::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use fields qw(fh conn);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Net::IMP; # import IMP_ constants
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::IMP::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Time::HiRes 'gettimeofday';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp 'tempfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub INTERFACE { return (
15 [
16 undef, # all types/protocols are supported
17 [
18 0 ▶ IMP_PREPASS, # nothing gets ever changed or denied
Subroutine INTERFACE: not called CC=1 SCAR=6.9
Pod undocumented
19 IMP_ACCTFIELD, # to account the session log file
20 ]
21 ]
22 )}
23  
24 sub validate_cfg {
25 0 ▶ my ($class,%args) = @_;
Subroutine validate_cfg: not called CC=6 SCAR=37.4
Pod documented
26 0 my @err;
27 0 ▶ my $fmt = delete $args{format} || 'bin';
Condition0%0/2
delete $args{'format'} || 'bin'
l!l
00
MC/DC0%0/1
delete $args{'format'} || 'bin'
delete $args{'format'}
Truth table0%0/2
A
A= delete $args{'format'}
Aresult
11
00
28 0 ▶ if ( $fmt eq 'pcap' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq 'pcap') { } 0 0 0
elsif ($fmt ne 'bin') { } 0 0 0
29 0 ▶ push @err, "cannot load Net::PcapWriter needed for format pcap: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless eval "require Net::PcapWriter" 0 0 0
30 if ! eval "require Net::PcapWriter";
31 } elsif ( $fmt ne 'bin' ) {
32 0 push @err, "format should be bin or pcap"
33 }
34  
35 0 my $dir = delete $args{dir};
36 0 ▶ push @err, "no dir given" if ! $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
37  
38 0 push @err,$class->SUPER::validate_cfg(%args);
39 0 return @err;
40 }
41  
42  
43 # create new context object
44 # - open log file
45 # - prepare initial and only results (PREPASS in both directions)
46 sub new_analyzer {
47 0 ▶ my ($factory,%args) = @_;
Subroutine new_analyzer: not called CC=5 SCAR=34.0
Pod documented
48  
49 0 my $dir = $factory->{factory_args}{dir};
50 0 ▶ my $fmt = $factory->{factory_args}{format} || 'bin';
Condition0%0/2
$factory->{'factory_args'}{'format'} || 'bin'
l!l
00
MC/DC0%0/1
$factory->{'factory_args'}{'format'} || 'bin'
$factory->{'factory_args'}{'format'}
Truth table0%0/2
A
A= $factory->{'factory_args'}{'format'}
Aresult
11
00
51  
52 0 my $meta = $args{meta};
53 my ($fh,$fname) = tempfile(
54 sprintf("%d-%s.%s-%s.%s-XXXXX", time(),
55 0 ▶ @{$meta}{qw(caddr cport saddr sport)}),
Branch: 1 branch
BranchTrueFalseTotal
() = tempfile(sprintf("%d-%s.%s-%s.%s-XXXXX", time, @{$meta;}{"caddr", "cport", "saddr", "sport"}), "SUFFIX", ".$fmt", "DIR", $dir) or croak("cannot create tmpfile: $!") 0 0 0
56 SUFFIX => ".$fmt",
57 DIR => $dir,
58 ) or croak("cannot create tmpfile: $!");
59 0 ▶ $DEBUG && debug("new context with filename $fname");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::SessionLog::DEBUG and debug("new context with filename $fname") 0 0 0
60  
61 0 binmode($fh);
62 0 $fh->autoflush(1);
63  
64 my $conn = $fmt eq 'pcap'
65 && Net::PcapWriter->new($fh)
66 0 ▶ ->tcp_conn( @{$meta}{qw(caddr cport saddr sport)} );
Condition0%0/3
$fmt eq 'pcap' && 'Net::PcapWriter'->new($fh)->tcp_conn(@{$meta;}{'caddr', 'cport', 'saddr', 'sport'})
!ll&&!rl&&r
000
MC/DC0%0/2
$fmt eq 'pcap' && 'Net::PcapWriter'->new($fh)->tcp_conn(@{$meta;}{'caddr', 'cport', 'saddr', 'sport'})
$fmt eq 'pcap' 'Net::PcapWriter'->new($fh)->tcp_conn(@{$meta;}{'caddr', 'cport', 'saddr', 'sport'})
Truth table0%0/3
A && B
A= $fmt eq 'pcap'B= 'Net::PcapWriter'->new($fh)->tcp_conn(@{$meta;}{'caddr', 'cport', 'saddr', 'sport'})
ABresult
0X0
100
111
67  
68 0 my $analyzer = $factory->SUPER::new_analyzer( %args );
69 0 $analyzer->{fh} = $fh;
70 0 $analyzer->{conn} = $conn;
71  
72 # only results for both directions + acct
73 0 $analyzer->add_results(
74 [ IMP_ACCTFIELD,'logfile',$fname ],
75 [ IMP_PREPASS,0,IMP_MAXOFFSET ],
76 [ IMP_PREPASS,1,IMP_MAXOFFSET ]
77 );
78  
79 0 return $analyzer;
80 }
81  
82 sub data {
83 0 ▶ my ($analyzer,$dir,$data) = @_;
Subroutine data: not called CC=2 SCAR=17.9
Pod documented
84 0 ▶ if ( my $c = $analyzer->{conn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $analyzer->{'conn'}) { } 0 0 0
85 # pcap format
86 0 $c->write($dir,$data,[gettimeofday()]);
87 } else {
88 # bin format
89 0 print {$analyzer->{fh}} pack("NNcN/a*",gettimeofday(),$dir,$data);
90 }
91 0 $analyzer->run_callback;
92 }
93  
94 1 1;
95  
96 __END__
« IMP.pm ↑ Summary