Tshark/Field.pm

filter: Statementstmt 14.6% 17 / 116 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 17.8% 5 / 28 Podpod 77.7% 7 / 9 totaltotal 13.7% 29 / 211 CC 24 SCAR 38.5
CC
24
Cov
10%
new
156.0
FETCH
56.0
hash
12.0
CRAP 47.0
↑ Summary Tshark.pm »
1 package Net::Tshark::Field;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 1 our $VERSION = '0.04';
6
7 1 ▶use List::MoreUtils qw(any all uniq after);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 1 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=1.2
11 0 ▶ q("") => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 0 my $self = tied %{ $_[0] };
13 0 $self->{__value};
14 }
15 1 );
16
17 sub new
18 0 ▶{
Subroutine new: not called CC=12 SCAR=50.5
Pod undocumented
19 0 my ($class, $field_data) = @_;
20 0 ▶ return if (!defined $field_data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field_data 0 0 0
21
22 # Extract the value, and child nodes of this field
23 my $value =
24 (defined $field_data->{show})
25 ? $field_data->{show}
26 0 ▶ : $field_data->{showname};
Branch: 1 branch
BranchTrueFalseTotal
defined $field_data->{'show'} ? : 0 0 0
27 my @child_nodes =
28 0 ▶ (@{ $field_data->{field} || [] }, @{ $field_data->{proto} || [] });
Condition0%0/4
$field_data->{'field'} || []
l!l
00
$field_data->{'proto'} || []
l!l
00
MC/DC0%0/1
$field_data->{'field'} || []
$field_data->{'field'}
MC/DC0%0/1
$field_data->{'proto'} || []
$field_data->{'proto'}
Truth table0%0/2
A
A= $field_data->{'field'}
Aresult
11
00
Truth table0%0/2
A
A= $field_data->{'proto'}
Aresult
11
00
29
30 # If this node has no children, we can just return a scalar
31 0 ▶ return $value if (!@child_nodes);
Branch: 1 branch
BranchTrueFalseTotal
unless @child_nodes 0 0 0
32
33 # If a field doesn't have a name, give it a name based on
34 # its showname or show attribute.
35 0 foreach (@child_nodes)
36 {
37 0 ▶ if (!defined $_->{name} || !length $_->{name})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_->{"name"} and length $_->{"name"}) 0 0 0
Condition0%0/3
defined $_->{"name"} and length $_->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{"name"} and length $_->{"name"}
defined $_->{"name"} length $_->{"name"}
Truth table0%0/3
A and B
A= defined $_->{"name"}B= length $_->{"name"}
ABresult
0X0
100
111
38 {
39 $_->{name} =
40 defined $_->{showname} && length($_->{showname}) ? $_->{showname}
41 : defined $_->{show} ? $_->{show}
42 0 ▶ : q();
Branch: 2 branches
BranchTrueFalseTotal
defined $_->{'showname'} && length $_->{'showname'} ? : 0 0 0
defined $_->{'show'} ? : 0 0 0
Condition0%0/3
defined $_->{"showname"} and length $_->{"showname"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{"showname"} and length $_->{"showname"}
defined $_->{"showname"} length $_->{"showname"}
Truth table0%0/3
A and B
A= defined $_->{"showname"}B= length $_->{"showname"}
ABresult
0X0
100
111
43 }
44 }
45
46 my $data = {
47 show => $field_data->{show},
48 showname => $field_data->{showname},
49 name => $field_data->{name},
50 size => $field_data->{size},
51 value => $field_data->{value},
52 0 __value => $value,
53 __child_nodes => \@child_nodes,
54 };
55
56 # Tie a new hash to this package so we can access parts of the parsed
57 # PDML using hash notation (e.g. $packet->{ip}). Note that the TIEHASH
58 # subroutine does the actual construction of the object.
59 0 my $self = {};
60 0 tie %{$self}, $class, $data;
61 0 return bless $self, $class;
62 }
63
64 sub fields
65 0 ▶{
Subroutine fields: not called CC=1 SCAR=6.9
Pod documented
66 0 my ($field) = @_;
67 0 my $self = tied %{$field};
68 0 return map { Net::Tshark::Field->new($_) } @{ $self->{__child_nodes} };
69 }
70
71 sub show
72 0 ▶{
Subroutine show: not called CC=1 SCAR=6.9
Pod documented
73 0 my ($field) = @_;
74 0 my $self = tied %{$field};
75 0 return $self->{show};
76 }
77
78 sub showname
79 0 ▶{
Subroutine showname: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($field) = @_;
81 0 my $self = tied %{$field};
82 0 return $self->{showname};
83 }
84
85 sub name
86 0 ▶{
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
87 0 my ($field) = @_;
88 0 my $self = tied %{$field};
89 0 return $self->{name};
90 }
91
92 sub size
93 0 ▶{
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
94 0 my ($field) = @_;
95 0 my $self = tied %{$field};
96 0 return $self->{size};
97 }
98
99 sub value
100 0 ▶{
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
101 0 my ($field) = @_;
102 0 my $self = tied %{$field};
103 0 return $self->{value};
104 }
105
106 sub hash
107 0 ▶{
Subroutine hash: not called CC=3 SCAR=24.8
Pod documented
108 0 my ($field) = @_;
109
110 0 my %hash = %{$field};
111 0 ▶ while (my ($key, $value) = each %hash)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %hash) 0 0 0
112 {
113 0 ▶ if (ref $hash{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash{$key}) 0 0 0
114 {
115 0 my $sub_hash = $hash{$key}->hash;
116 0 $hash{$key} = $sub_hash;
117 }
118 }
119
120 0 return \%hash;
121 }
122
123 sub TIEHASH
124 0 ▶{
Subroutine TIEHASH: not called CC=1 SCAR=6.9
125 0 my ($class, $self) = @_;
126 0 return bless $self, $class;
127 }
128
129 sub STORE
130 0 ▶{
Subroutine STORE: not called CC=1 SCAR=0.0
131
132 # Do nothing. If someone tries to access a field that doesn't exist,
133 # Perl will try to create it via autovilification. We don't want to
134 # create anything, but we also don't want this to trigger any warnings.
135 }
136
137 sub FETCH
138 0 ▶{
Subroutine FETCH: not called CC=7 SCAR=40.3
139 0 my ($self, $key) = @_;
140 0 my @nodes = $self->__fields($key);
141
142 # If nothing was found, do a deep search in the child nodes for a name match
143 0 ▶ if (!@nodes)
Branch: 1 branch
BranchTrueFalseTotal
unless (@nodes) 0 0 0
144 {
145 0 foreach my $child (@{ $self->{__child_nodes} })
146 {
147 push @nodes,
148 0 grep { $_->{name} =~ /^(?:.*\.)?$key$/i }
149 0 ▶ (@{ $child->{field} || [] }, @{ $child->{proto} || [] });
Condition0%0/4
$child->{'field'} || []
l!l
00
$child->{'proto'} || []
l!l
00
MC/DC0%0/1
$child->{'field'} || []
$child->{'field'}
MC/DC0%0/1
$child->{'proto'} || []
$child->{'proto'}
Truth table0%0/2
A
A= $child->{'field'}
Aresult
11
00
Truth table0%0/2
A
A= $child->{'proto'}
Aresult
11
00
150 }
151 }
152
153 # If all the matching fields are leaves, append all their values and
154 # return them as a constructed field
155 0 ▶ if (all { !defined $_->{field} && !defined $_->{proto} } @nodes)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { not defined $_->{"field"} || defined $_->{"proto"}; } , @nodes)) 0 0 0
Condition0%0/3
defined $_->{'field'} || defined $_->{'proto'}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $_->{'field'} || defined $_->{'proto'}
defined $_->{'field'} defined $_->{'proto'}
Truth table0%0/3
A || B
A= defined $_->{'field'}B= defined $_->{'proto'}
ABresult
1X1
011
000
156 {
157 my $show = join(q(),
158 0 ▶ map { (defined $_->{show}) ? $_->{show} : $_->{showname} } @nodes);
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'show'} ? : 0 0 0
159 0 return Net::Tshark::Field->new({ show => $show });
160 }
161
162 # Otherwise, return the first matching node
163 0 return Net::Tshark::Field->new($nodes[0]);
164 }
165
166 sub EXISTS
167 0 ▶{
Subroutine EXISTS: not called CC=1 SCAR=6.9
168 0 my ($self, $key) = @_;
169 0 ▶ return any { $_->{name} =~ /^(?:.*\.)?$key$/i } @{ $self->{__child_nodes} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 }
171
172 sub DEFINED
173 0 ▶{
Subroutine DEFINED: not called CC=1 SCAR=6.9
Pod undocumented
174 0 return EXISTS(@_);
175 }
176
177 sub CLEAR
178 0 ▶{
Subroutine CLEAR: not called CC=1 SCAR=6.9
179 0 warn 'You cannot clear a ' . __PACKAGE__ . ' object';
180 0 return;
181 }
182
183 sub DELETE
184 0 ▶{
Subroutine DELETE: not called CC=1 SCAR=6.9
185 0 warn 'You cannot delete from a ' . __PACKAGE__ . ' object';
186 0 return;
187 }
188
189 sub FIRSTKEY
190 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
191 0 my ($self) = @_;
192 0 return (@{ $self->{__child_nodes} })[0]->{name};
193 }
194
195 sub NEXTKEY
196 0 ▶{
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
197 0 my ($self, $last_key) = @_;
198
199 # Get a set of all the names of the child nodes, with no repeats
200 0 my @keys = uniq(map { $_->{name} } @{ $self->{__child_nodes} });
201 0 ▶ return (after { $_ eq $last_key } (@keys))[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 }
203
204 sub __fields
205 0 ▶{
Subroutine __fields: not called CC=3 SCAR=24.8
206 0 my ($self, $key) = @_;
207
208 # Message bodies are named differently in different versions of Wireshark
209 0 ▶ if ($key eq 'Message body' || $key eq 'msg_body')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "Message body" or $key eq "msg_body") 0 0 0
Condition0%0/3
$key eq "Message body" or $key eq "msg_body"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "Message body" or $key eq "msg_body"
$key eq "Message body" $key eq "msg_body"
Truth table0%0/3
A or B
A= $key eq "Message body"B= $key eq "msg_body"
ABresult
1X1
011
000
210 {
211 0 $key = qr/Message body|msg_body/;
212 }
213
214 # Find all the fields with a name that matches $key.
215 my @matching_nodes =
216 0 grep { $_->{name} =~ /^(?:.*\.)?$key$/i } @{ $self->{__child_nodes} };
217
218 # Choose the shortest matching field name
219 0 ▶ my $shortestName = reduce { length($a) < length($b) ? $a : $b }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $a < length $b ? : 0 0 0
220 0 map { $_->{name} } @matching_nodes;
221
222 # If there are more than one matching field, choose the
223 # field or protocol with the shortest name.
224 0 my @nodes = grep { $_->{name} eq $shortestName } (@matching_nodes);
225
226 0 return @nodes;
227 }
228
229 1 1;
230
231 __END__
↑ Summary Tshark.pm »