Use a random squirrel id and an incremental nonce

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

View File

@ -1,6 +1,7 @@
# Copyright (C) 2020 by eichhornchen, ÿnérant
# SPDX-License-Identifier: GPL-3.0-or-later
from datetime import datetime
from random import randint
from typing import Any, Tuple
from ipaddress import IPv6Address
from threading import Thread
@ -17,6 +18,7 @@ class Hazelnut:
"""
def __init__(self, nickname: str = None, address: str = "localhost", port: int = 2500):
self.nickname = nickname
self.id = -1
try:
# Resolve DNS as an IPv6
@ -37,6 +39,11 @@ class Squirrel(Hazelnut):
"""
def __init__(self, instance: Any, nickname: str):
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
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
# Bind the socket
@ -175,7 +182,7 @@ class Squirrel(Hazelnut):
elif key != "\n":
# Insert the pressed key in the current message
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.
curses.beep()
return
@ -194,7 +201,7 @@ class Squirrel(Hazelnut):
msg = f"{self.nickname}: {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()):
self.send_packet(hazelnut, pkt)