| 1 | # | ||
| 2 | # $Id: Dump.pm,v 1.3.2.10 2006/09/29 14:12:15 gomor Exp $ | ||
| 3 | # | ||
| 4 | package Net::Packet::Dump; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | require Class::Gomor::Array; | ||
| 10 | our @ISA = qw(Class::Gomor::Array); | ||
| 11 | |||
| 12 | use Net::Packet::Env qw($Env); | ||
| 13 | require Net::Packet::Frame; | ||
| 14 | use Net::Packet::Utils qw(getRandom32bitsInt); | ||
| 15 | use Net::Packet::Consts qw(:dump); | ||
| 16 | |||
| 17 | use Net::Pcap; | ||
| 18 | use Time::HiRes qw(gettimeofday); | ||
| 19 | use Storable qw(lock_store lock_retrieve); | ||
| 20 | |||
| 21 | our @AS = qw( | ||
| 22 | dev | ||
| 23 | env | ||
| 24 | file | ||
| 25 | filter | ||
| 26 | overwrite | ||
| 27 | timeoutOnNext | ||
| 28 | timeout | ||
| 29 | promisc | ||
| 30 | link | ||
| 31 | nextFrame | ||
| 32 | isRunning | ||
| 33 | unlinkOnClean | ||
| 34 | noStore | ||
| 35 | noLayerWipe | ||
| 36 | mode | ||
| 37 | _pid | ||
| 38 | _pcapd | ||
| 39 | _dumper | ||
| 40 | _stats | ||
| 41 | _firstTime | ||
| 42 | _sName | ||
| 43 | _sDataAwaiting | ||
| 44 | ); | ||
| 45 | our @AA = qw( | ||
| 46 | frames | ||
| 47 | ); | ||
| 48 | our @AO = qw( | ||
| 49 | framesSorted | ||
| 50 | ); | ||
| 51 | __PACKAGE__->cgBuildIndices; | ||
| 52 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 53 | __PACKAGE__->cgBuildAccessorsArray(\@AA); | ||
| 54 | |||
| 55 | no strict 'vars'; | ||
| 56 | |||
| 57 | BEGIN { | ||
| 58 | my $osname = { | ||
| 59 | cygwin => \&_killTcpdumpWin32, | ||
| 60 | MSWin32 => \&_killTcpdumpWin32, | ||
| 61 | }; | ||
| 62 | |||
| 63 | *_killTcpdump = $osname->{$^O} || \&_killTcpdumpOther; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub new { | ||
| 67 | my $self = shift->SUPER::new( | ||
| 68 | dev => $Env->dev, | ||
| 69 | env => $Env, | ||
| 70 | file => "netpacket-tmp-$$.@{[getRandom32bitsInt()]}.pcap", | ||
| 71 | filter => '', | ||
| 72 | overwrite => 0, | ||
| 73 | timeout => 0, | ||
| 74 | promisc => 0, | ||
| 75 | timeoutOnNext => 3, | ||
| 76 | isRunning => 0, | ||
| 77 | unlinkOnClean => 1, | ||
| 78 | noStore => 0, | ||
| 79 | noLayerWipe => 0, | ||
| 80 | framesSorted => {}, | ||
| 81 | frames => [], | ||
| 82 | mode => NP_DUMP_MODE_ONLINE, | ||
| 83 | _sDataAwaiting => 0, | ||
| 84 | _sName => "netpacket-tmp-$$.@{[getRandom32bitsInt()]}.storable", | ||
| 85 | @_, | ||
| 86 | ); | ||
| 87 | |||
| 88 | unless ($self->[$__file]) { | ||
| 89 | confess("You MUST set `file' attribute\n"); | ||
| 90 | } | ||
| 91 | |||
| 92 | $Env->dump($self) unless $Env->noDumpAutoSet; | ||
| 93 | |||
| 94 | $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub isModeOnline { shift->[$__mode] eq NP_DUMP_MODE_ONLINE } | ||
| 98 | sub isModeOffline { shift->[$__mode] eq NP_DUMP_MODE_OFFLINE } | ||
| 99 | sub isModeWriter { shift->[$__mode] eq NP_DUMP_MODE_WRITER } | ||
| 100 | |||
| 101 | sub start { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | $self->cgDebugPrint(1, 'will run in mode: '.$self->mode); | ||
| 105 | |||
| 106 | $self->[$__isRunning] = 1; | ||
| 107 | |||
| 108 | if ($self->isModeOnline) { | ||
| 109 | if (-f $self->[$__file] && ! $self->[$__overwrite]) { | ||
| 110 | croak("We will not overwrite a file by default. Use `overwrite' ". | ||
| 111 | "attribute to do it\n"); | ||
| 112 | } | ||
| 113 | $self->_sStore(0); | ||
| 114 | $self->_waitFileSize($self->[$___sName]); | ||
| 115 | $self->_startTcpdump; | ||
| 116 | $self->_openFileOffline; | ||
| 117 | } | ||
| 118 | elsif ($self->isModeOffline) { | ||
| 119 | if (! -f $self->[$__file]) { | ||
| 120 | croak("File does not exists: ".$self->[$__file]."\n"); | ||
| 121 | } | ||
| 122 | $self->_openFileOffline; | ||
| 123 | $self->_setFilter; | ||
| 124 | } | ||
| 125 | elsif ($self->isModeWriter) { | ||
| 126 | if (-f $self->[$__file] && ! $self->[$__overwrite]) { | ||
| 127 | croak("We will not overwrite a file by default. Use `overwrite' ". | ||
| 128 | "attribute to do it\n"); | ||
| 129 | } | ||
| 130 | $self->_openFileWriter; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub stop { | ||
| 137 | my $self = shift; | ||
| 138 | |||
| 139 | return unless $self->[$__isRunning]; | ||
| 140 | return if $self->isSon; | ||
| 141 | |||
| 142 | if ($self->isModeOnline) { | ||
| 143 | $self->_killTcpdump; | ||
| 144 | $self->[$___pid] = undef; | ||
| 145 | if ($self->[$___sName] && -f $self->[$___sName]) { | ||
| 146 | unlink($self->[$___sName]); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | elsif ($self->isModeWriter) { | ||
| 150 | Net::Pcap::dump_close($self->[$___dumper]); | ||
| 151 | } | ||
| 152 | elsif ($self->isModeOffline) { | ||
| 153 | # Nothing to do here | ||
| 154 | } | ||
| 155 | |||
| 156 | Net::Pcap::close($self->[$___pcapd]); | ||
| 157 | $self->[$__isRunning] = 0; | ||
| 158 | |||
| 159 | 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub isFather { shift->[$___pid] } | ||
| 163 | sub isSon { ! shift->[$___pid] } | ||
| 164 | |||
| 165 | sub _sStore { | ||
| 166 | lock_store(\$_[1], $_[0]->[$___sName]) | ||
| 167 | or carp("@{[(caller(0))[3]]}: lock_store: @{[$_[0]->[$___sName]]}: $!\n"); | ||
| 168 | } | ||
| 169 | sub _sRetrieve { ${lock_retrieve(shift->[$___sName])} } | ||
| 170 | |||
| 171 | sub _sonPrintStats { | ||
| 172 | my $self = shift; | ||
| 173 | |||
| 174 | my $stats = $self->getStats; | ||
| 175 | Net::Pcap::breakloop($self->[$___pcapd]); | ||
| 176 | Net::Pcap::close($self->[$___pcapd]); | ||
| 177 | |||
| 178 | $self->cgDebugPrint(1, 'Frames received : '.$stats->{ps_recv}); | ||
| 179 | $self->cgDebugPrint(1, 'Frames dropped : '.$stats->{ps_drop}); | ||
| 180 | $self->cgDebugPrint(1, 'Frames if dropped: '.$stats->{ps_ifdrop}); | ||
| 181 | exit(0); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _waitFile { | ||
| 185 | my $self = shift; | ||
| 186 | my ($file) = @_; | ||
| 187 | my $startTime = gettimeofday(); | ||
| 188 | my $thisTime = $startTime; | ||
| 189 | while (! -f $file) { | ||
| 190 | if ($thisTime - $startTime > 10) { | ||
| 191 | croak("@{[(caller(0))[3]]}: too long for file creation: $file\n") | ||
| 192 | } | ||
| 193 | $thisTime = gettimeofday(); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _waitFileSize { | ||
| 198 | my $self = shift; | ||
| 199 | my ($file) = @_; | ||
| 200 | |||
| 201 | $self->_waitFile($file); | ||
| 202 | |||
| 203 | my $startTime = gettimeofday(); | ||
| 204 | my $thisTime = $startTime; | ||
| 205 | while (! ((stat($file))[7] > 0)) { | ||
| 206 | if ($thisTime - $startTime > 10) { | ||
| 207 | $self->clean; | ||
| 208 | croak("@{[(caller(0))[3]]}: too long for file creation2: $file\n") | ||
| 209 | } | ||
| 210 | $thisTime = gettimeofday(); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | sub _startTcpdump { | ||
| 215 | my $self = shift; | ||
| 216 | |||
| 217 | my $err; | ||
| 218 | my $pd = Net::Pcap::open_live( | ||
| 219 | $self->[$__dev], | ||
| 220 | 1514, | ||
| 221 | $self->[$__promisc], | ||
| 222 | 1000, | ||
| 223 | \$err, | ||
| 224 | ); | ||
| 225 | unless ($pd) { | ||
| 226 | croak("@{[(caller(0))[3]]}: open_live: $err\n"); | ||
| 227 | } | ||
| 228 | |||
| 229 | my $net = 0; | ||
| 230 | my $mask = 0; | ||
| 231 | Net::Pcap::lookupnet($self->[$__dev], \$net, \$mask, \$err); | ||
| 232 | if ($err) { | ||
| 233 | carp("@{[(caller(0))[3]]}: lookupnet: $err\n"); | ||
| 234 | } | ||
| 235 | |||
| 236 | my $fcode; | ||
| 237 | if (Net::Pcap::compile($pd, \$fcode, $self->[$__filter], 0, $mask) < 0) { | ||
| 238 | croak("@{[(caller(0))[3]]}: compile: ". Net::Pcap::geterr($pd). "\n"); | ||
| 239 | } | ||
| 240 | |||
| 241 | if (Net::Pcap::setfilter($pd, $fcode) < 0) { | ||
| 242 | croak("@{[(caller(0))[3]]}: setfilter: ". Net::Pcap::geterr($pd). "\n"); | ||
| 243 | } | ||
| 244 | |||
| 245 | my $p = Net::Pcap::dump_open($pd, $self->[$__file]); | ||
| 246 | unless ($p) { | ||
| 247 | croak("@{[(caller(0))[3]]}: dump_open: ". Net::Pcap::geterr($pd). "\n"); | ||
| 248 | } | ||
| 249 | Net::Pcap::dump_flush($p); | ||
| 250 | |||
| 251 | $SIG{CHLD} = 'IGNORE'; | ||
| 252 | |||
| 253 | my $pid = fork(); | ||
| 254 | croak("@{[(caller(0))[3]]}: fork: $!\n") unless defined $pid; | ||
| 255 | if ($pid) { | ||
| 256 | $self->[$___pid] = $pid; | ||
| 257 | return 1; | ||
| 258 | } | ||
| 259 | else { | ||
| 260 | $self->[$___pcapd] = $pd; | ||
| 261 | $SIG{INT} = sub { $self->_sonPrintStats }; | ||
| 262 | $SIG{TERM} = sub { $self->_sonPrintStats }; | ||
| 263 | $self->cgDebugPrint(1, "dev: [@{[$self->[$__dev]]}]\n". | ||
| 264 | "file: [@{[$self->[$__file]]}]\n". | ||
| 265 | "filter: [@{[$self->[$__filter]]}]"); | ||
| 266 | Net::Pcap::loop($pd, -1, \&_tcpdumpCallback, [ $p, $self ]); | ||
| 267 | Net::Pcap::close($pd); | ||
| 268 | exit(0); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | sub _tcpdumpCallback { | ||
| 273 | my ($data, $hdr, $pkt) = @_; | ||
| 274 | my $p = $data->[0]; | ||
| 275 | my $self = $data->[1]; | ||
| 276 | |||
| 277 | Net::Pcap::dump($p, $hdr, $pkt); | ||
| 278 | Net::Pcap::dump_flush($p); | ||
| 279 | |||
| 280 | my $n = $self->_sRetrieve; | ||
| 281 | $self->_sStore(++$n); | ||
| 282 | } | ||
| 283 | |||
| 284 | sub _killTcpdumpWin32 { | ||
| 285 | my $self = shift; | ||
| 286 | return unless $self->[$___pid]; | ||
| 287 | kill('KILL', $self->[$___pid]); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub _killTcpdumpOther { | ||
| 291 | my $self = shift; | ||
| 292 | return unless $self->[$___pid]; | ||
| 293 | kill('TERM', $self->[$___pid]); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub clean { | ||
| 297 | my $self = shift; | ||
| 298 | |||
| 299 | if ($self->isModeOnline) { | ||
| 300 | if ($self->[$__unlinkOnClean] | ||
| 301 | && $self->[$__file] && -f $self->[$__file]) { | ||
| 302 | unlink($self->[$__file]); | ||
| 303 | $self->cgDebugPrint(1, "@{[$self->[$__file]]} removed"); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | 1; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub getStats { | ||
| 311 | my $self = shift; | ||
| 312 | |||
| 313 | unless ($self->[$___pcapd]) { | ||
| 314 | carp("@{[(caller(0))[3]]}: unable to get stats, no pcap descriptor ". | ||
| 315 | "opened\n"); | ||
| 316 | return undef; | ||
| 317 | } | ||
| 318 | |||
| 319 | my %stats; | ||
| 320 | Net::Pcap::stats($self->[$___pcapd], \%stats); | ||
| 321 | $self->[$___stats] = \%stats; | ||
| 322 | \%stats; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub flush { | ||
| 326 | my $self = shift; | ||
| 327 | $self->[$__frames] = []; | ||
| 328 | $self->[$__framesSorted] = {}; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub _setFilter { | ||
| 332 | my $self = shift; | ||
| 333 | my $str = $self->[$__filter]; | ||
| 334 | |||
| 335 | return unless $str; | ||
| 336 | |||
| 337 | my ($net, $mask, $err); | ||
| 338 | Net::Pcap::lookupnet($self->[$__dev], \$net, \$mask, \$err); | ||
| 339 | if ($err) { | ||
| 340 | croak("@{[(caller(0))[3]]}: Net::Pcap::lookupnet: @{[$self->[$__dev]]}: ". | ||
| 341 | "$err\n"); | ||
| 342 | } | ||
| 343 | |||
| 344 | my $filter; | ||
| 345 | Net::Pcap::compile($self->[$___pcapd], \$filter, $str, 0, | ||
| 346 | $mask); | ||
| 347 | unless ($filter) { | ||
| 348 | croak("@{[(caller(0))[3]]}: Net::Pcap::compile: error\n"); | ||
| 349 | } | ||
| 350 | |||
| 351 | Net::Pcap::setfilter($self->[$___pcapd], $filter); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub _openFileOffline { | ||
| 355 | my $self = shift; | ||
| 356 | |||
| 357 | my $err; | ||
| 358 | $self->[$___pcapd] = Net::Pcap::open_offline($self->[$__file], \$err); | ||
| 359 | unless ($self->[$___pcapd]) { | ||
| 360 | croak("@{[(caller(0))[3]]}: Net::Pcap::open_offline: ". | ||
| 361 | "@{[$self->[$__file]]}: $err\n"); | ||
| 362 | } | ||
| 363 | |||
| 364 | $self->[$__link] = Net::Pcap::datalink($self->[$___pcapd]); | ||
| 365 | } | ||
| 366 | |||
| 367 | sub _getPcapHeader { | ||
| 368 | my $self = shift; | ||
| 369 | # 24 bytes header of a DLT_RAW pcap file | ||
| 370 | "\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00". | ||
| 371 | "\x00\x00\x00\x00\xdc\x05\x00\x00\x0c\x00\x00\x00"; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub _openFileWriter { | ||
| 375 | my $self = shift; | ||
| 376 | my $file = $self->[$__file]; | ||
| 377 | |||
| 378 | my $hdr = $self->_getPcapHeader; | ||
| 379 | open(my $fh, '>', $file) | ||
| 380 | or croak("@{[(caller(0))[3]]}: open: $file: $!\n"); | ||
| 381 | syswrite($fh, $hdr, length($hdr)); | ||
| 382 | close($fh); | ||
| 383 | |||
| 384 | my $err; | ||
| 385 | my $pcapd = Net::Pcap::open_offline($file, \$err); | ||
| 386 | unless ($pcapd) { | ||
| 387 | croak("@{[(caller(0))[3]]}: Net::Pcap::open_offline: ". | ||
| 388 | "$file: $err\n"); | ||
| 389 | } | ||
| 390 | $self->[$___pcapd] = $pcapd; | ||
| 391 | |||
| 392 | $self->[$___dumper] = Net::Pcap::dump_open($pcapd, $file); | ||
| 393 | unless ($self->[$___dumper]) { | ||
| 394 | croak("@{[(caller(0))[3]]}: Net::Pcap::dump_open: ". | ||
| 395 | Net::Pcap::geterr($pcapd)."\n"); | ||
| 396 | } | ||
| 397 | |||
| 398 | 1; | ||
| 399 | } | ||
| 400 | |||
| 401 | sub _addToFramesSorted { | ||
| 402 | my $self = shift; | ||
| 403 | my ($frame) = @_; | ||
| 404 | $self->framesSorted($frame); | ||
| 405 | push @{$self->[$__frames]}, $frame; | ||
| 406 | } | ||
| 407 | |||
| 408 | sub _pcapNext { | ||
| 409 | my $self = shift; | ||
| 410 | my %hdr; | ||
| 411 | if (my $raw = Net::Pcap::next($self->[$___pcapd], \%hdr)) { | ||
| 412 | $hdr{tv_usec} = sprintf("%06d", $hdr{tv_usec}); | ||
| 413 | my $frame = Net::Packet::Frame->new( | ||
| 414 | env => $self->env, | ||
| 415 | raw => $raw, | ||
| 416 | timestamp => "$hdr{tv_sec}.$hdr{tv_usec}", | ||
| 417 | ) or return undef; | ||
| 418 | |||
| 419 | $self->_addToFramesSorted($frame) unless $self->[$__noStore]; | ||
| 420 | return $frame; | ||
| 421 | } | ||
| 422 | undef; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub _getNextAwaitingFrameOffline { | ||
| 426 | my $self = shift; | ||
| 427 | $self->_pcapNext; | ||
| 428 | } | ||
| 429 | |||
| 430 | sub _getNextAwaitingFrameOnline { | ||
| 431 | my $self = shift; | ||
| 432 | my $last = $self->[$___sDataAwaiting]; | ||
| 433 | my $new = $self->_sRetrieve; | ||
| 434 | |||
| 435 | # Return if nothing new is awaiting | ||
| 436 | return undef if ($new <= $last); | ||
| 437 | |||
| 438 | $self->[$___sDataAwaiting]++; | ||
| 439 | $self->_pcapNext; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub _getNextAwaitingFrame { | ||
| 443 | my $self = shift; | ||
| 444 | $self->isModeOnline ? $self->_getNextAwaitingFrameOnline | ||
| 445 | : $self->_getNextAwaitingFrameOffline; | ||
| 446 | } | ||
| 447 | |||
| 448 | # XXX: need more work | ||
| 449 | #sub _getNextAwaitingFrames { | ||
| 450 | #my $self = shift; | ||
| 451 | #my $last = $self->[$___sDataAwaiting]; | ||
| 452 | #my $new = $self->_sRetrieve; | ||
| 453 | #my $diff = $new - $last; | ||
| 454 | #return [] if $diff <= 0; # Nothing awaiting | ||
| 455 | #$self->[$___sDataAwaiting] += $diff; | ||
| 456 | #my $frames = []; | ||
| 457 | #while ($diff--) { | ||
| 458 | #push @$frames, $self->_pcapNext; | ||
| 459 | #} | ||
| 460 | #$frames; | ||
| 461 | #} | ||
| 462 | |||
| 463 | sub _nextTimeoutHandle { | ||
| 464 | my $self = shift; | ||
| 465 | |||
| 466 | # Handle timeout | ||
| 467 | my $thisTime = gettimeofday() if $self->[$__timeoutOnNext]; | ||
| 468 | $self->[$___firstTime] = $thisTime unless $self->[$___firstTime]; | ||
| 469 | |||
| 470 | if ($self->[$__timeoutOnNext] && $self->[$___firstTime]) { | ||
| 471 | if (($thisTime - $self->[$___firstTime]) > $self->[$__timeoutOnNext]) { | ||
| 472 | $self->[$__timeout] = 1; | ||
| 473 | $self->[$___firstTime] = 0; | ||
| 474 | $self->cgDebugPrint(1, "Timeout occured"); | ||
| 475 | return undef; | ||
| 476 | } | ||
| 477 | } | ||
| 478 | 1; | ||
| 479 | } | ||
| 480 | |||
| 481 | sub _nextTimeoutReset { shift->[$___firstTime] = 0 } | ||
| 482 | |||
| 483 | sub next { | ||
| 484 | my $self = shift; | ||
| 485 | |||
| 486 | unless ($self->[$__isRunning]) { | ||
| 487 | croak("You MUST call start() method before using next() method\n"); | ||
| 488 | } | ||
| 489 | |||
| 490 | $self->_nextTimeoutHandle or return undef; | ||
| 491 | |||
| 492 | my $frame = $self->_getNextAwaitingFrame; | ||
| 493 | $self->_nextTimeoutReset if $frame; | ||
| 494 | |||
| 495 | $frame ? do { $self->[$__nextFrame] = $frame } : undef; | ||
| 496 | } | ||
| 497 | |||
| 498 | sub nextAll { my $self = shift; while ($self->next) {} } | ||
| 499 | |||
| 500 | sub write { | ||
| 501 | my $self = shift; | ||
| 502 | my ($frame) = @_; | ||
| 503 | |||
| 504 | unless ($self->isModeWriter) { | ||
| 505 | croak("Dump is not in writer mode\n"); | ||
| 506 | } | ||
| 507 | |||
| 508 | # Rebuild the frame without possible layer 2 | ||
| 509 | my $new; | ||
| 510 | $new .= $frame->l3->raw if $frame->l3; | ||
| 511 | $new .= $frame->l4->raw if $frame->l4; | ||
| 512 | $new .= $frame->l7->raw if $frame->l7; | ||
| 513 | |||
| 514 | # Create pcap header | ||
| 515 | my ($sec, $usec) = split('\.', $frame->timestamp); | ||
| 516 | my $hdr = { | ||
| 517 | len => length($new), | ||
| 518 | caplen => length($new), | ||
| 519 | tv_sec => $sec, | ||
| 520 | tv_usec => $usec, | ||
| 521 | }; | ||
| 522 | |||
| 523 | Net::Pcap::pcap_dump($self->[$___dumper], $hdr, $new); | ||
| 524 | Net::Pcap::dump_flush($self->[$___dumper]); | ||
| 525 | } | ||
| 526 | |||
| 527 | # XXX: broken for now | ||
| 528 | #sub nextAll { | ||
| 529 | #my $self = shift; | ||
| 530 | #$self->_nextTimeoutHandle or return []; | ||
| 531 | |||
| 532 | #my $frames = $self->_getNextAwaitingFrames; | ||
| 533 | #$self->_nextTimeoutReset if @$frames; | ||
| 534 | #$frames; | ||
| 535 | #} | ||
| 536 | |||
| 537 | sub timeoutReset { shift->[$__timeout] = 0 } | ||
| 538 | |||
| 539 | sub framesFor { | ||
| 540 | my $self = shift; | ||
| 541 | my ($f) = @_; | ||
| 542 | |||
| 543 | my $l2Key = ($f->l2 && $f->l2->getKeyReverse($f)) || 'all'; | ||
| 544 | my $l3Key = ($f->l3 && $f->l3->getKeyReverse($f)) || 'all'; | ||
| 545 | my $l4Key = ($f->l4 && $f->l4->getKeyReverse($f)) || 'all'; | ||
| 546 | my $aref = $self->[$__framesSorted]->{$l2Key}{$l3Key}{$l4Key}; | ||
| 547 | |||
| 548 | $aref ? @$aref : (); | ||
| 549 | } | ||
| 550 | |||
| 551 | # | ||
| 552 | # Other accessors | ||
| 553 | # | ||
| 554 | |||
| 555 | sub framesSorted { | ||
| 556 | my $self = shift; | ||
| 557 | my ($f) = @_; | ||
| 558 | |||
| 559 | if ($f) { | ||
| 560 | # Wipe headers, since if not, framesFor() will not be able to find them. | ||
| 561 | # Because if you create a Frame from L3, no headers are set for L2, but | ||
| 562 | # the Dump will have them and store them into the l2Key. | ||
| 563 | if ($self->env->desc && ! $self->[$__noLayerWipe]) { | ||
| 564 | $f->l2(undef) if ref($self->env->desc) =~ /L3|L4/; | ||
| 565 | $f->l3(undef) if ref($self->env->desc) =~ /L4/; | ||
| 566 | } | ||
| 567 | |||
| 568 | my $l2Key = ($f->l2 && $f->l2->getKey($f)) || 'all'; | ||
| 569 | my $l3Key = ($f->l3 && $f->l3->getKey($f)) || 'all'; | ||
| 570 | my $l4Key = ($f->l4 && $f->l4->getKey($f)) || 'all'; | ||
| 571 | push @{$self->[$__framesSorted]->{$l2Key}{$l3Key}{$l4Key}}, $f; | ||
| 572 | |||
| 573 | # We store a second time for ICMP messages | ||
| 574 | if ($f->isIcmp) { | ||
| 575 | my $l3Key = ($f->l3 && $f->l3->is.':'.$f->l3->dst) || 'all'; | ||
| 576 | push @{$self->[$__framesSorted]->{$l2Key}{$l3Key}{$l4Key}}, $f; | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | $self->[$__framesSorted]; | ||
| 581 | } | ||
| 582 | |||
| 583 | 1; | ||
| 584 | |||
| 585 | __END__ |