Exemple #1
0
        /// <summary>
        /// Reads a TPKT from the socket Async
        /// </summary>
        /// <param name="stream">The stream to read from</param>
        /// <returns>Task TPKT Instace</returns>
        public static async Task <TPKT> ReadAsync(Stream stream)
        {
            var buf = new byte[4];
            int len = await stream.ReadAsync(buf, 0, 4);

            if (len < 4)
            {
                Logger.E($"TPKT is incomplete / invalid if (len({len}) < 4)"); return(null);
                //throw new TPKTInvalidException("TPKT is incomplete / invalid");
            }

            var pkt = new TPKT
            {
                Version   = buf[0],
                Reserved1 = buf[1],
                Length    = buf[2] * 256 + buf[3] //BigEndian
            };

            if (pkt.Length > 0)
            {
                pkt.Data = new byte[pkt.Length - 4];
                len      = await stream.ReadAsync(pkt.Data, 0, pkt.Length - 4);

                if (len < pkt.Length - 4)
                {
                    Logger.E($"TPKT is incomplete / invalid if (len({len}) < pkt.Length({ pkt.Length}) - 4))"); return(null);
                    //throw new TPKTInvalidException("TPKT is incomplete / invalid");
                }
            }
            return(pkt);
        }
Exemple #2
0
            /// <summary>
            /// Reads COTP TPDU (Transport protocol data unit) from the network stream
            /// See: https://tools.ietf.org/html/rfc905
            /// </summary>
            /// <param name="stream">The socket to read from</param>
            /// <returns>COTP DPDU instance</returns>
            public static TPDU Read(Stream stream)
            {
                var tpkt = TPKT.Read(stream);

                if (tpkt.Length > 0)
                {
                    return(new TPDU(tpkt));
                }
                return(null);
            }
Exemple #3
0
            /// <summary>
            /// Reads COTP TPDU (Transport protocol data unit) from the network stream
            /// See: https://tools.ietf.org/html/rfc905
            /// </summary>
            /// <param name="stream">The socket to read from</param>
            /// <returns>COTP DPDU instance</returns>
            public static async Task <TPDU> ReadAsync(Stream stream)
            {
                var tpkt = await TPKT.ReadAsync(stream);

                if (tpkt.Length > 0)
                {
                    return(new TPDU(tpkt));
                }
                return(null);
            }
Exemple #4
0
            public TPDU(TPKT tPKT)
            {
                TPkt = tPKT;

                var br = new BinaryReader(new MemoryStream(tPKT.Data));

                HeaderLength = br.ReadByte();
                if (HeaderLength >= 2)
                {
                    PDUType = br.ReadByte();
                    if (PDUType == 0xf0) //DT Data
                    {
                        var flags = br.ReadByte();
                        TPDUNumber   = flags & 0x7F;
                        LastDataUnit = (flags & 0x80) > 0;
                        Data         = br.ReadBytes(tPKT.Length - HeaderLength - 4); //4 = TPKT Size
                        return;
                    }
                    //TODO: Handle other PDUTypes
                }
                Data = new byte[0];
            }