Tshark.pm

filter: Statementstmt 28.0% 23 / 82 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 41.1% 7 / 17 Podpod 100% 6 / 6 totaltotal 25.1% 36 / 143 CC 20 SCAR 29.3
CC
20
Cov
20%
start
56.0
new
20.0
__get_decoded_packet
20.0
CRAP 18.6
« Tshark/Field.pm ↑ Summary Tshark/Packet.pm »
1 #!/usr/bin/perl -w
2 package Net::Tshark;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 our $VERSION = '0.04';
7
8 1 ▶use IPC::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::Tshark::Packet;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 # These thresholds are used to prevent the possibility
13 # of an infinite loop while waiting for packet data.
14 1 ▶use constant MAX_POLLING_ITERATIONS => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant MAX_PACKETS_RETURNED_AT_ONCE => 10_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 sub new
18 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
19 0 my ($class) = @_;
20
21 # Try to find tshark
22 0 my $tshark_path = which('tshark');
23 0 ▶ if (!defined $tshark_path)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tshark_path) 0 0 0
24 {
25 0 ▶ if ($^O eq 'MSWin32' && -x "C:\\Program Files\\Wireshark\\tshark.exe")
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32' and -x 'C:\\Program Files\\Wireshark\\tshark.exe') { } 0 0 0
Condition0%0/3
$^O eq "MSWin32" and -x "C:\\Program Files\\Wireshark\\tshark.exe"
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and -x "C:\\Program Files\\Wireshark\\tshark.exe"
$^O eq "MSWin32" -x "C:\\Program Files\\Wireshark\\tshark.exe"
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= -x "C:\\Program Files\\Wireshark\\tshark.exe"
ABresult
0X0
100
111
26 {
27 0 $tshark_path = "C:\\Program Files\\Wireshark\\tshark.exe";
28 }
29 else
30 {
31 0 warn 'Could not find TShark installed. Is it in your PATH?';
32 0 return;
33 }
34 }
35
36 0 my $self = {
37 in => q(),
38 out => q(),
39 err => q(),
40 tshark_path => $tshark_path,
41 };
42
43 0 return bless $self, $class;
44 }
45
46 sub DESTROY
47 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
48 0 my ($self) = @_;
49 0 return $self->stop;
50 }
51
52 sub start
53 0 ▶{
Subroutine start: not called CC=7 SCAR=40.3
Pod documented
54 0 my ($self, %args) = @_;
55 my ($interface, $capture_filter, $display_filter, $duration, $promiscuous)
56 0 = @args{qw(interface capture_filter display_filter duration promiscuous)};
57
58 # Construct the command to execute tshark
59 0 my @command = ($self->{tshark_path});
60 0 ▶ push @command, '-a duration:', int($duration) if ($duration);
Branch: 1 branch
BranchTrueFalseTotal
if $duration 0 0 0
61 0 ▶ push @command, '-f', $capture_filter if ($capture_filter);
Branch: 1 branch
BranchTrueFalseTotal
if $capture_filter 0 0 0
62 0 ▶ push @command, '-i', $interface if (defined $interface);
Branch: 1 branch
BranchTrueFalseTotal
if defined $interface 0 0 0
63 0 push @command, '-l'; # Flush the standard output after each packet
64 0 ▶ push @command, '-p' if (defined $promiscuous && !$promiscuous);
Branch: 1 branch
BranchTrueFalseTotal
if defined $promiscuous and not $promiscuous 0 0 0
Condition0%0/3
defined $promiscuous and not $promiscuous
!ll&&!rl&&r
000
MC/DC0%0/2
defined $promiscuous and not $promiscuous
defined $promiscuous not $promiscuous
Truth table0%0/3
A and not(B)
A= defined $promiscuousB= not $promiscuous
ABresult
0X0
100
111
65 0 ▶ push @command, '-R', $display_filter if ($display_filter);
Branch: 1 branch
BranchTrueFalseTotal
if $display_filter 0 0 0
66 0 push @command, '-T', 'pdml'; # Output XML
67
68 # Start a tshark process and pipe its input, output, and error streams
69 # so that we can read and write to it while it runs
70 $self->{tshark} = IPC::Run::start \@command, \$self->{in}, \$self->{out},
71 0 \$self->{err};
72
73 0 return 1;
74 }
75
76 sub is_running
77 0 ▶{
Subroutine is_running: not called CC=1 SCAR=6.9
Pod documented
78 0 my ($self) = @_;
79 0 return defined $self->{tshark};
80 }
81
82 sub stop
83 0 ▶{
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
84 0 my ($self) = @_;
85
86 0 ▶ if (defined $self->{tshark})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"tshark"}) 0 0 0
87 {
88
89 # Send Ctrl-C to gracefully end the process
90 0 $self->{tshark}->signal('INT');
91
92 # Get all of its stdout
93 0 $self->__get_all_output;
94
95 # Make sure the process has terminated
96 0 $self->{tshark}->kill_kill;
97 0 $self->{tshark}->finish;
98 0 undef $self->{tshark};
99 }
100
101 0 return;
102 }
103
104 sub get_packet
105 0 ▶{
Subroutine get_packet: not called CC=2 SCAR=17.9
Pod documented
106 0 my ($self) = @_;
107
108 # Get the decoded string for one packet.
109 0 ▶ my $pkt_string = $self->__get_decoded_packet
Branch: 1 branch
BranchTrueFalseTotal
my $pkt_string = $self->__get_decoded_packet or return 0 0 0
110 or return;
111
112 # Create a packet object from the string and return it.
113 0 return Net::Tshark::Packet->new($pkt_string);
114 }
115
116 sub get_packets
117 0 ▶{
Subroutine get_packets: not called CC=3 SCAR=24.8
Pod documented
118 0 my ($self) = @_;
119
120 0 my @packets;
121 0 for (1 .. MAX_PACKETS_RETURNED_AT_ONCE)
122 {
123 0 my $packet = $self->get_packet;
124 0 ▶ last if !defined $packet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packet 0 0 0
125
126 0 push @packets, $packet;
127 }
128
129 0 return @packets;
130 }
131
132 sub __get_decoded_packet
133 0 ▶{
Subroutine __get_decoded_packet: not called CC=4 SCAR=30.0
134 0 my ($self) = @_;
135
136 0 for (1 .. MAX_POLLING_ITERATIONS)
137 {
138 # Wait for us to see an entire packet
139 0 ▶ if (my ($packet) = $self->{out} =~ /(<packet>.*?<\/packet>)/s)
Branch: 1 branch
BranchTrueFalseTotal
if (my($packet) = $self->{"out"} =~ m[(<packet>.*?</packet>)]s) 0 0 0
140 {
141
142 # Remove the packet from the buffer and process it
143 0 $self->{out} =~ s/\Q$packet\E//;
144
145 0 return $packet;
146 }
147
148 # Get the latest output from the tshark process
149 # and quit if there is no more output to get
150 0 ▶ last if !$self->__get_more_output;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->__get_more_output 0 0 0
151 }
152
153 0 return;
154 }
155
156 sub __get_more_output
157 0 ▶{
Subroutine __get_more_output: not called CC=2 SCAR=17.9
158 0 my ($self) = @_;
159 0 ▶ return if !defined $self->{tshark};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tshark"} 0 0 0
160
161 0 my $buf_len = length $self->{out};
162 0 $self->{tshark}->pump_nb;
163 0 return length $self->{out} > $buf_len;
164 }
165
166 sub __get_all_output
167 0 ▶{
Subroutine __get_all_output: not called CC=3 SCAR=24.8
168 0 my ($self) = @_;
169
170 0 for (1 .. MAX_POLLING_ITERATIONS)
171 {
172 0 ▶ last if !$self->__get_more_output;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->__get_more_output 0 0 0
173 }
174
175 0 return;
176 }
177
178 1 1;
179
180 __END__
« Tshark/Field.pm ↑ Summary Tshark/Packet.pm »