-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
279 additions
and
128 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
pytcp/protocols/dhcp4/options/dhcp4_option__message_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.