Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 5, 2024
1 parent e944709 commit 4b9967e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 75 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ disable = """
superfluous-parens,
expression-not-assigned,
no-member,
too-many-ancestors,
"""
8 changes: 5 additions & 3 deletions pytcp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
144 changes: 72 additions & 72 deletions pytcp/subsystems/packet_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"<INFO>Stack listening on unicast MAC address: "
f"{self.mac_unicast}</>",
)
log(
"stack",
"<INFO>Stack listening on multicast MAC addresses: "
f"{', '.join([str(mac_multicast) for mac_multicast in set(self.mac_multicast)])}</>",
)
log(
"stack",
"<INFO>Stack listening on broadcast MAC address: "
f"{self.mac_broadcast}</>",
)

if config.IP6__SUPPORT_ENABLED:
log(
"stack",
"<INFO>Stack listening on unicast IPv6 addresses: "
f"{', '.join([str(ip6_unicast) for ip6_unicast in self.ip6_unicast])}</>",
)
log(
"stack",
"<INFO>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",
"<INFO>Stack listening on unicast IPv4 addresses: "
f"{', '.join([str(ip4_unicast) for ip4_unicast in self.ip4_unicast])}</>",
)
log(
"stack",
"<INFO>Stack listening on multicast IPv4 addresses: "
f"{', '.join([str(ip4_multicast) for ip4_multicast in self.ip4_multicast])}</>",
)
log(
"stack",
"<INFO>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.
Expand Down Expand Up @@ -252,34 +324,13 @@ 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.
"""

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.
Expand Down Expand Up @@ -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",
"<INFO>Stack listening on unicast MAC address: "
f"{self.mac_unicast}</>",
)
log(
"stack",
"<INFO>Stack listening on multicast MAC addresses: "
f"{', '.join([str(mac_multicast) for mac_multicast in set(self.mac_multicast)])}</>",
)
log(
"stack",
"<INFO>Stack listening on broadcast MAC address: "
f"{self.mac_broadcast}</>",
)

if config.IP6__SUPPORT_ENABLED:
log(
"stack",
"<INFO>Stack listening on unicast IPv6 addresses: "
f"{', '.join([str(ip6_unicast) for ip6_unicast in self.ip6_unicast])}</>",
)
log(
"stack",
"<INFO>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",
"<INFO>Stack listening on unicast IPv4 addresses: "
f"{', '.join([str(ip4_unicast) for ip4_unicast in self.ip4_unicast])}</>",
)
log(
"stack",
"<INFO>Stack listening on multicast IPv4 addresses: "
f"{', '.join([str(ip4_multicast) for ip4_multicast in self.ip4_multicast])}</>",
)
log(
"stack",
"<INFO>Stack listening on broadcast IPv4 addresses: "
f"{', '.join([str(ip4_broadcast) for ip4_broadcast in self.ip4_broadcast])}</>",
)

0 comments on commit 4b9967e

Please sign in to comment.