From a8d38faa625b4e1ada90b7994a0d72203b167f20 Mon Sep 17 00:00:00 2001 From: Eichhornchen Date: Thu, 7 Jan 2021 18:13:20 +0100 Subject: [PATCH] better? --- squinnondation/messages.py | 16 ---------------- squinnondation/peers.py | 8 ++++++-- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/squinnondation/messages.py b/squinnondation/messages.py index a7d0673..7a228ef 100644 --- a/squinnondation/messages.py +++ b/squinnondation/messages.py @@ -198,22 +198,6 @@ class HelloTLV(TLV): if not self.is_long: user.send_packet(sender, Packet.construct(HelloTLV.construct(16, user, sender))) - - def handle_multicast(self, user: Any, sender: Any) -> None: - if sender.id > 0 and sender.id != self.source_id: - user.add_system_message(f"A client known as the id {sender.id} declared that it uses " - f"the id {self.source_id}.") - sender.id = self.source_id - - if self.source_id == user.id: - sender.marked_as_banned = True - - if not sender.active: - sender.id = self.source_id # The sender we are given misses an id - - # Add entry to/actualize the active peers dictionnary - user.update_peer_table(sender) - user.add_system_message(f"{self.source_id} sent me a Hello on multicast") @property def is_long(self) -> bool: diff --git a/squinnondation/peers.py b/squinnondation/peers.py index 60a448b..e74b1c3 100644 --- a/squinnondation/peers.py +++ b/squinnondation/peers.py @@ -106,6 +106,8 @@ class User(Peer): if self.squinnondation.multicast: # Create multicast socket self.multicast_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) + # Allows address to be reused + self.multicast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.multicast_socket.bind(('', 1212)) #listen on all interfaces? # To join the group, we need to give setsockopt a binary packed representation of the multicast group's address and on what interfaces we will listen (here all) mreq = struct.pack("16s15s", socket.inet_pton(socket.AF_INET6, "ff02::4242:4242"), bytes(socket.INADDR_ANY)) #The string "16s15s" corresponds to the packing options: here it packs the arguments into a 16-byte string and a 15-byte string. @@ -1061,6 +1063,7 @@ class Multicastlistener(Thread): self.user = user def run(self) -> None: + self.user.add_system_message("running") while True: try: pkt, peer = self.user.receive_hello_multicast() @@ -1074,8 +1077,9 @@ class Multicastlistener(Thread): continue for tlv in pkt.body: - if tlv.type==2 and tlv.length==8: # Only short hello TLVs allowed - tlv.handle_multicast(self.user, peer) + # We are only supposed to receive HelloTlVs via this communication mean + self.user.add_system_message("Via multicast :") + tlv.handle(self.user, peer) self.user.refresh_history() self.user.refresh_input()