private static void ComparePacketsToWireshark(IEnumerable<Packet> packets) { string pcapFilename = Path.GetTempPath() + "temp." + new Random().NextByte() + ".pcap"; #pragma warning disable 162 // ReSharper disable ConditionIsAlwaysTrueOrFalse // ReSharper disable HeuristicUnreachableCode if (!IsRetry) { PacketDumpFile.Dump(pcapFilename, new PcapDataLink(packets.First().DataLink.Kind), PacketDevice.DefaultSnapshotLength, packets); } else { pcapFilename = Path.GetTempPath() + "temp." + RetryNumber + ".pcap"; List<Packet> packetsList = new List<Packet>(); using (PacketCommunicator communicator = new OfflinePacketDevice(pcapFilename).Open()) { communicator.ReceivePackets(-1, packetsList.Add); } packets = packetsList; } // ReSharper restore HeuristicUnreachableCode // ReSharper restore ConditionIsAlwaysTrueOrFalse #pragma warning restore 162 // Create pdml file string documentFilename = pcapFilename + ".pdml"; using (Process process = new Process()) { process.StartInfo = new ProcessStartInfo { // Wireshark's preferences file is %APPDATA%\Wireshark\preferences FileName = WiresharkTsharkPath, Arguments = "-o ip.check_checksum:TRUE " + "-o ipv6.use_geoip:FALSE " + "-o udp.check_checksum:TRUE " + "-o tcp.relative_sequence_numbers:FALSE " + "-o tcp.analyze_sequence_numbers:FALSE " + "-o tcp.track_bytes_in_flight:FALSE " + "-o tcp.desegment_tcp_streams:FALSE " + "-o tcp.check_checksum:TRUE " + "-o http.dechunk_body:FALSE " + "-t r -n -r \"" + pcapFilename + "\" -T pdml", WorkingDirectory = WiresharkDiretory, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; Console.WriteLine("Starting process " + process.StartInfo.FileName + " " + process.StartInfo.Arguments); Assert.IsTrue(process.Start()); string output = process.StandardOutput.ReadToEnd(); string errorOutput = process.StandardError.ReadToEnd(); process.WaitForExit(); Console.WriteLine(errorOutput); File.WriteAllText(documentFilename, output); Assert.AreEqual(0, process.ExitCode); } // Fix pdml file string fixedDocumentFilename = documentFilename + ".fixed"; File.WriteAllBytes(fixedDocumentFilename, new List<byte>(from b in File.ReadAllBytes(documentFilename) select b == 0x0A || b == 0x0D ? b : Math.Max((byte)0x20, Math.Min(b, (byte)0x7F))).ToArray()); try { Compare(XDocument.Load(fixedDocumentFilename, LoadOptions.None), packets); } catch (AssertFailedException exception) { throw new AssertFailedException("Failed comparing packets in file " + pcapFilename + ". Message: " + exception.Message, exception); } }
private static void ComparePacketsToWireshark(IEnumerable <Packet> packets) { string pcapFilename = Path.GetTempPath() + "temp." + new Random().NextByte() + ".pcap"; #pragma warning disable 162 // ReSharper disable ConditionIsAlwaysTrueOrFalse // ReSharper disable HeuristicUnreachableCode if (!IsRetry) { PacketDumpFile.Dump(pcapFilename, new PcapDataLink(packets.First().DataLink.Kind), PacketDevice.DefaultSnapshotLength, packets); } else { pcapFilename = Path.GetTempPath() + "temp." + RetryNumber + ".pcap"; List <Packet> packetsList = new List <Packet>(); using (PacketCommunicator communicator = new OfflinePacketDevice(pcapFilename).Open()) { communicator.ReceivePackets(-1, packetsList.Add); } packets = packetsList; } // ReSharper restore HeuristicUnreachableCode // ReSharper restore ConditionIsAlwaysTrueOrFalse #pragma warning restore 162 // Create pdml file string documentFilename = pcapFilename + ".pdml"; using (Process process = new Process()) { process.StartInfo = new ProcessStartInfo { // Wireshark's preferences file is %APPDATA%\Wireshark\preferences FileName = WiresharkTsharkPath, Arguments = "-o ip.check_checksum:TRUE " + "-o ipv6.use_geoip:FALSE " + "-o udp.check_checksum:TRUE " + "-o tcp.relative_sequence_numbers:FALSE " + "-o tcp.analyze_sequence_numbers:FALSE " + "-o tcp.track_bytes_in_flight:FALSE " + "-o tcp.desegment_tcp_streams:FALSE " + "-o tcp.check_checksum:TRUE " + "-o http.dechunk_body:FALSE " + "-t r -n -r \"" + pcapFilename + "\" -T pdml", WorkingDirectory = WiresharkDiretory, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; Console.WriteLine("Starting process " + process.StartInfo.FileName + " " + process.StartInfo.Arguments); Assert.IsTrue(process.Start()); string output = process.StandardOutput.ReadToEnd(); string errorOutput = process.StandardError.ReadToEnd(); process.WaitForExit(); Console.WriteLine(errorOutput); File.WriteAllText(documentFilename, output); Assert.AreEqual(0, process.ExitCode); } // Fix pdml file string fixedDocumentFilename = documentFilename + ".fixed"; File.WriteAllBytes(fixedDocumentFilename, new List <byte>(from b in File.ReadAllBytes(documentFilename) select b == 0x0A || b == 0x0D ? b : Math.Max((byte)0x20, Math.Min(b, (byte)0x7F))).ToArray()); try { Compare(XDocument.Load(fixedDocumentFilename, LoadOptions.None), packets); } catch (Exception exception) { throw new AssertFailedException("Failed comparing packets in file " + pcapFilename + ". " + exception, exception); } }
public void LoadData(string pcapfile) { //try //{ int pktCnt; frames = new List<byte[]>(); LogItems.addStreamInfo(String.Format("Starting to parse {0}", pcapfile)); using (PacketCommunicator communicator = new OfflinePacketDevice(pcapfile).Open(65536, PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { //communicator.ReceiveSomePackets(out pktCnt,-1, extractUDP); communicator.ReceivePackets(0,extractUDP); } //for(int f=0;f!=frames.Count;f++) //{ // parseFrame(frames[f], f); //} //frames.Clear(); //} //} //catch (Exception e) //{ // var msg = (String.Format("Cannot open {0} or crashed during parsing, please make sure that file is not in use by other program\nRead the rest of the crash report below\n\n\n{1}", // pcapfile, e.ToString())); // LogItems.addParsingError(msg); //} }
/// <summary> /// experiment to parse packets in parallel /// </summary> /// <param name="pcapfile"></param> public void LoadDataParallel(string pcapfile) { Dictionary<int, Dictionary<string, double[]>> streamParameters = Globals.limitPCAP; try { int pktCnt = 0; frames = new List<byte[]>(); using (PacketCommunicator communicator = new OfflinePacketDevice(pcapfile).Open(65536, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { //communicator.ReceiveSomePackets(out pktCnt, 10, parsePacketParrallel); communicator.ReceivePackets(0, getParallel); //communicator.ReceivePackets(0, parsePacketDownSampled); //communicator.ReceivePackets(0, parsePacketPTP); } //ThreadPool.GetMaxThreads(out threadCnt,out threadDone); ThreadPool.SetMaxThreads(50, 50); int threadCnt = 0; int threadDone = 0; ThreadPool.GetAvailableThreads(out threadCnt, out threadDone); if (frames.Count == 0) return; for (int i = 0; i != frames.Count - 1; i++) { ThreadPool.QueueUserWorkItem(state => parseFrame(frames[i], i)); } //Parallel.For(0, frames.Count, i => // parseFrame(frames[i], i) // ); while (true) { ThreadPool.GetAvailableThreads(out threadCnt, out threadDone); if ((threadCnt == 50) && (threadDone == 50)) { break; } Thread.Sleep(100); } } catch (Exception e) { MessageBox.Show(String.Format("Cannot open {0} or crashed during parsing, please make sure that file is not in use by other program\nRead the rest of the crash report below\n\n\n{1}", pcapfile, e.ToString())); } }
//[MethodImpl(MethodImplOptions.AggressiveInlining)] public void LoadDataDownSampled(string pcapfile) { Dictionary<int, Dictionary<string, double[]>> streamParameters = Globals.limitPCAP; try { int pktCnt=0; frames = new List<byte[]>(); using (PacketCommunicator communicator = new OfflinePacketDevice(pcapfile).Open(65536, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { //communicator.ReceiveSomePackets(out pktCnt, 10, parsePacketParrallel); communicator.ReceivePackets(0, parsePacketDownSampled); //communicator.ReceivePackets(0, parsePacketPTP); } } catch (Exception e) { MessageBox.Show(String.Format("Cannot open {0} or crashed during parsing, please make sure that file is not in use by other program\nRead the rest of the crash report below\n\n\n{1}", pcapfile, e.ToString())); } }
public void LoadData(string pcapfile) { Dictionary<int, Dictionary<string, double[]>> streamParameters = Globals.limitPCAP; try { int pktCnt; using (PacketCommunicator communicator = new OfflinePacketDevice(pcapfile).Open(65536, PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { communicator.ReceivePackets(0,parsePacket2); //communicator.ReceivePackets(0, parsePacketPTP); //communicator.ReceiveSomePackets(out pktCnt, -1, parsePacketParrallel); } //} } catch (Exception e) { MessageBox.Show(String.Format("Cannot open {0} or crashed during parsing, please make sure that file is not in use by other program\nRead the rest of the crash report below\n\n\n{1}", pcapfile, e.ToString())); } }