some interfaces where missing when checking if an address is local

This commit is contained in:
study-faraphel 2025-01-07 10:43:25 +01:00
parent a4ca2865c4
commit f85559e15c
3 changed files with 13 additions and 29 deletions

View file

@ -5,6 +5,7 @@ sortedcontainers
numpy
# networking
netifaces
msgpack
# cryptography

View file

@ -6,6 +6,7 @@ import zlib
from datetime import datetime
import bidict
import netifaces
from source import packets, utils, structures
from source.behaviors import roles
@ -23,6 +24,7 @@ class CommunicationManager:
self.manager = manager
self.broadcast_address = broadcast_address
self.interface = interface
self.port = port
# create an IPv6 UDP socket
@ -221,14 +223,15 @@ class CommunicationManager:
# decode the payload
return self.packet_decode(payload), address
@staticmethod
def get_local_addresses() -> list[tuple]:
def get_local_hosts(self) -> typing.Iterator[str]:
"""
Get the local addresses of the machine
:return: the local addresses of the machine
Get the local hosts addresses of the machine
:return: the local hosts addresses of the machine
"""
return socket.getaddrinfo(socket.gethostname(), None)
for interface_id, interface_addresses in netifaces.ifaddresses(self.interface).items():
for interface_address in interface_addresses:
yield interface_address["addr"].split("%")[0]
def is_address_local(self, address: tuple) -> bool:
"""
@ -238,28 +241,8 @@ class CommunicationManager:
host, _, _, scope = address
# check for all the interfaces of our machine
for interface in self.get_local_addresses():
# unpack the interface information
interface_family, _, _, _, interface_address = interface
match interface_family:
case socket.AddressFamily.AF_INET:
interface_host, interface_port = interface_address
# check if it matches the address interface
if host == interface_host:
return True
case socket.AddressFamily.AF_INET6:
interface_host, interface_port, interface_flowinfo, interface_scope = interface_address
# check if it matches the address interface
if host == interface_host and scope == interface_scope:
return True
# no matching interfaces have been found
return False
# check if the host is in our local hosts list
return host in list(self.get_local_hosts())
def save_trusted_peers(self) -> None:
"""

View file

@ -9,9 +9,9 @@ class Peer:
master: bool = dataclasses.field()
# public asymmetric key
public_key: bytes = dataclasses.field()
public_key: bytes = dataclasses.field(repr=False)
# secret symmetric key
secret_key: Optional[bytes] = dataclasses.field(default=None)
secret_key: Optional[bytes] = dataclasses.field(default=None, repr=False)
# is the machine trusted
trusted: bool = dataclasses.field(default=False)