The address property of a hazel is now a string. The whole code has been changed accordingly.
This commit is contained in:
parent
c4e03ed427
commit
913f804f5e
|
@ -3,7 +3,7 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from random import randint
|
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, RLock
|
from threading import Thread, RLock
|
||||||
import curses
|
import curses
|
||||||
import re
|
import re
|
||||||
|
@ -30,7 +30,7 @@ class Hazelnut:
|
||||||
# See https://fr.wikipedia.org/wiki/Adresse_IPv6_mappant_IPv4
|
# See https://fr.wikipedia.org/wiki/Adresse_IPv6_mappant_IPv4
|
||||||
address = "::ffff:" + socket.getaddrinfo(address, None, socket.AF_INET)[0][4][0]
|
address = "::ffff:" + socket.getaddrinfo(address, None, socket.AF_INET)[0][4][0]
|
||||||
|
|
||||||
self.address = IPv6Address(address)
|
self.address = address #IPv6Address(address)
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ class Squirrel(Hazelnut):
|
||||||
# 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
|
||||||
self.socket.bind((str(self.address), self.port))
|
self.socket.bind((self.address, self.port))
|
||||||
|
|
||||||
self.squinnondation = instance
|
self.squinnondation = instance
|
||||||
|
|
||||||
|
@ -79,13 +79,13 @@ class Squirrel(Hazelnut):
|
||||||
#to have.
|
#to have.
|
||||||
|
|
||||||
self.add_system_message(f"Listening on {self.address}:{self.port}")
|
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)
|
Returns a new hazelnut (with no id nor nickname)
|
||||||
"""
|
"""
|
||||||
hazelnut = Hazelnut(address=str(address), port=port)
|
hazelnut = Hazelnut(address=address, port=port)
|
||||||
return hazelnut
|
return hazelnut
|
||||||
|
|
||||||
def is_active(self, hazel: Hazelnut) -> bool :
|
def is_active(self, hazel: Hazelnut) -> bool :
|
||||||
|
@ -94,8 +94,8 @@ class Squirrel(Hazelnut):
|
||||||
def is_potential(self, hazel: Hazelnut) -> bool :
|
def is_potential(self, hazel: Hazelnut) -> bool :
|
||||||
return (hazel.address, hazel.port) in self.potentialhazelnuts
|
return (hazel.address, hazel.port) in self.potentialhazelnuts
|
||||||
|
|
||||||
def remove_from_potential(self, address: IPv6Address, port: int)-> None:
|
def remove_from_potential(self, address: str, port: int)-> None:
|
||||||
self.potentialhazelnuts.pop((str(address), port), None)
|
self.potentialhazelnuts.pop((address, port), None)
|
||||||
|
|
||||||
def find_hazelnut(self, address: str, port: int) -> Hazelnut:
|
def find_hazelnut(self, address: str, port: int) -> Hazelnut:
|
||||||
"""
|
"""
|
||||||
|
@ -124,7 +124,7 @@ class Squirrel(Hazelnut):
|
||||||
Send a raw packet to a client.
|
Send a raw packet to a client.
|
||||||
"""
|
"""
|
||||||
self.refresh_lock.acquire()
|
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()
|
self.refresh_lock.release()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
@ -546,8 +546,8 @@ class Squirrel(Hazelnut):
|
||||||
gatlv = GoAwayTLV().construct(GoAwayType.TIMEOUT, "you did not talk to me")
|
gatlv = GoAwayTLV().construct(GoAwayType.TIMEOUT, "you did not talk to me")
|
||||||
pkt = Packet().construct(gatlv)
|
pkt = Packet().construct(gatlv)
|
||||||
self.send_packet(hazelnut[0], pkt)
|
self.send_packet(hazelnut[0], pkt)
|
||||||
self.activehazelnuts.pop((str(hazelnut[0].address), hazelnut[0].port))
|
self.activehazelnuts.pop((hazelnut[0].address, hazelnut[0].port))
|
||||||
self.potentialhazelnuts[(str(hazelnut[0].address), hazelnut[0].port)] = hazelnut[0]
|
self.potentialhazelnuts[(hazelnut[0].address, hazelnut[0].port)] = hazelnut[0]
|
||||||
|
|
||||||
self.refresh_lock.release()
|
self.refresh_lock.release()
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
import re
|
import re
|
||||||
from typing import Any, List, Optional
|
from typing import Any, List, Optional
|
||||||
from ipaddress import IPv6Address
|
#from ipaddress import IPv6Address
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
@ -162,12 +162,12 @@ class HelloTLV(TLV):
|
||||||
sender.id = self.source_id #The sender we are given misses an id
|
sender.id = self.source_id #The sender we are given misses an id
|
||||||
timeHL = time.time()
|
timeHL = time.time()
|
||||||
else :
|
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 :
|
if self.is_long and self.dest_id == squirrel.id :
|
||||||
timeHL = time.time()
|
timeHL = time.time()
|
||||||
squirrel.remove_from_potential(sender.address, sender.port)
|
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]
|
timeHL]
|
||||||
squirrel.nbNS += 1
|
squirrel.nbNS += 1
|
||||||
squirrel.add_system_message(f"Aaaawwww, {self.source_id} spoke to me and said me Hello "
|
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):
|
class NeighbourTLV(TLV):
|
||||||
type: int = 3
|
type: int = 3
|
||||||
length: int
|
length: int
|
||||||
ip_address: IPv6Address
|
ip_address: str
|
||||||
port: int
|
port: int
|
||||||
|
|
||||||
def unmarshal(self, raw_data: bytes) -> None:
|
def unmarshal(self, raw_data: bytes) -> None:
|
||||||
self.type = raw_data[0]
|
self.type = raw_data[0]
|
||||||
self.length = raw_data[1]
|
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)
|
self.port = int.from_bytes(raw_data[18:20], sys.byteorder)
|
||||||
|
|
||||||
def marshal(self) -> bytes:
|
def marshal(self) -> bytes:
|
||||||
|
@ -211,12 +211,12 @@ class NeighbourTLV(TLV):
|
||||||
self.port.to_bytes(2, sys.byteorder)
|
self.port.to_bytes(2, sys.byteorder)
|
||||||
|
|
||||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
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:
|
if not (self.ip_address,self.port) in squirrel.activehazelnuts and not (self.ip_address,self.port) in squirrel.potentialhazelnuts:
|
||||||
squirrel.potentialhazelnuts[(str(self.ip_address), self.port)] = squirrel.new_hazel(self.ip_address, self.port)
|
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}!")
|
squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def construct(address: IPv6Address, port: int) -> "NeighbourTLV":
|
def construct(address: str, port: int) -> "NeighbourTLV":
|
||||||
tlv = NeighbourTLV()
|
tlv = NeighbourTLV()
|
||||||
tlv.type = 3
|
tlv.type = 3
|
||||||
tlv.length = 18 #A priori...
|
tlv.length = 18 #A priori...
|
||||||
|
@ -351,7 +351,7 @@ class GoAwayTLV(TLV):
|
||||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||||
if squirrel.is_active(sender) :
|
if squirrel.is_active(sender) :
|
||||||
squirrel.activehazelnuts.pop((sender.addess, sender.port))
|
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)
|
squirrel.add_system_message("Some told me that he went away : "+self.message)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
Loading…
Reference in New Issue