private void ExtractUpperPacket(TcpPacket tcp) { TpktFileds tf = new TpktFileds(); string srcIp = tcp.ParentPacket <IPv4Packet>().SourceAddress.ToString(); tpktBuff = TpktPacketBufferFactory.GetBuffer(srcIp); if (tpktBuff.Count > 0 && !tpktBuff.IsReassembled) { tf.LeadWithSegment = true; tf.LeadingSegmentLength = tpktBuff.Last.NextFrameSegmentLength; } byte[] header = tcp.PayloadData.Take(TpktFileds.HeaderLength).ToArray(); if (tf.LeadWithSegment || TpktPacket.IsTpkt(header)) { TpktPacket tpkt = new TpktPacket(tcp.PayloadData, tcp, tf); if (tpkt.PayloadPacket != null || tpkt.PayloadData != null) { tpktBuff.Add(tpkt); if (tpktBuff.IsReassembled) { foreach (TpktPacket reassTpkt in tpktBuff.Reassembled) { CotpPacket cotp = reassTpkt.Extract <CotpPacket>(); if (cotp.Type == CotpPacket.TpduType.DataTransfer) { cotpBuff = CotpPacketBufferFactory.GetBuffer(srcIp); cotpBuff.Add(cotp); if (cotpBuff.IsReassembled) { CotpPacket reassCotp = cotpBuff.Reassembled; var session = new OsiSessionPacket(reassCotp.PayloadData, reassCotp); //packets.Add(); var mms = session.Extract <MmsPacket>(); if (mms != null) { packets.Push(mms); } else { packets.Push(session); } cotpBuff.Reset(); } } } tpktBuff.Reset(); } } } }
public void ParseFromFile() { int pcnt = 1; var dev = new CaptureFileReaderDevice(@"..\..\CapturedFiles\20140813-150920_0005ED9B-50+60_MMS.pcap"); dev.Filter = "ip src 198.121.0.115 and tcp"; // 92 or 115 // won't work correctly if not give the ip source ( The buffer will take all the packets from different source) dev.Open(); RawCapture rawCapture; List <Packet> packets = new List <Packet>(); TpktPacketBuffer tpktBuff = new TpktPacketBuffer(); CotpPacketBuffer cotpBuff = new CotpPacketBuffer(); rawCapture = dev.GetNextPacket(); while (rawCapture != null) { Packet p = Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data); TcpPacket tcp = p.Extract <TcpPacket>(); if (tcp.PayloadData.Length > 0) { TpktFileds tf = new TpktFileds(); if (tpktBuff.Count > 0 && !tpktBuff.IsReassembled) { tf.LeadWithSegment = true; tf.LeadingSegmentLength = tpktBuff.Last.NextFrameSegmentLength; } TpktPacket tpkt = new TpktPacket(tcp.PayloadData, tcp, tf); tpktBuff.Add(tpkt); if (tpktBuff.IsReassembled) { foreach (TpktPacket reassTpkt in tpktBuff.Reassembled) { CotpPacket cotp = reassTpkt.Extract <CotpPacket>(); if (cotp.Type == CotpPacket.TpduType.DataTransfer) { cotpBuff.Add(cotp); if (cotpBuff.IsReassembled) { CotpPacket reassCotp = cotpBuff.Reassembled; packets.Add(new OsiSessionPacket(reassCotp.PayloadData, reassCotp)); cotpBuff.Reset(); #region For debug #if DEBUG && SHOW_DETAILS MmsPacket mms = (MmsPacket)packets.Last().Extract(typeof(MmsPacket)); if (mms.Pdu is UnconfirmedPdu) { var pdu = mms.Pdu as UnconfirmedPdu; string dsRef = pdu.Service.InformationReport.ListOfAccessResult[3].Success.GetValue <IEC61850Packet.Asn1.Types.VisibleString>().Value; Console.WriteLine(dsRef); } #endif #endregion } } } tpktBuff.Reset(); } } rawCapture = dev.GetNextPacket(); pcnt++; } dev.Close(); }
private static void ExtractUpperPacket(TcpPacket tcp) { TpktFileds tf = new TpktFileds(); string srcIp = tcp.ParentPacket <IPv4Packet>().SourceAddress.ToString(); tpktBuff = TpktPacketBufferFactory.GetBuffer(srcIp); if (tpktBuff.Count > 0 && !tpktBuff.IsReassembled) { tf.LeadWithSegment = true; tf.LeadingSegmentLength = tpktBuff.Last.NextFrameSegmentLength; } byte[] header = tcp.PayloadData.Take(TpktFileds.HeaderLength).ToArray(); if (tf.LeadWithSegment || TpktPacket.IsTpkt(header)) { TpktPacket tpkt = new TpktPacket(tcp.PayloadData, tcp, tf); if (tpkt.PayloadPacket != null || tpkt.PayloadData != null) { tpktBuff.Add(tpkt); if (tpktBuff.IsReassembled) { foreach (TpktPacket reassTpkt in tpktBuff.Reassembled) { CotpPacket cotp = reassTpkt.Extract <CotpPacket>(); if (cotp.Type == CotpPacket.TpduType.DataTransfer) { cotpBuff = CotpPacketBufferFactory.GetBuffer(srcIp); cotpBuff.Add(cotp); if (cotpBuff.IsReassembled) { CotpPacket reassCotp = cotpBuff.Reassembled; var session = new OsiSessionPacket(reassCotp.PayloadData, reassCotp); //packets.Add(); var mms = session.Extract <MmsPacket>(); if (mms != null) { packets.Add(mms); } else { packets.Add(session); } cotpBuff.Reset(); #region For debug #if DEBUG && SHOW_DETAILS MmsPacket mms = (MmsPacket)packets.Last().Extract(typeof(MmsPacket)); if (mms.Pdu is UnconfirmedPdu) { var pdu = mms.Pdu as UnconfirmedPdu; string dsRef = pdu.Service.InformationReport.ListOfAccessResult[3].Success.GetValue <IEC61850Packet.Asn1.Types.VisibleString>().Value; Console.WriteLine("No. {0}: {1}", currentPacketIndex, dsRef); } #endif #endregion } } } tpktBuff.Reset(); } } } }