Skip to content

Commit

Permalink
Converted IPv4 protocol files to the nes naming schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Jul 13, 2024
1 parent 36a8c8f commit b8e3803
Show file tree
Hide file tree
Showing 26 changed files with 706 additions and 606 deletions.
2 changes: 1 addition & 1 deletion pytcp/lib/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
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
from pytcp.protocols.ip4.parser import Ip4Parser
from pytcp.protocols.ip6.fpp import Ip6Parser
from pytcp.protocols.ip6_ext_frag.fpp import Ip6ExtFragParser
from pytcp.protocols.tcp.fpp import TcpParser
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 @@ -62,7 +62,7 @@ class PacketHandlerTxArp(ABC):
from pytcp.lib.ip4_address import Ip4Address
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.protocols.ethernet.base import EthernetPayload
from pytcp.protocols.ip4.fpa import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip4.assembler import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.raw.fpa import RawAssembler

Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ethernet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
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.ip4.assembler import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.raw.fpa import RawAssembler

Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/ethernet/packet_handler_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from pytcp.lib.mac_address import MacAddress
from pytcp.lib.tx_status import TxStatus
from pytcp.protocols.ethernet.assembler import EthernetAssembler
from pytcp.protocols.ip4.fpa import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip4.assembler import Ip4Assembler, Ip4FragAssembler
from pytcp.protocols.ip6.fpa import Ip6Assembler
from pytcp.protocols.raw.fpa import RawAssembler

Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/icmp4/fpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
Icmp4Message,
Icmp4PortUnreachableMessage,
)
from pytcp.protocols.ip4.ps import IP4_HEADER_LEN
from pytcp.protocols.ip4.header import IP4_HEADER_LEN


class Icmp4Assembler(Icmp4, ProtoAssembler):
Expand Down
5 changes: 3 additions & 2 deletions pytcp/protocols/icmp4/phrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
############################################################################

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

"""
Module contains packet handler for the inbound ICMPv4 packets.
Expand All @@ -51,7 +52,7 @@
Icmp4EchoRequestMessage,
Icmp4PortUnreachableMessage,
)
from pytcp.protocols.ip4.ps import IP4_HEADER_LEN, Ip4Proto
from pytcp.protocols.ip4.header import IP4_HEADER_LEN, Ip4Proto
from pytcp.protocols.udp.metadata import UdpMetadata
from pytcp.protocols.udp.ps import UDP_HEADER_LEN

Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/icmp4/phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PacketHandlerTxIcmp4(ABC):
from pytcp.config import IP4_DEFAULT_TTL
from pytcp.lib.ip4_address import Ip4Address
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.protocols.ip4.ps import Ip4Payload
from pytcp.protocols.ip4.base import Ip4Payload
from pytcp.protocols.raw.fpa import RawAssembler
from pytcp.protocols.tcp.fpa import TcpAssembler
from pytcp.protocols.udp.fpa import UdpAssembler
Expand Down
3 changes: 2 additions & 1 deletion pytcp/protocols/icmp4/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@

from pytcp.lib.enum import ProtoEnum
from pytcp.lib.proto import Proto
from pytcp.protocols.ip4.ps import IP4_HEADER_LEN, IP4_MIN_MTU, Ip4Proto
from pytcp.protocols.ip4.base import IP4_MIN_MTU
from pytcp.protocols.ip4.header import IP4_HEADER_LEN, Ip4Proto

# Echo reply message (0/0).

Expand Down
20 changes: 7 additions & 13 deletions pytcp/protocols/ip4/fpa.py → pytcp/protocols/ip4/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
# pylint: disable = redefined-builtin

"""
Module contain Fast Packet Assembler support class for the IPv4 protocol.
Module contain assembler support class for the IPv4 protocol.
pytcp/protocols/ip4/fpa.py
pytcp/protocols/ip4/assembler.py
ver 2.8
ver 2.9
"""


Expand All @@ -46,19 +46,13 @@
from pytcp.lib.proto import ProtoAssembler
from pytcp.lib.tracker import Tracker
from pytcp.protocols.ethernet.header import EthernetType
from pytcp.protocols.ip4.ps import (
IP4_HEADER_LEN,
Ip4,
Ip4Header,
Ip4Option,
Ip4OptionEol,
Ip4OptionNop,
Ip4Proto,
)
from pytcp.protocols.ip4.base import Ip4
from pytcp.protocols.ip4.header import IP4_HEADER_LEN, Ip4Header, Ip4Proto
from pytcp.protocols.ip4.options import Ip4Option, Ip4OptionEol, Ip4OptionNop
from pytcp.protocols.raw.fpa import RawAssembler

if TYPE_CHECKING:
from pytcp.protocols.ip4.ps import Ip4Payload
from pytcp.protocols.ip4.base import Ip4Payload


class Ip4Assembler(Ip4, ProtoAssembler):
Expand Down
178 changes: 178 additions & 0 deletions pytcp/protocols/ip4/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/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 IPv4 protccol.
pytcp/protocols/ip4/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.ethernet.header import EthernetType
from pytcp.protocols.ip4.header import Ip4HeaderProperties
from pytcp.protocols.ip4.options import Ip4Option

if TYPE_CHECKING:
from pytcp.protocols.icmp4.fpa import Icmp4Assembler
from pytcp.protocols.ip4.header import Ip4Header
from pytcp.protocols.raw.fpa import RawAssembler
from pytcp.protocols.tcp.fpa import TcpAssembler
from pytcp.protocols.udp.fpa import UdpAssembler

Ip4Payload: TypeAlias = (
Icmp4Assembler | TcpAssembler | UdpAssembler | RawAssembler
)


IP4_MIN_MTU = 576 # RFC 791


class Ip4(Proto, Ip4HeaderProperties):
"""
Base class for IPv4 packet parser and assembler.
"""

__ethernet_type = EthernetType.IP4

_header: Ip4Header
_options: list[Ip4Option]

_hlen: int
_plen: int
_olen: int
_dlen: int

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

log = (
f"IPv4 {self._header.src} > {self._header.dst}, "
f"proto {self._header.proto}, id {self._header.id}"
f"{', DF' if self._header.flag_df else ''}"
f"{', MF' if self._header.flag_mf else ''}, "
f"offset {self._header.offset}, plen {self._header.plen}, "
f"ttl {self._header.ttl}"
)

for option in self._options:
log += ", " + str(option)

return log

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

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

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

return bytes(self._header) + b"".join(
bytes(option) for option in self._options
)

@property
def ethernet_type(self) -> EthernetType:
"""
Get the '_ethernet_type' attribute.
"""

return self.__ethernet_type

@property
def options(self) -> list[Ip4Option]:
"""
Getter for '_options' attribute.
"""

return self._options

@property
def plen(self) -> int:
"""
Getter for '_plen' attribute.
"""

return self._plen

@property
def hlen(self) -> int:
"""
Getter for '_hlen' attribute.
"""

return self._hlen

@property
def olen(self) -> int:
"""
Getter for '_olen' attribute.
"""

return self._olen

@property
def dlen(self) -> int:
"""
Getter for '_dlen' attribute.
"""

return self._dlen

@property
def pshdr_sum(self) -> int:
"""
Create IPv4 pseudo header used by TCP and UDP to compute
their checksums.
"""

pseudo_header = struct.pack(
"! 4s 4s BBH",
bytes(self._header.src),
bytes(self._header.dst),
0,
int(self._header.proto),
self._plen - self._hlen,
)

return sum(struct.unpack("! 3L", pseudo_header))
Loading

0 comments on commit b8e3803

Please sign in to comment.