static void deserializeDNP3(ref byte[] buffer) { var sb = new StringBuilder("new byte[] { "); for (var i = 0; i < buffer.Length; i++) { var b = buffer[i]; sb.Append(b); if (i < buffer.Length - 1) { sb.Append(", "); } } sb.Append(" }"); DNP3Simple.LinkLayer ll = new DNP3Simple.LinkLayer(); ll.deserialize(ref buffer); DNP3Simple.TransportLayer tl = new DNP3Simple.TransportLayer(); tl.deserialize(ref buffer); DNP3Simple.ApplicationLayer al = new DNP3Simple.ApplicationLayer(); al.deserialize(ref buffer); Console.WriteLine("DNP3 Application request " + sb.ToString()); DNP3Response(ll, tl, al, ref buffer); }
public static byte[] TransportResponse(TransportLayer tl_, ApplicationLayer al_) { TransportLayer tl = new TransportLayer(); tl.TransportData = al_.ApplicationData; tl.seq = (byte)(tl_.seq + 1); tl.FIN = 1; tl.FIR = 1; tl.serialize(ref tl.TransportData); return(tl.TransportData); }
public static byte[] LinkResponse(LinkLayer ll_, TransportLayer tl_) { LinkLayer ll = new LinkLayer(); ll.LinkData = tl_.TransportData; ll.source = ll_.destination; ll.destination = ll_.source; ll.controlByte = ModifyControlByte(ref ll_); ll.serialize(ref ll.LinkData); return(ll.LinkData); }
/* * public void ProcessTransport(byte transportByte) * { * FIN = (byte)((transportByte & 0x80) >> 7); * FIR = (byte)((transportByte & 0x40) >> 6); * seq = (byte)((transportByte & 0x20) & 0x3F); * }*/ public static void ProcessTransport(ref TransportLayer tl_) { if ((byte)(tl_.seq + 1) == 64) { tl_.expectedSegment = 0; } else { tl_.expectedSegment = (byte)(tl_.seq + 1); } //Rule 1: transport seg-series may only begin with FIR bit set. if (tl_.isMultipleSegment) { if (tl_.FIR == (byte)FIRTrans.SET && tl_.segSeriesStarted == false) { // Transport segment series starts tl_.segSeriesStarted = true; } // Rule 2: transport seg-series ends with FIN bit set. if (tl_.FIN == (byte)FINTrans.SET && tl_.segSeriesEnded == false) { // Transport segment series ends tl_.segSeriesEnded = true; } // Rule 3: no trans seg-series in progress and FIR not set will be discarded if (tl_.recvdAllSegment && tl_.FIR == (byte)FIRTrans.UNSET) { // Do not consider the FIR bit } } // Rule 4: A trans seg with FIR bit set may have any seq no 0 to 63 without regard to history if (tl_.segSeriesStarted) { // Rule 5 (b): received trans seg with FIR set will discard in progress seg-series. if (tl_.isMultipleSegment && tl_.FIR == (byte)FIRTrans.SET) { tl_.discard = true; } // Rule 5 (c): seg no. equivalent to previous is discarded if (tl_.seq == tl_.previousSegment) { tl_.discard = true; } // Rule 5 (d): if FIR is UNSET and seq no. different then expected discard if (tl_.FIR == (byte)FIRTrans.UNSET && (tl_.seq != tl_.expectedSegment)) { // discard further segments and exit tl_.discard = true; } } // Rule 6: there is only one segment if FIR and FIN are set if (tl_.FIR == (byte)FIRTrans.SET && tl_.FIN == (byte)FINTrans.SET) { tl_.isMultipleSegment = false; tl_.recvdAllSegment = true; } // Rule 7: if all the segments are assembled then only send the payload to Application Layer if (tl_.isMultipleSegment && tl_.FIN == (byte)FINTrans.SET && tl_.discard == false) { tl_.recvdAllSegment = true; tl_.segSeriesEnded = true; } tl_.previousSegment = tl_.seq; }