diff --git a/pimdm/Kernel.py b/pimdm/Kernel.py index e1e826c..4231045 100644 --- a/pimdm/Kernel.py +++ b/pimdm/Kernel.py @@ -51,9 +51,9 @@ def __init__(self, kernel_socket): self.tree_logger = Main.logger.getChild('KernelTree') # receive signals from kernel with a background thread - handler_thread = Thread(target=self.handler) - handler_thread.daemon = True - handler_thread.start() + self.handler_thread = Thread(target=self.handler) + self.handler_thread.daemon = True + self.handler_thread.start() ''' Structure to create/remove virtual interfaces @@ -177,9 +177,14 @@ def remove_multicast_route(self, kernel_entry: KernelEntry): raise NotImplementedError @abstractmethod - def exit(self): + def close_socket(self): raise NotImplementedError + def exit(self): + self.running = False + self.close_socket() + self.handler_thread.join() + @abstractmethod def handler(self): raise NotImplementedError @@ -412,12 +417,15 @@ def remove_multicast_route(self, kernel_entry: KernelEntry): if len(self.routing[kernel_entry.source_ip]) == 0: self.routing.pop(kernel_entry.source_ip) - def exit(self): - self.running = False + def close_socket(self): + with socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IGMP) as s: + s.sendto(b'', ('', 0)) + self.socket.close() + def exit(self): # MRT DONE self.socket.setsockopt(socket.IPPROTO_IP, self.MRT_DONE, 1) - self.socket.close() + super().exit() ''' @@ -660,12 +668,15 @@ def remove_multicast_route(self, kernel_entry: KernelEntry): if len(self.routing[kernel_entry.source_ip]) == 0: self.routing.pop(kernel_entry.source_ip) - def exit(self): - self.running = False + def close_socket(self): + with socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_ICMPV6) as s: + s.sendto(b'0000', ('', 0)) + self.socket.close() + def exit(self): # MRT DONE self.socket.setsockopt(socket.IPPROTO_IPV6, self.MRT6_DONE, 1) - self.socket.close() + super().exit() ''' /* diff --git a/pimdm/Run.py b/pimdm/Run.py index 7b7ea14..9e67ab5 100644 --- a/pimdm/Run.py +++ b/pimdm/Run.py @@ -178,10 +178,8 @@ def main(): if args.start: start() - sys.exit(0) elif args.stop: client_socket(args) - sys.exit(0) elif args.config: try: from pimdm import Config @@ -189,22 +187,19 @@ def main(): pim_globals.MULTICAST_TABLE_ID, pim_globals.UNICAST_TABLE_ID = Config.get_vrfs(conf_file_path) start(conf_file_path) except (ImportError, ModuleNotFoundError): - sys.exit("PYYAML needs to be installed. Execute \"pip3 install pyyaml\"") + raise Exception("PYYAML needs to be installed. Execute \"pip3 install pyyaml\"") elif args.verbose: os.system("tail -f {}".format(PROCESS_LOG_STDOUT_FILE.format(pim_globals.MULTICAST_TABLE_ID))) - sys.exit(0) elif args.multicast_routes: if args.ipv4 or not args.ipv6: os.system("ip mroute show table " + str(pim_globals.MULTICAST_TABLE_ID)) elif args.ipv6: os.system("ip -6 mroute show table " + str(pim_globals.MULTICAST_TABLE_ID)) - sys.exit(0) elif not is_running(): print("PIM-DM is not running") parser.print_usage() - sys.exit(0) - - client_socket(args) + else: + client_socket(args) def process_file_path(): return os.path.join(PROCESS_DIRECTORY, str(pim_globals.MULTICAST_TABLE_ID))