Skip to content

Commit

Permalink
Reorganized file schema for the Ethernet protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Jul 13, 2024
1 parent 64abb36 commit 36a8c8f
Show file tree
Hide file tree
Showing 25 changed files with 145 additions and 96 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"python.analysis.extraPaths": [
"./pytcp"
"./"
],
"python.formatting.provider": "none",
"[python]": {
"editor.defaultFormatter": "mikoz.black-py"
}
}
}
2 changes: 1 addition & 1 deletion pytcp/lib/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

if TYPE_CHECKING:
from pytcp.protocols.arp.parser import ArpParser
from pytcp.protocols.ethernet.fpp import EthernetParser
from pytcp.protocols.ethernet.parser import EthernetParser
from pytcp.protocols.icmp4.fpp import Icmp4Parser
from pytcp.protocols.icmp6.fpp import Icmp6Parser
from pytcp.protocols.ip4.fpp import Ip4Parser
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/arp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

from pytcp.lib.proto import Proto
from pytcp.protocols.arp.header import ArpHeaderProperties
from pytcp.protocols.ethernet.ps import EthernetType
from pytcp.protocols.ethernet.header import EthernetType

if TYPE_CHECKING:
from pytcp.protocols.arp.header import ArpHeader
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/arp/packet_handler_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PacketHandlerTxArp(ABC):
if TYPE_CHECKING:
from pytcp.lib.ip4_address import Ip4Address
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.protocols.ethernet.ps import EthernetPayload
from pytcp.protocols.ethernet.base import EthernetPayload
from pytcp.protocols.ip4.fpa import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.raw.fpa import RawAssembler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@


"""
Module contains Fast Packet Assembler support class for the Ethernet protocol.
Module contains assembler support class for the Ethernet protocol.
pytcp/protocols/ethernet/fpa.py
pytcp/protocols/ethernet/assembler.py
ver 2.9
"""
Expand All @@ -39,11 +39,12 @@

from pytcp.lib.mac_address import MacAddress
from pytcp.lib.proto import ProtoAssembler
from pytcp.protocols.ethernet.ps import Ethernet, EthernetHeader
from pytcp.protocols.ethernet.base import Ethernet
from pytcp.protocols.ethernet.header import EthernetHeader
from pytcp.protocols.raw.fpa import RawAssembler

if TYPE_CHECKING:
from pytcp.protocols.ethernet.ps import EthernetPayload
from pytcp.protocols.ethernet.base import EthernetPayload


