def decodeSignatureInfoAndValue(self, signatureInfo, signatureValue): """ Decode signatureInfo as a signature info and signatureValue as the related SignatureValue, and return a new object which is a subclass of Signature. :param signatureInfo: The array with the signature info input buffer to decode. :type signatureInfo: An array type with int elements :param signatureValue: The array with the signature value input buffer to decode. :type signatureValue: An array type with int elements :return: A new object which is a subclass of Signature. :rtype: a subclass of Signature """ # Use a SignatureHolder to imitate a Data object for _decodeSignatureInfo. signatureHolder = self.SignatureHolder() decoder = TlvDecoder(signatureInfo) self._decodeSignatureInfo(signatureHolder, decoder) decoder = TlvDecoder(signatureValue) signatureHolder.getSignature().setSignature( Blob(decoder.readBlobTlv(Tlv.SignatureValue))) return signatureHolder.getSignature()
def onData(self, interest, responseData): """ We received the response. Do a quick check of expected name components. """ if self._isNfdCommand: # Decode responseData->getContent() and check for a success code. # TODO: Move this into the TLV code. statusCode = None try: decoder = TlvDecoder(responseData.getContent().buf()) decoder.readNestedTlvsStart(Tlv.NfdCommand_ControlResponse) statusCode = decoder.readNonNegativeIntegerTlv( Tlv.NfdCommand_StatusCode) except ValueError: # Error decoding the ControlResponse. self._onRegisterFailed_(self._prefix) return # Status code 200 is "OK". if statusCode != 200: self._onRegisterFailed_(self._prefix) # Otherwise, silently succeed. else: expectedName = Name("/ndnx/.../selfreg") if (responseData.getName().size() < 4 or responseData.getName()[0] != expectedName[0] or responseData.getName()[2] != expectedName[2]): self._onRegisterFailed(self._prefix) return
def wireDecode(self, wire): self.m_hasName = False self.m_hasStartBlockId = False self.m_hasEndBlockId = False self.m_hasProcessId = False self.m_hasMaxInterestNum = False self.m_hasWatchTimeout = False self.m_hasInterestLifetime = False #self.m_wire = wire decoder = TlvDecoder(wire) endOffset = decoder.readNestedTlvsStart(repoTlv.RepoCommandParameter) # Name if (decoder.peekType(Tlv.Name)): self.m_hasName = True Tlv0_1WireFormat._decodeName(self.m_name, decoder) # Selectors if (decoder.peekType(Tlv.Selectors)): Tlv0_1WireFormat._decodeSelectors(self.m_selector, decoder) self.m_selectors.wireDecode(*val) # StartBlockId if (decoder.peekType(repoTlv.StartBlockId)): self.m_hasStartBlockId = True self.m_startBlockId = decoder.readNonNegativeIntegerTlv( repoTlv.StartBlockId) # EndBlockId if (decoder.peekType(repoTlv.EndBlockId)): self.m_hasEndBlockId = True self.m_endBlockId = decoder.readNonNegativeIntegerTlv( repoTlv.EndBlockId) # ProcessId if (decoder.peekType(repoTlv.ProcessId)): self.m_hasProcessId = True self.m_processId = decoder.readNonNegativeInteger( repoTlv.ProcessId) # MaxInterestNum if (decoder.peekType(repoTlv.MaxInterestNum)): self.m_hasMaxInterestNum = True self.m_maxInterestNum = decoder.readNonNegativeInteger( repoTlv.MaxInterestNum) # WatchTimeout if (decoder.peekType(repoTlv.WatchTimeout)): self.m_hasWatchTimeout = True self.m_watchTimeout = milliseconds( decoder.readNonNegativeInteger(repoTlv.WatchTimeout)) # InterestLifeTime if (decoder.peekType(Tlv.InterestLifetime)): self.m_hasInterestLifetime = True self.m_interestLifetime = milliseconds( decoder.readNonNegativeInteger(Tlv.InterestLifetime))
def onReceivedElement(self, element): """ This is called by the transport's ElementReader to process an entire received Data or Interest element. :param element: The bytes of the incoming element. :type element: An array type with int elements """ lpPacket = None if element[0] == Tlv.LpPacket_LpPacket: # Decode the LpPacket and replace element with the fragment. lpPacket = LpPacket() # Set copy False so that the fragment is a slice which will be # copied below. The header fields are all integers and don't need to # be copied. TlvWireFormat.get().decodeLpPacket(lpPacket, element, False) element = lpPacket.getFragmentWireEncoding().buf() # First, decode as Interest or Data. data = None decoder = TlvDecoder(element) if decoder.peekType(Tlv.Data, len(element)): data = Data() data.wireDecode(element, TlvWireFormat.get()) if lpPacket != None: data.setLpPacket(lpPacket) # Now process as Interest or Data. if data != None: if self._onBtleData: self._onBtleData(data)
def wireDecode(self, input): """ Decode the input and update this Schedule object. :param input: The array with the bytes to decode. :type input: An array type with int elements :raises ValueError: For invalid encoding. """ # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input # For now, don't use WireFormat and hardcode to use TLV since the # encoding doesn't go out over the wire, only into the local SQL database. decoder = TlvDecoder(decodeBuffer) endOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_Schedule) # Decode the whiteIntervalList. self._whiteIntervalList = [] listEndOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_WhiteIntervalList) while decoder.getOffset() < listEndOffset: Schedule._sortedSetAdd( self._whiteIntervalList, Schedule._decodeRepetitiveInterval(decoder)) decoder.finishNestedTlvs(listEndOffset) # Decode the blackIntervalList. self._blackIntervalList = [] listEndOffset = decoder.readNestedTlvsStart(Tlv.Encrypt_BlackIntervalList) while decoder.getOffset() < listEndOffset: Schedule._sortedSetAdd( self._blackIntervalList, Schedule._decodeRepetitiveInterval(decoder)) decoder.finishNestedTlvs(listEndOffset) decoder.finishNestedTlvs(endOffset)
def decodeData(self, data, input): """ Decode input as an NDN-TLV data packet, set the fields in the data object, and return the signed offsets. :param Data data: The Data object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. :rtype: (int, int) """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Data) signedPortionBeginOffset = decoder.getOffset() self._decodeName(data.getName(), decoder) self._decodeMetaInfo(data.getMetaInfo(), decoder) data.setContent(Blob(decoder.readBlobTlv(Tlv.Content))) self._decodeSignatureInfo(data, decoder) signedPortionEndOffset = decoder.getOffset() data.getSignature().setSignature( Blob(decoder.readBlobTlv(Tlv.SignatureValue))) decoder.finishNestedTlvs(endOffset) return (signedPortionBeginOffset, signedPortionEndOffset)
def wireDecode(self, input): """ Decode the input as an NDN-TLV SafeBag and update this object. :param input: The array with the bytes to decode. :type input: A Blob or an array type with int elements """ if isinstance(input, Blob): input = input.buf() # Decode directly as TLV. We don't support the WireFormat abstraction # because this isn't meant to go directly on the wire. decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.SafeBag_SafeBag) # Get the bytes of the certificate and decode. certificateBeginOffset = decoder.getOffset() certificateEndOffset = decoder.readNestedTlvsStart(Tlv.Data) decoder.seek(certificateEndOffset) self._certificate = Data() self._certificate.wireDecode( decoder.getSlice(certificateBeginOffset, certificateEndOffset), TlvWireFormat.get()) self._privateKeyBag = Blob( decoder.readBlobTlv(Tlv.SafeBag_EncryptedKeyBag), True) decoder.finishNestedTlvs(endOffset)
def decodeInterest(self, interest, input): """ Decode input as an NDN-TLV interest and set the fields of the interest object. :param Interest interest: The Interest object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). :rtype: (int, int) """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.Interest) self._decodeName(interest.getName(), decoder) if decoder.peekType(Tlv.Selectors, endOffset): self._decodeSelectors(interest, decoder) # Require a Nonce, but don't force it to be 4 bytes. nonce = Blob(decoder.readBlobTlv(Tlv.Nonce)) interest.setScope( decoder.readOptionalNonNegativeIntegerTlv(Tlv.Scope, endOffset)) interest.setInterestLifetimeMilliseconds( decoder.readOptionalNonNegativeIntegerTlvAsFloat( Tlv.InterestLifetime, endOffset)) # Set the nonce last because setting other interest fields clears it. interest.setNonce(nonce) decoder.finishNestedTlvs(endOffset)
def decodeStateVector(input): """ Decode the input as a TLV state vector. :param input: The array with the bytes to decode. :type input: An array type with int elements :return: A new dictionary where the key is the member ID string and the value is the sequence number. If the input encoding has repeated entries with the same member ID, this uses only the last entry. :rtype: dict<str,int> :raises ValueError: For invalid encoding. """ stateVector = {} # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input decoder = TlvDecoder(decodeBuffer) endOffset = decoder.readNestedTlvsStart( StateVectorSync2018.TLV_StateVector) while decoder.getOffset() < endOffset: entryEndOffset = decoder.readNestedTlvsStart( StateVectorSync2018.TLV_StateVectorEntry) memberIdBlob = Blob( decoder.readBlobTlv( StateVectorSync2018.TLV_StateVector_MemberId), False) stateVector[str(memberIdBlob)] = decoder.readNonNegativeIntegerTlv( StateVectorSync2018.TLV_StateVector_SequenceNumber) decoder.finishNestedTlvs(entryEndOffset) decoder.finishNestedTlvs(endOffset) return stateVector
def decodeName(self, name, input): """ Decode input as an NDN-TLV name and set the fields of the Name object. :param Name name: The Name object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements """ decoder = TlvDecoder(input) self._decodeName(name, decoder)
def decode(message, input): """ Decode the input as NDN-TLV and update the fields of the Protobuf message object. :param google.protobuf.message message: The Protobuf message object. This does not first clear the object. :param input: The array with the bytes to decode. :type input: An array type with int elements """ # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input decoder = TlvDecoder(decodeBuffer) ProtobufTlv._decodeMessageValue(message, decoder, len(input))
def onData(self, interest, responseData): """ We received the response. Do a quick check of expected name components. """ if self._isNfdCommand: # Decode responseData->getContent() and check for a success code. # TODO: Move this into the TLV code. statusCode = None try: decoder = TlvDecoder(responseData.getContent().buf()) decoder.readNestedTlvsStart(Tlv.NfdCommand_ControlResponse) statusCode = decoder.readNonNegativeIntegerTlv(Tlv.NfdCommand_StatusCode) except ValueError as ex: logging.getLogger(__name__).info( "Register prefix failed: Error decoding the NFD response: %s", str(ex)) self._onRegisterFailed(self._prefix) return # Status code 200 is "OK". if statusCode != 200: logging.getLogger(__name__).info( "Register prefix failed: Expected NFD status code 200, got: %d", statusCode) self._onRegisterFailed(self._prefix) return logging.getLogger(__name__).info( "Register prefix succeeded with the NFD forwarder for prefix %s", self._prefix.toUri()) else: expectedName = Name("/ndnx/.../selfreg") if (responseData.getName().size() < 4 or responseData.getName()[0] != expectedName[0] or responseData.getName()[2] != expectedName[2]): logging.getLogger(__name__).info( "Register prefix failed: Unexpected name in NDNx response: %s", responseData.getName().toUri()) self._onRegisterFailed(self._prefix) return logging.getLogger(__name__).info( "Register prefix succeeded with the NDNx forwarder for prefix %s", self._prefix.toUri())
def decodeForwardingEntry(self, forwardingEntry, input): """ Decode input as an forwardingEntry and set the fields of the forwardingEntry object. :param forwardingEntry: The ForwardingEntry object whose fields are updated. :type forwardingEntry: ForwardingEntry :param input: The array with the bytes to decode. :type input: An array type with int elements """ decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart(Tlv.ForwardingEntry) actionBytes = decoder.readOptionalBlobTlv(Tlv.Action, endOffset) if actionBytes != None: # Convert bytes to a str. forwardingEntry.setAction("".join(map(chr, actionBytes))) else: forwardingEntry.setAction(None) if decoder.peekType(Tlv.Name, endOffset): self._decodeName(forwardingEntry.getPrefix(), decoder) else: forwardingEntry.getPrefix().clear() forwardingEntry.setFaceId( decoder.readOptionalNonNegativeIntegerTlv(Tlv.FaceID, endOffset)) forwardingEntryFlags = decoder.readOptionalNonNegativeIntegerTlv( Tlv.ForwardingFlags, endOffset) if forwardingEntryFlags != None: forwardingEntry.getForwardingFlags().setForwardingEntryFlags( forwardingEntryFlags) else: # This sets the default flags. forwardingEntry.setForwardingFlags(ForwardingFlags()) forwardingEntry.setFreshnessPeriod( decoder.readOptionalNonNegativeIntegerTlvAsFloat( Tlv.FreshnessPeriod, endOffset)) decoder.finishNestedTlvs(endOffset)
def wireDecode(self, wire): decoder = TlvDecoder(wire) beginOffset = decoder.getOffset() endOffset = decoder.readNestedTlvsStart(Tlv.SignatureInfo) signatureType = decoder.readNonNegativeIntegerTlv(Tlv.SignatureType) if signatureType != Tlv.SignatureType_Sha256WithAbsSignature: raise RuntimeError("Invalid SignatureType code: expected %d, got %d" % (Tlv.SignatureType_Sha256WithAbsSignature, signatureType)) keyLocator = KeyLocator() Tlv0_2WireFormat._decodeKeyLocator(Tlv.KeyLocator, keyLocator, decoder, True) self._keyLocator = ChangeCounter(keyLocator) # if decoder.peekType(Tlv.ValidityPeriod_ValidityPeriod, endOffset): # Tlv0_2WireFormat._decodeValidityPeriod( # signatureInfo.getValidityPeriod(), decoder) decoder.finishNestedTlvs(endOffset)
def onReceivedElement(self, element): """ This is called by the transport's ElementReader to process an entire received Data or Interest element. :param element: The bytes of the incoming element. :type element: An array type with int elements """ # The type codes for TLV Interest and Data packets are chosen to not # conflict with the first byte of a binary XML packet, so we canjust # look at the first byte. if not (element[0] == Tlv.Interest or element[0] == Tlv.Data): # Ignore non-TLV elements. return # First, decode as Interest or Data. interest = None data = None decoder = TlvDecoder(element) if decoder.peekType(Tlv.Interest, len(element)): interest = Interest() interest.wireDecode(element, TlvWireFormat.get()) elif decoder.peekType(Tlv.Data, len(element)): data = Data() data.wireDecode(element, TlvWireFormat.get()) # Now process as Interest or Data. if interest != None: entry = self._getEntryForRegisteredPrefix(interest.getName()) if entry != None: entry.getOnInterest()(entry.getPrefix(), interest, self._transport, entry.getRegisteredPrefixId()) elif data != None: pendingInterests = self._extractEntriesForExpressedInterest( data.getName()) for pendingInterest in pendingInterests: pendingInterest.getOnData()(pendingInterest.getInterest(), data)
def wireDecode(self, input): """ Decode the input as an NDN-TLV PSyncContent and update this object. :param input: The array with the bytes to decode. :type input: Blob or an array type with int elements """ # If input is a blob, get its buf(). decodeBuffer = input.buf() if isinstance(input, Blob) else input self.clear() # Decode directly as TLV. We don't support the WireFormat abstraction # because this isn't meant to go directly on the wire. decoder = TlvDecoder(decodeBuffer) endOffset = decoder.readNestedTlvsStart(PSyncState.Tlv_PSyncContent) # Decode a sequence of Name. while decoder.getOffset() < len(decodeBuffer): name = Name() Tlv0_3WireFormat._decodeName(name, decoder, True) self._content.append(name) decoder.finishNestedTlvs(endOffset)
def decodeControlParameters(self, controlParameters, input): """ Decode input as an NDN-TLV ControlParameters and set the fields of the controlParameters object. :param ControlParameters controlParameters: The ControlParameters object whose fields are updated. :param input: The array with the bytes to decode. :type input: An array type with int elements """ controlParameters.clear() decoder = TlvDecoder(input) endOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_ControlParameters) # decode name if decoder.peekType(Tlv.Name, endOffset): name = Name() self._decodeName(name, decoder) controlParameters.setName(name) # decode face ID controlParameters.setFaceId( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_FaceId, endOffset)) # decode URI if decoder.peekType(Tlv.ControlParameters_Uri, endOffset): uri = Blob( decoder.readOptionalBlobTlv(Tlv.ControlParameters_Uri, endOffset), False) controlParameters.setUri(str(uri)) # decode integers controlParameters.setLocalControlFeature( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_LocalControlFeature, endOffset)) controlParameters.setOrigin( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_Origin, endOffset)) controlParameters.setCost( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_Cost, endOffset)) # set forwarding flags if decoder.peekType(Tlv.ControlParameters_Flags, endOffset): flags = ForwardingFlags() flags.setNfdForwardingFlags( decoder.readNonNegativeIntegerTlv(Tlv.ControlParameters_Flags)) controlParameters.setForwardingFlags(flags) # decode strategy if decoder.peekType(Tlv.ControlParameters_Strategy, endOffset): strategyEndOffset = decoder.readNestedTlvsStart( Tlv.ControlParameters_Strategy) self._decodeName(controlParameters.getStrategy(), decoder) decoder.finishNestedTlvs(strategyEndOffset) # decode expiration period controlParameters.setExpirationPeriod( decoder.readOptionalNonNegativeIntegerTlv( Tlv.ControlParameters_ExpirationPeriod, endOffset)) decoder.finishNestedTlvs(endOffset)
def onReceivedElement(self, element): """ This is called by the transport's ElementReader to process an entire received Data or Interest element. :param element: The bytes of the incoming element. :type element: An array type with int elements """ lpPacket = None if element[0] == Tlv.LpPacket_LpPacket: # Decode the LpPacket and replace element with the fragment. lpPacket = LpPacket() # Set copy False so that the fragment is a slice which will be # copied below. The header fields are all integers and don't need to # be copied. TlvWireFormat.get().decodeLpPacket(lpPacket, element, False) element = lpPacket.getFragmentWireEncoding().buf() # First, decode as Interest or Data. interest = None data = None decoder = TlvDecoder(element) if decoder.peekType(Tlv.Interest, len(element)): interest = Interest() interest.wireDecode(element, TlvWireFormat.get()) if lpPacket != None: interest.setLpPacket(lpPacket) elif decoder.peekType(Tlv.Data, len(element)): data = Data() data.wireDecode(element, TlvWireFormat.get()) if lpPacket != None: data.setLpPacket(lpPacket) if lpPacket != None: # We have decoded the fragment, so remove the wire encoding to save # memory. lpPacket.setFragmentWireEncoding(Blob()) networkNack = NetworkNack.getFirstHeader(lpPacket) if networkNack != None: if interest == None: # We got a Nack but not for an Interest, so drop the packet. return pendingInterests = [] self._pendingInterestTable.extractEntriesForNackInterest( interest, pendingInterests) for pendingInterest in pendingInterests: try: pendingInterest.getOnNetworkNack()( pendingInterest.getInterest(), networkNack) except: logging.exception("Error in onNetworkNack") # We have processed the network Nack packet. return # Now process as Interest or Data. if interest != None: self._dispatchInterest(interest) elif data != None: self._satisfyPendingInterests(data)
def onReceivedElement(self, element): """ This is called by the transport's ElementReader to process an entire received Data or Interest element. :param element: The bytes of the incoming element. :type element: An array type with int elements """ lpPacket = None if element[0] == Tlv.LpPacket_LpPacket: # Decode the LpPacket and replace element with the fragment. lpPacket = LpPacket() # Set copy False so that the fragment is a slice which will be # copied below. The header fields are all integers and don't need to # be copied. TlvWireFormat.get().decodeLpPacket(lpPacket, element, False) element = lpPacket.getFragmentWireEncoding().buf() # First, decode as Interest or Data. interest = None data = None decoder = TlvDecoder(element) if decoder.peekType(Tlv.Interest, len(element)): interest = Interest() interest.wireDecode(element, TlvWireFormat.get()) if lpPacket != None: interest.setLpPacket(lpPacket) elif decoder.peekType(Tlv.Data, len(element)): data = Data() data.wireDecode(element, TlvWireFormat.get()) if lpPacket != None: data.setLpPacket(lpPacket) if lpPacket != None: # We have decoded the fragment, so remove the wire encoding to save # memory. lpPacket.setFragmentWireEncoding(Blob()) networkNack = NetworkNack.getFirstHeader(lpPacket) if networkNack != None: if interest == None: # We got a Nack but not for an Interest, so drop the packet. return pendingInterests = [] self._pendingInterestTable.extractEntriesForNackInterest( interest, pendingInterests) for pendingInterest in pendingInterests: try: pendingInterest.getOnNetworkNack()( pendingInterest.getInterest(), networkNack) except: logging.exception("Error in onNetworkNack") # We have processed the network Nack packet. return # Now process as Interest or Data. if interest != None: # Call all interest filter callbacks which match. matchedFilters = [] self._interestFilterTable.getMatchedFilters( interest, matchedFilters) for i in range(len(matchedFilters)): entry = matchedFilters[i] includeFilter = True onInterestCall = entry.getOnInterest() # If onInterest is not a function nor a method assumes it is a # calleable object if (not inspect.isfunction(onInterestCall) and not inspect.ismethod(onInterestCall)): onInterestCall = onInterestCall.__call__ # Use getcallargs to test if onInterest accepts 5 args. try: inspect.getcallargs(onInterestCall, None, None, None, None, None) except TypeError: # Assume onInterest is old-style with 4 arguments. includeFilter = False if includeFilter: try: entry.getOnInterest()(entry.getFilter().getPrefix(), interest, entry.getFace(), entry.getInterestFilterId(), entry.getFilter()) except: logging.exception("Error in onInterest") else: # Old-style onInterest without the filter argument. We # still pass a Face instead of Transport since Face also # has a send method. try: entry.getOnInterest()(entry.getFilter().getPrefix(), interest, entry.getFace(), entry.getInterestFilterId()) except: logging.exception("Error in onInterest") elif data != None: pendingInterests = [] self._pendingInterestTable.extractEntriesForExpressedInterest( data, pendingInterests) for pendingInterest in pendingInterests: try: pendingInterest.getOnData()(pendingInterest.getInterest(), data) except: logging.exception("Error in onData")
def findElementEnd(self, input): """ Continue scanning input starting from self._offset to find the element end. If the end of the element which started at offset 0 is found, this returns True and getOffset() is the length of the element. Otherwise, this returns False which means you should read more into input and call again. :param input: The input buffer. You have to pass in input each time because the buffer could be reallocated. :type input: An array type with int elements :return: True if found the element end, False if not. :rtype: bool """ if self._gotElementEnd: # Someone is calling when we already got the end. return True decoder = TlvDecoder(input) while True: if self._offset >= len(input): # All the cases assume we have some input. Return and wait # for more. return False if self._state == self.READ_TYPE: firstOctet = input[self._offset] self._offset += 1 if firstOctet < 253: # The value is simple, so we can skip straight to reading # the length. self._state = self.READ_LENGTH else: # Set up to skip the type bytes. if firstOctet == 253: self._nBytesToRead = 2 elif firstOctet == 254: self._nBytesToRead = 4 else: # value == 255. self._nBytesToRead = 8 self._state = self.READ_TYPE_BYTES elif self._state == self.READ_TYPE_BYTES: nRemainingBytes = len(input) - self._offset if nRemainingBytes < self._nBytesToRead: # Need more. self._offset += nRemainingBytes self._nBytesToRead -= nRemainingBytes return False # Got the type bytes. Move on to read the length. self._offset += self._nBytesToRead self._state = self.READ_LENGTH elif self._state == self.READ_LENGTH: firstOctet = input[self._offset] self._offset += 1 if firstOctet < 253: # The value is simple, so we can skip straight to reading # the value bytes. self._nBytesToRead = firstOctet if self._nBytesToRead == 0: # No value bytes to read. We're finished. self._gotElementEnd = True return True self._state = self.READ_VALUE_BYTES else: # We need to read the bytes in the extended encoding of # the length. if firstOctet == 253: self._nBytesToRead = 2 elif firstOctet == 254: self._nBytesToRead = 4 else: # value == 255. self._nBytesToRead = 8 # We need to use firstOctet in the next state. self._firstOctet = firstOctet self._state = self.READ_LENGTH_BYTES elif self._state == self.READ_LENGTH_BYTES: nRemainingBytes = len(input) - self._offset if (not self._useHeaderBuffer and nRemainingBytes >= self._nBytesToRead): # We don't have to use the headerBuffer. Set nBytesToRead. decoder.seek(self._offset) self._nBytesToRead = decoder.readExtendedVarNumber( self._firstOctet) # Update self._offset to the decoder's offset after reading. self._offset = decoder.getOffset() else: self._useHeaderBuffer = True nNeededBytes = self._nBytesToRead - self._headerLength if nNeededBytes > nRemainingBytes: # We can't get all of the header bytes from this input. # Save in headerBuffer. if (self._headerLength + nRemainingBytes > len(self._headerBuffer)): # We don't expect this to happen. raise RuntimeError( "Cannot store more header bytes than the size of headerBuffer") self._headerBuffer[ self._headerLength:self._headerLength + nRemainingBytes] = \ input[self._offset:self._offset + nRemainingBytes] self._offset += nRemainingBytes self._headerLength += nRemainingBytes return False # Copy the remaining bytes into headerBuffer, read the # length and set nBytesToRead. if (self._headerLength + nNeededBytes > len(self._headerBuffer)): # We don't expect this to happen. raise RuntimeError( "Cannot store more header bytes than the size of headerBuffer") self._headerBuffer[ self._headerLength:self._headerLength + nNeededBytes] = \ input[self._offset:self._offset + nNeededBytes] self._offset += nNeededBytes # Use a local decoder just for the headerBuffer. bufferDecoder = TlvDecoder(self._headerBuffer) # Replace nBytesToRead with the length of the value. self._nBytesToRead = bufferDecoder.readExtendedVarNumber( self._firstOctet) if self._nBytesToRead == 0: # No value bytes to read. We're finished. self._gotElementEnd = True return True # Get ready to read the value bytes. self._state = self.READ_VALUE_BYTES elif self._state == self.READ_VALUE_BYTES: nRemainingBytes = len(input) - self._offset if nRemainingBytes < self._nBytesToRead: # Need more. self._offset += nRemainingBytes self._nBytesToRead -= nRemainingBytes return False # Got the bytes. We're finished. self._offset += self._nBytesToRead self._gotElementEnd = True return True else: # We don't expect this to happen. raise RuntimeError("findElementEnd: unrecognized state")
def onReceivedElement(self, element): """ This is called by the transport's ElementReader to process an entire received Data or Interest element. :param element: The bytes of the incoming element. :type element: An array type with int elements """ # The type codes for TLV Interest and Data packets are chosen to not # conflict with the first byte of a binary XML packet, so we canjust # look at the first byte. if not (element[0] == Tlv.Interest or element[0] == Tlv.Data): # Ignore non-TLV elements. # Assume it is Binary XML. if not WireFormat.ENABLE_NDNX: raise RuntimeError( "BinaryXmlWireFormat (NDNx) is deprecated. To enable while you upgrade your network to use NDN-TLV, set WireFormat.ENABLE_NDNX = True") return # First, decode as Interest or Data. interest = None data = None decoder = TlvDecoder(element) if decoder.peekType(Tlv.Interest, len(element)): interest = Interest() interest.wireDecode(element, TlvWireFormat.get()) elif decoder.peekType(Tlv.Data, len(element)): data = Data() data.wireDecode(element, TlvWireFormat.get()) # Now process as Interest or Data. if interest != None: # Call all interest filter callbacks which match. for i in range(len(self._interestFilterTable)): entry = self._interestFilterTable[i] if entry.getFilter().doesMatch(interest.getName()): includeFilter = True # Use getcallargs to test if onInterest accepts 5 args. try: inspect.getcallargs(entry.getOnInterest(), None, None, None, None, None) except TypeError: # Assume onInterest is old-style with 4 arguments. includeFilter = False if includeFilter: entry.getOnInterest()( entry.getFilter().getPrefix(), interest, entry.getFace(), entry.getInterestFilterId(), entry.getFilter()) else: # Old-style onInterest without the filter argument. We # still pass a Face instead of Transport since Face also # has a send method. entry.getOnInterest()( entry.getFilter().getPrefix(), interest, entry.getFace(), entry.getInterestFilterId()) elif data != None: pendingInterests = self._extractEntriesForExpressedInterest( data.getName()) for pendingInterest in pendingInterests: pendingInterest.getOnData()(pendingInterest.getInterest(), data)