blib/lib/Net/TcpDumpLog.pm

filter: Statementstmt 64.1% 84 / 131 Branchbran 40.4% 17 / 42 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 42.8% 6 / 14 Podpod 91.6% 11 / 12 totaltotal 53.8% 125 / 232 CC 29 SCAR 36.6
CC
29
Cov
56%
read
69.4
new
9.8
maxindex
2.0
CRAP 38.7
↑ Summary
1 #!/bin/perl -w
2 #
3 # TcpDumpLog.pm - Net::TcpDumpLog library to read tcpdump/libpcap files.
4 #
5 # 17-Oct-2003 Brendan Gregg
6 # 19-Oct-2003 Brendan Gregg Added code to check endian of files.
7  
8 package Net::TcpDumpLog;
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 $VERSION = '0.11';
14  
15 # new - create the tcpdump object.
16 # An optional argument is the number of bits this OS uses to store
17 # times. Without this argument, this will use whatever the OS thinks
18 # it should use. By using this argument (32/64) you can force
19 # behaviour, which may be useful when transferring logs from one
20 # OS to another. Why this is so important is that the actual
21 # tcpdump/libpcap file format changes depending on the bits.
22 #
23 1 ▶sub new {
Subroutine new: called CC=8 SCAR=22.8
Pod documented
24 1 my $proto = shift;
25 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
26 1 my $self = {};
27  
28 1 my $bits = shift;
29 1 my $skip = shift;
30  
31 1 $self->{major} = undef;
32 1 $self->{minor} = undef;
33 1 $self->{zoneoffset} = undef;
34 1 $self->{accuracy} = undef;
35 1 $self->{dumplength} = undef;
36 1 $self->{linktype} = undef;
37 1 $self->{bigendian} = undef;
38 1 $self->{data} = [];
39 1 $self->{length_orig} = [];
40 1 $self->{length_inc} = [];
41 1 $self->{drops} = [];
42 1 $self->{seconds} = [];
43 1 $self->{msecs} = [];
44 1 $self->{count} = 0;
45 1 $self->{sizeint} = length(pack("I",0));
46  
47 1 ▶ if (defined $bits && $bits == 64) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $bits and $bits == 64) { } 0 1 1
elsif (defined $bits and $bits == 32) { } 1 0 1
Condition33%1/3
defined $bits and $bits == 64
!ll&&!rl&&r
010
MC/DC0%0/2
defined $bits and $bits == 64
defined $bits $bits == 64
Truth table33%1/3
A and B
A= defined $bitsB= $bits == 64
ABresult
0X0
100
111
48 0 $self->{bits} = 64;
49 ▶ } elsif (defined $bits && $bits == 32) {
Condition33%1/3
defined $bits and $bits == 32
!ll&&!rl&&r
001
MC/DC0%0/2
defined $bits and $bits == 32
defined $bits $bits == 32
Truth table33%1/3
A and B
A= defined $bitsB= $bits == 32
ABresult
0X0
100
111
50 1 $self->{bits} = 32;
51 } else {
52 0 $self->{bits} = 0; # Use native OS bits
53 }
54  
55 1 ▶ if (defined $skip && $skip > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $skip and $skip > 0) 0 1 1
Condition33%1/3
defined $skip and $skip > 0
!ll&&!rl&&r
100
MC/DC0%0/2
defined $skip and $skip > 0
defined $skip $skip > 0
Truth table33%1/3
A and B
A= defined $skipB= $skip > 0
ABresult
0X0
100
111
56 0 $self->{skip} = $skip;
57 }
58  
59 1 bless($self,$class);
60 1 return $self;
61 }
62  
63 # read - read the tcpdump file into memory
64 #
65 1 ▶sub read {
Subroutine read: called CC=22 SCAR=42.4
Pod documented
66 1 my $self = shift;
67 1 my $file = shift;
68 1 my ($header,$length,$ident,$version,$linktype,$header_rec,
69 $zoneoffset,$accuracy,$frame_length_inc,$frame_length_orig,
70 $frame_drops,$frame_seconds,$frame_msecs,$frame_data,
71 $pad,$major,$minor,$dumplength,$rest,$more);
72 1 $self->{count} = 0;
73 1 my $num = 0;
74  
75 ### Open tcpdump file
76 1 ▶ open(TCPDUMPFILE,"$file") ||
Branch: 1 branch
BranchTrueFalseTotal
open TCPDUMPFILE, "$file" or die "ERROR: Can't read log ${file}: $!\n" 1 0 1
77 die "ERROR: Can't read log $file: $!\n";
78 1 binmode(TCPDUMPFILE); # backward OSs
79  
80 ### Fetch tcpdump header
81 1 $length = read(TCPDUMPFILE,$header,24);
82 1 ▶ die "ERROR: Can't read from log $file\n" if $length < 24;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 24 0 1 1
83  
84 ### Check file really is a tcpdump file
85 1 ($ident,$rest) = unpack('a4a20',$header);
86  
87 1 ▶ if ($ident !~ /^\241\262\303\324/ &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($ident =~ /^\241\262\303\324/ or $ident =~ /^\324\303\262\241/ or $ident =~ /^\241\262\315\064/ or $ident =~ /^\064\315\262\241/) 0 1 1
Condition33%3/9
$ident =~ /^\241\262\303\324/ or $ident =~ /^\324\303\262\241/ or $ident =~ /^\241\262\315\064/ or $ident =~ /^\064\315\262\241/
l!l&&r!l&&!r
100
$ident =~ /^\241\262\303\324/ or $ident =~ /^\324\303\262\241/ or $ident =~ /^\241\262\315\064/
l!l&&r!l&&!r
100
$ident =~ /^\241\262\303\324/ or $ident =~ /^\324\303\262\241/
l!l&&r!l&&!r
100
MC/DC0%0/4
$ident =~ /^\241\262\303\324/ or $ident =~ /^\324\303\262\241/ or $ident =~ /^\241\262\315\064/ or $ident =~ /^\064\315\262\241/
$ident =~ /^\241\262\303\324/ $ident =~ /^\324\303\262\241/ $ident =~ /^\241\262\315\064/ $ident =~ /^\064\315\262\241/
Truth table20%1/5
A or B or C or D
A= $ident =~ /^\241\262\303\324/B= $ident =~ /^\324\303\262\241/C= $ident =~ /^\241\262\315\064/D= $ident =~ /^\064\315\262\241/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
88 $ident !~ /^\324\303\262\241/ &&
89 $ident !~ /^\241\262\315\064/ &&
90 $ident !~ /^\064\315\262\241/){
91 0 die "ERROR: Not a tcpdump file (or unknown version) $file\n";
92 }
93  
94 ### Find out what type of tcpdump file it is
95 1 ▶ if ($ident =~ /^\241\262\303\324/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident =~ /^\241\262\303\324/) 1 0 1
96 #
97 # Standard format big endian, header "a1b2c3d4"
98 # Seen from:
99 # Solaris tcpdump
100 # Solaris Ethereal "libpcap" format
101 #
102 1 $self->{style} = "standard1";
103 1 $self->{bigendian} = 1;
104 1 ($ident,$major,$minor,$zoneoffset,$accuracy,$dumplength,
105 $linktype) = unpack('a4nnNNNN',$header);
106 }
107 1 ▶ if ($ident =~ /^\324\303\262\241/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident =~ /^\324\303\262\241/) 0 1 1
108 #
109 # Standard format little endian, header "d4c3b2a1"
110 # Seen from:
111 # Windows Ethereal "libpcap" format
112 #
113 0 $self->{style} = "standard2";
114 0 $self->{bigendian} = 0;
115 0 ($ident,$major,$minor,$zoneoffset,$accuracy,$dumplength,
116 $linktype) = unpack('a4vvVVVV',$header);
117 }
118 1 ▶ if ($ident =~ /^\241\262\315\064/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident =~ /^\241\262\315\064/) 0 1 1
119 #
120 # Modified format big endian, header "a1b2cd34"
121 # Seen from:
122 # Solaris Ethereal "modified" format
123 #
124 0 $self->{style} = "modified1";
125 0 $self->{bigendian} = 1;
126 0 ($ident,$major,$minor,$zoneoffset,$accuracy,$dumplength,
127 $linktype) = unpack('a4nnNNNN',$header);
128 }
129 1 ▶ if ($ident =~ /^\064\315\262\241/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident =~ /^\064\315\262\241/) 0 1 1
130 #
131 # Modified format little endian, header "cd34a1b2"
132 # Seen from:
133 # Red Hat tcpdump
134 # Windows Ethereal "modified" format
135 #
136 0 $self->{style} = "modified2";
137 0 $self->{bigendian} = 0;
138 0 ($ident,$major,$minor,$zoneoffset,$accuracy,$dumplength,
139 $linktype) = unpack('a4vvVVVV',$header);
140 }
141  
142 ### Store values
143 1 $self->{version} = $version;
144 1 $self->{major} = $major;
145 1 $self->{minor} = $minor;
146 1 $self->{zoneoffset} = $zoneoffset;
147 1 $self->{accuracy} = $accuracy;
148 1 $self->{dumplength} = $dumplength;
149 1 $self->{linktype} = $linktype;
150  
151 #
152 # Read all packets into memory
153 #
154 1 $num = 0;
155 1 while (1) {
156
157 186 ▶ if ($self->{bits} == 64) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'bits'} == 64) { } 0 186 186
elsif ($self->{'bits'} == 32) { } 186 0 186
158 #
159 # 64-bit timestamps, quads
160 #
161  
162 ### Fetch record header
163 0 $length = read(TCPDUMPFILE,$header_rec,24);
164  
165 ### Quit loop if at end of file
166 0 ▶ last if $length < 24;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 24 0 0 0
167  
168 ### Unpack header
169 0 ($frame_seconds,$frame_msecs,$frame_length_inc,
170 $frame_length_orig) = unpack('QQLL',$header_rec);
171  
172 } elsif ($self->{bits} == 32) {
173 #
174 # 32-bit timestamps, big-endian
175 #
176  
177 ### Fetch record header
178 186 $length = read(TCPDUMPFILE,$header_rec,16);
179  
180 ### Quit loop if at end of file
181 186 ▶ last if $length < 16;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 16 1 185 186
182  
183 ### Unpack header
184 185 ▶ if ($self->{bigendian}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bigendian'}) { } 185 0 185
185 185 ($frame_seconds,$frame_msecs,
186 $frame_length_inc,$frame_length_orig)
187 = unpack('NNNN',$header_rec);
188 } else {
189 0 ($frame_seconds,$frame_msecs,
190 $frame_length_inc,$frame_length_orig)
191 = unpack('VVVV',$header_rec);
192 }
193  
194 } else {
195 #
196 # Default to OS native timestamps
197 #
198  
199 ### Fetch record header
200 $length = read(TCPDUMPFILE,$header_rec,
201 0 ($self->{sizeint} * 2 + 8) );
202  
203 ### Quit loop if at end of file
204 0 ▶ last if $length < ($self->{sizeint} * 2 + 8);
Branch: 1 branch
BranchTrueFalseTotal
if $length < $self->{"sizeint"} * 2 + 8 0 0 0
205  
206 ### Unpack header
207 0 ▶ if ($self->{sizeint} == 4) { # 32-bit
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sizeint'} == 4) { } 0 0 0
208 0 ▶ if ($self->{bigendian}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bigendian'}) { } 0 0 0
209 0 ($frame_seconds,$frame_msecs,
210 $frame_length_inc,$frame_length_orig)
211 = unpack('NNNN',$header_rec);
212 } else {
213 0 ($frame_seconds,$frame_msecs,
214 $frame_length_inc,$frame_length_orig)
215 = unpack('VVVV',$header_rec);
216 }
217 } else { # 64-bit?
218 0 ▶ if ($self->{bigendian}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bigendian'}) { } 0 0 0
219 0 ($frame_seconds,$frame_msecs,
220 $frame_length_inc,$frame_length_orig)
221 = unpack('IINN',$header_rec);
222 } else {
223 0 ($frame_seconds,$frame_msecs,
224 $frame_length_inc,$frame_length_orig)
225 = unpack('IIVV',$header_rec);
226 }
227 }
228  
229 }
230  
231 ### Fetch extra info if in modified format
232 185 ▶ if ($self->{style} =~ /^modified/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"style"} =~ /^modified/) 0 185 185
233 0 $length = read(TCPDUMPFILE,$more,8);
234 }
235
236 ### Check for skip bytes
237 185 ▶ if (defined $self->{skip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"skip"}) 0 185 185
238 0 $length = read(TCPDUMPFILE,$more,$self->{skip});
239 }
240  
241 ### Fetch the data
242 185 $length = read(TCPDUMPFILE,$frame_data,$frame_length_inc);
243  
244 185 $frame_drops = $frame_length_orig - $frame_length_inc;
245  
246 ### Store values in memory
247 185 $self->{data}[$num] = $frame_data;
248 185 $self->{length_orig}[$num] = $frame_length_orig;
249 185 $self->{length_inc}[$num] = $frame_length_inc;
250 185 $self->{drops}[$num] = $frame_drops;
251 185 $self->{seconds}[$num] = $frame_seconds;
252 185 $self->{msecs}[$num] = $frame_msecs;
253 185 $self->{count}++;
254 185 $num++;
255 }
256  
257 1 close TCPDUMPFILE;
258 }
259  
260 # indexes - return a list of index numbers for the packets.
261 # indexes start at "0"
262 #
263 1 ▶sub indexes {
Subroutine indexes: called CC=1 SCAR=0.0
Pod documented
264 1 my $self = shift;
265 1 my $max = $self->{count} - 1;
266 1 return (0..$max);
267 }
268  
269 # maxindex - return the index number for the last packet.
270 # indexes start at "0"
271 #
272 0 ▶sub maxindex {
Subroutine maxindex: not called CC=1 SCAR=6.9
Pod documented
273 0 my $self = shift;
274 0 my $max = $self->{count} - 1;
275 0 return $max;
276 }
277  
278 # header - return header data for a given index
279 #
280 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
281 0 my $self = shift;
282 0 my $num = shift;
283 return ($self->{length_orig}[$num],
284 $self->{length_inc}[$num],
285 $self->{drops}[$num],
286 $self->{seconds}[$num],
287 0 $self->{msecs}[$num]);
288 }
289  
290 # data - return packet data for a given index
291 #
292 1 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
Pod documented
293 1 my $self = shift;
294 1 my $num = shift;
295 1 return $self->{data}[$num];
296 }
297  
298 # version - return log file version
299 #
300 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
301 0 my $self = shift;
302 0 return sprintf("%u.%u",$self->{major},$self->{minor});
303 }
304  
305 # linktype - return linktype
306 #
307 0 ▶sub linktype {
Subroutine linktype: not called CC=1 SCAR=6.9
Pod documented
308 0 my $self = shift;
309 0 return sprintf("%u",$self->{linktype});
310 }
311  
312 # zoneoffset - return zoneoffset
313 #
314 0 ▶sub zoneoffset {
Subroutine zoneoffset: not called CC=1 SCAR=6.9
Pod documented
315 0 my $self = shift;
316 0 return sprintf("%u",$self->{zoneoffset});
317 }
318  
319 # accuracy - return accuracy
320 #
321 0 ▶sub accuracy {
Subroutine accuracy: not called CC=1 SCAR=6.9
Pod documented
322 0 my $self = shift;
323 0 return sprintf("%u",$self->{accuracy});
324 }
325  
326 # dumplength - return dumplength
327 #
328 0 ▶sub dumplength {
Subroutine dumplength: not called CC=1 SCAR=6.9
Pod documented
329 0 my $self = shift;
330 0 return sprintf("%u",$self->{dumplength});
331 }
332  
333 # clear - clear tcpdump file from memory
334 #
335 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
336 0 my $self = shift;
337 0 delete $self->{data};
338 0 $self
339 }
340  
341  
342 1 1;
343 __END__
↑ Summary