lib/Net/SinFP3/Input/Pcap.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 130 CC 10 SCAR 32.5
CC
10
Cov
0%
run
56.0
new
20.0
give
2.0
CRAP 25.7
1 #
2 # $Id: Pcap.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $
3 #
4 package Net::SinFP3::Input::Pcap;
5 use strict;
6 use warnings;
7  
8 use base qw(Net::SinFP3::Input);
9 our @AS = qw(
10 count
11 filter
12 file
13 _files
14 _dump
15 );
16 __PACKAGE__->cgBuildIndices;
17 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
18  
19 use Net::SinFP3::Next::Frame;
20 use Net::SinFP3::Next::MultiFrame;
21  
22 sub give {
23 return [
24 'Net::SinFP3::Next::Frame',
25 'Net::SinFP3::Next::MultiFrame',
26 ];
27 }
28  
29 sub new {
30 my $self = shift->SUPER::new(
31 count => 0,
32 @_,
33 );
34  
35 my $global = $self->global;
36 my $log = $global->log;
37  
38 if (!defined($self->file)) {
39 $log->fatal("You must provide file attribute");
40 }
41  
42 # If user did not provide its own pcap filter
43 if (!defined($self->filter)) {
44 my $filter = $global->ipv6 ? '(ip6 and tcp)' : '(ip and tcp)';
45 $self->filter($filter);
46 }
47  
48 return $self;
49 }
50  
51 sub _getDump {
52 my $self = shift;
53 my ($file) = @_;
54  
55 my $global = $self->global;
56  
57 my $oDump = $global->getDumpOffline(
58 file => $file,
59 filter => $self->filter,
60 ) or return;
61  
62 return $oDump;
63 }
64  
65 sub init {
66 my $self = shift->SUPER::init(@_) or return;
67  
68 my $files = $self->global->expandFiles(files => $self->file);
69 $self->_files($files);
70  
71 return 1;
72 }
73  
74 sub run {
75 my $self = shift->SUPER::run(@_) or return;
76  
77 my $global = $self->global;
78 my $log = $global->log;
79  
80 my $oDump;
81 if (!defined($self->_dump)) {
82 # Get next file
83 my $list = $self->_files;
84 my $file = shift @$list;
85 $self->_files($list);
86  
87 $log->info("Get frames from pcap file [$file]");
88  
89 $oDump = $self->_getDump($file) or return;
90 $oDump->start or return;
91 $self->_dump($oDump);
92 }
93 else {
94 $oDump = $self->_dump;
95 }
96  
97 my @frames = ();
98 my $read = 0;
99 my $count = $self->count;
100 while (my $h = $oDump->next) {
101 my $frame = Net::Frame::Simple->newFromDump($h);
102 $read++;
103  
104 # Due to some buggy pcap installs that miss ip6 filter
105 if ($global->ipv6 && !$frame->ref->{IPv6}) {
106 next;
107 }
108  
109 push @frames, $frame;
110  
111 # If user specified a count number, we return this number of frames
112 if ($count) {
113 if (@frames == $count) {
114 $log->debug("Returning $count frames");
115 my $next = Net::SinFP3::Next::MultiFrame->new(
116 global => $global,
117 frameList => \@frames,
118 );
119 return $next;
120 }
121 }
122 else {
123 return Net::SinFP3::Next::Frame->new(
124 global => $global,
125 frame => $frame,
126 );
127 }
128 }
129  
130 # If we are here, we are done with this file (next returned undef)
131 $oDump->stop;
132 $self->_dump(undef);
133  
134 # Last if no more files in array
135 if (@{$self->_files} == 0) {
136 $self->last(1);
137 }
138  
139 # Else we will return all frames available in pcap file
140 if ($count) {
141 $log->debug("Returning ".@frames." frames (all available frames)");
142 return Net::SinFP3::Next::MultiFrame->new(
143 global => $global,
144 frameList => \@frames,
145 );
146 }
147  
148 return;
149 }
150  
151 1;
152  
153 __END__