From 592cbc3792a007022a470b56e94a3bfabdf1cf29 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:04:09 +0100 Subject: [PATCH] Add basic implementations for all TLVs Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 9600d9c..d916c5f 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -236,6 +236,11 @@ class Pad1TLV(TLV): """ return self.type.to_bytes(1, "big") + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + # TODO Add some easter eggs + squirrel.add_system_message(f"For each byte in the packet that I received, you will die today. " + "And eat cookies.") + class PadNTLV(TLV): """ @@ -264,6 +269,10 @@ class PadNTLV(TLV): """ return self.type.to_bytes(1, "big") + self.length.to_bytes(1, "big") + self.mbz[:self.length] + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + # TODO Add some easter eggs + squirrel.add_system_message(f"I received {self.length} zeros, am I so a bag guy ? :cold_sweat:") + class HelloTLV(TLV): type: int = 2 @@ -281,7 +290,7 @@ class HelloTLV(TLV): self.type = raw_data[0] self.length = raw_data[1] self.source_id = int.from_bytes(raw_data[2:10], "big") - if self.length == 16: + if self.is_long: self.dest_id = int.from_bytes(raw_data[10:18], "big") def marshal(self) -> bytes: @@ -290,6 +299,15 @@ class HelloTLV(TLV): data += self.dest_id.to_bytes(8, "big") return data + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + # TODO Implement HelloTLV + squirrel.add_system_message("Aaaawwww, someone spoke to me and said me Hello smiling_face_with_" + + (":3_hearts:" if self.is_long else "smiling_eyes:")) + + @property + def is_long(self): + return self.length == 16 + class NeighbourTLV(TLV): type: int = 3 @@ -309,6 +327,11 @@ class NeighbourTLV(TLV): self.ip_address.packed + \ self.port.to_bytes(2, "big") + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + # TODO Implement NeighbourTLV + squirrel.add_system_message("I have a friend!") + squirrel.add_system_message(f"Welcome {self.ip_address}:{self.port}!") + class DataTLV(TLV): type: int = 4 @@ -357,6 +380,10 @@ class AckTLV(TLV): self.sender_id.to_bytes(8, "big") + \ self.nonce.to_bytes(4, "big") + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + # TODO Implement AckTLV + squirrel.add_system_message("I received an AckTLV. I don't know what to do with it. Please implement me!") + class GoAwayTLV(TLV): class GoAwayType(Enum): @@ -382,6 +409,11 @@ class GoAwayTLV(TLV): self.code.value.to_bytes(1, "big") + \ self.message.encode("UTF-8")[:self.length - 1] + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + # TODO Implement GoAwayTLV + squirrel.add_system_message("Some told me that he went away. That's not very nice :( " + "I should send him some cake.") + class WarningTLV(TLV): type: int = 7