コード例 #1
0
ファイル: Schedule.cs プロジェクト: named-data/ndn-dot-net
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Encode the signatureValue in the Signature object as a SignatureValue (the
        /// signature bits) in NDN-TLV and return the encoding.
        /// </summary>
        ///
        /// <param name="signature"></param>
        /// <returns>A Blob containing the encoding.</returns>
        public override Blob encodeSignatureValue(Signature signature)
        {
            TlvEncoder encoder = new TlvEncoder(256);
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureValue, signature.getSignature().buf());

            return new Blob(encoder.getOutput(), false);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
        /// <summary>
        /// Encode interest using NDN-TLV and return the encoding.
        /// </summary>
        ///
        /// <param name="interest">The Interest object to encode.</param>
        /// <param name="signedPortionBeginOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param>
        /// <param name="signedPortionEndOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param>
        /// <returns>A Blob containing the encoding.</returns>
        public override Blob encodeInterest(Interest interest,
				int[] signedPortionBeginOffset, int[] signedPortionEndOffset)
        {
            TlvEncoder encoder = new TlvEncoder();
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeOptionalNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SelectedDelegation,
                    interest.getSelectedDelegationIndex());
            try {
                Blob linkWireEncoding = interest.getLinkWireEncoding(this);
                if (!linkWireEncoding.isNull())
                    // Encode the entire link as is.
                    encoder.writeBuffer(linkWireEncoding.buf());
            } catch (EncodingException ex) {
                throw new Exception(ex.Message);
            }

            encoder.writeOptionalNonNegativeIntegerTlvFromDouble(
                    net.named_data.jndn.encoding.tlv.Tlv.InterestLifetime,
                    interest.getInterestLifetimeMilliseconds());

            // Encode the Nonce as 4 bytes.
            if (interest.getNonce().size() == 0) {
                // This is the most common case. Generate a nonce.
                ByteBuffer nonce = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(4);
                random_.nextBytes(nonce.array());
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce);
            } else if (interest.getNonce().size() < 4) {
                ByteBuffer nonce_0 = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(4);
                // Copy existing nonce bytes.
                nonce_0.put(interest.getNonce().buf());

                // Generate random bytes for remaining bytes in the nonce.
                for (int i = 0; i < 4 - interest.getNonce().size(); ++i)
                    nonce_0.put((byte) random_.Next());

                nonce_0.flip();
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce_0);
            } else if (interest.getNonce().size() == 4)
                // Use the nonce as-is.
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, interest.getNonce().buf());
            else {
                // Truncate.
                ByteBuffer nonce_1 = interest.getNonce().buf();
                // buf() returns a new ByteBuffer, so we can change its limit.
                nonce_1.limit(nonce_1.position() + 4);
                encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce, nonce_1);
            }

            encodeSelectors(interest, encoder);
            int[] tempSignedPortionBeginOffset = new int[1];
            int[] tempSignedPortionEndOffset = new int[1];
            encodeName(interest.getName(), tempSignedPortionBeginOffset,
                    tempSignedPortionEndOffset, encoder);
            int signedPortionBeginOffsetFromBack = encoder.getLength()
                    - tempSignedPortionBeginOffset[0];
            int signedPortionEndOffsetFromBack = encoder.getLength()
                    - tempSignedPortionEndOffset[0];

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.Interest, encoder.getLength()
                    - saveLength);
            signedPortionBeginOffset[0] = encoder.getLength()
                    - signedPortionBeginOffsetFromBack;
            signedPortionEndOffset[0] = encoder.getLength()
                    - signedPortionEndOffsetFromBack;

            return new Blob(encoder.getOutput(), false);
        }
コード例 #6
0
        /// <summary>
        /// Encode data in NDN-TLV and return the encoding.
        /// </summary>
        ///
        /// <param name="data">The Data object to encode.</param>
        /// <param name="signedPortionBeginOffset">If you are not encoding in order to sign, you can call encodeData(data) to ignore this returned value.</param>
        /// <param name="signedPortionEndOffset">If you are not encoding in order to sign, you can call encodeData(data) to ignore this returned value.</param>
        /// <returns>A Blob containing the encoding.</returns>
        public override Blob encodeData(Data data, int[] signedPortionBeginOffset,
				int[] signedPortionEndOffset)
        {
            TlvEncoder encoder = new TlvEncoder(1500);
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureValue, (data.getSignature())
                    .getSignature().buf());
            int signedPortionEndOffsetFromBack = encoder.getLength();

            encodeSignatureInfo(data.getSignature(), encoder);
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Content, data.getContent().buf());
            encodeMetaInfo(data.getMetaInfo(), encoder);
            encodeName(data.getName(), new int[1], new int[1], encoder);
            int signedPortionBeginOffsetFromBack = encoder.getLength();

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

            signedPortionBeginOffset[0] = encoder.getLength()
                    - signedPortionBeginOffsetFromBack;
            signedPortionEndOffset[0] = encoder.getLength()
                    - signedPortionEndOffsetFromBack;
            return new Blob(encoder.getOutput(), false);
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        private static void encodeValidityPeriod(ValidityPeriod validityPeriod,
				TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.ValidityPeriod_NotAfter,
                    new Blob(net.named_data.jndn.encrypt.Schedule.toIsoString(validityPeriod.getNotAfter()))
                            .buf());
            encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.ValidityPeriod_NotBefore,
                    new Blob(net.named_data.jndn.encrypt.Schedule.toIsoString(validityPeriod.getNotBefore()))
                            .buf());

            encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.ValidityPeriod_ValidityPeriod,
                    encoder.getLength() - saveLength);
        }
コード例 #9
0
        /// <summary>
        /// Encode the name component to the encoder as NDN-TLV. This handles different
        /// component types such as ImplicitSha256DigestComponent.
        /// </summary>
        ///
        /// <param name="component">The name component to encode.</param>
        /// <param name="encoder">The TlvEncoder to receive the encoding.</param>
        private static void encodeNameComponent(Name.Component component,
				TlvEncoder encoder)
        {
            int type = (component.isImplicitSha256Digest()) ? net.named_data.jndn.encoding.tlv.Tlv.ImplicitSha256DigestComponent
                    : net.named_data.jndn.encoding.tlv.Tlv.NameComponent;
            encoder.writeBlobTlv(type, component.getValue().buf());
        }
コード例 #10
0
        private static void encodeKeyLocator(int type, KeyLocator keyLocator,
				TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            if (keyLocator.getType() != net.named_data.jndn.KeyLocatorType.NONE) {
                if (keyLocator.getType() == net.named_data.jndn.KeyLocatorType.KEYNAME)
                    encodeName(keyLocator.getKeyName(), new int[1], new int[1],
                            encoder);
                else if (keyLocator.getType() == net.named_data.jndn.KeyLocatorType.KEY_LOCATOR_DIGEST
                        && keyLocator.getKeyData().size() > 0)
                    encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.KeyLocatorDigest, keyLocator
                            .getKeyData().buf());
                else
                    throw new Exception("Unrecognized KeyLocatorType "
                            + keyLocator.getType());
            }

            encoder.writeTypeAndLength(type, encoder.getLength() - saveLength);
        }