From 913f804f5ebdd7121ca21a328e29ed5c936490e6 Mon Sep 17 00:00:00 2001 From: eichhornchen Date: Mon, 28 Dec 2020 15:00:36 +0100 Subject: [PATCH] The address property of a hazel is now a string. The whole code has been changed accordingly. --- squinnondation/hazel.py | 22 +++++++++++----------- squinnondation/messages.py | 18 +++++++++--------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/squinnondation/hazel.py b/squinnondation/hazel.py index 4dc371a..4d1806e 100644 --- a/squinnondation/hazel.py +++ b/squinnondation/hazel.py @@ -3,7 +3,7 @@ from datetime import datetime from random import randint from typing import Any, Tuple -from ipaddress import IPv6Address +#from ipaddress import IPv6Address from threading import Thread, RLock import curses import re @@ -30,7 +30,7 @@ class Hazelnut: # See https://fr.wikipedia.org/wiki/Adresse_IPv6_mappant_IPv4 address = "::ffff:" + socket.getaddrinfo(address, None, socket.AF_INET)[0][4][0] - self.address = IPv6Address(address) + self.address = address #IPv6Address(address) self.port = port @@ -48,7 +48,7 @@ class Squirrel(Hazelnut): # Create UDP socket self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) # Bind the socket - self.socket.bind((str(self.address), self.port)) + self.socket.bind((self.address, self.port)) self.squinnondation = instance @@ -79,13 +79,13 @@ class Squirrel(Hazelnut): #to have. self.add_system_message(f"Listening on {self.address}:{self.port}") - self.add_system_message(f"I am {self.id}") + self.add_system_message(f"I am {self.id}}") - def new_hazel(self, address: IPv6Address, port: int) -> Hazelnut: + def new_hazel(self, address: str, port: int) -> Hazelnut: """ Returns a new hazelnut (with no id nor nickname) """ - hazelnut = Hazelnut(address=str(address), port=port) + hazelnut = Hazelnut(address=address, port=port) return hazelnut def is_active(self, hazel: Hazelnut) -> bool : @@ -94,8 +94,8 @@ class Squirrel(Hazelnut): def is_potential(self, hazel: Hazelnut) -> bool : return (hazel.address, hazel.port) in self.potentialhazelnuts - def remove_from_potential(self, address: IPv6Address, port: int)-> None: - self.potentialhazelnuts.pop((str(address), port), None) + def remove_from_potential(self, address: str, port: int)-> None: + self.potentialhazelnuts.pop((address, port), None) def find_hazelnut(self, address: str, port: int) -> Hazelnut: """ @@ -124,7 +124,7 @@ class Squirrel(Hazelnut): Send a raw packet to a client. """ self.refresh_lock.acquire() - res = self.socket.sendto(data, (str(client.address), client.port)) + res = self.socket.sendto(data, (client.address, client.port)) self.refresh_lock.release() return res @@ -546,8 +546,8 @@ class Squirrel(Hazelnut): gatlv = GoAwayTLV().construct(GoAwayType.TIMEOUT, "you did not talk to me") pkt = Packet().construct(gatlv) self.send_packet(hazelnut[0], pkt) - self.activehazelnuts.pop((str(hazelnut[0].address), hazelnut[0].port)) - self.potentialhazelnuts[(str(hazelnut[0].address), hazelnut[0].port)] = hazelnut[0] + self.activehazelnuts.pop((hazelnut[0].address, hazelnut[0].port)) + self.potentialhazelnuts[(hazelnut[0].address, hazelnut[0].port)] = hazelnut[0] self.refresh_lock.release() diff --git a/squinnondation/messages.py b/squinnondation/messages.py index 519a8d5..cdc7ff7 100644 --- a/squinnondation/messages.py +++ b/squinnondation/messages.py @@ -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 @@ -162,12 +162,12 @@ class HelloTLV(TLV): sender.id = self.source_id #The sender we are given misses an id timeHL = time.time() else : - timeHL = squirrel.activehazelnuts[(str(sender.address), sender.port)] + timeHL = squirrel.activehazelnuts[(sender.address, sender.port)] if self.is_long and self.dest_id == squirrel.id : timeHL = time.time() squirrel.remove_from_potential(sender.address, sender.port) - squirrel.activehazelnuts[(str(sender.address), sender.port)] = [sender, timeH,\ + squirrel.activehazelnuts[(sender.address, sender.port)] = [sender, timeH,\ timeHL] squirrel.nbNS += 1 squirrel.add_system_message(f"Aaaawwww, {self.source_id} spoke to me and said me Hello " @@ -195,13 +195,13 @@ class HelloTLV(TLV): class NeighbourTLV(TLV): type: int = 3 length: int - ip_address: IPv6Address + ip_address: str port: int def unmarshal(self, raw_data: bytes) -> None: self.type = raw_data[0] self.length = raw_data[1] - self.ip_address = IPv6Address(raw_data[2:18]) + self.ip_address = raw_data[2:18]#IPv6Address() self.port = int.from_bytes(raw_data[18:20], sys.byteorder) def marshal(self) -> bytes: @@ -211,12 +211,12 @@ class NeighbourTLV(TLV): self.port.to_bytes(2, sys.byteorder) def handle(self, squirrel: Any, sender: Any) -> None: - 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(self.ip_address, self.port) + 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) squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!") @staticmethod - def construct(address: IPv6Address, port: int) -> "NeighbourTLV": + def construct(address: str, port: int) -> "NeighbourTLV": tlv = NeighbourTLV() tlv.type = 3 tlv.length = 18 #A priori... @@ -351,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.potentialhazelnuts[(str(sender.address), sender.port)] = sender + squirrel.potentialhazelnuts[(sender.address, sender.port)] = sender squirrel.add_system_message("Some told me that he went away : "+self.message) @staticmethod