From 806287f834d9ebeae423f588506e43e6449f4669 Mon Sep 17 00:00:00 2001 From: eichhornchen Date: Fri, 25 Dec 2020 21:22:52 +0100 Subject: [PATCH] Added construct functions to all TLVs that did not have one. --- squinnondation/messages.py | 43 +++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/squinnondation/messages.py b/squinnondation/messages.py index 051ab58..5a68bec 100644 --- a/squinnondation/messages.py +++ b/squinnondation/messages.py @@ -83,6 +83,12 @@ class Pad1TLV(TLV): """ return 1 + @staticmethod + def construct() -> "Pad1TLV": + tlv = Pad1TLV() + tlv.type = 0 + return tlv + class PadNTLV(TLV): """ @@ -116,6 +122,13 @@ class PadNTLV(TLV): # TODO Add some easter eggs squirrel.add_system_message(f"I received {self.length} zeros, am I so a bag guy ? :cold_sweat:") + @staticmethod + def construct(length: int) -> "Pad1TLV": + tlv = PadNTLV() + tlv.type = 1 + tlv.length = length + tlv.mbz = b'0'*length + return tlv class HelloTLV(TLV): type: int = 2 @@ -147,7 +160,7 @@ class HelloTLV(TLV): timeH = time.time() timeHL = None if not squirrel.is_active(sender) : - sender.id = source_id #The sender we are given misses an id + sender.id = self.source_id #The sender we are given misses an id else : timeHL = squirrel.activehazelnuts[(sender.address, sender.port)] if self.is_long and dest_id == squirrel.id : @@ -165,6 +178,18 @@ class HelloTLV(TLV): def is_long(self) -> bool: return self.length == 16 + @staticmethod + def construct(length: str, squirrel: Any) -> "HelloTLV": + tlv = HelloTLV() + tlv.type = 2 + tlv.source_id = squirrel.id if squirrel else 0 + if length == "short": + tlv.length = 8 + else : + tlv.length = 16 + tlv.dest_id = None + return tlv + class NeighbourTLV(TLV): type: int = 3 @@ -189,6 +214,14 @@ class NeighbourTLV(TLV): squirrel.potentialhazelnuts[(ip_address, port)] = squirrel.new_hazel(ip_address, port) squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!") + @staticmethod + def construct(address: IPv6Address, port: int) -> "Pad1TLV": + tlv = NeighbourTLV() + tlv.type = 3 + tlv.length = 18 #A priori... + tlv.ip_address = address + tlv.port = port + return tlv class DataTLV(TLV): type: int = 4 @@ -320,6 +353,14 @@ class GoAwayTLV(TLV): squirrel.potentialhazelnuts[(sender.address, sender.port)] = sender squirrel.add_system_message("Some told me that he went away : "+message) + @staticmethod + def construct(GAtype: GoAwayType, message: str) -> "Pad1TLV": + tlv = GoAwayTLV() + tlv.type = 6 + tlv.code = GAtype + tlv.message = message.encode("UTF-8") + tlv.length = 1 + len(tlv.message) + return tlv class WarningTLV(TLV): type: int = 7