class EthernetAssembler(Ethernet, ProtoAssembler):
Expand Down
92 changes: 92 additions & 0 deletions pytcp/protocols/ethernet/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/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 packet structure information for the Ethernet protccol.
pytcp/protocols/ethernet/base.py
ver 2.9
"""


from __future__ import annotations

from typing import TYPE_CHECKING, TypeAlias, override

from pytcp.lib.proto import Proto
from pytcp.protocols.ethernet.header import EthernetHeaderProperties

if TYPE_CHECKING:
from pytcp.protocols.arp.assembler import ArpAssembler
from pytcp.protocols.ethernet.header import EthernetHeader
from pytcp.protocols.ip4.fpa import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.raw.fpa import RawAssembler

EthernetPayload: TypeAlias = (
ArpAssembler
| Ip4Assembler
| Ip4FragAssembler
| Ip6Assembler
| RawAssembler
)


class Ethernet(Proto, EthernetHeaderProperties):
"""
Base class for Ethernet packet parser and assembler.
"""

_header: EthernetHeader

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

return (
f"ETHER {self._header.src} > {self._header.dst}, "
f"0x{int(self._header.type):0>4x} "
f"({self._header.type}), plen {len(self)}"
)

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

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

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

return bytes(self._header)
55 changes: 6 additions & 49 deletions pytcp/protocols/ethernet/ps.py → pytcp/protocols/ethernet/header.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@


"""
Module contains packet structure information for the Ethernet protccol.
Module contains header support classes for the Ethernet protccol.
pytcp/protocols/ethernet/ps.py
pytcp/protocols/ethernet/header.py
ver 2.9
"""
Expand All @@ -36,26 +36,11 @@
from __future__ import annotations

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

from pytcp.lib.enum import ProtoEnum
from pytcp.lib.mac_address import MacAddress
from pytcp.lib.proto import Proto

if TYPE_CHECKING:
from pytcp.protocols.arp.assembler import ArpAssembler
from pytcp.protocols.ip4.fpa import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.raw.fpa import RawAssembler

EthernetPayload: TypeAlias = (
ArpAssembler
| Ip4Assembler
| Ip4FragAssembler
| Ip6Assembler
| RawAssembler
)

# Ethernet packet header.

Expand All @@ -66,7 +51,7 @@
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Source MAC Address +
# > |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | EthernetType |
# | EthernetType |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

ETHERNET_HEADER_LEN = 14
Expand Down Expand Up @@ -129,41 +114,13 @@ def from_frame(frame: bytes) -> EthernetHeader:
)


class Ethernet(Proto):
class EthernetHeaderProperties(ABC):
"""
Base class for Ethernet packet parser and assembler.
Abstract class containing Ethernet header properties.
"""

_header: EthernetHeader

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

return (
f"ETHER {self._header.src} > {self._header.dst}, "
f"0x{int(self._header.type):0>4x} "
f"({self._header.type}), plen {len(self)}"
)

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

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

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

return bytes(self._header)

@property
def dst(self) -> MacAddress:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
# #
############################################################################

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

"""
Module contains packet handler for the inbound Ethernet packets.
pytcp/protocols/ethernet/phrx.py
pytcp/protocols/ethernet/packet_handler_rx.py
ver 2.8
ver 2.9
"""

from __future__ import annotations
Expand All @@ -42,8 +43,8 @@
from pytcp import config
from pytcp.lib.errors import PacketValidationError
from pytcp.lib.logger import log
from pytcp.protocols.ethernet.fpp import EthernetParser
from pytcp.protocols.ethernet.ps import EthernetType
from pytcp.protocols.ethernet.header import EthernetType
from pytcp.protocols.ethernet.parser import EthernetParser


class PacketHandlerRxEthernet(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
# pylint: disable = too-many-branches
# pylint: disable = too-many-statements
# pylint: disable = expression-not-assigned
# pylint: disable = protected-access
# pylint: disable = no-else-return
# pylint: disable = import-outside-toplevel

"""
Module contains packet handler for the outbound Ethernet packets.
pytcp/protocols/ethernet/phtx.py
ver 2.8
ver 2.9
"""


Expand All @@ -49,7 +49,7 @@
from pytcp.lib.logger import log
from pytcp.lib.mac_address import MacAddress
from pytcp.lib.tx_status import TxStatus
from pytcp.protocols.ethernet.fpa import EthernetAssembler
from pytcp.protocols.ethernet.assembler import EthernetAssembler
from pytcp.protocols.ip4.fpa import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.raw.fpa import RawAssembler
Expand All @@ -65,7 +65,7 @@ class PacketHandlerTxEthernet(ABC):
from pytcp.lib.ip6_address import Ip6Host
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.protocols.arp.assembler import ArpAssembler
from pytcp.protocols.ethernet.ps import EthernetPayload
from pytcp.protocols.ethernet.base import EthernetPayload

packet_stats_tx: PacketStatsTx
mac_unicast: MacAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

# pylint: disable = too-many-instance-attributes
# pylint: disable = attribute-defined-outside-init
# pylint: disable = fixme

"""
Module contains Fast Packet Parser support class for the Ethernet protocol.
Module contains parser support class for the Ethernet protocol.
pytcp/protocols/ethernet/fpp.py
pytcp/protocols/ethernet/parser.py
ver 2.9
"""
Expand All @@ -40,11 +41,8 @@

from pytcp.lib.errors import PacketIntegrityError, PacketSanityError
from pytcp.lib.proto import ProtoParser
from pytcp.protocols.ethernet.ps import (
ETHERNET_HEADER_LEN,
Ethernet,
EthernetHeader,
)
from pytcp.protocols.ethernet.base import Ethernet
from pytcp.protocols.ethernet.header import ETHERNET_HEADER_LEN, EthernetHeader

if TYPE_CHECKING:
from pytcp.lib.packet import PacketRx
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ip4/fpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from pytcp.lib.ip_helper import inet_cksum
from pytcp.lib.proto import ProtoAssembler
from pytcp.lib.tracker import Tracker
from pytcp.protocols.ethernet.ps import EthernetType
from pytcp.protocols.ethernet.header import EthernetType
from pytcp.protocols.ip4.ps import (
IP4_HEADER_LEN,
Ip4,
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ip4/phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PacketHandlerTxIp4(ABC):
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tracker import Tracker
from pytcp.protocols.arp.assembler import ArpAssembler
from pytcp.protocols.ethernet.ps import EthernetPayload
from pytcp.protocols.ethernet.base import EthernetPayload
from pytcp.protocols.ip4.ps import Ip4Payload
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.tcp.fpa import TcpAssembler
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ip4/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pytcp.lib.enum import ProtoEnum
from pytcp.lib.ip4_address import Ip4Address
from pytcp.lib.proto import Proto
from pytcp.protocols.ethernet.ps import EthernetType
from pytcp.protocols.ethernet.header import EthernetType

if TYPE_CHECKING:
from pytcp.protocols.icmp4.fpa import Icmp4Assembler
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ip6/fpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pytcp import config
from pytcp.lib.ip6_address import Ip6Address
from pytcp.lib.proto import ProtoAssembler
from pytcp.protocols.ethernet.ps import EthernetType
from pytcp.protocols.ethernet.header import EthernetType
from pytcp.protocols.ip6.ps import IP6_HEADER_LEN, Ip6, Ip6Header, Ip6Next
from pytcp.protocols.raw.fpa import RawAssembler

Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ip6/phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class PacketHandlerTxIp6(ABC):
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tracker import Tracker
from pytcp.protocols.arp.assembler import ArpAssembler
from pytcp.protocols.ethernet.ps import EthernetPayload
from pytcp.protocols.ethernet.base import EthernetPayload
from pytcp.protocols.ip6.ps import Ip6Payload
from pytcp.protocols.ip6_ext_frag.fpa import Ip6ExtFragAssembler
from pytcp.protocols.tcp.fpa import TcpAssembler
Expand Down
Loading

0 comments on commit 36a8c8f

Please sign in to comment.