private static void encodeControlParameters(
				ControlParameters controlParameters, TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeOptionalNonNegativeIntegerTlvFromDouble(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_ExpirationPeriod,
                    controlParameters.getExpirationPeriod());

            // Encode strategy
            if (controlParameters.getStrategy().size() != 0) {
                int strategySaveLength = encoder.getLength();
                encodeName(controlParameters.getStrategy(), new int[1], new int[1],
                        encoder);
                encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Strategy,
                        encoder.getLength() - strategySaveLength);
            }

            // Encode ForwardingFlags
            int flags = controlParameters.getForwardingFlags()
                    .getNfdForwardingFlags();
            if (flags != new ForwardingFlags().getNfdForwardingFlags())
                // The flags are not the default value.
                encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Flags,
                        flags);

            encoder.writeOptionalNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Cost,
                    controlParameters.getCost());
            encoder.writeOptionalNonNegativeIntegerTlv(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Origin, controlParameters.getOrigin());
            encoder.writeOptionalNonNegativeIntegerTlv(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_LocalControlFeature,
                    controlParameters.getLocalControlFeature());

            // Encode URI
            if (controlParameters.getUri().Length != 0) {
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_Uri, new Blob(
                        controlParameters.getUri()).buf());
            }

            encoder.writeOptionalNonNegativeIntegerTlv(
                    net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_FaceId, controlParameters.getFaceId());

            // Encode name
            if (controlParameters.getName() != null) {
                encodeName(controlParameters.getName(), new int[1], new int[1],
                        encoder);
            }

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_ControlParameters,
                    encoder.getLength() - saveLength);
        }
