Beispiel #1
0
    def encodeEncryptedContent(self, encryptedContent):
        """
        Encode the EncryptedContent in NDN-TLV and return the encoding.

        :param EncryptedContent encryptedContent: The EncryptedContent object to
          encode.
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        encoder.writeBlobTlv(
          Tlv.Encrypt_EncryptedPayload, encryptedContent.getPayload().buf())
        encoder.writeOptionalBlobTlv(
          Tlv.Encrypt_InitialVector, encryptedContent.getInitialVector().buf())
        # Assume the algorithmType value is the same as the TLV type.
        encoder.writeNonNegativeIntegerTlv(
          Tlv.Encrypt_EncryptionAlgorithm, encryptedContent.getAlgorithmType())
        Tlv0_1_1WireFormat._encodeKeyLocator(
          Tlv.KeyLocator, encryptedContent.getKeyLocator(), encoder)

        encoder.writeTypeAndLength(
          Tlv.Encrypt_EncryptedContent, len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
Beispiel #2
0
    def encodeForwardingEntry(self, forwardingEntry):
        """
        Encode forwardingEntry and return the encoding.

        :param forwardingEntry: The ForwardingEntry object to encode.
        :type forwardingEntry: ForwardingEntry
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
            Tlv.FreshnessPeriod, forwardingEntry.getFreshnessPeriod())
        encoder.writeNonNegativeIntegerTlv(
            Tlv.ForwardingFlags,
            forwardingEntry.getForwardingFlags().getForwardingEntryFlags())
        encoder.writeOptionalNonNegativeIntegerTlv(Tlv.FaceID,
                                                   forwardingEntry.getFaceId())
        self._encodeName(forwardingEntry.getPrefix(), encoder)
        if (forwardingEntry.getAction() != None
                and len(forwardingEntry.getAction()) > 0):
            # Convert str to a bytearray.
            encoder.writeBlobTlv(
                Tlv.Action, bytearray(forwardingEntry.getAction(), 'ascii'))

        encoder.writeTypeAndLength(Tlv.ForwardingEntry,
                                   len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
    def encodeForwardingEntry(self, forwardingEntry):
        """
        Encode forwardingEntry and return the encoding.

        :param forwardingEntry: The ForwardingEntry object to encode.
        :type forwardingEntry: ForwardingEntry
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
          Tlv.FreshnessPeriod, forwardingEntry.getFreshnessPeriod())
        encoder.writeNonNegativeIntegerTlv(
          Tlv.ForwardingFlags,
          forwardingEntry.getForwardingFlags().getForwardingEntryFlags())
        encoder.writeOptionalNonNegativeIntegerTlv(
          Tlv.FaceID, forwardingEntry.getFaceId())
        self._encodeName(forwardingEntry.getPrefix(), encoder)
        if (forwardingEntry.getAction() != None and
             len(forwardingEntry.getAction()) > 0):
            # Convert str to a bytearray.
            encoder.writeBlobTlv(
              Tlv.Action, bytearray(forwardingEntry.getAction(), 'ascii'))

        encoder.writeTypeAndLength(Tlv.ForwardingEntry,
                                   len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
Beispiel #4
0
    def encodeDelegationSet(self, delegationSet):
        """
        Encode the DelegationSet in NDN-TLV 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.

        :param DelegationSet delegationSet: The DelegationSet object to
          encode.
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)

        # Encode backwards.
        for i in range(delegationSet.size() - 1, -1, -1):
            saveLength = len(encoder)

            Tlv0_1_1WireFormat._encodeName(delegationSet.get(i).getName(), encoder)
            encoder.writeNonNegativeIntegerTlv(
              Tlv.Link_Preference, delegationSet.get(i).getPreference())

            encoder.writeTypeAndLength(
              Tlv.Link_Delegation, len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
Beispiel #5
0
    def encodeControlResponse(self, controlResponse):
        """
        Encode controlResponse and return the encoding.

        :param controlResponse: The ControlResponse object to encode.
        :type controlResponse: ControlResponse
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.

        # Encode the body.
        if controlResponse.getBodyAsControlParameters() != None:
            self._encodeControlParameters(
              controlResponse.getBodyAsControlParameters(), encoder)

        encoder.writeBlobTlv(
          Tlv.NfdCommand_StatusText, Blob(controlResponse.getStatusText()).buf())
        encoder.writeNonNegativeIntegerTlv(
          Tlv.NfdCommand_StatusCode, controlResponse.getStatusCode())

        encoder.writeTypeAndLength(Tlv.NfdCommand_ControlResponse,
                                   len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
Beispiel #6
0
    def encodeStateVector(stateVector, stateVectorKeys):
        """
        Encode the stateVector as TLV.

        :param dict<str,int> stateVector: The state vector dictionary where
          the key is the member ID string and the value is the sequence number.
        :param list<str> stateVectorKeys: The key strings of stateVector,
          sorted in the order to be encoded.
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        for i in range(len(stateVectorKeys) - 1, -1, -1):
            saveLengthForEntry = len(encoder)

            encoder.writeNonNegativeIntegerTlv(
                StateVectorSync2018.TLV_StateVector_SequenceNumber,
                stateVector[stateVectorKeys[i]])
            encoder.writeBlobTlv(StateVectorSync2018.TLV_StateVector_MemberId,
                                 Blob(stateVectorKeys[i]).buf())
            encoder.writeTypeAndLength(
                StateVectorSync2018.TLV_StateVectorEntry,
                len(encoder) - saveLengthForEntry)

        encoder.writeTypeAndLength(StateVectorSync2018.TLV_StateVector,
                                   len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
    def wireEncode(self):
        encoder = TlvEncoder(500)
        saveLength = len(encoder)
        
        # if self.getValidityPeriod().hasPeriod():
        #     Tlv0_2WireFormat._encodeValidityPeriod(self.getValidityPeriod(), encoder)
        Tlv0_2WireFormat._encodeKeyLocator(Tlv.KeyLocator, self.getKeyLocator(), encoder)
        encoder.writeNonNegativeIntegerTlv(Tlv.SignatureType, Tlv.SignatureType_Sha256WithAbsSignature)
        encoder.writeTypeAndLength(Tlv.SignatureInfo, len(encoder) - saveLength)

        self.setSignatureInfoEncoding(Blob(encoder.getOutput(), False), Tlv.SignatureType_Sha256WithAbsSignature)
Beispiel #8
0
    def encodeControlParameters(self, controlParameters):
        """
        Encode controlParameters and return the encoding.

        :param controlParameters: The ControlParameters object to encode.
        :type controlParameters: ControlParameters
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
            Tlv.ControlParameters_ExpirationPeriod,
            controlParameters.getExpirationPeriod())

        if controlParameters.getStrategy().size() > 0:
            strategySaveLength = len(encoder)
            self._encodeName(controlParameters.getStrategy(), encoder)
            encoder.writeTypeAndLength(Tlv.ControlParameters_Strategy,
                                       len(encoder) - strategySaveLength)

        flags = controlParameters.getForwardingFlags().getNfdForwardingFlags()
        if (flags != ForwardingFlags().getNfdForwardingFlags()):
            # The flags are not the default value.
            encoder.writeNonNegativeIntegerTlv(Tlv.ControlParameters_Flags,
                                               flags)

        encoder.writeOptionalNonNegativeIntegerTlv(Tlv.ControlParameters_Cost,
                                                   controlParameters.getCost())
        encoder.writeOptionalNonNegativeIntegerTlv(
            Tlv.ControlParameters_Origin, controlParameters.getOrigin())
        encoder.writeOptionalNonNegativeIntegerTlv(
            Tlv.ControlParameters_LocalControlFeature,
            controlParameters.getLocalControlFeature())

        if len(controlParameters.getUri()) != 0:
            encoder.writeBlobTlv(Tlv.ControlParameters_Uri,
                                 Blob(controlParameters.getUri()).buf())

        encoder.writeOptionalNonNegativeIntegerTlv(
            Tlv.ControlParameters_FaceId, controlParameters.getFaceId())
        if controlParameters.getName() != None:
            self._encodeName(controlParameters.getName(), encoder)

        encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters,
                                   len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
    def encodeControlParameters(self, controlParameters):
        """
        Encode controlParameters and return the encoding.

        :param controlParameters: The ControlParameters object to encode.
        :type controlParameters: ControlParameters
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
          Tlv.ControlParameters_ExpirationPeriod,
          controlParameters.getExpirationPeriod())

        # TODO: Encode Strategy.

        flags = controlParameters.getForwardingFlags().getNfdForwardingFlags()
        if (flags != ForwardingFlags().getNfdForwardingFlags()):
            # The flags are not the default value.
            encoder.writeNonNegativeIntegerTlv(
              Tlv.ControlParameters_Flags, flags)

        encoder.writeOptionalNonNegativeIntegerTlv(
          Tlv.ControlParameters_Cost, controlParameters.getCost())
        encoder.writeOptionalNonNegativeIntegerTlv(
          Tlv.ControlParameters_Origin, controlParameters.getOrigin())
        encoder.writeOptionalNonNegativeIntegerTlv(
          Tlv.ControlParameters_LocalControlFeature,
          controlParameters.getLocalControlFeature())

        # TODO: Encode Uri.

        encoder.writeOptionalNonNegativeIntegerTlv(
          Tlv.ControlParameters_FaceId, controlParameters.getFaceId())
        if controlParameters.getName().size() > 0:
          self._encodeName(controlParameters.getName(), encoder)

        encoder.writeTypeAndLength(Tlv.ControlParameters_ControlParameters,
                                   len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
Beispiel #10
0
    def _encodeLpNack(interest, networkNack):
        """
        Encode the interest into an NDN-TLV LpPacket as a NACK with the reason
        code in the networkNack object.
        TODO: Generalize this and move to WireFormat.encodeLpPacket.
        
        :param Interest interest: The Interest to put in the LpPacket fragment.
        :param NetworkNack networkNack: The NetworkNack with the reason code.
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        # Encode the fragment with the Interest.
        encoder.writeBlobTlv(
          Tlv.LpPacket_Fragment, interest.wireEncode(TlvWireFormat.get()).buf())

        # Encode the reason.
        if (networkNack.getReason() == NetworkNack.Reason.NONE or
             networkNack.getReason() == NetworkNack.Reason.CONGESTION or
             networkNack.getReason() == NetworkNack.Reason.DUPLICATE or
             networkNack.getReason() == NetworkNack.Reason.NO_ROUTE):
            # The Reason enum is set up with the correct integer for each NDN-TLV Reason.
            reason = networkNack.getReason()
        elif networkNack.getReason() == NetworkNack.Reason.OTHER_CODE:
            reason = networkNack.getOtherReasonCode()
        else:
            # We don't expect this to happen.
            raise RuntimeError("unrecognized NetworkNack.getReason() value")

        nackSaveLength = len(encoder)
        encoder.writeNonNegativeIntegerTlv(Tlv.LpPacket_NackReason, reason)
        encoder.writeTypeAndLength(
          Tlv.LpPacket_Nack, len(encoder) - nackSaveLength)

        encoder.writeTypeAndLength(
          Tlv.LpPacket_LpPacket, len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
Beispiel #11
0
    def _encodeLpNack(interest, networkNack):
        """
        Encode the interest into an NDN-TLV LpPacket as a NACK with the reason
        code in the networkNack object.
        TODO: Generalize this and move to WireFormat.encodeLpPacket.
        
        :param Interest interest: The Interest to put in the LpPacket fragment.
        :param NetworkNack networkNack: The NetworkNack with the reason code.
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        # Encode the fragment with the Interest.
        encoder.writeBlobTlv(Tlv.LpPacket_Fragment,
                             interest.wireEncode(TlvWireFormat.get()).buf())

        # Encode the reason.
        if (networkNack.getReason() == NetworkNack.Reason.NONE
                or networkNack.getReason() == NetworkNack.Reason.CONGESTION
                or networkNack.getReason() == NetworkNack.Reason.DUPLICATE
                or networkNack.getReason() == NetworkNack.Reason.NO_ROUTE):
            # The Reason enum is set up with the correct integer for each NDN-TLV Reason.
            reason = networkNack.getReason()
        elif networkNack.getReason() == NetworkNack.Reason.OTHER_CODE:
            reason = networkNack.getOtherReasonCode()
        else:
            # We don't expect this to happen.
            raise RuntimeError("unrecognized NetworkNack.getReason() value")

        nackSaveLength = len(encoder)
        encoder.writeNonNegativeIntegerTlv(Tlv.LpPacket_NackReason, reason)
        encoder.writeTypeAndLength(Tlv.LpPacket_Nack,
                                   len(encoder) - nackSaveLength)

        encoder.writeTypeAndLength(Tlv.LpPacket_LpPacket,
                                   len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)