Use a random squirrel id and an incremental nonce

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:04:16 +01:00
parent ebd6c18032
commit e1f0d54ac8
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 16 additions and 5 deletions

View File

@ -1,6 +1,7 @@
# Copyright (C) 2020 by eichhornchen, ÿnérant # Copyright (C) 2020 by eichhornchen, ÿnérant
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from datetime import datetime from datetime import datetime
from random import randint
from typing import Any, Tuple from typing import Any, Tuple
from ipaddress import IPv6Address from ipaddress import IPv6Address
from threading import Thread from threading import Thread
@ -17,6 +18,7 @@ class Hazelnut:
""" """
def __init__(self, nickname: str = None, address: str = "localhost", port: int = 2500): def __init__(self, nickname: str = None, address: str = "localhost", port: int = 2500):
self.nickname = nickname self.nickname = nickname
self.id = -1
try: try:
# Resolve DNS as an IPv6 # Resolve DNS as an IPv6
@ -37,6 +39,11 @@ class Squirrel(Hazelnut):
""" """
def __init__(self, instance: Any, nickname: str): def __init__(self, instance: Any, nickname: str):
super().__init__(nickname, instance.bind_address, instance.bind_port) super().__init__(nickname, instance.bind_address, instance.bind_port)
# Random identifier on 64 bits
self.id = randint(0, 1 << 64 - 1)
self.incr_nonce = 0
# Create UDP socket # Create UDP socket
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
# Bind the socket # Bind the socket
@ -175,7 +182,7 @@ class Squirrel(Hazelnut):
elif key != "\n": elif key != "\n":
# Insert the pressed key in the current message # Insert the pressed key in the current message
new_buffer = self.input_buffer[:self.input_index] + key + self.input_buffer[self.input_index:] new_buffer = self.input_buffer[:self.input_index] + key + self.input_buffer[self.input_index:]
if len(DataTLV.construct(f"{self.nickname}: {new_buffer}")) > 255 - 8 - 4: if len(DataTLV.construct(f"{self.nickname}: {new_buffer}", None)) > 255 - 8 - 4:
# The message is too long to be sent once. We don't allow the user to type any other character. # The message is too long to be sent once. We don't allow the user to type any other character.
curses.beep() curses.beep()
return return
@ -194,7 +201,7 @@ class Squirrel(Hazelnut):
msg = f"{self.nickname}: {msg}" msg = f"{self.nickname}: {msg}"
self.add_message(msg) self.add_message(msg)
pkt = Packet.construct(DataTLV.construct(msg)) pkt = Packet.construct(DataTLV.construct(msg, self))
for hazelnut in list(self.hazelnuts.values()): for hazelnut in list(self.hazelnuts.values()):
self.send_packet(hazelnut, pkt) self.send_packet(hazelnut, pkt)

View File

@ -227,13 +227,17 @@ class DataTLV(TLV):
sender.nickname = nickname sender.nickname = nickname
@staticmethod @staticmethod
def construct(message: str) -> "DataTLV": def construct(message: str, squirrel: Any) -> "DataTLV":
tlv = DataTLV() tlv = DataTLV()
tlv.type = 4 tlv.type = 4
tlv.sender_id = 42 # FIXME Use the good sender id tlv.sender_id = squirrel.id if squirrel else 0
tlv.nonce = 42 # FIXME Use an incremental nonce tlv.nonce = squirrel.incr_nonce if squirrel else 0
tlv.data = message.encode("UTF-8") tlv.data = message.encode("UTF-8")
tlv.length = 12 + len(tlv.data) tlv.length = 12 + len(tlv.data)
if squirrel:
squirrel.incr_nonce += 1
return tlv return tlv