Exemplo n.º 1
0
        private void verifyReceivedData(byte[] data)
        {
            if ((data[1] & 0xF0) != HDLC_FORMAT)
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_FRAME_FORMAT);
            }
            int frameSize = ((data[1] & 0x0F) << 8) | (data[2] & 0xFF);

            if (frameSize != data.Length - 2)
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_FRAME_FORMAT);
            }
            if (parameters.clientAddress != data[3])
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_ADDRESS);
            }
            int offset = 4;

            while (offset < frameSize && (data[offset++] & 0x01) != 0x01)
            {
            }

            connection.receivedControl = data[offset++] & 0xFF;
            if (data.Length - offset > 3)
            {
                connection.receivedData = helper.extensions.copyOfRange(data, offset + 2, data.Length - 3);
            }
            else
            {
                connection.receivedData = new byte[0];
            }

            short fcs = BitConverter.ToInt16((new byte[] { data[offset], data[offset + 1] }), 0);

            if (fcs != HdlcLinkLayer.calcFcs(data, 1, offset - 1))
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_CHECK_SEQUENCE);
            }

            fcs = BitConverter.ToInt16((new byte[] { data[data.Length - 3], data[data.Length - 3 + 1] }), 0);
            if (fcs != HdlcLinkLayer.calcFcs(data, 1, data.Length - 4))
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_CHECK_SEQUENCE);
            }
            connection.isFinalPoll      = (connection.receivedControl & 0x10) == 0x10;
            connection.receivedControl &= 0xEF; //remove p/f bit from control
            connection.receivedRrr      = 0;
            connection.receivedSss      = 0;
            if ((connection.receivedControl & 0x01) == 0x00)
            {
                connection.receivedRrr      = ((int)((uint)connection.receivedControl >> 5)) & 0x07;
                connection.receivedSss      = ((int)((uint)connection.receivedControl >> 1)) & 0x07;
                connection.receivedControl &= 0x01; //remove sss and rrr bits from control
                connection.lastFrameHadSss  = true;
            }
            else if ((connection.receivedControl & 0x02) == 0x00)
            {
                connection.receivedRrr      = ((int)((uint)connection.receivedControl >> 5)) & 0x07;
                connection.receivedControl &= 0x0F; //remove rrr bits from control
            }
            if (connection.receivedControl == FRMR_CONTROL)
            {
                throw new LinkLayerException(LinkLayerExceptionReason.SERVER_REPORTS_FRAME_REJECTED);
            }
        }