| 1 | #!/bin/perl -w | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # SnoopLog.pm - Net::SnoopLog library to read snoop ver 2 files (RFC1761). | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # 17-Oct-2003 Brendan Gregg | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | package Net::SnoopLog; | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | $VERSION = '0.12'; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | # new - create the snoop object | ||||||||||||||||||||
| 15 | # | ||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 17 | 1 | my $proto = shift; | |||||||||||||||||||
| 18 | 1 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 19 | 1 | my $self = {}; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | $self->{version} = undef; | |||||||||||||||||||
| 22 | 1 | $self->{datalink} = undef; | |||||||||||||||||||
| 23 | 1 | $self->{data} = []; | |||||||||||||||||||
| 24 | 1 | $self->{length_orig} = []; | |||||||||||||||||||
| 25 | 1 | $self->{length_inc} = []; | |||||||||||||||||||
| 26 | 1 | $self->{drops} = []; | |||||||||||||||||||
| 27 | 1 | $self->{seconds} = []; | |||||||||||||||||||
| 28 | 1 | $self->{msecs} = []; | |||||||||||||||||||
| 29 | 1 | $self->{count} = 0; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 1 | bless($self,$class); | |||||||||||||||||||
| 32 | 1 | return $self; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # read - read the snoop file into memory | ||||||||||||||||||||
| 36 | # | ||||||||||||||||||||
| 37 | 1 | ▶ | sub read { | ||||||||||||||||||
|
Subroutine
read: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 38 | 1 | my $self = shift; | |||||||||||||||||||
| 39 | 1 | my $file = shift; | |||||||||||||||||||
| 40 | 1 | my ($header,$length,$ident,$version,$datalink,$header_rec, | |||||||||||||||||||
| 41 | $record_length_orig,$record_length_inc,$record_length_rec, | ||||||||||||||||||||
| 42 | $record_drops,$record_seconds,$record_msecs,$record_data,$skip,$pad); | ||||||||||||||||||||
| 43 | 1 | $self->{count} = 0; | |||||||||||||||||||
| 44 | 1 | my $num = 0; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | ### Open snoop file | ||||||||||||||||||||
| 47 | 1 | ▶ | open(SNOOPFILE,"$file") || | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | die "ERROR: Can't read snoop log $file: $!\n"; | ||||||||||||||||||||
| 49 | 1 | binmode(SNOOPFILE); # backward OSs | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | ### Fetch snoop header | ||||||||||||||||||||
| 52 | 1 | $length = read(SNOOPFILE,$header,16); | |||||||||||||||||||
| 53 | 1 | ▶ | die "ERROR: Can't read from snoop log $file\n" if $length < 16; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | ### Check file really is a snoop file | ||||||||||||||||||||
| 56 | 1 | ($ident,$version,$datalink) = unpack('A8NN',$header); | |||||||||||||||||||
| 57 | 1 | ▶ | die "ERROR: Not a snoop file $file\n" if $ident ne "snoop"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | ### Store values | ||||||||||||||||||||
| 60 | 1 | $self->{version} = $version; | |||||||||||||||||||
| 61 | 1 | $self->{datalink} = $datalink; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # | ||||||||||||||||||||
| 64 | # Read all packets into memory | ||||||||||||||||||||
| 65 | # | ||||||||||||||||||||
| 66 | 1 | $num = 0; | |||||||||||||||||||
| 67 | 1 | while (1) { | |||||||||||||||||||
| 68 | ### Fetch record header | ||||||||||||||||||||
| 69 | 173 | $length = read(SNOOPFILE,$header_rec,24); | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | ### Quit loop if at end of file | ||||||||||||||||||||
| 72 | 173 | ▶ | last if $length < 24; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | ### Unpack header | ||||||||||||||||||||
| 75 | 172 | ($record_length_orig,$record_length_inc,$record_length_rec, | |||||||||||||||||||
| 76 | $record_drops,$record_seconds,$record_msecs) = | ||||||||||||||||||||
| 77 | unpack('NNNNNN',$header_rec); | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | ### Skip padding | ||||||||||||||||||||
| 80 | 172 | $length = read(SNOOPFILE,$record_data,$record_length_inc); | |||||||||||||||||||
| 81 | 172 | $skip = read(SNOOPFILE,$pad,($record_length_rec - | |||||||||||||||||||
| 82 | $record_length_inc - 24)); | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | ### Store values in memory | ||||||||||||||||||||
| 85 | 172 | $self->{data}[$num] = $record_data; | |||||||||||||||||||
| 86 | 172 | $self->{length_orig}[$num] = $record_length_orig; | |||||||||||||||||||
| 87 | 172 | $self->{length_inc}[$num] = $record_length_inc; | |||||||||||||||||||
| 88 | 172 | $self->{drops}[$num] = $record_drops; | |||||||||||||||||||
| 89 | 172 | $self->{seconds}[$num] = $record_seconds; | |||||||||||||||||||
| 90 | 172 | $self->{msecs}[$num] = $record_msecs; | |||||||||||||||||||
| 91 | 172 | $self->{count}++; | |||||||||||||||||||
| 92 | 172 | $num++; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | close SNOOPFILE; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # indexes - return a list of index numbers for the packets. | ||||||||||||||||||||
| 99 | # indexes start at "0" | ||||||||||||||||||||
| 100 | # | ||||||||||||||||||||
| 101 | 1 | ▶ | sub indexes { | ||||||||||||||||||
|
Subroutine
indexes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 102 | 1 | my $self = shift; | |||||||||||||||||||
| 103 | 1 | my $max = $self->{count} - 1; | |||||||||||||||||||
| 104 | 1 | return (0..$max); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # maxindex - return the index number for the last packet. | ||||||||||||||||||||
| 108 | # indexes start at "0" | ||||||||||||||||||||
| 109 | # | ||||||||||||||||||||
| 110 | 0 | ▶ | sub maxindex { | ||||||||||||||||||
|
Subroutine
maxindex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||
| 112 | 0 | my $max = $self->{count} - 1; | |||||||||||||||||||
| 113 | 0 | return $max; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | # header - return header data for a given index | ||||||||||||||||||||
| 117 | # | ||||||||||||||||||||
| 118 | 0 | ▶ | sub header { | ||||||||||||||||||
|
Subroutine
header: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||
| 120 | 0 | my $num = shift; | |||||||||||||||||||
| 121 | return ($self->{length_orig}[$num], | ||||||||||||||||||||
| 122 | $self->{length_inc}[$num], | ||||||||||||||||||||
| 123 | $self->{drops}[$num], | ||||||||||||||||||||
| 124 | $self->{seconds}[$num], | ||||||||||||||||||||
| 125 | 0 | $self->{msecs}[$num]); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # data - return packet data for a given index | ||||||||||||||||||||
| 129 | # | ||||||||||||||||||||
| 130 | 1 | ▶ | sub data { | ||||||||||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 131 | 1 | my $self = shift; | |||||||||||||||||||
| 132 | 1 | my $num = shift; | |||||||||||||||||||
| 133 | 1 | return $self->{data}[$num]; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | # version - return snoop file version | ||||||||||||||||||||
| 137 | # | ||||||||||||||||||||
| 138 | 0 | ▶ | sub version { | ||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||||||||||
| 140 | 0 | return sprintf("%u",$self->{version}); | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | # datalink - return snoop datalink type | ||||||||||||||||||||
| 144 | # | ||||||||||||||||||||
| 145 | 0 | ▶ | sub datalink { | ||||||||||||||||||
|
Subroutine
datalink: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||
| 147 | 0 | return sprintf("%u",$self->{datalink}); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | # clear - clear snoop file from memory | ||||||||||||||||||||
| 151 | # | ||||||||||||||||||||
| 152 | 0 | ▶ | sub clear { | ||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 153 | 0 | my $self = shift; | |||||||||||||||||||
| 154 | 0 | delete $self->{data}; | |||||||||||||||||||
| 155 | 0 | $self | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 1 | 1; | |||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||