From f1e04b1dc843f1056f1ba66616e27b165408a8ec Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:04:08 +0100 Subject: [PATCH] Properly handle TLVs Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 1eb5d65..866176a 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -111,6 +111,8 @@ class Squinnondation: elif key == "KEY_END": squirrel.input_index = len(squirrel.input_buffer) continue + elif key == "KEY_RESIZE": + continue elif key == "KEY_MOUSE": try: _, x, y, _, attr = curses.getmouse() @@ -205,6 +207,12 @@ class TLV: """ return True + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + """ + Indicates what to do when this TLV is received from a given hazel. + It is ensured that the data is valid. + """ + @staticmethod def tlv_classes(): return [Pad1TLV, PadNTLV, HelloTLV, NeighbourTLV, DataTLV, AckTLV, GoAwayTLV, WarningTLV] @@ -323,6 +331,13 @@ class DataTLV(TLV): self.nonce.to_bytes(4, "big") + \ self.data + def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None: + """ + A message has been sent. We log it. + TODO: Check that the tuple (sender_id, nonce) is unique to avoid duplicates. + """ + squirrel.add_message(self.data.decode('UTF-8')) + class AckTLV(TLV): type: int = 5 @@ -714,9 +729,11 @@ class Worm(Thread): while True: try: pkt, hazelnut = self.squirrel.receive_packet() + pkt.validate_data() except ValueError as error: self.squirrel.add_system_message("An error occurred while receiving a packet: {}".format(error)) else: - self.squirrel.add_message(pkt.body[0].data.decode('UTF-8')) + for tlv in pkt.body: + tlv.handle(self.squirrel, hazelnut) self.squirrel.refresh_history() self.squirrel.refresh_input()