Skip to content

Commit

Permalink
Refactor ICMPv4 message parsing for code clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Jul 14, 2024
1 parent 1674f0b commit c5097f7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
9 changes: 9 additions & 0 deletions pytcp/protocols/icmp4/message__echo_reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ def __bytes__(self) -> bytes:
bytes(self._data),
)

def _parse(self, *, frame: bytes) -> None:
"""
Parse message from the frame.
"""

self._id: int = struct.unpack("! H", frame[4:6])[0]
self._seq: int = struct.unpack("! H", frame[6:8])[0]
self._data: bytes = frame[8:]

@property
def id(self) -> int:
"""
Expand Down
9 changes: 9 additions & 0 deletions pytcp/protocols/icmp4/message__echo_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ def __bytes__(self) -> bytes:
bytes(self._data),
)

def _parse(self, *, frame: bytes) -> None:
"""
Parse the message from the frame.
"""

self._id: int = struct.unpack("! H", frame[4:6])[0]
self._seq: int = struct.unpack("! H", frame[6:8])[0]
self._data: bytes = frame[8:]

@property
def id(self) -> int:
"""
Expand Down
8 changes: 8 additions & 0 deletions pytcp/protocols/icmp4/message__unknown.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ def __bytes__(self) -> bytes:
"""

raise NotImplementedError

def _parse(self, *, frame: bytes) -> None:
"""
Parse message from the frame.
"""

self._type = Icmp4Type.from_frame(frame)
self._code = Icmp4Code.from_frame(frame)
7 changes: 7 additions & 0 deletions pytcp/protocols/icmp4/message__unreachable.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,10 @@ def __bytes__(self) -> bytes:
self._reserved,
bytes(self._data),
)

def _parse(self, *, frame: bytes) -> None:
"""
Parse message from the frame.
"""

self._data: bytes = frame[8:]
18 changes: 6 additions & 12 deletions pytcp/protocols/icmp4/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@

from __future__ import annotations

import struct
from typing import TYPE_CHECKING, override

from pytcp.lib.errors import PacketIntegrityError, PacketSanityError
from pytcp.lib.ip_helper import inet_cksum
from pytcp.lib.proto import ProtoParser
from pytcp.protocols.icmp4.base import Icmp4
from pytcp.protocols.icmp4.message import ICMP4_HEADER_LEN, Icmp4Code, Icmp4Type
from pytcp.protocols.icmp4.message import ICMP4_HEADER_LEN, Icmp4Type
from pytcp.protocols.icmp4.message__echo_reply import (
ICMP4_MESSAGE_LEN__ECHO_REPLY,
Icmp4EchoReplyMessage,
Expand Down Expand Up @@ -221,9 +220,7 @@ def __init__(self, *, frame: bytes) -> None:
Class constructor.
"""

self._id: int = struct.unpack("! H", frame[4:6])[0]
self._seq: int = struct.unpack("! H", frame[6:8])[0]
self._data: bytes = frame[8:]
self._parse(frame=frame)


class Icmp4UnreachablePortMessageParser(Icmp4PortUnreachableMessage):
Expand All @@ -236,7 +233,7 @@ def __init__(self, *, frame: bytes) -> None:
Class constructor.
"""

self._data: bytes = frame[8:]
self._parse(frame=frame)


class Icmp4EchoRequestMessageParser(Icmp4EchoRequestMessage):
Expand All @@ -249,9 +246,7 @@ def __init__(self, *, frame: bytes) -> None:
Class constructor.
"""

self._id: int = struct.unpack("! H", frame[4:6])[0]
self._seq: int = struct.unpack("! H", frame[6:8])[0]
self._data: bytes = frame[8:]
self._parse(frame=frame)


class Icmp4UnknownMessageParser(Icmp4UnknownMessage):
Expand All @@ -261,8 +256,7 @@ class Icmp4UnknownMessageParser(Icmp4UnknownMessage):

def __init__(self, *, frame: bytes) -> None:
"""
Create the message object.
Class constructor.
"""

self._type = Icmp4Type.from_frame(frame)
self._code = Icmp4Code.from_frame(frame)
self._parse(frame=frame)

0 comments on commit c5097f7

Please sign in to comment.