public unsafe void BenchmarkICaptureDeviceUnsafe() { int packetsRead = 0; var startTime = DateTime.Now; while (packetsRead < packetsToRead) { ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice(TestHelper.GetFile("10k_packets.pcap")); captureDevice.Open(); RawCapture rawCapture = null; do { rawCapture = captureDevice.GetNextPacket(); packetsRead++; }while (rawCapture != null); captureDevice.Close(); } var endTime = DateTime.Now; var rate = new Rate(startTime, endTime, packetsRead, "packets captured"); Console.WriteLine("{0}", rate.ToString()); }
public unsafe void BenchmarkICaptureDeviceUnsafe() { int packetsRead = 0; var startTime = DateTime.Now; while(packetsRead < packetsToRead) { ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice("../../capture_files/10k_packets.pcap"); captureDevice.Open(); RawCapture rawCapture = null; do { rawCapture = captureDevice.GetNextPacket(); packetsRead++; } while(rawCapture != null); captureDevice.Close(); } var endTime = DateTime.Now; var rate = new Rate(startTime, endTime, packetsRead, "packets captured"); Console.WriteLine("{0}", rate.ToString()); }
public void BenchmarkICaptureDevice() { int packetsRead = 0; var startTime = DateTime.Now; while (packetsRead < packetsToRead) { ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice(TestHelper.GetFile("10k_packets.pcap")); captureDevice.Open(); RawCapture rawCapture = null; do { rawCapture = captureDevice.GetNextPacket(); // Parse the packet using PacketDotNet if (rawCapture != null) { Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data); } packetsRead++; }while (rawCapture != null); captureDevice.Close(); } var endTime = DateTime.Now; var rate = new Rate(startTime, endTime, packetsRead, "packets parsed"); Console.WriteLine("{0}", rate.ToString()); }
void openfile(string capFile) { try { this.device.StopCapture(); this.device.Close(); } catch (Exception) { ; } this.packets = new ArrayList(); this.dataGridView1.Rows.Clear(); SharpPcap.LibPcap.CaptureFileReaderDevice captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(capFile); SharpPcap.RawCapture pPacket; int indx = 0; // Go through all packets in the file while ((pPacket = captureFileReader.GetNextPacket()) != null) { try { packet temp = new packet(pPacket); temp.index = indx; indx++; this.packets.Add(temp); // if (filter_check(temp)) // { if (this.dataGridView1.InvokeRequired) { this.dataGridView1.BeginInvoke(new setDataGridViewDelegate(setDataGridView), new object[] { temp }); } else { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.FromName(temp.color); this.dataGridView1.Rows[index].Cells[0].Value = temp.time; this.dataGridView1.Rows[index].Cells[1].Value = temp.srcIp; this.dataGridView1.Rows[index].Cells[2].Value = temp.destIp; this.dataGridView1.Rows[index].Cells[3].Value = temp.protocol; this.dataGridView1.Rows[index].Cells[4].Value = temp.info; this.dataGridView1.Rows[index].Cells[5].Value = temp.index; this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1; } // } } catch (Exception) { ; } } //this.is_saved = true; captureFileReader.Close(); MessageBox.Show("读取完毕"); }
public void BenchmarkICaptureDevice() { int packetsRead = 0; var startTime = DateTime.Now; while(packetsRead < packetsToRead) { ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice("../../capture_files/10k_packets.pcap"); captureDevice.Open(); RawCapture rawCapture = null; do { rawCapture = captureDevice.GetNextPacket(); // Parse the packet using PacketDotNet if(rawCapture != null) Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data); packetsRead++; } while(rawCapture != null); captureDevice.Close(); } var endTime = DateTime.Now; var rate = new Rate(startTime, endTime, packetsRead, "packets parsed"); Console.WriteLine("{0}", rate.ToString()); }
public void PrintString() { Console.WriteLine("Loading the sample capture file"); var dev = new SharpPcap.LibPcap.CaptureFileReaderDevice(NUnitSetupClass.CaptureDirectory + "arp_request_response.pcap"); dev.Open(); Console.WriteLine("Reading packet data"); dev.GetNextPacket(); var rawPacket = dev.GetNextPacket(); dev.Close(); Console.WriteLine("Printing human readable string"); Console.WriteLine(rawPacket.ToString()); }
private void button5_Click(object sender, EventArgs e) { if (this.is_saved == true || MessageBox.Show("不保存并读取文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { try { this.device.StopCapture(); this.device.Close(); } catch (Exception) { ; } string capFile = ""; OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates); ofd.Filter = "PCAP(*.pcap)|*.pcap"; ofd.ValidateNames = true; ofd.CheckFileExists = true; ofd.CheckPathExists = true; if (ofd.ShowDialog() == DialogResult.OK) { this.packets = new ArrayList(); this.dataGridView1.Rows.Clear(); capFile = ofd.FileName; SharpPcap.LibPcap.CaptureFileReaderDevice captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(capFile); SharpPcap.RawCapture pPacket; // Go through all packets in the file while ((pPacket = captureFileReader.GetNextPacket()) != null) { try { packet temp = new packet(pPacket); this.packets.Add(temp); if (filter_check(temp)) { if (this.dataGridView1.InvokeRequired) { this.dataGridView1.BeginInvoke(new setDataGridViewDelegate(setDataGridView), new object[] { temp, this.packets.Count - 1 }); } else { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.FromName(temp.color); this.dataGridView1.Rows[index].Cells[0].Value = temp.time; this.dataGridView1.Rows[index].Cells[1].Value = temp.srcIp; this.dataGridView1.Rows[index].Cells[2].Value = temp.destIp; this.dataGridView1.Rows[index].Cells[3].Value = temp.protocol; this.dataGridView1.Rows[index].Cells[4].Value = temp.info; this.dataGridView1.Rows[index].Cells[5].Value = packets.Count - 1; this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1; } } } catch (Exception) { ; } } this.is_saved = true; captureFileReader.Close(); MessageBox.Show("读取完毕"); } } }
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 void ui_populate_packet_view() { // There's a race condition here, but imo not worth changing the architecture to fix yet. if (cFileUtilities.get_size(_backend_file) == _backend_size) { return; } _backend_size = cFileUtilities.get_size(_backend_file); ICaptureDevice pcap_device = null; bool has_temp_file_open = false; string temp_file = ""; _packet_bytes.Clear(); try { temp_file = cFileUtilities.get_temp_copy(_backend_file); has_temp_file_open = true; pcap_device = new SharpPcap.LibPcap.CaptureFileReaderDevice(temp_file); pcap_device.Open(); RawCapture capture; int current_packet = 0; while ((capture = pcap_device.GetNextPacket()) != null) { var eth_packet = PacketDotNet.Packet.ParsePacket(capture.LinkLayerType, capture.Data); PacketDotNet.IPPacket ip_packet = eth_packet.Extract <PacketDotNet.IPPacket>(); if (_stream_type == "tcp") { PacketDotNet.TcpPacket tcp_packet = eth_packet.Extract <PacketDotNet.TcpPacket>(); _packet_bytes.Add(tcp_packet.PayloadData.ToList()); object packet_view_item = new { PacketNumber = current_packet.ToString(), Source = ip_packet.SourceAddress.ToString() + ":" + tcp_packet.SourcePort.ToString(), Destination = ip_packet.DestinationAddress.ToString() + ":" + tcp_packet.DestinationPort.ToString(), PayloadLength = tcp_packet.PayloadData.Length.ToString(), }; if (packet_stream_view.Items.Contains(packet_view_item) == false) { packet_stream_view.Items.Add(packet_view_item); } } else { PacketDotNet.UdpPacket udp_packet = eth_packet.Extract <PacketDotNet.UdpPacket>(); _packet_bytes.Add(udp_packet.PayloadData.ToList()); object packet_view_item = new { PacketNumber = current_packet.ToString(), Source = ip_packet.SourceAddress.ToString() + ":" + udp_packet.SourcePort.ToString(), Destination = ip_packet.DestinationAddress.ToString() + ":" + udp_packet.DestinationPort.ToString(), PayloadLength = udp_packet.PayloadData.Length.ToString(), }; if (packet_stream_view.Items.Contains(packet_view_item) == false) { packet_stream_view.Items.Add(packet_view_item); } } current_packet++; } } catch (Exception e) { if (has_temp_file_open == true) { cFileUtilities.remove_temp_copy(temp_file); has_temp_file_open = false; } MessageBox.Show("Error: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } if (pcap_device != null) { pcap_device.Close(); } if (has_temp_file_open == true) { cFileUtilities.remove_temp_copy(temp_file); has_temp_file_open = false; } }