private static Packet GetNextPacket() { if (_currentFapi != null) { Log.DebugFormat("currentFAPI.PacketIndexes.Count {0}," + "currentPacketIndex {1}", _currentFapi.PacketIndexes.Count, _currentPacketIndex); } else { Log.Debug("currentFAPI is null"); } // do we need to open a file up or are we done with the current file? if ((_packetFileName == null) || (_currentFapi == null) || (_currentFapi.PacketIndexes.Count == _currentPacketIndex)) { Log.Debug("opening a new file up"); // close the open device if there was one if (_captureFileReader != null) { _captureFileReader.Close(); _captureFileReader = null; } // do we have any more files to process? if (_fileAndPacketIndex >= FilePacketIndexes.Count) { Log.DebugFormat("totalPacketsReturned {0}, expectedTotalPackets {1}", _totalPacketsReturned, ExpectedTotalPackets); Assert.AreEqual(ExpectedTotalPackets, _totalPacketsReturned, "expectedTotalPackets does not match totalPacketsReturned"); return(null); } _currentFapi = FilePacketIndexes[_fileAndPacketIndex]; _currentPacketIndex = 0; _packetFileName = _currentFapi.Filename; // opening a new file, we are at the first index into the new file _indexIntoPacketFile = 0; try { Log.DebugFormat("Opening {0}", _currentFapi.Filename); _captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(_currentFapi.Filename); _captureFileReader.Open(); _fileAndPacketIndex++; } catch (Exception e) { Log.Error("caught exception", e); throw; } } Packet p; do { Log.DebugFormat("currentPacketIndex {0}", _currentPacketIndex); Log.DebugFormat("indexIntoPacketFile {0}, currentFAPI.PacketIndexes[currentPacketIndex] {1}", _indexIntoPacketFile, _currentFapi.PacketIndexes[_currentPacketIndex]); Log.Debug("retrieving packet"); // read the next packet var packet = _captureFileReader.GetNextPacket(); Assert.IsNotNull(packet, "Expected a valid packet but it was null"); p = Packet.ParsePacket(packet.LinkLayerType, packet.Data); _currentPacketDescription = _currentFapi.PacketDescription[_currentPacketIndex]; // advance our index into the current packet file _indexIntoPacketFile++; }while ((_indexIntoPacketFile - 1) != _currentFapi.PacketIndexes[_currentPacketIndex]); // does the current index match the index of the packet we want? // and because we got a packet we advance our index into the FileAndPacketIndex class _currentPacketIndex++; Log.Debug("returning packet"); _totalPacketsReturned++; return(p); }
private static Packet GetNextPacket() { if(currentFAPI != null) { log.DebugFormat("currentFAPI.PacketIndexes.Count {0}," + "currentPacketIndex {1}", currentFAPI.PacketIndexes.Count, currentPacketIndex); } else { log.Debug("currentFAPI is null"); } // do we need to open a file up or are we done with the current file? if((packetFileName == null) || (currentFAPI == null) || (currentFAPI.PacketIndexes.Count == currentPacketIndex)) { log.Debug("opening a new file up"); // close the open device if there was one if(captureFileReader != null) { captureFileReader.Close(); captureFileReader = null; } // do we have any more files to process? if(fileAndPacketIndex >= fileAndPacketIndexes.Count) { log.DebugFormat("totalPacketsReturned {0}, expectedTotalPackets {1}", totalPacketsReturned, expectedTotalPackets); Assert.AreEqual(expectedTotalPackets, totalPacketsReturned, "expectedTotalPackets does not match totalPacketsReturned"); return null; } else { currentFAPI = fileAndPacketIndexes[fileAndPacketIndex]; currentPacketIndex = 0; packetFileName = currentFAPI.Filename; // opening a new file, we are at the first index into the new file indexIntoPacketFile = 0; try { log.DebugFormat("Opening {0}", currentFAPI.Filename); captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(currentFAPI.Filename); captureFileReader.Open(); fileAndPacketIndex++; } catch(System.Exception e) { log.Error("caught exception",e); throw; } } } Packet p = null; do { log.DebugFormat("currentPacketIndex {0}", currentPacketIndex); log.DebugFormat("indexIntoPacketFile {0}, currentFAPI.PacketIndexes[currentPacketIndex] {1}", indexIntoPacketFile, currentFAPI.PacketIndexes[currentPacketIndex]); log.Debug("retrieving packet"); // read the next packet var packet = captureFileReader.GetNextPacket(); Assert.IsNotNull(packet, "Expected a valid packet but it was null"); p = Packet.ParsePacket(packet.LinkLayerType, packet.Data); currentPacketDescription = currentFAPI.PacketDescription[currentPacketIndex]; // advance our index into the current packet file indexIntoPacketFile++; } while((indexIntoPacketFile -1) != currentFAPI.PacketIndexes[currentPacketIndex]); // does the current index match the index of the packet we want? // and because we got a packet we advance our index into the FileAndPacketIndex class currentPacketIndex++; log.Debug("returning packet"); totalPacketsReturned++; return p; }