Exemplo n.º 1
0
        /// <summary>
        /// Create a MsgBlk response.
        /// </summary>
        /// <param name="segmentId">The segmentId.</param>
        /// <param name="block">The block.</param>
        /// <param name="cryptoAlgoIdValues">The cryptoAlgoId.</param>
        /// <param name="msgTypeValues">The msgType.</param>
        /// <param name="isLastBLKAvail">If it is true, the block is the last available block.</param>
        /// <returns>The MsgBlk response.</returns>
        public PccrrBLKResponsePacket CreateMsgBlkResponse(
            byte[] segmentId,
            byte[] block,
            CryptoAlgoId_Values cryptoAlgoIdValues,
            MsgType_Values msgTypeValues,
            bool isLastBLKAvail)
        {
            if (segmentId == null)
            {
                segmentId = new byte[0];
            }

            if (block == null)
            {
                block = new byte[0];
            }

            PccrrBLKResponsePacket packet = new PccrrBLKResponsePacket();

            MSG_BLK msgBlkResp = new MSG_BLK();

            byte[] zeroPad   = new byte[0] {
            };
            byte[] zeroPad_2 = new byte[0] {
            };
            byte[] zeroPad_3 = new byte[0] {
            };
            byte[] iVBlock   = new byte[0] {
            };
            msgBlkResp.ZeroPad3        = zeroPad_3;
            msgBlkResp.ZeroPad2        = zeroPad_2;
            msgBlkResp.ZeroPad         = zeroPad;
            msgBlkResp.VrfBlock        = null;
            msgBlkResp.SizeOfVrfBlock  = 0;
            msgBlkResp.SizeOfSegmentId = (uint)segmentId.Length;
            msgBlkResp.SizeOfIVBlock   = (uint)iVBlock.Length;
            msgBlkResp.SizeOfBlock     = (uint)block.Length;
            msgBlkResp.SegmentId       = segmentId;
            msgBlkResp.IVBlock         = iVBlock;
            msgBlkResp.Block           = block;
            msgBlkResp.BlockIndex      = 0;

            if (!isLastBLKAvail)
            {
                msgBlkResp.NextBlockIndex = 1;
            }
            else
            {
                msgBlkResp.NextBlockIndex = 0;
            }

            MESSAGE_HEADER messageHeader = PccrrUtitlity.CreateMessageHeader(cryptoAlgoIdValues, msgTypeValues, new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            });

            packet.MsgBLK        = msgBlkResp;
            packet.MessageHeader = messageHeader;

            return(packet);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Decode MSG_BLK message.
        /// </summary>
        /// <param name="data">Data to be decoded.</param>
        /// <param name="index">Started index.</param>
        /// <returns>The MSG_BLK struct.</returns>
        private MSG_BLK DecodeMSG_BLK(byte[] data, ref int index)
        {
            MSG_BLK ret = new MSG_BLK();

            ret.SizeOfSegmentId = MarshalHelper.GetUInt32(data, ref index, false);
            ret.SegmentId       = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId);
            ret.ZeroPad         = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId % 4);
            if (ret.ZeroPad == null)
            {
                ret.ZeroPad = new byte[0];
            }

            ret.BlockIndex     = MarshalHelper.GetUInt32(data, ref index, false);
            ret.NextBlockIndex = MarshalHelper.GetUInt32(data, ref index, false);
            ret.SizeOfBlock    = MarshalHelper.GetUInt32(data, ref index, false);
            ret.Block          = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfBlock);
            if (ret.Block == null)
            {
                ret.Block = new byte[0];
            }

            ret.ZeroPad2 = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfBlock % 4);
            if (ret.ZeroPad2 == null)
            {
                ret.ZeroPad2 = new byte[0];
            }

            ret.SizeOfVrfBlock = MarshalHelper.GetUInt32(data, ref index, false);
            ret.VrfBlock       = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfVrfBlock);
            if (ret.VrfBlock == null)
            {
                ret.VrfBlock = new byte[0];
            }

            ret.ZeroPad3 = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfVrfBlock % 4);
            if (ret.ZeroPad3 == null)
            {
                ret.ZeroPad3 = new byte[0];
            }

            ret.SizeOfIVBlock = MarshalHelper.GetUInt32(data, ref index, false);
            ret.IVBlock       = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfIVBlock);

            if (ret.IVBlock == null)
            {
                ret.IVBlock = new byte[0];
            }

            return(ret);
        }
        /// <summary>
        /// Send message MSG_BLK.
        /// </summary>
        /// <param name="isTimerExpire">The timer for SendMsgBlk from client will expire or not.</param>
        /// <param name="isSameSegment">The message is for the segment that client request.</param>
        /// <param name="dwHashAlgoValues">The dwHashAlgo value.</param>
        /// <param name="index">Block index</param>
        /// <param name="isLastBLK">If it is true, the block is the last BLK.</param>
        public void SendMsgBlk(bool isTimerExpire, bool isSameSegment, DWHashAlgValues dwHashAlgoValues, uint index, bool isLastBLK)
        {
            PccrrBLKResponsePacket pccrrBLKResponsePacket;
            if (!isSameSegment && dwHashAlgoValues == DWHashAlgValues.V1)
            {
                this.sid = Encoding.UTF8.GetBytes(this.GetProperty("PCCRR.Protocol.ErrorSegmentId"));
                pccrrBLKResponsePacket = this.pccrrStackSer.CreateMsgBlkResponse(this.sid, new byte[] { }, this.cryptoAlgo, MsgType_Values.MSG_BLK, this.protoVer, isLastBLK);
            }
            else
            {
                if (dwHashAlgoValues == DWHashAlgValues.V3)
                {
                    this.sid = PccrrUtitlity.ToByteArray(this.GetProperty("PCCRR.Protocol.SHA512.SegmentId"));
                }
                else
                {
                    this.sid = PccrrUtitlity.ToByteArray(this.GetProperty("PCCRR.Protocol.SHA384.SegmentId"));
                }

                pccrrBLKResponsePacket = this.pccrrStackSer.CreateMsgBlkResponse(this.sid, new byte[] { }, this.cryptoAlgo, MsgType_Values.MSG_BLK, this.protoVer, isLastBLK);
            }

            MSG_BLK msgBLK = new MSG_BLK();
            msgBLK = pccrrBLKResponsePacket.MsgBLK;
            msgBLK.BlockIndex = index;

            if (!isLastBLK)
            {
                if (index == uint.MaxValue)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                else
                {
                    msgBLK.NextBlockIndex = index + 1;
                }
            }

            pccrrBLKResponsePacket.MsgBLK = msgBLK;

            try
            {
                this.pccrrStackSer.SendPacket(pccrrBLKResponsePacket);
            }
            catch (HttpListenerException ex)
            {
                if (ex.ErrorCode == 1229 && isTimerExpire)
                {
                    this.ReceivingTimeOut();
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Decode response message.
        /// </summary>
        /// <param name="rawdata">The raw data.</param>
        /// <returns>The PccrrPacket.</returns>
        public PccrrPacket DecodeResponseMessage(byte[] rawdata)
        {
            if (rawdata == null)
            {
                throw new ArgumentNullException("rawdata");
            }

            if (rawdata.Length == 0)
            {
                throw new ArgumentException("The raw data should not be empty.");
            }

            int messageLength = 0;

            messageLength = rawdata.Length;

            PccrrPacket packet = null;

            if (messageLength > 0)
            {
                int index = 0;

                RESPONSE_MESSAGE ret = new RESPONSE_MESSAGE();
                ret.TRANSPORTRESPONSEHEADER.Size = MarshalHelper.GetUInt32(rawdata, ref index, false);
                ret.MESSAGEHEADER = this.DecodeMessageHeader(rawdata, ref index);

                switch (ret.MESSAGEHEADER.MsgType)
                {
                case MsgType_Values.MSG_BLKLIST:
                    PccrrBLKLISTResponsePacket pccrrBLKLISTResponsePacket = new PccrrBLKLISTResponsePacket();

                    MSG_BLKLIST msgBLKLIST = this.DecodeMSG_BLKLIST(rawdata, ref index);
                    pccrrBLKLISTResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrBLKLISTResponsePacket.MsgBLKLIST    = msgBLKLIST;
                    pccrrBLKLISTResponsePacket.MessageHeader = ret.MESSAGEHEADER;
                    packet = pccrrBLKLISTResponsePacket;
                    break;

                case MsgType_Values.MSG_BLK:
                    PccrrBLKResponsePacket pccrrBLKResponsePacket = new PccrrBLKResponsePacket();

                    MSG_BLK msgBLK = this.DecodeMSG_BLK(rawdata, ref index);
                    pccrrBLKResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrBLKResponsePacket.MsgBLK        = msgBLK;
                    pccrrBLKResponsePacket.MessageHeader = ret.MESSAGEHEADER;
                    packet = pccrrBLKResponsePacket;
                    break;

                case MsgType_Values.MSG_NEGO_RESP:
                    PccrrNegoResponsePacket pccrrNegoResponsePacket = new PccrrNegoResponsePacket();

                    MSG_NEGO_RESP msgNEGORESP = this.DecodeMSG_NEGO_RESP(rawdata, ref index);
                    pccrrNegoResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrNegoResponsePacket.MsgNegoResp             = msgNEGORESP;
                    pccrrNegoResponsePacket.MessageHeader           = ret.MESSAGEHEADER;
                    packet = pccrrNegoResponsePacket;
                    break;

                case MsgType_Values.MSG_SEGLIST:
                    PccrrSegListResponsePacket pccrrSegListResponsePacket = new PccrrSegListResponsePacket();
                    pccrrSegListResponsePacket.MsgSegList = TypeMarshal.ToStruct <MSG_SEGLIST>(rawdata, ref index);
                    pccrrSegListResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER;
                    pccrrSegListResponsePacket.MessageHeader           = ret.MESSAGEHEADER;
                    packet = pccrrSegListResponsePacket;
                    break;
                }
            }

            return(packet);
        }
        /// <summary>
        /// Decode MSG_BLK message.
        /// </summary>
        /// <param name="data">Data to be decoded.</param>
        /// <param name="index">Started index.</param>
        /// <returns>The MSG_BLK struct.</returns>
        private MSG_BLK DecodeMSG_BLK(byte[] data, ref int index)
        {
            MSG_BLK ret = new MSG_BLK();
            ret.SizeOfSegmentId = MarshalHelper.GetUInt32(data, ref index, false);
            ret.SegmentId = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId);
            ret.ZeroPad = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfSegmentId % 4);
            if (ret.ZeroPad == null)
            {
                ret.ZeroPad = new byte[0];
            }

            ret.BlockIndex = MarshalHelper.GetUInt32(data, ref index, false);
            ret.NextBlockIndex = MarshalHelper.GetUInt32(data, ref index, false);
            ret.SizeOfBlock = MarshalHelper.GetUInt32(data, ref index, false);
            ret.Block = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfBlock);
            if (ret.Block == null)
            {
                ret.Block = new byte[0];
            }

            ret.ZeroPad2 = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfBlock % 4);
            if (ret.ZeroPad2 == null)
            {
                ret.ZeroPad2 = new byte[0];
            }

            ret.SizeOfVrfBlock = MarshalHelper.GetUInt32(data, ref index, false);
            ret.VrfBlock = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfVrfBlock);
            if (ret.VrfBlock == null)
            {
                ret.VrfBlock = new byte[0];
            }

            ret.ZeroPad3 = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfVrfBlock % 4);
            if (ret.ZeroPad3 == null)
            {
                ret.ZeroPad3 = new byte[0];
            }

            ret.SizeOfIVBlock = MarshalHelper.GetUInt32(data, ref index, false);
            ret.IVBlock = MarshalHelper.GetBytes(data, ref index, (int)ret.SizeOfIVBlock);

            if (ret.IVBlock == null)
            {
                ret.IVBlock = new byte[0];
            }

            return ret;
        }
        /// <summary>
        /// Create a MsgBlk response.
        /// </summary>
        /// <param name="segmentId">The segmentId.</param>
        /// <param name="block">The block.</param>
        /// <param name="cryptoAlgoIdValues">The cryptoAlgoId.</param>
        /// <param name="msgTypeValues">The msgType.</param>
        /// <param name="isLastBLKAvail">If it is true, the block is the last available block.</param>
        /// <returns>The MsgBlk response.</returns>
        public PccrrBLKResponsePacket CreateMsgBlkResponse(
            byte[] segmentId,
            byte[] block,
            CryptoAlgoId_Values cryptoAlgoIdValues,
            MsgType_Values msgTypeValues,
            bool isLastBLKAvail)
        {
            if (segmentId == null)
            {
                segmentId = new byte[0];
            }

            if (block == null)
            {
                block = new byte[0];
            }

            PccrrBLKResponsePacket packet = new PccrrBLKResponsePacket();

            MSG_BLK msgBlkResp = new MSG_BLK();
            byte[] zeroPad = new byte[0] { };
            byte[] zeroPad_2 = new byte[0] { };
            byte[] zeroPad_3 = new byte[0] { };
            byte[] iVBlock = new byte[0] { };
            msgBlkResp.ZeroPad3 = zeroPad_3;
            msgBlkResp.ZeroPad2 = zeroPad_2;
            msgBlkResp.ZeroPad = zeroPad;
            msgBlkResp.VrfBlock = null;
            msgBlkResp.SizeOfVrfBlock = 0;
            msgBlkResp.SizeOfSegmentId = (uint)segmentId.Length;
            msgBlkResp.SizeOfIVBlock = (uint)iVBlock.Length;
            msgBlkResp.SizeOfBlock = (uint)block.Length;
            msgBlkResp.SegmentId = segmentId;
            msgBlkResp.IVBlock = iVBlock;
            msgBlkResp.Block = block;
            msgBlkResp.BlockIndex = 0;

            if (!isLastBLKAvail)
            {
                msgBlkResp.NextBlockIndex = 1;
            }
            else
            {
                msgBlkResp.NextBlockIndex = 0;
            }

            MESSAGE_HEADER messageHeader = PccrrUtitlity.CreateMessageHeader(cryptoAlgoIdValues, msgTypeValues, new ProtoVersion { MajorVersion = 1, MinorVersion = 0 });
            packet.MsgBLK = msgBlkResp;
            packet.MessageHeader = messageHeader;

            return packet;
        }