| line | stmt | bran | cond | sub | pod | time | code | 
| 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 |  |  | 1 |  | 9370 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 42 |  | 
| 10 | 1 |  |  | 1 |  | 5 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 1371 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | $VERSION = '0.12'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # new - create the snoop object | 
| 15 |  |  |  |  |  |  | # | 
| 16 |  |  |  |  |  |  | sub new { | 
| 17 | 1 |  |  | 1 | 1 | 146 | my $proto = shift; | 
| 18 | 1 |  | 33 |  |  | 9 | my $class = ref($proto) || $proto; | 
| 19 | 1 |  |  |  |  | 3 | my $self = {}; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 1 |  |  |  |  | 3 | $self->{version} = undef; | 
| 22 | 1 |  |  |  |  | 3 | $self->{datalink} = undef; | 
| 23 | 1 |  |  |  |  | 3 | $self->{data} = []; | 
| 24 | 1 |  |  |  |  | 2 | $self->{length_orig} = []; | 
| 25 | 1 |  |  |  |  | 3 | $self->{length_inc} = []; | 
| 26 | 1 |  |  |  |  | 4 | $self->{drops} = []; | 
| 27 | 1 |  |  |  |  | 2 | $self->{seconds} = []; | 
| 28 | 1 |  |  |  |  | 4 | $self->{msecs} = []; | 
| 29 | 1 |  |  |  |  | 3 | $self->{count} = 0; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 1 |  |  |  |  | 3 | bless($self,$class); | 
| 32 | 1 |  |  |  |  | 4 | return $self; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # read - read the snoop file into memory | 
| 36 |  |  |  |  |  |  | # | 
| 37 |  |  |  |  |  |  | sub read { | 
| 38 | 1 |  |  | 1 | 1 | 6 | my $self = shift; | 
| 39 | 1 |  |  |  |  | 3 | my $file = shift; | 
| 40 | 1 |  |  |  |  | 2 | 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 |  |  |  |  | 7 | $self->{count} = 0; | 
| 44 | 1 |  |  |  |  | 2 | my $num = 0; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | ### Open snoop file | 
| 47 | 1 | 50 |  |  |  | 42 | open(SNOOPFILE,"$file") || | 
| 48 |  |  |  |  |  |  | die "ERROR: Can't read snoop log $file: $!\n"; | 
| 49 | 1 |  |  |  |  | 3 | binmode(SNOOPFILE);		# backward OSs | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | ### Fetch snoop header | 
| 52 | 1 |  |  |  |  | 36 | $length = read(SNOOPFILE,$header,16); | 
| 53 | 1 | 50 |  |  |  | 5 | die "ERROR: Can't read from snoop log $file\n" if $length < 16; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | ### Check file really is a snoop file | 
| 56 | 1 |  |  |  |  | 9 | ($ident,$version,$datalink) = unpack('A8NN',$header); | 
| 57 | 1 | 50 |  |  |  | 4 | die "ERROR: Not a snoop file $file\n" if $ident ne "snoop"; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | ### Store values | 
| 60 | 1 |  |  |  |  | 3 | $self->{version} = $version; | 
| 61 | 1 |  |  |  |  | 3 | $self->{datalink} = $datalink; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # | 
| 64 |  |  |  |  |  |  | #  Read all packets into memory | 
| 65 |  |  |  |  |  |  | # | 
| 66 | 1 |  |  |  |  | 2 | $num = 0; | 
| 67 | 1 |  |  |  |  | 2 | while (1) { | 
| 68 |  |  |  |  |  |  | ### Fetch record header | 
| 69 | 173 |  |  |  |  | 332 | $length = read(SNOOPFILE,$header_rec,24); | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | ### Quit loop if at end of file | 
| 72 | 173 | 100 |  |  |  | 325 | last if $length < 24; | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | ### Unpack header | 
| 75 | 172 |  |  |  |  | 454 | ($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 |  |  |  |  | 329 | $length = read(SNOOPFILE,$record_data,$record_length_inc); | 
| 81 | 172 |  |  |  |  | 253 | $skip = read(SNOOPFILE,$pad,($record_length_rec - | 
| 82 |  |  |  |  |  |  | $record_length_inc - 24)); | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | ### Store values in memory | 
| 85 | 172 |  |  |  |  | 346 | $self->{data}[$num] = $record_data; | 
| 86 | 172 |  |  |  |  | 257 | $self->{length_orig}[$num] = $record_length_orig; | 
| 87 | 172 |  |  |  |  | 219 | $self->{length_inc}[$num] = $record_length_inc; | 
| 88 | 172 |  |  |  |  | 259 | $self->{drops}[$num] = $record_drops; | 
| 89 | 172 |  |  |  |  | 238 | $self->{seconds}[$num] = $record_seconds; | 
| 90 | 172 |  |  |  |  | 248 | $self->{msecs}[$num] = $record_msecs; | 
| 91 | 172 |  |  |  |  | 200 | $self->{count}++; | 
| 92 | 172 |  |  |  |  | 311 | $num++; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 1 |  |  |  |  | 15 | close SNOOPFILE; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | # indexes - return a list of index numbers for the packets. | 
| 99 |  |  |  |  |  |  | #		indexes start at "0" | 
| 100 |  |  |  |  |  |  | # | 
| 101 |  |  |  |  |  |  | sub indexes { | 
| 102 | 1 |  |  | 1 | 1 | 43 | my $self = shift; | 
| 103 | 1 |  |  |  |  | 2 | my $max = $self->{count} - 1; | 
| 104 | 1 |  |  |  |  | 22 | return (0..$max); | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # maxindex - return the index number for the last packet. | 
| 108 |  |  |  |  |  |  | #		indexes start at "0" | 
| 109 |  |  |  |  |  |  | # | 
| 110 |  |  |  |  |  |  | sub maxindex { | 
| 111 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 112 | 0 |  |  |  |  | 0 | my $max = $self->{count} - 1; | 
| 113 | 0 |  |  |  |  | 0 | return $max; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # header - return header data for a given index | 
| 117 |  |  |  |  |  |  | # | 
| 118 |  |  |  |  |  |  | sub header { | 
| 119 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 120 | 0 |  |  |  |  | 0 | my $num = shift; | 
| 121 | 0 |  |  |  |  | 0 | return ($self->{length_orig}[$num], | 
| 122 |  |  |  |  |  |  | $self->{length_inc}[$num], | 
| 123 |  |  |  |  |  |  | $self->{drops}[$num], | 
| 124 |  |  |  |  |  |  | $self->{seconds}[$num], | 
| 125 |  |  |  |  |  |  | $self->{msecs}[$num]); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # data - return packet data for a given index | 
| 129 |  |  |  |  |  |  | # | 
| 130 |  |  |  |  |  |  | sub data { | 
| 131 | 1 |  |  | 1 | 1 | 45 | my $self = shift; | 
| 132 | 1 |  |  |  |  | 2 | my $num = shift; | 
| 133 | 1 |  |  |  |  | 4 | return $self->{data}[$num]; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | # version - return snoop file version | 
| 137 |  |  |  |  |  |  | # | 
| 138 |  |  |  |  |  |  | sub version { | 
| 139 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 140 | 0 |  |  |  |  |  | return sprintf("%u",$self->{version}); | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | # datalink - return snoop datalink type | 
| 144 |  |  |  |  |  |  | # | 
| 145 |  |  |  |  |  |  | sub datalink { | 
| 146 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 147 | 0 |  |  |  |  |  | return sprintf("%u",$self->{datalink}); | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | # clear - clear snoop file from memory | 
| 151 |  |  |  |  |  |  | # | 
| 152 |  |  |  |  |  |  | sub clear { | 
| 153 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 154 | 0 |  |  |  |  |  | delete $self->{data}; | 
| 155 | 0 |  |  |  |  |  | $self | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | 1; | 
| 160 |  |  |  |  |  |  | __END__ |