示例#2
0
        /// <summary>
        /// Encode the RepetitiveInterval as NDN-TLV to the encoder.
        /// </summary>
        ///
        /// <param name="repetitiveInterval">The RepetitiveInterval to encode.</param>
        /// <param name="encoder">The TlvEncoder to receive the encoding.</param>
        private static void encodeRepetitiveInterval(
				RepetitiveInterval repetitiveInterval, TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepeatUnit,
                    net.named_data.jndn.encrypt.RepetitiveInterval.getRepeatUnitNumericType(repetitiveInterval
                            .getRepeatUnit()));
            encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_NRepeats,
                    repetitiveInterval.getNRepeats());
            encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_IntervalEndHour,
                    repetitiveInterval.getIntervalEndHour());
            encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_IntervalStartHour,
                    repetitiveInterval.getIntervalStartHour());
            // Use Blob to convert the string to UTF8 encoding.
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EndDate, new Blob(
                    toIsoString(repetitiveInterval.getEndDate())).buf());
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_StartDate, new Blob(
                    toIsoString(repetitiveInterval.getStartDate())).buf());

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepetitiveInterval,
                    encoder.getLength() - saveLength);
        }
        /// <summary>
        /// Encode the EncryptedContent in NDN-TLV and return the encoding.
        /// </summary>
        ///
        /// <param name="encryptedContent">The EncryptedContent object to encode.</param>
        /// <returns>A Blob containing the encoding.</returns>
        public override Blob encodeEncryptedContent(EncryptedContent encryptedContent)
        {
            TlvEncoder encoder = new TlvEncoder(256);
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptedPayload, encryptedContent
                    .getPayload().buf());
            encoder.writeOptionalBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_InitialVector,
                    encryptedContent.getInitialVector().buf());
            // Assume the algorithmType value is the same as the TLV type.
            encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptionAlgorithm,
                    encryptedContent.getAlgorithmType().getNumericType());
            Tlv0_2WireFormat.encodeKeyLocator(net.named_data.jndn.encoding.tlv.Tlv.KeyLocator,
                    encryptedContent.getKeyLocator(), encoder);

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptedContent,
                    encoder.getLength() - saveLength);

            return new Blob(encoder.getOutput(), false);
        }
        /// <summary>
        /// Encode delegationSet as a sequence of NDN-TLV Delegation, and return the
        /// encoding. Note that the sequence of Delegation does not have an outer TLV
        /// type and length because it is intended to use the type and length of a Data
        /// packet's Content.
        /// </summary>
        ///
        /// <param name="delegationSet">The DelegationSet object to encode.</param>
        /// <returns>A Blob containing the encoding.</returns>
        public override Blob encodeDelegationSet(DelegationSet delegationSet)
        {
            TlvEncoder encoder = new TlvEncoder(256);

            // Encode backwards.
            for (int i = delegationSet.size() - 1; i >= 0; --i) {
                int saveLength = encoder.getLength();

                encodeName(delegationSet.get(i).getName(), new int[1], new int[1],
                        encoder);
                encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Link_Preference,
                        delegationSet.get(i).getPreference());

                encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Link_Delegation, encoder.getLength()
                        - saveLength);
            }

            return new Blob(encoder.getOutput(), false);
        }
        /// <summary>
        /// Encode controlResponse in NDN-TLV and return the encoding.
        /// </summary>
        ///
        /// <param name="controlResponse">The ControlResponse object to encode.</param>
        /// <returns>A Blob containing the encoding.</returns>
        public override Blob encodeControlResponse(ControlResponse controlResponse)
        {
            TlvEncoder encoder = new TlvEncoder(256);
            int saveLength = encoder.getLength();

            // Encode backwards.

            // Encode the body.
            if (controlResponse.getBodyAsControlParameters() != null)
                encodeControlParameters(
                        controlResponse.getBodyAsControlParameters(), encoder);

            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusText, new Blob(
                    controlResponse.getStatusText()).buf());
            encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusCode,
                    controlResponse.getStatusCode());

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_ControlResponse,
                    encoder.getLength() - saveLength);

            return new Blob(encoder.getOutput(), false);
        }
        /// <summary>
        /// An internal method to encode signature as the appropriate form of
        /// SignatureInfo in NDN-TLV.
        /// </summary>
        ///
        /// <param name="signature">An object of a subclass of Signature to encode.</param>
        /// <param name="encoder">The TlvEncoder to receive the encoding.</param>
        private void encodeSignatureInfo(Signature signature, TlvEncoder encoder)
        {
            if (signature  is  GenericSignature) {
                // Handle GenericSignature separately since it has the entire encoding.
                Blob encoding = ((GenericSignature) signature)
                        .getSignatureInfoEncoding();

                // Do a test decoding to sanity check that it is valid TLV.
                try {
                    TlvDecoder decoder = new TlvDecoder(encoding.buf());
                    int endOffset = decoder.readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.SignatureInfo);
                    decoder.readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureType);
                    decoder.finishNestedTlvs(endOffset);
                } catch (EncodingException ex) {
                    throw new Exception(
                            "The GenericSignature encoding is not a valid NDN-TLV SignatureInfo: "
                                    + ex.Message);
                }

                encoder.writeBuffer(encoding.buf());
                return;
            }

            int saveLength = encoder.getLength();

            // Encode backwards.
            if (signature  is  Sha256WithRsaSignature) {
                if (((Sha256WithRsaSignature) signature).getValidityPeriod()
                        .hasPeriod())
                    encodeValidityPeriod(
                            ((Sha256WithRsaSignature) signature)
                                    .getValidityPeriod(),
                            encoder);
                encodeKeyLocator(net.named_data.jndn.encoding.tlv.Tlv.KeyLocator,
                        ((Sha256WithRsaSignature) signature).getKeyLocator(),
                        encoder);
                encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureType,
                        net.named_data.jndn.encoding.tlv.Tlv.SignatureType_SignatureSha256WithRsa);
            } else if (signature  is  Sha256WithEcdsaSignature) {
                if (((Sha256WithEcdsaSignature) signature).getValidityPeriod()
                        .hasPeriod())
                    encodeValidityPeriod(
                            ((Sha256WithEcdsaSignature) signature)
                                    .getValidityPeriod(),
                            encoder);
                encodeKeyLocator(net.named_data.jndn.encoding.tlv.Tlv.KeyLocator,
                        ((Sha256WithEcdsaSignature) signature).getKeyLocator(),
                        encoder);
                encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureType,
                        net.named_data.jndn.encoding.tlv.Tlv.SignatureType_SignatureSha256WithEcdsa);
            } else if (signature  is  HmacWithSha256Signature) {
                encodeKeyLocator(net.named_data.jndn.encoding.tlv.Tlv.KeyLocator,
                        ((HmacWithSha256Signature) signature).getKeyLocator(),
                        encoder);
                encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureType,
                        net.named_data.jndn.encoding.tlv.Tlv.SignatureType_SignatureHmacWithSha256);
            } else if (signature  is  DigestSha256Signature)
                encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureType,
                        net.named_data.jndn.encoding.tlv.Tlv.SignatureType_DigestSha256);
            else
                throw new Exception(
                        "encodeSignatureInfo: Unrecognized Signature object type");

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.SignatureInfo, encoder.getLength()
                    - saveLength);
        }
        private static void encodeMetaInfo(MetaInfo metaInfo, TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            ByteBuffer finalBlockIdBuf = metaInfo.getFinalBlockId().getValue()
                    .buf();
            if (finalBlockIdBuf != null && finalBlockIdBuf.remaining() > 0) {
                // FinalBlockId has an inner NameComponent.
                int finalBlockIdSaveLength = encoder.getLength();
                encodeNameComponent(metaInfo.getFinalBlockId(), encoder);
                encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.FinalBlockId, encoder.getLength()
                        - finalBlockIdSaveLength);
            }

            encoder.writeOptionalNonNegativeIntegerTlvFromDouble(
                    net.named_data.jndn.encoding.tlv.Tlv.FreshnessPeriod, metaInfo.getFreshnessPeriod());
            if (!(metaInfo.getType() == net.named_data.jndn.ContentType.BLOB)) {
                // Not the default, so we need to encode the type.
                if (metaInfo.getType() == net.named_data.jndn.ContentType.LINK
                        || metaInfo.getType() == net.named_data.jndn.ContentType.KEY
                        || metaInfo.getType() == net.named_data.jndn.ContentType.NACK)
                    // The ContentType enum is set up with the correct integer for
                    // each NDN-TLV ContentType.
                    encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ContentType, metaInfo
                            .getType().getNumericType());
                else if (metaInfo.getType() == net.named_data.jndn.ContentType.OTHER_CODE)
                    encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.ContentType,
                            metaInfo.getOtherTypeCode());
                else
                    // We don't expect this to happen.
                    throw new Exception("unrecognized TLV ContentType");
            }

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.MetaInfo, encoder.getLength()
                    - saveLength);
        }