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