Ejemplo n.º 1
0
        // get bytes without lenght indicator and pdu type
        public byte[] GetBytes()
        {
            int len = 5; // dest-ref, src-ref, option

            foreach (VarParam v in Varpart)
            {
                len += 1 + 1 + v.length;
            }
            byte[] res = new byte[len];

            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(ByteConvert.DoReverseEndian(DstRef)), 0, res, 0, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(ByteConvert.DoReverseEndian(SrcRef)), 0, res, 2, 2);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(DstRef), 0, res, 0, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(SrcRef), 0, res, 2, 2);
            }
            res[4] = (byte)ClassOption;

            int pos = 5;

            foreach (VarParam v in Varpart)
            {
                res[pos++] = v.code;
                res[pos++] = v.length;
                Buffer.BlockCopy(v.value, 0, res, pos, v.length);
                pos += v.length;
            }
            return(res);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Extract data from TPKT packet. Length is read from parameter packetLen
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="packetLen"></param>
        public TPKT(byte[] packet, int packetLen)
        {
            if (packetLen < TPKT_HEADER_LENGTH)
            {
                throw new Exception("TPKT: The packet did not contain the minimum number of bytes for an TPKT header packet.");
            }

            Version = packet[0];

            if (Version != 3)
            {
                throw new Exception("TPKT: Version in header is not valid (!=3).");
            }

            if (BitConverter.IsLittleEndian)
            {
                Length = ByteConvert.DoReverseEndian(BitConverter.ToUInt16(packet, 2));
            }
            else
            {
                Length = BitConverter.ToUInt16(packet, 2);
            }

            if (Length > packetLen)
            {
                throw new Exception("TPKT: Length in header is greater than packet length.");
            }

            PayloadLength = Length - TPKT_HEADER_LENGTH;
            _Payload      = new byte[PayloadLength];
            Array.Copy(packet, 4, _Payload, 0, PayloadLength);
        }
Ejemplo n.º 3
0
        public override void OnReceiveData(ConnectionState state)
        {
            client = state;
            byte[] buffer  = new byte[1460];
            int    tpktlen = 0;

            while (state.AvailableData > 0)
            {
                int readBytes;
                // Read length from TPKT header first
                readBytes = client.Read(buffer, 0, 4);
                if (readBytes > 0)
                {
                    // TPKT Header
                    if (buffer[0] == 3 && buffer[1] == 0)   // Version = 3 / Reserved = 0
                    {
                        if (BitConverter.IsLittleEndian)
                        {
                            tpktlen = ByteConvert.DoReverseEndian(BitConverter.ToUInt16(buffer, 2));
                        }
                        else
                        {
                            tpktlen = BitConverter.ToUInt16(buffer, 2);
                        }
                        // try to read the TPDU to offset 4 in buffer, this is the length from TPKT minus length of TPKT header
                        readBytes = client.Read(buffer, 4, tpktlen - 4);
                    }
                    else
                    {
                        // Wrong TPKT header
                        client.EndConnection();
                        return;
                    }
                }
                // If TDPU could be read completely
                if (readBytes == (tpktlen - 4))
                {
                    try
                    {
                        ISOsrv.Process(this, buffer, readBytes + 4);
                        if (ISOsrv.Connected == false)
                        {
                            client.EndConnection();
                        }
                    }
                    catch (Exception)
                    {
                        client.EndConnection();
                    }
                }
                else
                {
                    client.EndConnection();
                }
            }
        }
Ejemplo n.º 4
0
        public TPDUConnection(byte[] packet)
        {
            int pos;
            int li = packet[0];

            TPDU.TPDU_TYPES type = (TPDU.TPDU_TYPES)(packet[1] >> 4);

            if ((type != TPDU.TPDU_TYPES.CR) && (type != TPDU.TPDU_TYPES.CC))
            {
                throw new ApplicationException("TPDU: This can only handle CC/CR TDPUs");
            }

            if (BitConverter.IsLittleEndian)
            {
                DstRef = ByteConvert.DoReverseEndian(BitConverter.ToUInt16(packet, 2));
                SrcRef = ByteConvert.DoReverseEndian(BitConverter.ToUInt16(packet, 4));
            }
            else
            {
                DstRef = BitConverter.ToUInt16(packet, 2);
                SrcRef = BitConverter.ToUInt16(packet, 4);
            }

            ClassOption = ((packet[6] & 0xf0) >> 4);
            if (ClassOption > 4)
            {
                throw new Exception("TPDU: Class option number not allowed.");
            }

            pos = 7;
            // read variable part
            Varpart = new ArrayList();
            try
            {
                while (pos < li)
                {
                    VarParam vp = new VarParam();
                    vp.code   = packet[pos];
                    pos      += 1;
                    vp.length = packet[pos];
                    pos      += 1;
                    vp.value  = new byte[vp.length];
                    Array.Copy(packet, pos, vp.value, 0, vp.length);
                    pos += vp.length;
                    Varpart.Add(vp);
                }
            }
            catch (Exception)
            {
                throw new Exception("TPDU: Error parsing variable part of CR/CC PDU.");
            }
        }
Ejemplo n.º 5
0
        public byte[] GetBytes()
        {
            byte[] tpkt = new byte[Length];

            tpkt[0] = Convert.ToByte(Version);
            tpkt[1] = Convert.ToByte(Reserved);

            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(ByteConvert.DoReverseEndian(Length)), 0, tpkt, 2, 2);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(Length), 0, tpkt, 2, 2);
            }

            Array.Copy(_Payload, 0, tpkt, TPKT_HEADER_LENGTH, PayloadLength);
            return(tpkt);
        }