Skip to content

Commit

Permalink
Cleaned up IPv6 Ext Frag code
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Jul 16, 2024
1 parent 0372e5e commit 5f250da
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 51 deletions.
13 changes: 7 additions & 6 deletions pytcp/protocols/ip6/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def __init__(self, *, packet_rx: PacketRx) -> None:
self._validate_sanity()

packet_rx.ip = packet_rx.ip6 = self
packet_rx.frame = packet_rx.frame[IP6_HEADER_LEN: IP6_HEADER_LEN + self._header.dlen]
packet_rx.frame = packet_rx.frame[
IP6_HEADER_LEN : IP6_HEADER_LEN + self._header.dlen
]

@override
def __len__(self) -> int:
Expand All @@ -108,7 +110,9 @@ def data_copy(self) -> bytes:
Return copy of packet data.
"""

return bytes(self._frame[IP6_HEADER_LEN : IP6_HEADER_LEN + self._header.dlen])
return bytes(
self._frame[IP6_HEADER_LEN : IP6_HEADER_LEN + self._header.dlen]
)

@property
def packet_copy(self) -> bytes:
Expand All @@ -130,10 +134,7 @@ def _validate_integrity(self) -> None:
"The wrong packet length (I)",
)

if (
int.from_bytes(self._frame[4:6])
!= len(self) - IP6_HEADER_LEN
):
if int.from_bytes(self._frame[4:6]) != len(self) - IP6_HEADER_LEN:
raise Ip6IntegrityError(
"The wrong packet length (II)",
)
Expand Down
15 changes: 5 additions & 10 deletions pytcp/protocols/ip6_ext_frag/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
# pylint: disable = too-many-instance-attributes
# pylint: disable = redefined-builtin
# pylint: disable = unused-argument
# pylint: disable = too-many-arguments


"""
Module contains assembler support class for the IPv6 fragment
Expand All @@ -46,19 +48,14 @@
from pytcp.lib.tracker import Tracker
from pytcp.protocols.ip6.header import Ip6Next
from pytcp.protocols.ip6_ext_frag.base import Ip6ExtFrag
from pytcp.protocols.ip6_ext_frag.header import (
IP6_EXT_FRAG_HEADER_LEN,
Ip6ExtFragHeader,
)
from pytcp.protocols.ip6_ext_frag.header import Ip6ExtFragHeader


class Ip6ExtFragAssembler(Ip6ExtFrag, ProtoAssembler):
"""
IPv6 fragment extension header assembler support class.
"""

ip6_next = Ip6Next.FRAG

def __init__(
self,
*,
Expand All @@ -83,17 +80,15 @@ def __init__(
id=id,
)

self._data: bytes = data
self._dlen: int = len(data)
self._plen: int = len(self)
self._data = data

@override
def __len__(self) -> int:
"""
Get length of the packet.
"""

return IP6_EXT_FRAG_HEADER_LEN + len(self._data)
return len(self._header) + len(self._data)

@property
def tracker(self) -> Tracker:
Expand Down
22 changes: 14 additions & 8 deletions pytcp/protocols/ip6_ext_frag/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,11 @@ class Ip6ExtFrag(Proto, Ip6ExtFragHeaderProperties):
Base class for IPv4 packet parser and assembler.
"""

__ip6_next = Ip6Next.FRAG
__ip6__next = Ip6Next.FRAG

_header: Ip6ExtFragHeader

_data: bytes

_dlen: int

@override
def __str__(self) -> str:
"""
Expand All @@ -78,7 +75,8 @@ def __str__(self) -> str:

return (
f"IPv6_FRAG id {self._header.id}{', MF' if self._header.flag_mf else ''}, "
f"offset {self._header.offset}, next {self._header.next}"
f"offset {self._header.offset}, next {self._header.next}, "
f"len {len(self._header)}+{len(self._data)}"
)

@override
Expand All @@ -96,7 +94,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
f"! BBH L {self._dlen}s",
f"! BBH L {len(self._data)}s",
int(self._header.next),
0,
self._header.offset | self._header.flag_mf,
Expand All @@ -107,7 +105,15 @@ def __bytes__(self) -> bytes:
@property
def ip6_next(self) -> Ip6Next:
"""
Get the '__ip6_next' attribute.
Get the '__ip6__next' attribute.
"""

return self.__ip6__next

@property
def dlen(self) -> int:
"""
Get the length of the data.
"""

return self.__ip6_next
return len(self._data)
1 change: 1 addition & 0 deletions pytcp/protocols/ip6_ext_frag/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

# pylint: disable = redefined-builtin


"""
Module contains header support classes for the IPv6 fragmentation
extension header.
Expand Down
40 changes: 13 additions & 27 deletions pytcp/protocols/ip6_ext_frag/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,44 +81,24 @@ def __init__(self, *, packet_rx: PacketRx) -> None:
"""

self._frame = packet_rx.frame
self._plen = packet_rx.ip6.dlen
self._ip6__dlen = packet_rx.ip6.dlen

self._validate_integrity()
self._parse()
self._validate_sanity()

packet_rx.ip6_ext_frag = self
packet_rx.frame = packet_rx.frame[IP6_EXT_FRAG_HEADER_LEN:]
packet_rx.frame = packet_rx.frame[
IP6_EXT_FRAG_HEADER_LEN : IP6_EXT_FRAG_HEADER_LEN + self._ip6__dlen
]

@override
def __len__(self) -> int:
"""
Number of bytes remaining in the frame.
"""
return len(self._frame)

@property
def hlen(self) -> int:
"""
Calculate header length.
"""
return IP6_EXT_FRAG_HEADER_LEN

@property
def dlen(self) -> int:
"""
Calculate data length.
"""

return self._plen - IP6_EXT_FRAG_HEADER_LEN

@property
def plen(self) -> int:
"""
Calculate packet length.
"""

return self._plen
return len(self._frame)

@property
def header_copy(self) -> bytes:
Expand All @@ -134,15 +114,20 @@ def data_copy(self) -> bytes:
Return copy of packet data.
"""

return bytes(self._frame[IP6_EXT_FRAG_HEADER_LEN : self.plen])
return bytes(
self._frame[
IP6_EXT_FRAG_HEADER_LEN : IP6_EXT_FRAG_HEADER_LEN
+ self._ip6__dlen
]
)

@property
def packet_copy(self) -> bytes:
"""
Return copy of whole packet.
"""

return bytes(self._frame[: self.plen])
return bytes(self._frame[: IP6_EXT_FRAG_HEADER_LEN + self._ip6__dlen])

@override
def _validate_integrity(self) -> None:
Expand All @@ -163,6 +148,7 @@ def _parse(self) -> None:
"""

self._header = Ip6ExtFragHeader.from_bytes(self._frame)
self._data = self._frame[IP6_EXT_FRAG_HEADER_LEN:]

@override
def _validate_sanity(self) -> None:
Expand Down

0 comments on commit 5f250da

Please sign in to comment.