diff --git a/pytcp/protocols/icmp4/message__echo_reply.py b/pytcp/protocols/icmp4/message__echo_reply.py index aacdb7aa..0767e97b 100644 --- a/pytcp/protocols/icmp4/message__echo_reply.py +++ b/pytcp/protocols/icmp4/message__echo_reply.py @@ -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: """ diff --git a/pytcp/protocols/icmp4/message__echo_request.py b/pytcp/protocols/icmp4/message__echo_request.py index 059bdffe..54e1971a 100644 --- a/pytcp/protocols/icmp4/message__echo_request.py +++ b/pytcp/protocols/icmp4/message__echo_request.py @@ -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: """ diff --git a/pytcp/protocols/icmp4/message__unknown.py b/pytcp/protocols/icmp4/message__unknown.py index 8269ab5c..47c76c8e 100644 --- a/pytcp/protocols/icmp4/message__unknown.py +++ b/pytcp/protocols/icmp4/message__unknown.py @@ -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) diff --git a/pytcp/protocols/icmp4/message__unreachable.py b/pytcp/protocols/icmp4/message__unreachable.py index 1052203e..4bbbd104 100644 --- a/pytcp/protocols/icmp4/message__unreachable.py +++ b/pytcp/protocols/icmp4/message__unreachable.py @@ -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:] diff --git a/pytcp/protocols/icmp4/parser.py b/pytcp/protocols/icmp4/parser.py index 0e151388..3a87af4e 100755 --- a/pytcp/protocols/icmp4/parser.py +++ b/pytcp/protocols/icmp4/parser.py @@ -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, @@ -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): @@ -236,7 +233,7 @@ def __init__(self, *, frame: bytes) -> None: Class constructor. """ - self._data: bytes = frame[8:] + self._parse(frame=frame) class Icmp4EchoRequestMessageParser(Icmp4EchoRequestMessage): @@ -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): @@ -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)