Skip to content

Commit

Permalink
Updated naming schema for IPv6 fragmentation extension protocol files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Jul 13, 2024
1 parent 89c651d commit 87450be
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 100 deletions.
2 changes: 1 addition & 1 deletion pytcp/lib/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from pytcp.protocols.icmp6.fpp import Icmp6Parser
from pytcp.protocols.ip4.parser import Ip4Parser
from pytcp.protocols.ip6.parser import Ip6Parser
from pytcp.protocols.ip6_ext_frag.fpp import Ip6ExtFragParser
from pytcp.protocols.ip6_ext_frag.parser import Ip6ExtFragParser
from pytcp.protocols.tcp.parser import TcpParser
from pytcp.protocols.udp.fpp import UdpParser

Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/icmp6/phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class PacketHandlerTxIcmp6(ABC):
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tx_status import TxStatus
from pytcp.protocols.ip6.base import Ip6Payload
from pytcp.protocols.ip6_ext_frag.fpa import Ip6ExtFragAssembler
from pytcp.protocols.ip6_ext_frag.assembler import Ip6ExtFragAssembler
from pytcp.protocols.raw.fpa import RawAssembler
from pytcp.protocols.tcp.assembler import TcpAssembler
from pytcp.protocols.udp.fpa import UdpAssembler
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ip6/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

if TYPE_CHECKING:
from pytcp.protocols.icmp6.fpa import Icmp6Assembler
from pytcp.protocols.ip6_ext_frag.fpa import Ip6ExtFragAssembler
from pytcp.protocols.ip6_ext_frag.assembler import Ip6ExtFragAssembler
from pytcp.protocols.raw.fpa import RawAssembler
from pytcp.protocols.tcp.assembler import TcpAssembler
from pytcp.protocols.udp.fpa import UdpAssembler
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ip6/packet_handler_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PacketHandlerTxIp6(ABC):
from pytcp.protocols.arp.assembler import ArpAssembler
from pytcp.protocols.ethernet.base import EthernetPayload
from pytcp.protocols.ip6.base import Ip6Payload
from pytcp.protocols.ip6_ext_frag.fpa import Ip6ExtFragAssembler
from pytcp.protocols.ip6_ext_frag.assembler import Ip6ExtFragAssembler
from pytcp.protocols.tcp.assembler import TcpAssembler
from pytcp.protocols.udp.fpa import UdpAssembler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@

# pylint: disable = too-many-instance-attributes
# pylint: disable = redefined-builtin
# pylint: disable = unused-argument

"""
Module contains Fast Packet Assembler support class for the IPv6 fragment
Module contains assembler support class for the IPv6 fragment
extension header.
pytcp/protocols/ip6_ext_frag/fpa.py
pytcp/protocols/ip6_ext_frag/assembler.py
ver 2.8
ver 2.9
"""


Expand All @@ -44,9 +45,9 @@
from pytcp.lib.proto import ProtoAssembler
from pytcp.lib.tracker import Tracker
from pytcp.protocols.ip6.header import Ip6Next
from pytcp.protocols.ip6_ext_frag.ps import (
from pytcp.protocols.ip6_ext_frag.base import Ip6ExtFrag
from pytcp.protocols.ip6_ext_frag.header import (
IP6_EXT_FRAG_HEADER_LEN,
Ip6ExtFrag,
Ip6ExtFragHeader,
)

Expand Down
113 changes: 113 additions & 0 deletions pytcp/protocols/ip6_ext_frag/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env python3

############################################################################
# #
# PyTCP - Python TCP/IP stack #
# Copyright (C) 2020-present Sebastian Majewski #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# Author's email: [email protected] #
# Github repository: https://github.com/ccie18643/PyTCP #
# #
############################################################################


"""
Module contains base class for the IPv6 fragmentation
extension header.
pytcp/protocols/ip6_ext_frag/base.py
ver 2.9
"""


from __future__ import annotations

import struct
from typing import TYPE_CHECKING, TypeAlias, override

from pytcp.lib.proto import Proto
from pytcp.protocols.ip6.header import Ip6Next
from pytcp.protocols.ip6_ext_frag.header import (
Ip6ExtFragHeader,
Ip6ExtFragHeaderProperties,
)

if TYPE_CHECKING:
from pytcp.protocols.icmp6.fpa import Icmp6Assembler
from pytcp.protocols.raw.fpa import RawAssembler
from pytcp.protocols.tcp.assembler import TcpAssembler
from pytcp.protocols.udp.fpa import UdpAssembler

Ip6ExtFragPayload: TypeAlias = (
Icmp6Assembler | TcpAssembler | UdpAssembler | RawAssembler
)


class Ip6ExtFrag(Proto, Ip6ExtFragHeaderProperties):
"""
Base class for IPv4 packet parser and assembler.
"""

__ip6_next = Ip6Next.FRAG

_header: Ip6ExtFragHeader

_data: bytes

_dlen: int

@override
def __str__(self) -> str:
"""
Get packet log string.
"""

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

@override
def __repr__(self) -> str:
"""
Get the packet representation string.
"""

return f"{self.__class__.__name__}({self._header!r})"

@override
def __bytes__(self) -> bytes:
"""
Get the packet in raw form.
"""

return struct.pack(
f"! BBH L {self._dlen}s",
int(self._header.next),
0,
self._header.offset | self._header.flag_mf,
self._header.id,
bytes(self._data),
)

@property
def ip6_next(self) -> Ip6Next:
"""
Get the '__ip6_next' attribute.
"""

return self.__ip6_next
80 changes: 10 additions & 70 deletions pytcp/protocols/ip6_ext_frag/ps.py → pytcp/protocols/ip6_ext_frag/header.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,23 @@


"""
Module contains base class for the IPv6 fragmentation
Module contains header support classes for the IPv6 fragmentation
extension header.
pytcp/protocols/ip6_ext_frag/ps.py
pytcp/protocols/ip6_ext_frag/header.py
ver 2.8
ver 2.9
"""


from __future__ import annotations

import struct
from abc import ABC
from dataclasses import dataclass
from typing import TYPE_CHECKING, TypeAlias, override

from pytcp.lib.proto import Proto
from pytcp.protocols.ip6.header import Ip6Next

if TYPE_CHECKING:
from pytcp.protocols.icmp6.fpa import Icmp6Assembler
from pytcp.protocols.raw.fpa import RawAssembler
from pytcp.protocols.tcp.assembler import TcpAssembler
from pytcp.protocols.udp.fpa import UdpAssembler

Ip6ExtFragPayload: TypeAlias = (
Icmp6Assembler | TcpAssembler | UdpAssembler | RawAssembler
)

# IPv6 protocol fragmentation extension header

# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Expand All @@ -61,6 +50,8 @@
# | Id |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

IP6_EXT_FRAG_HEADER_LEN = 8


@dataclass
class Ip6ExtFragHeader:
Expand Down Expand Up @@ -103,76 +94,25 @@ def from_frame(frame: bytes) -> Ip6ExtFragHeader:
)


