Added a marker to active neighbours to remember if they are symmetric. Fixed a marshal function broken by the last commit

This commit is contained in:
eichhornchen
2020-12-28 18:33:57 +01:00
parent 913f804f5e
commit c01fd697cd
2 changed files with 33 additions and 17 deletions

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import re
from typing import Any, List, Optional
#from ipaddress import IPv6Address
from ipaddress import IPv6Address
from enum import Enum
import socket
import sys
@ -168,7 +168,7 @@ class HelloTLV(TLV):
squirrel.remove_from_potential(sender.address, sender.port)
squirrel.activehazelnuts[(sender.address, sender.port)] = [sender, timeH,\
timeHL]
timeHL, True]
squirrel.nbNS += 1
squirrel.add_system_message(f"Aaaawwww, {self.source_id} spoke to me and said me Hello "
+ ("long" if self.is_long else "short"))
@ -195,13 +195,13 @@ class HelloTLV(TLV):
class NeighbourTLV(TLV):
type: int = 3
length: int
ip_address: str
ip_address: IPv6Address
port: int
def unmarshal(self, raw_data: bytes) -> None:
self.type = raw_data[0]
self.length = raw_data[1]
self.ip_address = raw_data[2:18]#IPv6Address()
self.ip_address = IPv6Address(raw_data[2:18])
self.port = int.from_bytes(raw_data[18:20], sys.byteorder)
def marshal(self) -> bytes:
@ -211,8 +211,8 @@ class NeighbourTLV(TLV):
self.port.to_bytes(2, sys.byteorder)
def handle(self, squirrel: Any, sender: Any) -> None:
if not (self.ip_address,self.port) in squirrel.activehazelnuts and not (self.ip_address,self.port) in squirrel.potentialhazelnuts:
squirrel.potentialhazelnuts[(self.ip_address, self.port)] = squirrel.new_hazel(self.ip_address, self.port)
if not (str(self.ip_address),self.port) in squirrel.activehazelnuts and not (str(self.ip_address),self.port) in squirrel.potentialhazelnuts:
squirrel.potentialhazelnuts[(str(self.ip_address), self.port)] = squirrel.new_hazel(str(self.ip_address), self.port)
squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!")
@staticmethod
@ -220,7 +220,7 @@ class NeighbourTLV(TLV):
tlv = NeighbourTLV()
tlv.type = 3
tlv.length = 18 #A priori...
tlv.ip_address = address
tlv.ip_address = IPv6Address(address)
tlv.port = port
return tlv
@ -256,12 +256,13 @@ class DataTLV(TLV):
TODO: Check that the tuple (sender_id, nonce) is unique to avoid duplicates.
"""
msg = self.data.decode('UTF-8')
if not squirrel.receive_message_from(msg, self.sender_id, self.nonce):
# The message was already received
return
# Acknowledge the packet
squirrel.send_packet(sender, Packet.construct(AckTLV.construct(self.sender_id, self.nonce)))
if not squirrel.receive_message_from(msg, self.sender_id, self.nonce):
# The message was already received, do not print it
return
nickname_match = re.match("(.*): (.*)", msg)
if nickname_match is None:
@ -339,7 +340,7 @@ class GoAwayTLV(TLV):
def unmarshal(self, raw_data: bytes) -> None:
self.type = raw_data[0]
self.length = raw_data[1]
self.code = GoAwayTLV.GoAwayType(raw_data[2])
self.code = GoAwayType(raw_data[2])
self.message = raw_data[3:self.length - 1].decode("UTF-8")
def marshal(self) -> bytes:
@ -350,7 +351,7 @@ class GoAwayTLV(TLV):
def handle(self, squirrel: Any, sender: Any) -> None:
if squirrel.is_active(sender) :
squirrel.activehazelnuts.pop((sender.addess, sender.port))
squirrel.activehazelnuts.pop((sender.address, sender.port))
squirrel.potentialhazelnuts[(sender.address, sender.port)] = sender
squirrel.add_system_message("Some told me that he went away : "+self.message)