Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 7, 2024
1 parent 51b68bc commit c7acdc7
Show file tree
Hide file tree
Showing 27 changed files with 279 additions and 128 deletions.
151 changes: 151 additions & 0 deletions pytcp/protocols/dhcp4/options/dhcp4_option__message_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/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 the DHCPv4 Message Type option support code.
pytcp/protocols/dhcp4/options/dhcp4_option__message_type.py
ver 3.0.2
"""


from __future__ import annotations

"""
import struct
from dataclasses import dataclass, field
from typing import override
from pytcp.lib.int_checks import is_uint16
from pytcp.protocols.tcp.options.tcp_option import (
TCP__OPTION__LEN,
TcpOption,
TcpOptionType,
)
from pytcp.protocols.tcp.tcp__errors import TcpIntegrityError
"""

# The DHCPv4 Message Type option [RFC 2132].

# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Type = 1 | Length = 1 | Value |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


DHCP4__OPTION__MESSAGE_TYPE__LEN = 4

'''
@dataclass(frozen=True, kw_only=True)
class TcpOptionMss(TcpOption):
"""
The TCP Mss (Maximum Segment Size) option support class.
"""
type: TcpOptionType = field(
repr=False,
init=False,
default=TcpOptionType.MSS,
)
len: int = field(
repr=False,
init=False,
default=TCP__OPTION_MSS__LEN,
)
mss: int
@override
def __post_init__(self) -> None:
"""
Validate the TCP Mss option fields.
"""
assert is_uint16(self.mss), (
f"The 'mss' field must be a 16-bit unsigned integer. "
f"Got: {self.mss}"
)
@override
def __str__(self) -> str:
"""
Get the TCP Mss option log string.
"""
return f"mss {self.mss}"
@override
def __bytes__(self) -> bytes:
"""
Get the TCP Mss option as bytes.
"""
return struct.pack(
"! BB H",
int(self.type),
self.len,
self.mss,
)
@staticmethod
def _validate_integrity(_bytes: bytes, /) -> None:
"""
Validate the TCP Mss option integrity before parsing it.
"""
if (value := _bytes[1]) != TCP__OPTION_MSS__LEN:
raise TcpIntegrityError(
f"The TCP Mss option length must be {TCP__OPTION_MSS__LEN} "
f"bytes. Got: {value!r}"
)
if (value := _bytes[1]) > len(_bytes):
raise TcpIntegrityError(
"The TCP Mss option length must be less than or equal to "
f"the length of provided bytes ({len(_bytes)}). Got: {value!r}"
)
@override
@staticmethod
def from_bytes(_bytes: bytes, /) -> TcpOptionMss:
"""
Initialize the TCP Mss option from bytes.
"""
assert (value := len(_bytes)) >= TCP__OPTION__LEN, (
f"The minimum length of the TCP Mss option must be "
f"{TCP__OPTION__LEN} bytes. Got: {value!r}"
)
assert (value := _bytes[0]) == int(TcpOptionType.MSS), (
f"The TCP Mss option type must be {TcpOptionType.MSS!r}. "
f"Got: {TcpOptionType.from_int(value)!r}"
)
TcpOptionMss._validate_integrity(_bytes)
return TcpOptionMss(mss=int.from_bytes(_bytes[2:4]))
'''
4 changes: 2 additions & 2 deletions pytcp/protocols/icmp6/message/nd/option/icmp6_nd_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

from pytcp.lib.proto_option import ProtoOption, ProtoOptionType

ICMP6__ND_OPTION__STRUCT = "! BB"
ICMP6__ND_OPTION__LEN = 2
ICMP6__ND__OPTION__STRUCT = "! BB"
ICMP6__ND__OPTION__LEN = 2


class Icmp6NdOptionType(ProtoOptionType):
Expand Down
20 changes: 10 additions & 10 deletions pytcp/protocols/icmp6/message/nd/option/icmp6_nd_option__pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from pytcp.lib.net_addr import Ip6Address, Ip6Mask, Ip6Network
from pytcp.protocols.icmp6.icmp6__errors import Icmp6IntegrityError
from pytcp.protocols.icmp6.message.nd.option.icmp6_nd_option import (
ICMP6__ND_OPTION__LEN,
ICMP6__ND__OPTION__LEN,
Icmp6NdOption,
Icmp6NdOptionType,
)
Expand All @@ -68,8 +68,8 @@
# | |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

ICMP6__ND_OPTION_PI__LEN = 32
ICMP6__ND_OPTION_PI__STRUCT = "! BB BB L L L 16s"
ICMP6__ND__OPTION__PI__LEN = 32
ICMP6__ND__OPTION__PI__STRUCT = "! BB BB L L L 16s"


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -100,7 +100,7 @@ class Icmp6NdOptionPi(Icmp6NdOption):
len: int = field(
repr=False,
init=False,
default=ICMP6__ND_OPTION_PI__LEN,
default=ICMP6__ND__OPTION__PI__LEN,
)

flag_l: bool = False
Expand Down Expand Up @@ -168,7 +168,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
ICMP6__ND_OPTION_PI__STRUCT,
ICMP6__ND__OPTION__PI__STRUCT,
int(self.type),
self.len >> 3,
len(self.prefix.mask),
Expand All @@ -188,9 +188,9 @@ def _validate_integrity(_bytes: bytes, /) -> None:
Validate the ICMPv6 ND Pi option integrity before parsing it.
"""

