The PccrrGETBLKSRequestPacket is sent by the client.
Inheritance: PccrrPacket
Example #1
0
        /// <summary>
        /// Create a MsgGetBlks request.
        /// </summary>
        /// <param name="segmentId">The segmentId.</param>
        /// <param name="cryptoAlgoIdValues">The cryptoAlgoId.</param>
        /// <param name="msgTypeValues">The msgType.</param>
        /// <returns>MsgGetBlks request.</returns>
        public PccrrGETBLKSRequestPacket CreateMsgGetBlksRequest(
            byte[] segmentId,
            CryptoAlgoId_Values cryptoAlgoIdValues,
            MsgType_Values msgTypeValues)
        {
            if (segmentId == null)
            {
                segmentId = new byte[0];
            }

            PccrrGETBLKSRequestPacket packet = new PccrrGETBLKSRequestPacket();

            MSG_GETBLKS msgGetBlksReq = new MSG_GETBLKS();

            msgGetBlksReq.DataForVrfBlock = null;
            byte[] zeroPad = new byte[0] {
            };
            BLOCK_RANGE[] reqBlockRanges = new BLOCK_RANGE[1];
            reqBlockRanges[0].Index             = 0;
            reqBlockRanges[0].Count             = 1;
            msgGetBlksReq.ReqBlockRanges        = reqBlockRanges;
            msgGetBlksReq.ReqBlockRangeCount    = (uint)reqBlockRanges.Length;
            msgGetBlksReq.ZeroPad               = zeroPad;
            msgGetBlksReq.SizeOfSegmentID       = (uint)segmentId.Length;
            msgGetBlksReq.SizeOfDataForVrfBlock = 0;
            msgGetBlksReq.SegmentID             = segmentId;
            MESSAGE_HEADER messageHeader = PccrrUtitlity.CreateMessageHeader(cryptoAlgoIdValues, msgTypeValues, new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            });

            packet.MsgGetBLKS    = msgGetBlksReq;
            packet.MessageHeader = messageHeader;

            return(packet);
        }