IP6_EXT_FRAG_HEADER_LEN = 8


class Ip6ExtFrag(Proto):
class Ip6ExtFragHeaderProperties(ABC):
"""
Base class for IPv4 packet parser and assembler.
Abstract class for the IPv6 fragmentation extension header properties.
"""

_ip6_next = Ip6Next.FRAG

_header: Ip6ExtFragHeader

_data: bytes

_dlen: int

@override
def __str__(self) -> str:
"""
Get packet log string.
"""

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

@override
def __repr__(self) -> str:
"""
Get the packet representation string.
"""

return f"{self.__class__.__name__}({self._header!r})"

@override
def __bytes__(self) -> bytes:
"""
Get the packet in raw form.
"""

return struct.pack(
f"! BBH L {self._dlen}s",
int(self._header.next),
0,
self._header.offset | self._header.flag_mf,
self._header.id,
bytes(self._data),
)

@property
def ip6_next(self) -> Ip6Next:
"""
Get the 'Next' field.
"""

return self._ip6_next

@property
def next(self) -> Ip6Next:
"""
Get the '_next' header field.
Get the 'next' header field.
"""

return self._header.next

@property
def offset(self) -> int:
"""
Get the '_offset' header field.
Get the 'offset' header field.
"""

return self._header.offset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
############################################################################

# pylint: disable = expression-not-assigned
# pylint: disable = protected-access
# pylint: disable = too-few-public-methods
# pylint: disable = unused-argument
# pylint: disable = import-outside-toplevel


"""
Module contains packet handler for inbound the IPv6 fragment extension header.
pytcp/protocols/ip6_ext_frag/phrx.py
pytcp/protocols/ip6_ext_frag/packet_handler_rx.py
ver 2.8
ver 2.9
"""


Expand All @@ -45,7 +48,7 @@
from pytcp import config
from pytcp.lib.logger import log
from pytcp.lib.packet import PacketRx
from pytcp.protocols.ip6_ext_frag.fpp import Ip6ExtFragParser
from pytcp.protocols.ip6_ext_frag.parser import Ip6ExtFragParser


class PacketHandlerRxIp6ExtFrag(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
############################################################################

# pylint: disable = expression-not-assigned
# pylint: disable = protected-access
# pylint: disable = import-outside-toplevel
# pylint: disable = unused-argument


"""
Module contains packet handler for the outbound IPv6 fragment extension header.
pytcp/protocols/ip6_ext_frag/phtx.py
pytcp/protocols/ip6_ext_frag/packet_handler_tx.py
ver 2.8
ver 2.9
"""


Expand All @@ -44,8 +46,8 @@
from pytcp.lib.logger import log
from pytcp.lib.tx_status import TxStatus
from pytcp.protocols.ip6.header import IP6_HEADER_LEN
from pytcp.protocols.ip6_ext_frag.fpa import Ip6ExtFragAssembler
from pytcp.protocols.ip6_ext_frag.ps import IP6_EXT_FRAG_HEADER_LEN
from pytcp.protocols.ip6_ext_frag.assembler import Ip6ExtFragAssembler
from pytcp.protocols.ip6_ext_frag.header import IP6_EXT_FRAG_HEADER_LEN


class PacketHandlerTxIp6ExtFrag(ABC):
Expand Down
Loading

0 comments on commit 87450be

Please sign in to comment.