if (value := _bytes[1] << 3) != ICMP6__ND_OPTION_PI__LEN:
if (value := _bytes[1] << 3) != ICMP6__ND__OPTION__PI__LEN:
raise Icmp6IntegrityError(
f"The ICMPv6 ND Pi option length must be {ICMP6__ND_OPTION_PI__LEN} "
f"The ICMPv6 ND Pi option length must be {ICMP6__ND__OPTION__PI__LEN} "
f"bytes. Got: {value!r}"
)

Expand All @@ -207,9 +207,9 @@ def from_bytes(_bytes: bytes, /) -> Icmp6NdOptionPi:
Initialize the ICMPv6 ND Pi option from bytes.
"""

assert (value := len(_bytes)) >= ICMP6__ND_OPTION__LEN, (
assert (value := len(_bytes)) >= ICMP6__ND__OPTION__LEN, (
f"The minimum length of the ICMPv6 ND Pi option must be "
f"{ICMP6__ND_OPTION__LEN} bytes. Got: {value!r}"
f"{ICMP6__ND__OPTION__LEN} bytes. Got: {value!r}"
)

assert (value := _bytes[0]) == int(Icmp6NdOptionType.PI), (
Expand All @@ -229,7 +229,7 @@ def from_bytes(_bytes: bytes, /) -> Icmp6NdOptionPi:
_,
prefix,
) = struct.unpack(
ICMP6__ND_OPTION_PI__STRUCT, _bytes[:ICMP6__ND_OPTION_PI__LEN]
ICMP6__ND__OPTION__PI__STRUCT, _bytes[:ICMP6__ND__OPTION__PI__LEN]
)

return Icmp6NdOptionPi(
Expand Down
18 changes: 9 additions & 9 deletions pytcp/protocols/icmp6/message/nd/option/icmp6_nd_option__slla.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pytcp.lib.net_addr import MacAddress
from pytcp.protocols.icmp6.icmp6__errors import Icmp6IntegrityError
from pytcp.protocols.icmp6.message.nd.option.icmp6_nd_option import (
ICMP6__ND_OPTION__LEN,
ICMP6__ND__OPTION__LEN,
Icmp6NdOption,
Icmp6NdOptionType,
)
Expand All @@ -55,8 +55,8 @@
# > MAC Address |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

ICMP6__ND_OPTION_SLLA__LEN = 8
ICMP6__ND_OPTION_SLLA__STRUCT = "! BB 6s"
ICMP6__ND__OPTION__SLLA__LEN = 8
ICMP6__ND__OPTION__SLLA__STRUCT = "! BB 6s"


@dataclass(frozen=True, kw_only=True)
Expand All @@ -73,7 +73,7 @@ class Icmp6NdOptionSlla(Icmp6NdOption):
len: int = field(
repr=False,
init=False,
default=ICMP6__ND_OPTION_SLLA__LEN,
default=ICMP6__ND__OPTION__SLLA__LEN,
)

slla: MacAddress
Expand Down Expand Up @@ -103,7 +103,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
ICMP6__ND_OPTION_SLLA__STRUCT,
ICMP6__ND__OPTION__SLLA__STRUCT,
int(self.type),
self.len >> 3,
bytes(self.slla),
Expand All @@ -115,9 +115,9 @@ def _validate_integrity(_bytes: bytes, /) -> None:
Validate the ICMPv6 ND Slla option integrity before parsing it.
"""

