diff --git a/pyproject.toml b/pyproject.toml index 7bded3ef..4c607b8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -84,4 +84,5 @@ disable = """ superfluous-parens, expression-not-assigned, no-member, + too-many-ancestors, """ diff --git a/pytcp/__init__.py b/pytcp/__init__.py index 1180e9ab..e2ea2989 100755 --- a/pytcp/__init__.py +++ b/pytcp/__init__.py @@ -140,9 +140,11 @@ def start(self) -> None: stack.rx_ring.start(self.rx_fd) stack.tx_ring.start(self.tx_fd) stack.packet_handler.start() - stack.packet_handler._acquire_ip6_addresses() - stack.packet_handler._acquire_ip4_addresses() - stack.packet_handler._log_stack_address_info() + + stack.packet_handler.acquire_ip6_addresses() + stack.packet_handler.acquire_ip4_addresses() + + stack.packet_handler.log_stack_address_info() def stop(self) -> None: """ diff --git a/pytcp/subsystems/packet_handler.py b/pytcp/subsystems/packet_handler.py index 26a7c24e..d4ccb372 100755 --- a/pytcp/subsystems/packet_handler.py +++ b/pytcp/subsystems/packet_handler.py @@ -219,6 +219,78 @@ def stop(self) -> None: self._run_thread = False time.sleep(0.1) + def acquire_ip6_addresses(self) -> None: + """ + Assign the IPv6 addresses. + """ + + if config.IP6__SUPPORT_ENABLED: + self._assign_ip6_multicast(Ip6Address("ff02::1")) + self._create_stack_ip6_addressing() + + def acquire_ip4_addresses(self) -> None: + """ + Acquire the IPv4 addresses. + """ + + if config.IP4__SUPPORT_ENABLED: + if not self.ip4_host_candidate: + if config.IP4__HOST_DHCP: + if ip4_host := Dhcp4Client(self.mac_unicast).fetch(): + self.ip4_host_candidate.append(ip4_host) + self._create_stack_ip4_addressing() + + def log_stack_address_info(self) -> None: + """ + Log all the addresses stack will listen on + """ + + if __debug__: + log( + "stack", + "Stack listening on unicast MAC address: " + f"{self.mac_unicast}", + ) + log( + "stack", + "Stack listening on multicast MAC addresses: " + f"{', '.join([str(mac_multicast) for mac_multicast in set(self.mac_multicast)])}", + ) + log( + "stack", + "Stack listening on broadcast MAC address: " + f"{self.mac_broadcast}", + ) + + if config.IP6__SUPPORT_ENABLED: + log( + "stack", + "Stack listening on unicast IPv6 addresses: " + f"{', '.join([str(ip6_unicast) for ip6_unicast in self.ip6_unicast])}", + ) + log( + "stack", + "Stack listening on multicast IPv6 addresses: " + f"{', '.join([str(ip6_multicast) for ip6_multicast in set(self.ip6_multicast)])}", + ) + + if config.IP4__SUPPORT_ENABLED: + log( + "stack", + "Stack listening on unicast IPv4 addresses: " + f"{', '.join([str(ip4_unicast) for ip4_unicast in self.ip4_unicast])}", + ) + log( + "stack", + "Stack listening on multicast IPv4 addresses: " + f"{', '.join([str(ip4_multicast) for ip4_multicast in self.ip4_multicast])}", + ) + log( + "stack", + "Stack listening on broadcast IPv4 addresses: " + f"{', '.join([str(ip4_broadcast) for ip4_broadcast in self.ip4_broadcast])}", + ) + def __thread_packet_handler(self) -> None: """ Thread picks up incoming packets from RX ring and processes them. @@ -252,15 +324,6 @@ def _assign_ip6_address(self, *, ip6_host: Ip6Host) -> None: self.ip6_host_candidate.append(ip6_host) - def _acquire_ip6_addresses(self) -> None: - """ - Assign the IPv6 addresses. - """ - - if config.IP6__SUPPORT_ENABLED: - self._assign_ip6_multicast(Ip6Address("ff02::1")) - self._create_stack_ip6_addressing() - def _assign_ip4_address(self, *, ip4_host: Ip4Host) -> None: """ Assign IPv4 address information. @@ -268,18 +331,6 @@ def _assign_ip4_address(self, *, ip4_host: Ip4Host) -> None: self.ip4_host_candidate.append(ip4_host) - def _acquire_ip4_addresses(self) -> None: - """ - Acquire the IPv4 addresses. - """ - - if config.IP4__SUPPORT_ENABLED: - if not self.ip4_host_candidate: - if config.IP4__HOST_DHCP: - if ip4_host := Dhcp4Client(self.mac_unicast).fetch(): - self.ip4_host_candidate.append(ip4_host) - self._create_stack_ip4_addressing() - def _perform_ip6_nd_dad(self, *, ip6_unicast_candidate: Ip6Address) -> bool: """ Perform IPv6 ND Duplicate Address Detection, return True if passed. @@ -494,54 +545,3 @@ def _remove_mac_multicast(self, /, mac_multicast: MacAddress) -> None: self.mac_multicast.remove(mac_multicast) __debug__ and log("stack", f"Removed MAC multicast {mac_multicast}") - - def _log_stack_address_info(self) -> None: - """ - Log all the addresses stack will listen on - """ - - if __debug__: - log( - "stack", - "Stack listening on unicast MAC address: " - f"{self.mac_unicast}", - ) - log( - "stack", - "Stack listening on multicast MAC addresses: " - f"{', '.join([str(mac_multicast) for mac_multicast in set(self.mac_multicast)])}", - ) - log( - "stack", - "Stack listening on broadcast MAC address: " - f"{self.mac_broadcast}", - ) - - if config.IP6__SUPPORT_ENABLED: - log( - "stack", - "Stack listening on unicast IPv6 addresses: " - f"{', '.join([str(ip6_unicast) for ip6_unicast in self.ip6_unicast])}", - ) - log( - "stack", - "Stack listening on multicast IPv6 addresses: " - f"{', '.join([str(ip6_multicast) for ip6_multicast in set(self.ip6_multicast)])}", - ) - - if config.IP4__SUPPORT_ENABLED: - log( - "stack", - "Stack listening on unicast IPv4 addresses: " - f"{', '.join([str(ip4_unicast) for ip4_unicast in self.ip4_unicast])}", - ) - log( - "stack", - "Stack listening on multicast IPv4 addresses: " - f"{', '.join([str(ip4_multicast) for ip4_multicast in self.ip4_multicast])}", - ) - log( - "stack", - "Stack listening on broadcast IPv4 addresses: " - f"{', '.join([str(ip4_broadcast) for ip4_broadcast in self.ip4_broadcast])}", - )