Example #2
0
        /// <summary>
        /// Decode request message.
        /// </summary>
        /// <param name="rawdata">The raw data.</param>
        /// <param name="uri">The request uri.</param>
        /// <param name="method">The request method.</param>
        /// <returns>The PccrrPacket.</returns>
        public PccrrPacket DecodeRequestMessage(byte[] rawdata, Uri uri, HttpMethod method)
        {
            if (rawdata == null)
            {
                throw new ArgumentNullException("rawdata");
            }

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

            int messageLength = 0;

            messageLength = rawdata.Length;

            PccrrPacket packet = null;

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

                REQUEST_MESSAGE ret = new REQUEST_MESSAGE();
                ret.MESSAGEHEADER = this.DecodeMessageHeader(rawdata, ref index);

                switch (ret.MESSAGEHEADER.MsgType)
                {
                case MsgType_Values.MSG_NEGO_REQ:
                    PccrrNegoRequestPacket pccrrNegoRequestPacket = new PccrrNegoRequestPacket();

                    MSG_NEGO_REQ msgNEGOREQ = this.DecodeMSG_NEGO_REQ(rawdata, ref index);
                    pccrrNegoRequestPacket.MsgNegoReq    = msgNEGOREQ;
                    pccrrNegoRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                    pccrrNegoRequestPacket.Method        = method;
                    pccrrNegoRequestPacket.Uri           = uri;
                    packet = pccrrNegoRequestPacket;
                    break;

                case MsgType_Values.MSG_GETBLKLIST:
                    PccrrGETBLKLISTRequestPacket pccrrGETBLKLISTRequestPacket = new PccrrGETBLKLISTRequestPacket();

                    MSG_GETBLKLIST msgGETBLKLIST = this.DecodeMSG_GETBLKLIST(rawdata, ref index);
                    pccrrGETBLKLISTRequestPacket.MsgGetBLKLIST = msgGETBLKLIST;
                    pccrrGETBLKLISTRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                    pccrrGETBLKLISTRequestPacket.Method        = method;
                    pccrrGETBLKLISTRequestPacket.Uri           = uri;
                    packet = pccrrGETBLKLISTRequestPacket;
                    break;

                case MsgType_Values.MSG_GETBLKS:
                    PccrrGETBLKSRequestPacket pccrrGETBLKSRequestPacket = new PccrrGETBLKSRequestPacket();

                    MSG_GETBLKS msgGETBLKS = this.DecodeMSG_GETBLKS(rawdata, ref index);
                    pccrrGETBLKSRequestPacket.MsgGetBLKS    = msgGETBLKS;
                    pccrrGETBLKSRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                    pccrrGETBLKSRequestPacket.Method        = method;
                    pccrrGETBLKSRequestPacket.Uri           = uri;
                    packet = pccrrGETBLKSRequestPacket;
                    break;

                case MsgType_Values.MSG_GETSEGLIST:
                    PccrrGetSegListRequestPacket pccrrGetSegListRequestPacket = new PccrrGetSegListRequestPacket();
                    pccrrGetSegListRequestPacket.MsgGetSegList = TypeMarshal.ToStruct <MSG_GETSEGLIST>(rawdata, ref index);
                    pccrrGetSegListRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                    pccrrGetSegListRequestPacket.Method        = method;
                    pccrrGetSegListRequestPacket.Uri           = uri;
                    packet = pccrrGetSegListRequestPacket;
                    break;
                }
            }

            return(packet);
        }
        /// <summary>
        /// Decode request message.
        /// </summary>
        /// <param name="rawdata">The raw data.</param>
        /// <param name="uri">The request uri.</param>
        /// <param name="method">The request method.</param>
        /// <returns>The PccrrPacket.</returns>
        public PccrrPacket DecodeRequestMessage(byte[] rawdata, Uri uri, HttpMethod method)
        {
            if (rawdata == null)
            {
                throw new ArgumentNullException("rawdata");
            }

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

            int messageLength = 0;

            messageLength = rawdata.Length;

            PccrrPacket packet = null;

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

                REQUEST_MESSAGE ret = new REQUEST_MESSAGE();
                ret.MESSAGEHEADER = this.DecodeMessageHeader(rawdata, ref index);

                switch (ret.MESSAGEHEADER.MsgType)
                {
                    case MsgType_Values.MSG_NEGO_REQ:
                        PccrrNegoRequestPacket pccrrNegoRequestPacket = new PccrrNegoRequestPacket();

                        MSG_NEGO_REQ msgNEGOREQ = this.DecodeMSG_NEGO_REQ(rawdata, ref index);
                        pccrrNegoRequestPacket.MsgNegoReq = msgNEGOREQ;
                        pccrrNegoRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                        pccrrNegoRequestPacket.Method = method;
                        pccrrNegoRequestPacket.Uri = uri;
                        packet = pccrrNegoRequestPacket;
                        break;
                    case MsgType_Values.MSG_GETBLKLIST:
                        PccrrGETBLKLISTRequestPacket pccrrGETBLKLISTRequestPacket = new PccrrGETBLKLISTRequestPacket();

                        MSG_GETBLKLIST msgGETBLKLIST = this.DecodeMSG_GETBLKLIST(rawdata, ref index);
                        pccrrGETBLKLISTRequestPacket.MsgGetBLKLIST = msgGETBLKLIST;
                        pccrrGETBLKLISTRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                        pccrrGETBLKLISTRequestPacket.Method = method;
                        pccrrGETBLKLISTRequestPacket.Uri = uri;
                        packet = pccrrGETBLKLISTRequestPacket;
                        break;
                    case MsgType_Values.MSG_GETBLKS:
                        PccrrGETBLKSRequestPacket pccrrGETBLKSRequestPacket = new PccrrGETBLKSRequestPacket();

                        MSG_GETBLKS msgGETBLKS = this.DecodeMSG_GETBLKS(rawdata, ref index);
                        pccrrGETBLKSRequestPacket.MsgGetBLKS = msgGETBLKS;
                        pccrrGETBLKSRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                        pccrrGETBLKSRequestPacket.Method = method;
                        pccrrGETBLKSRequestPacket.Uri = uri;
                        packet = pccrrGETBLKSRequestPacket;
                        break;

                    case MsgType_Values.MSG_GETSEGLIST:
                        PccrrGetSegListRequestPacket pccrrGetSegListRequestPacket = new PccrrGetSegListRequestPacket();
                        pccrrGetSegListRequestPacket.MsgGetSegList = TypeMarshal.ToStruct<MSG_GETSEGLIST>(rawdata, ref index);
                        pccrrGetSegListRequestPacket.MessageHeader = ret.MESSAGEHEADER;
                        pccrrGetSegListRequestPacket.Method = method;
                        pccrrGetSegListRequestPacket.Uri = uri;
                        packet = pccrrGetSegListRequestPacket;
                        break;
                }
            }

            return packet;
        }
        /// <summary>
        /// Create a MsgGetBlks request.
        /// </summary>
        /// <param name="segmentId">The segmentId.</param>
        /// <param name="cryptoAlgoIdValues">The cryptoAlgoId.</param>
        /// <param name="msgTypeValues">The msgType.</param>
        /// <returns>MsgGetBlks request.</returns>
        public PccrrGETBLKSRequestPacket CreateMsgGetBlksRequest(
            byte[] segmentId,
            CryptoAlgoId_Values cryptoAlgoIdValues,
            MsgType_Values msgTypeValues)
        {
            if (segmentId == null)
            {
                segmentId = new byte[0];
            }

            PccrrGETBLKSRequestPacket packet = new PccrrGETBLKSRequestPacket();

            MSG_GETBLKS msgGetBlksReq = new MSG_GETBLKS();
            msgGetBlksReq.DataForVrfBlock = null;
            byte[] zeroPad = new byte[0] { };
            BLOCK_RANGE[] reqBlockRanges = new BLOCK_RANGE[1];
            reqBlockRanges[0].Index = 0;
            reqBlockRanges[0].Count = 1;
            msgGetBlksReq.ReqBlockRanges = reqBlockRanges;
            msgGetBlksReq.ReqBlockRangeCount = (uint)reqBlockRanges.Length;
            msgGetBlksReq.ZeroPad = zeroPad;
            msgGetBlksReq.SizeOfSegmentID = (uint)segmentId.Length;
            msgGetBlksReq.SizeOfDataForVrfBlock = 0;
            msgGetBlksReq.SegmentID = segmentId;
            MESSAGE_HEADER messageHeader = PccrrUtitlity.CreateMessageHeader(cryptoAlgoIdValues, msgTypeValues, new ProtoVersion { MajorVersion = 1, MinorVersion = 0 });
            packet.MsgGetBLKS = msgGetBlksReq;
            packet.MessageHeader = messageHeader;

            return packet;
        }
        protected override void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest)
        {
            uint offset = 0;
            int chunkIndex = -1;
            int segmentIndex = -1;
            for (int i = 0; i < contentInformationV2.chunks.Length; i++)
            {
                var chunk = contentInformationV2.chunks[i];
                for (int j = 0; j < chunk.chunkData.Length; j++)
                {
                    if (Enumerable.SequenceEqual(contentInformationV2.GetSegmentId(i, j), pccrrGetBlkRequest.MsgGetBLKS.SegmentID))
                    {
                        chunkIndex = i;
                        segmentIndex = j;
                        break;
                    }
                    else
                    {
                        offset += chunk.chunkData[j].cbSegment;
                    }
                }
            }
            PccrrBLKResponsePacket pccrrBlocksResponse;
            if (segmentIndex == -1) // Mached segement not found
            {
                pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                    pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                    new byte[0],
                    cryptoAlgoId,
                    MsgType_Values.MSG_BLK,
                    iv,
                    0,
                    0);
            }
            else
            {
                var segment = contentInformationV2.chunks[chunkIndex].chunkData[segmentIndex];

                byte[] block = content.Skip((int)offset).Take((int)segment.cbSegment).ToArray();

                if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    block = PccrrUtitlity.Encrypt(aes, block, contentInformationV2.chunks[chunkIndex].chunkData[segmentIndex].SegmentSecret, iv);

                pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                    pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                    block,
                    cryptoAlgoId,
                    MsgType_Values.MSG_BLK,
                    iv,
                    0,
                    0);
            }
            pccrrServer.SendPacket(pccrrBlocksResponse);
        }
        protected override void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest)
        {
            int segmentIndex = -1;
            for (int i = 0; i < contentInformation.cSegments; i++)
            {
                if (Enumerable.SequenceEqual(contentInformation.GetSegmentId(i), pccrrGetBlkRequest.MsgGetBLKS.SegmentID))
                {
                    segmentIndex = i;
                    break;
                }
            }

            PccrrBLKResponsePacket pccrrBlocksResponse;
            if (segmentIndex == -1) // Mached segement not found
            {
                pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                    pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                    new byte[0],
                    cryptoAlgoId,
                    MsgType_Values.MSG_BLK,
                    this.iv,
                    0,
                    0);
                pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                    pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                    new byte[0],
                    cryptoAlgoId,
                    MsgType_Values.MSG_BLK,
                    iv,
                    0,
                    0);
            }
            else
            {
                ulong blockSize = PccrcConsts.V1BlockSize;
                byte[] block = content.Skip((int)(contentInformation.segments[segmentIndex].ullOffsetInContent + pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index * blockSize)).Take((int)blockSize).ToArray();

                if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    block = PccrrUtitlity.Encrypt(aes, block, contentInformation.segments[segmentIndex].SegmentSecret, iv);

                pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                    pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                    block,
                    cryptoAlgoId,
                    MsgType_Values.MSG_BLK,
                    this.iv,
                    pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index,
                    pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index == contentInformation.cSegments - 1 ? 0 : pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index + 1);
            }
            pccrrServer.SendPacket(pccrrBlocksResponse);
        }
 protected abstract void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest);
            protected override void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest)
            {
                uint offset = 0;
                int chunkIndex = -1;
                int segmentIndex = -1;
                for (int i = 0; i < contentInformationV2.chunks.Length; i++)
                {
                    var chunk = contentInformationV2.chunks[i];
                    for (int j = 0; j < chunk.chunkData.Length; j++)
                    {
                        if (Enumerable.SequenceEqual(contentInformationV2.GetSegmentId(i, j), pccrrGetBlkRequest.MsgGetBLKS.SegmentID))
                        {
                            chunkIndex = i;
                            segmentIndex = j;
                            break;
                        }
                        else
                        {
                            offset += chunk.chunkData[j].cbSegment;
                        }
                    }
                }
                PccrrBLKResponsePacket pccrrBlocksResponse;
                if (segmentIndex == -1) // Mached segement not found
                {
                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        new byte[0],
                        cryptoAlgoId,
                        (MsgType_Values)0xFEFE,
                        iv,
                        0,
                        0);
                }
                else
                {
                    var segment = contentInformationV2.chunks[chunkIndex].chunkData[segmentIndex];

                    byte[] block = content.Skip((int)offset).Take((int)segment.cbSegment).ToArray();

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                        block = PccrrUtitlity.Encrypt(aes, block, contentInformationV2.chunks[chunkIndex].chunkData[segmentIndex].SegmentSecret, iv);

                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        block,
                        cryptoAlgoId,
                        (MsgType_Values)0xFEFE,
                        iv,
                        0,
                        0);
                }

                TestTools.StackSdk.BranchCache.Pccrr.MESSAGE_HEADER header = pccrrBlocksResponse.MessageHeader;
                header.ProtVer = new ProtoVersion { MajorVersion = 0xFEFE, MinorVersion = 0xFEFE };
                pccrrBlocksResponse.MessageHeader = header;

                pccrrServer.SendPacket(pccrrBlocksResponse);
            }
            protected override void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest)
            {
                int segmentIndex = -1;
                for (int i = 0; i < contentInformation.cSegments; i++)
                {
                    if (Enumerable.SequenceEqual(contentInformation.GetSegmentId(i), pccrrGetBlkRequest.MsgGetBLKS.SegmentID))
                    {
                        segmentIndex = i;
                        break;
                    }
                }

                PccrrBLKResponsePacket pccrrBlocksResponse;
                if (segmentIndex == -1) // Mached segement not found
                {
                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        new byte[0],
                        cryptoAlgoId,
                        MsgType_Values.MSG_BLK,
                        iv,
                        0,
                        0);
                }
                else
                {
                    ulong blockSize = PccrcConsts.V1BlockSize;
                    byte[] block = content.Skip((int)(contentInformation.segments[segmentIndex].ullOffsetInContent + pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index * blockSize)).Take((int)blockSize).ToArray();

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                        block = PccrrUtitlity.Encrypt(aes, block, contentInformation.segments[segmentIndex].SegmentSecret, iv);

                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        new byte[0],
                        cryptoAlgoId,
                        MsgType_Values.MSG_BLK,
                        iv,
                        0,
                        0);
                }

                TestTools.StackSdk.BranchCache.Pccrr.MESSAGE_HEADER header = pccrrBlocksResponse.MessageHeader;
                header.ProtVer = new ProtoVersion { MajorVersion = 0xFEFE, MinorVersion = 0xFEFE };
                pccrrBlocksResponse.MessageHeader = header;

                pccrrServer.SendPacket(pccrrBlocksResponse);
            }