if (value := _bytes[1] << 3) != ICMP6__ND_OPTION_SLLA__LEN:
if (value := _bytes[1] << 3) != ICMP6__ND__OPTION__SLLA__LEN:
raise Icmp6IntegrityError(
f"The ICMPv6 ND Slla option length must be {ICMP6__ND_OPTION_SLLA__LEN} "
f"The ICMPv6 ND Slla option length must be {ICMP6__ND__OPTION__SLLA__LEN} "
f"bytes. Got: {value!r}"
)

Expand All @@ -134,9 +134,9 @@ def from_bytes(_bytes: bytes, /) -> Icmp6NdOptionSlla:
Initialize the ICMPv6 ND Slla option from bytes.
"""

assert (value := len(_bytes)) >= ICMP6__ND_OPTION__LEN, (
assert (value := len(_bytes)) >= ICMP6__ND__OPTION__LEN, (
f"The minimum length of the ICMPv6 ND Slla option must be "
f"{ICMP6__ND_OPTION__LEN} bytes. Got: {value!r}"
f"{ICMP6__ND__OPTION__LEN} bytes. Got: {value!r}"
)

assert (value := _bytes[0]) == int(Icmp6NdOptionType.SLLA), (
Expand Down
18 changes: 9 additions & 9 deletions pytcp/protocols/icmp6/message/nd/option/icmp6_nd_option__tlla.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pytcp.lib.net_addr import MacAddress
from pytcp.protocols.icmp6.icmp6__errors import Icmp6IntegrityError
from pytcp.protocols.icmp6.message.nd.option.icmp6_nd_option import (
ICMP6__ND_OPTION__LEN,
ICMP6__ND__OPTION__LEN,
Icmp6NdOption,
Icmp6NdOptionType,
)
Expand All @@ -55,8 +55,8 @@
# > MAC Address |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

ICMP6__ND_OPTION_TLLA__LEN = 8
ICMP6__ND_OPTION_TLLA__STRUCT = "! BB 6s"
ICMP6__ND__OPTION__TLLA__LEN = 8
ICMP6__ND__OPTION__TLLA__STRUCT = "! BB 6s"


@dataclass(frozen=True, kw_only=True)
Expand All @@ -73,7 +73,7 @@ class Icmp6NdOptionTlla(Icmp6NdOption):
len: int = field(
repr=False,
init=False,
default=ICMP6__ND_OPTION_TLLA__LEN,
default=ICMP6__ND__OPTION__TLLA__LEN,
)

tlla: MacAddress
Expand Down Expand Up @@ -103,7 +103,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
ICMP6__ND_OPTION_TLLA__STRUCT,
ICMP6__ND__OPTION__TLLA__STRUCT,
int(self.type),
self.len >> 3,
bytes(self.tlla),
Expand All @@ -115,9 +115,9 @@ def _validate_integrity(_bytes: bytes, /) -> None:
Validate the integrity of the ICMPv6 ND Tlla option before parsing it.
"""

if (value := _bytes[1] << 3) != ICMP6__ND_OPTION_TLLA__LEN:
if (value := _bytes[1] << 3) != ICMP6__ND__OPTION__TLLA__LEN:
raise Icmp6IntegrityError(
f"The ICMPv6 ND Tlla option length must be {ICMP6__ND_OPTION_TLLA__LEN} "
f"The ICMPv6 ND Tlla option length must be {ICMP6__ND__OPTION__TLLA__LEN} "
f"bytes. Got: {value!r}"
)

Expand All @@ -134,9 +134,9 @@ def from_bytes(_bytes: bytes, /) -> Icmp6NdOptionTlla:
Initialize the ICMPv6 ND Tlla option from bytes.
"""

assert (value := len(_bytes)) >= ICMP6__ND_OPTION__LEN, (
assert (value := len(_bytes)) >= ICMP6__ND__OPTION__LEN, (
f"The minimum length of the ICMPv6 ND Tlla option must be "
f"{ICMP6__ND_OPTION__LEN} bytes. Got: {value!r}"
f"{ICMP6__ND__OPTION__LEN} bytes. Got: {value!r}"
)

assert (value := _bytes[0]) == int(Icmp6NdOptionType.TLLA), (
Expand Down
Loading

0 comments on commit c7acdc7

Please sign in to comment.