| 1 | # | ||
| 2 | # $Id: Dump.pm,v a93481b098ec 2020/05/01 08:55:56 gomor $ | ||
| 3 | # | ||
| 4 | package Net::Frame::Dump; | ||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '1.18'; | ||
| 9 | |||
| 10 | 2 | ▶ | use base qw(Class::Gomor::Array Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | our %EXPORT_TAGS = ( | ||
| 13 | consts => [qw( | ||
| 14 | NF_DUMP_LAYER_NULL | ||
| 15 | NF_DUMP_LAYER_ETH | ||
| 16 | NF_DUMP_LAYER_RAW | ||
| 17 | NF_DUMP_LAYER_SLL | ||
| 18 | NF_DUMP_LAYER_PPP | ||
| 19 | NF_DUMP_LAYER_80211_RADIOTAP | ||
| 20 | NF_DUMP_LAYER_80211 | ||
| 21 | NF_DUMP_LAYER_ERF | ||
| 22 | )], | ||
| 23 | ); | ||
| 24 | our @EXPORT_OK = ( | ||
| 25 | @{$EXPORT_TAGS{consts}}, | ||
| 26 | ); | ||
| 27 | |||
| 28 | 2 | ▶ | use constant NF_DUMP_LAYER_NULL => 0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 2 | ▶ | use constant NF_DUMP_LAYER_ETH => 1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 2 | ▶ | use constant NF_DUMP_LAYER_PPP => 9; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 2 | ▶ | use constant NF_DUMP_LAYER_RAW => 12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 2 | ▶ | use constant NF_DUMP_LAYER_80211 => 105; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | 2 | ▶ | use constant NF_DUMP_LAYER_SLL => 113; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | 2 | ▶ | use constant NF_DUMP_LAYER_80211_RADIOTAP => 127; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 2 | ▶ | use constant NF_DUMP_LAYER_ERF => 175; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | |||
| 37 | our @AS = qw( | ||
| 38 | file | ||
| 39 | filter | ||
| 40 | filterCodeOptimizer | ||
| 41 | overwrite | ||
| 42 | firstLayer | ||
| 43 | isRunning | ||
| 44 | keepTimestamp | ||
| 45 | _framesStored | ||
| 46 | _pcapd | ||
| 47 | _dumper | ||
| 48 | ); | ||
| 49 | our @AA = qw( | ||
| 50 | frames | ||
| 51 | ); | ||
| 52 | __PACKAGE__->cgBuildIndices; | ||
| 53 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 54 | __PACKAGE__->cgBuildAccessorsArray(\@AA); | ||
| 55 | |||
| 56 | 0 | ▶ | use Net::Pcap; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 57 | use Time::HiRes qw(gettimeofday); | ||
| 58 | use Net::Frame::Layer qw(:consts :subs); | ||
| 59 | |||
| 60 | sub new { | ||
| 61 | my $self = shift->SUPER::new( | ||
| 62 | filter => '', | ||
| 63 | overwrite => 0, | ||
| 64 | isRunning => 0, | ||
| 65 | keepTimestamp => 0, | ||
| 66 | filterCodeOptimizer => 0, | ||
| 67 | frames => [], | ||
| 68 | _framesStored => {}, | ||
| 69 | @_, | ||
| 70 | ); | ||
| 71 | |||
| 72 | if (!defined($self->file)) { | ||
| 73 | my $int = getRandom32bitsInt(); | ||
| 74 | $self->file("netframe-tmp-$$.$int.pcap"); | ||
| 75 | } | ||
| 76 | |||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub flush { | ||
| 81 | my $self = shift; | ||
| 82 | $self->frames([]); | ||
| 83 | $self->_framesStored({}); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _addStore { | ||
| 87 | my $self = shift; | ||
| 88 | my ($oSimple) = @_; | ||
| 89 | |||
| 90 | # If parameter, we store it | ||
| 91 | if ($oSimple) { | ||
| 92 | my $key = $oSimple->getKey; | ||
| 93 | push @{$self->_framesStored->{$key}}, $oSimple; | ||
| 94 | |||
| 95 | # If it is ICMPv4, we store a second time | ||
| 96 | if (exists $oSimple->ref->{ICMPv4}) { | ||
| 97 | push @{$self->_framesStored->{$oSimple->ref->{ICMPv4}->getKey}}, | ||
| 98 | $oSimple; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | # We return the hash ref | ||
| 103 | return $self->_framesStored; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub store { | ||
| 107 | my $self = shift; | ||
| 108 | my ($oSimple) = @_; | ||
| 109 | |||
| 110 | $self->_addStore($oSimple); | ||
| 111 | |||
| 112 | my @frames = $self->frames; | ||
| 113 | push @frames, $oSimple; | ||
| 114 | return $self->frames(\@frames); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _getTimestamp { | ||
| 118 | my $self = shift; | ||
| 119 | my ($hdr) = @_; | ||
| 120 | $hdr->{tv_sec}.'.'.sprintf("%06d", $hdr->{tv_usec}); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _setTimestamp { | ||
| 124 | my $self = shift; | ||
| 125 | my @time = Time::HiRes::gettimeofday(); | ||
| 126 | $time[0].'.'.sprintf("%06d", $time[1]); | ||
| 127 | } | ||
| 128 | |||
| 129 | my $mapLinks = { | ||
| 130 | NF_DUMP_LAYER_NULL() => 'NULL', | ||
| 131 | NF_DUMP_LAYER_ETH() => 'ETH', | ||
| 132 | NF_DUMP_LAYER_RAW() => 'RAW', | ||
| 133 | NF_DUMP_LAYER_SLL() => 'SLL', | ||
| 134 | NF_DUMP_LAYER_PPP() => 'PPP', | ||
| 135 | NF_DUMP_LAYER_80211() => '80211', | ||
| 136 | NF_DUMP_LAYER_80211_RADIOTAP() => '80211::Radiotap', | ||
| 137 | NF_DUMP_LAYER_ERF() => 'ERF', | ||
| 138 | }; | ||
| 139 | |||
| 140 | sub getFirstLayer { | ||
| 141 | my $self = shift; | ||
| 142 | my $link = Net::Pcap::datalink($self->_pcapd); | ||
| 143 | $self->firstLayer($mapLinks->{$link} || NF_LAYER_UNKNOWN); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub next { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my %hdr; | ||
| 150 | if (my $raw = Net::Pcap::next($self->_pcapd, \%hdr)) { | ||
| 151 | my $ts = $self->keepTimestamp ? $self->_getTimestamp(\%hdr) | ||
| 152 | : $self->_setTimestamp; | ||
| 153 | return { | ||
| 154 | firstLayer => $self->firstLayer, | ||
| 155 | timestamp => $ts, | ||
| 156 | raw => $raw, | ||
| 157 | }; | ||
| 158 | } | ||
| 159 | |||
| 160 | return; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub nextEx { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | my %hdr; | ||
| 167 | my $raw; | ||
| 168 | my $r = Net::Pcap::next_ex($self->_pcapd, \%hdr, \$raw); | ||
| 169 | if ($r > 0) { | ||
| 170 | my $ts = $self->keepTimestamp ? $self->_getTimestamp(\%hdr) | ||
| 171 | : $self->_setTimestamp; | ||
| 172 | return { | ||
| 173 | firstLayer => $self->firstLayer, | ||
| 174 | timestamp => $ts, | ||
| 175 | raw => $raw, | ||
| 176 | }; | ||
| 177 | } | ||
| 178 | |||
| 179 | return $r; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub getFramesFor { | ||
| 183 | my $self = shift; | ||
| 184 | my ($oSimple) = @_; | ||
| 185 | |||
| 186 | my $results; | ||
| 187 | my $key = $oSimple->getKeyReverse; | ||
| 188 | push @$results, @{$self->_framesStored->{$key}} | ||
| 189 | if exists $self->_framesStored->{$key}; | ||
| 190 | |||
| 191 | # Add also ICMPv4 | ||
| 192 | if (exists $self->_framesStored->{ICMPv4}) { | ||
| 193 | push @$results, @{$self->_framesStored->{ICMPv4}}; | ||
| 194 | } | ||
| 195 | |||
| 196 | return $results ? @$results : (); | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; | ||
| 200 | |||
| 201 | __END__ | ||