Example #1
0
 def encode_nack_reason(self, reason: NackReason) -> bytearray:
     """
     Encode a NackReason
     :param reason: NackReason
     :return: Nack reason in wire format
     """
     encoder = TlvEncoder()
     encoder.writeVarNumber(self.__nack_reason_values[reason])
     return encoder.getOutput().tobytes()
Example #2
0
 def encode_name(self, name: Name) -> bytearray:
     """
     Assembly a name-TLV
     :param name: Name
     :return: Name-TLV
     """
     encoder = TlvEncoder()
     # name = name[1:].split('/')
     # for component in name[::-1]:
     #     encoder.writeBlobTlv(Tlv.NameComponent, [ord(c) for c in component])
     # fill backwards
     if name.digest:
         encoder.writeBlobTlv(Tlv.ImplicitSha256DigestComponent, name.digest)
     for c in name._components[::-1]:
         encoder.writeBlobTlv(Tlv.NameComponent, c)
     encoder.writeTypeAndLength(Tlv.Name, len(encoder))
     return encoder.getOutput() #.tobytes()
Example #3
0
 def encode_interest(self, name: Name) -> bytearray:
     """
     Assembly an interest packet
     :param name: Name
     :return: Interest-TLV
     """
     encoder = TlvEncoder()
     # Add nonce
     nonce = bytearray(4)
     for i in range(4):
         nonce[i] = SystemRandom().randint(0, 0xff)
     encoder.writeBlobTlv(Tlv.Nonce, nonce)
     # TODO: add implicitDigest flag here ?
     # Add name
     encoder.writeBuffer(self.encode_name(name))
     # Add interest type and len
     encoder.writeTypeAndLength(Tlv.Interest, len(encoder))
     return encoder.getOutput().tobytes()
Example #4
0
 def encode_heartbeat(self, name: Name) -> bytearray:
     """
     Assembly an heartbeat packet
     :param name: Name
     :return: Heartbeat-TLV
     """
     encoder = TlvEncoder()
     # Add name
     encoder.writeBuffer(self.encode_name(name))
     # Add heartbeat type and len
     encoder.writeTypeAndLength(self.heartbeatTV, len(encoder))
     return encoder.getOutput().tobytes()
Example #5
0
 def encode_nack(self, name: Name, reason: NackReason, interest: Interest) -> bytearray:
     """
     Assembly a negative acknowledgement packet
     :param name: Name carried by interest for which this NACK is generated
     :param reason: Nack reason
     :param interest: Interest for which this NACk is generated
     :return:  NACK-TLV
     """
     encoder = TlvEncoder()
     # write fragment (interest packet)
     if interest.wire_format is None:
         interest._wire_format = self.encode(interest)
     encoder.writeBuffer(interest.wire_format)
     encoder.writeTypeAndLength(Tlv.LpPacket_Fragment, len(encoder))
     fragment_len = len(encoder)
     # write nack reason if needed
     if reason is not NackReason.NOT_SET:
         wire_reason = self.encode_nack_reason(reason)
         encoder.writeBuffer(wire_reason)
         encoder.writeTypeAndLength(Tlv.LpPacket_NackReason, len(wire_reason))
     # write nack header
     encoder.writeTypeAndLength(Tlv.LpPacket_Nack, len(encoder) - fragment_len)
     # write link packet header
     encoder.writeTypeAndLength(Tlv.LpPacket_LpPacket, len(encoder))
     return encoder.getOutput().tobytes()
Example #6
0
 def encode_data(self, name: Name, payload: bytearray) -> bytearray:
     """
     Assembly a data packet including a signature according to NDN packet format specification 0.3 (DigestSha256).
     :param name: Name
     :param payload: Payload
     :return: Data-TLV
     """
     encoder = TlvEncoder()
     # Add signature (DigestSha256, zeroed)
     encoder.writeBlobTlv(Tlv.SignatureValue, bytearray(32)) # signature value to set later
     encoder.writeBlobTlv(Tlv.SignatureInfo, bytearray([Tlv.SignatureType, 1, 0]))
     # Add content
     encoder.writeBlobTlv(Tlv.Content, payload)
     # Add meta info (empty)
     encoder.writeTypeAndLength(Tlv.MetaInfo, 0)
     # Add name
     encoder.writeBuffer(self.encode_name(name))
     # Add data type and len
     encoder.writeTypeAndLength(Tlv.Data, len(encoder))
     # Add signature value
     packet_without_sig = encoder.getOutput().tobytes()
     m = hashlib.sha256()
     m.update(packet_without_sig[:-32])
     sig = m.digest()
     packet_with_sig = packet_without_sig[:-32] + sig
     return packet_with_sig
Example #7
0
 def encode_data(self, name: Name, payload: bytearray) -> bytearray:
     """
     Assembly a data packet
     :param name: Name
     :param payload: Payload
     :return: Data-TLV
     """
     encoder = TlvEncoder()
     # Add content
     encoder.writeBlobTlv(Tlv.Content, payload)
     # Add meta info (empty)
     encoder.writeTypeAndLength(Tlv.MetaInfo, 0)
     # Add name
     encoder.writeBuffer(self.encode_name(name))
     # Add data type and len
     encoder.writeTypeAndLength(Tlv.Data, len(encoder))
     return encoder.getOutput().tobytes()