void Dev_OnOpened(object sender, DeviceOpenedEventArgs e) { while (dev.HasNextPacket) { Packet p = dev.GetNextPacket(); StringBuilder sb = new StringBuilder(); // StringWriter sw = new StringWriter(); if (p.GetType() == typeof(MmsPacket)) { MmsPacket mms = p as MmsPacket; if (mms.Pdu is UnconfirmedPdu) { AcsiMapping acsi = mms.Acsi; ReportedOptFldsType excpted = ReportedOptFldsType.DataSetName | ReportedOptFldsType.DataReference | ReportedOptFldsType.ReasonForInclusion | ReportedOptFldsType.ReportTimeStamp; bool test = (acsi.ReportedOptFlds & excpted) == excpted; if (test) { string ip = mms.ParentPacket <IPv4Packet>().SourceAddress.ToString(); string rawDatSet = acsi.DatSet; string dsName = rawDatSet.Substring(rawDatSet.LastIndexOf('$') + 1); sb.AppendFormat("RptID: {0}\n", acsi.RptID); sb.AppendFormat("ReportedFileds: {0}\n", acsi.ReportedOptFlds); sb.AppendFormat("TimeofEntry: {0}\n", acsi.TimeofEntry); sb.AppendFormat("DatSet: {0},dsName(not a field of the packet): {1}\n", rawDatSet, dsName); sb.AppendFormat("EntryID: {0}\n", acsi.EntryID); sb.AppendFormat("Inclusion-bitstring: {0}\n", acsi.Inclusion_Bitstring); for (int i = 0; i < acsi.DataRef.Count; i++) { sb.AppendFormat("{0}: {1}, Reason: {2}\n", acsi.DataRef[i], acsi.Value[i].ToString(), acsi.ReasonCode[i]); } } } } else if (p.GetType() == typeof(GoosePacket)) { var goose = (GoosePacket)p; string appId = goose.APPID.ToString("X4"); sb.AppendFormat("gocbRef: {0} APPID: {1}\n", goose.APDU.gocbRef.Value, appId); // Check if alarm sb.AppendFormat("timeAllowedtoLive:{0}", goose.APDU.timeAllowedtoLive); sb.AppendFormat("datSet: {0}", goose.APDU.datSet); sb.AppendFormat("numDatSetEntries: {0}", goose.APDU.numDatSetEntries); for (int i = 0; i < goose.APDU.numDatSetEntries.Value; i++) { var type = goose.APDU.allData[i].Type; Type t = System.Reflection.Assembly.Load("IEC61850Packet").GetType("IEC61850Packet.Asn1.Types.UtcTime"); object value; switch (type) { case VariableType.Boolean: value = goose.APDU.allData[i].GetValue <TAsn1.Boolean>().Value; sb.AppendFormat("No.{0}. Boolean: {1}\n", i + 1, value); break; case VariableType.FloatPoint: value = goose.APDU.allData[i].GetValue <FloatPoint>().Value; sb.AppendFormat("No.{0}. FloatPoint: {1}\n", i + 1, value); break; case VariableType.BitString: value = goose.APDU.allData[i].GetValue <BitString>().Value; sb.AppendFormat("No.{0}. BitString: {1}\n", i + 1, value); break; case VariableType.UtcTime: value = goose.APDU.allData[i].GetValue <UtcTime>().Value; sb.AppendFormat("No.{0}. UtcTime: {1}\n", i + 1, value); break; default: value = null; sb.AppendFormat("No.{0}. Unknown: {1}\n", i + 1, value); break; } } Console.WriteLine(sb.ToString()); } else if (p.GetType() == typeof(SvPacket)) { var sv = (SvPacket)p; Console.WriteLine("SV 9-2, APPID: {0}, svID: {1}", sv.APPID, sv.APDU.ASDU[0].svID); } Console.WriteLine(sb.ToString()); sb.Clear(); } Console.WriteLine("{0} packets have been resolved", dev.PacketCount); }
void dev_OnPacketArrival(object sender, CaptureEventArgs e) { bool broadcast = false; //ResolveDevice rd = new ResolveDevice(null); Packet p = ResolveDevice.Resovle(e.Packet); StringWriter sw = new StringWriter(); JsonWriter jw = new JsonTextWriter(sw); if (p.GetType() == typeof(MmsPacket)) { MmsPacket mms = p as MmsPacket; if (mms.Pdu is UnconfirmedPdu) { AcsiMapping acsi = mms.Acsi; ReportedOptFldsType excpted = ReportedOptFldsType.DataSetName | ReportedOptFldsType.DataReference | ReportedOptFldsType.ReasonForInclusion;// | ReportedOptFldsType.ReportTimeStamp; bool test = (acsi.ReportedOptFlds & excpted) == excpted; //test = true; if (test) { string ip = mms.ParentPacket <IPv4Packet>().SourceAddress.ToString(); pm_ied = Map.IED.SingleOrDefault(ied => ied.MMS.IP == ip); // SingleOrDefault IP重复配置 if (pm_ied == null) { // Consider Network B var tmp = ip.Split('.'); ip = tmp[0] + ".120." + tmp[2] + "." + tmp[3]; pm_ied = Map.IED.SingleOrDefault(ied => ied.MMS.IP == ip); } if (pm_ied != null) { string iedName = pm_ied.Name; string iedDesc = pm_ied.Desc; string rawDatSet = acsi.DatSet; string dsName = rawDatSet.Substring(rawDatSet.LastIndexOf('$') + 1); string ldInst = rawDatSet.Split('/')[0].Replace(iedName, ""); var pm_ds = pm_ied.MMS.DataSet.SingleOrDefault(ds => ds.LdInst == ldInst && ds.Name == dsName); string dsDesc = pm_ds.Desc; // TODO: Search the related // warn.Related = {"",""}; // warn.TimeofEntry = asci.TimeofEntry; jw.WriteStartObject(); jw.WritePropertyName("ied"); jw.WriteValue(iedName); jw.WritePropertyName("desc"); jw.WriteValue(iedDesc); jw.WritePropertyName("dataSet"); jw.WriteStartObject(); jw.WritePropertyName("type"); jw.WriteValue("MMS"); jw.WritePropertyName("name"); jw.WriteValue(dsName); jw.WritePropertyName("desc"); jw.WriteValue(dsDesc); jw.WritePropertyName("warnings"); jw.WriteStartArray(); if (dsName.Contains("dsAlarm") || dsName.Contains("dsWarning") || dsName.Contains("dsCommState") || dsName.Contains("dsTripInfo")) { for (int i = 0; i < acsi.DataRef.Count; i++) { var item = pm_ds.Item.SingleOrDefault(d => d.Ref == acsi.DataRef[i].Replace(iedName, "")); if (item.Alarm) { // sb.AppendFormat("{0}: {1}, Reason: {2}<br/>", item.Value, acsi.Value[i].ToString(), acsi.ReasonCode[i]); jw.WriteStartObject(); jw.WritePropertyName("ref"); jw.WriteValue(item.Ref); jw.WritePropertyName("desc"); jw.WriteValue(item.Value); jw.WritePropertyName("value"); jw.WriteValue(acsi.Value[i].ToString()); jw.WriteEndObject(); broadcast = true; } } } jw.WriteEndArray(); jw.WriteEndObject(); jw.WriteEndObject(); } } } } else if (p.GetType() == typeof(GoosePacket)) { var goose = (GoosePacket)p; string gocbRef = goose.APDU.gocbRef.Value; string appId = goose.APPID.ToString("X4"); TG.DataSet pm_ds = Map.IED.GetDataSetByAPPID(appId); pm_ied = Map.IED.GetIEDByAPPID(appId); // List<string> related = pm_ied.GetRelatedIed(); //sb.AppendFormat("gocbRef: {0} APPID: {1}<br/>", pm_ds.gocbRef, pm_ds.APPID); // Check if alarm if (pm_ied != null && pm_ds != null) { string iedName = pm_ied.Name; string iedDesc = pm_ied.Desc; jw.WriteStartObject(); jw.WritePropertyName("ied"); jw.WriteValue(iedName); jw.WritePropertyName("desc"); jw.WriteValue(iedDesc); jw.WritePropertyName("dataSet"); jw.WriteStartObject(); jw.WritePropertyName("type"); jw.WriteValue("GOOSE"); jw.WritePropertyName("desc"); jw.WriteValue(pm_ds.Desc); jw.WritePropertyName("gocbRef"); jw.WriteValue(pm_ds.gocbRef); jw.WritePropertyName("cbName"); jw.WriteValue(pm_ds.cbName); jw.WritePropertyName("VLAN"); jw.WriteValue(pm_ds.VLAN); jw.WritePropertyName("MAC"); jw.WriteValue(pm_ds.MAC); jw.WritePropertyName("APPID"); jw.WriteValue(pm_ds.APPID); jw.WritePropertyName("warnings"); jw.WriteStartArray(); for (int i = 0; i < pm_ds.Item.Length; i++) { var type = goose.APDU.allData[i].Type; object value; switch (type) { case IEC61850Packet.Mms.Types.Data.VariableType.Boolean: value = goose.APDU.allData[i].GetValue <TAsn1.Boolean>().Value; break; case IEC61850Packet.Mms.Types.Data.VariableType.FloatPoint: value = goose.APDU.allData[i].GetValue <FloatPoint>().Value; break; default: value = null; break; } var item = pm_ds.Item[i]; if (value is bool && item.Alarm) { // This packet is a warning packet and contains alarm //Console.ForegroundColor = ConsoleColor.Red; jw.WriteStartObject(); jw.WritePropertyName("ref"); jw.WriteValue(item.Ref); jw.WritePropertyName("desc"); jw.WriteValue(item.Value); jw.WritePropertyName("value"); jw.WriteValue(value); if (item.To != null) { jw.WritePropertyName("to"); jw.WriteValue(item.To); } if (item.ExtAddr != null) { jw.WritePropertyName("extAddr"); jw.WriteValue(item.ExtAddr); } if (item.ExtDesc != null) { jw.WritePropertyName("extDesc"); jw.WriteValue(item.ExtDesc); } jw.WriteEndObject(); // sb.AppendFormat("{0}: isAlarm: {1}, isTrigged: {2}<br/>", pm_ds.Item[i].Value, pm_ds.Item[i].Alarm, value); broadcast = true; //Console.ResetColor(); } // sb.AppendFormat("{0}: isAlarm: {1}, Value: {2}<br/>", pm_ds.Item[i].Value, pm_ds.Item[i].Alarm, value); } jw.WriteEndArray(); jw.WriteEndObject(); jw.WriteEndObject(); } } // sb.Append("<hr/>"); if (broadcast) { BroadcastHandler.Clients.Broadcast(sw.ToString()); broadcast = false; } jw.Flush(); }
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(); } } } }