Skip to content

Commit

Permalink
Refactored IPv6 ND Cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 15, 2024
1 parent 3759ff4 commit d38f9dd
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions pytcp/stack/nd_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from __future__ import annotations

from dataclasses import dataclass, field
import threading
import time

Expand All @@ -43,27 +44,28 @@
from pytcp.lib.logger import log


class NdCache:
@dataclass
class CacheEntry:
"""
Support for ICMPv6 ND Cache operations.
Container class for cache entries.
"""

class CacheEntry:
"""
Container class for IPv6 ND Cache entries.
"""
mac_address: MacAddress
permanent: bool = False
create_time: int = field(
init=False,
default_factory=lambda: int(time.time()),
)
hit_count: int = 0

def __init__(
self, mac_address: MacAddress, permanent: bool = False
) -> None:
"""
Class constructor.
"""

self.mac_address: MacAddress = mac_address
self.permanent: bool = permanent
self.creation_time: float = time.time()
self.hit_count: int = 0
class NdCache:
"""
Support for ICMPv6 ND Cache operations.
"""

_nd_cache: dict[Ip6Address, CacheEntry]
_run_thread: bool

def __init__(self) -> None:
"""
Expand All @@ -72,8 +74,15 @@ def __init__(self) -> None:

__debug__ and log("stack", "Initializing IPv6 ND Cache")

self._nd_cache: dict[Ip6Address, NdCache.CacheEntry] = {}
self._run_thread: bool = False
self._nd_cache = {}
self._run_thread = False

def __repr__(self) -> str:
"""
Return string representation of the ARP Cache.
"""

return repr(self._nd_cache)

def start(self) -> None:
"""
Expand Down Expand Up @@ -113,7 +122,7 @@ def _thread__nd_cache__maintain_entries(self) -> None:

# If entry age is over maximum age then discard the entry
if (
time.time() - self._nd_cache[ip6_address].creation_time
int(time.time()) - self._nd_cache[ip6_address].create_time
> stack.ICMP6__ND__CACHE__ENTRY_MAX_AGE
):
mac_address = self._nd_cache.pop(ip6_address).mac_address
Expand All @@ -127,7 +136,7 @@ def _thread__nd_cache__maintain_entries(self) -> None:
# used since last refresh then send out request in attempt
# to refresh it.
elif (
time.time() - self._nd_cache[ip6_address].creation_time
int(time.time()) - self._nd_cache[ip6_address].create_time
> stack.ICMP6__ND__CACHE__ENTRY_MAX_AGE
- stack.ICMP6__ND__CACHE__ENTRY_REFRESH_TIME
) and self._nd_cache[ip6_address].hit_count:
Expand Down Expand Up @@ -163,7 +172,7 @@ def add_entry(
f"{ip6_address} -> {mac_address}</>",
)

self._nd_cache[ip6_address] = self.CacheEntry(mac_address)
self._nd_cache[ip6_address] = CacheEntry(mac_address)

def find_entry(self, *, ip6_address: Ip6Address) -> MacAddress | None:
"""
Expand All @@ -175,7 +184,7 @@ def find_entry(self, *, ip6_address: Ip6Address) -> MacAddress | None:
__debug__ and log(
"nd-c",
f"Found {ip6_address} -> {nd_entry.mac_address} entry, "
f"age {time.time() - nd_entry.creation_time:.0f}s, "
f"age {int(time.time()) - nd_entry.create_time}s, "
f"hit_count {nd_entry.hit_count}",
)
return nd_entry.mac_address
Expand Down

0 comments on commit d38f9dd

Please sign in to comment.