From 8f7ce9daa522686b05b95c7facfefa6b8ddc4603 Mon Sep 17 00:00:00 2001 From: Sebastian Majewski Date: Sat, 7 Sep 2024 10:45:40 -0500 Subject: [PATCH] Code cleanup --- pytcp/protocols/ip4/options/ip4_option__eol.py | 1 + pytcp/protocols/ip4/options/ip4_option__nop.py | 1 + pytcp/protocols/tcp/options/tcp_option__eol.py | 1 + pytcp/protocols/tcp/options/tcp_option__mss.py | 3 ++- pytcp/protocols/tcp/options/tcp_option__nop.py | 1 + pytcp/protocols/tcp/options/tcp_option__sack.py | 6 ++++-- pytcp/protocols/tcp/options/tcp_option__sackperm.py | 3 ++- pytcp/protocols/tcp/options/tcp_option__timestamps.py | 7 +++++-- pytcp/protocols/tcp/options/tcp_option__wscale.py | 3 ++- 9 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pytcp/protocols/ip4/options/ip4_option__eol.py b/pytcp/protocols/ip4/options/ip4_option__eol.py index 3c3fa04e..a2be59bd 100644 --- a/pytcp/protocols/ip4/options/ip4_option__eol.py +++ b/pytcp/protocols/ip4/options/ip4_option__eol.py @@ -48,6 +48,7 @@ IP4__OPTION__EOL__LEN = 1 +IP4__OPTION__EOL__STRUCT = "! B" @dataclass(frozen=True, kw_only=True) diff --git a/pytcp/protocols/ip4/options/ip4_option__nop.py b/pytcp/protocols/ip4/options/ip4_option__nop.py index faa1e64c..6506f323 100644 --- a/pytcp/protocols/ip4/options/ip4_option__nop.py +++ b/pytcp/protocols/ip4/options/ip4_option__nop.py @@ -48,6 +48,7 @@ IP4__OPTION__NOP__LEN = 1 +IP4__OPTION__NOP__STRUCT = "! B" @dataclass(frozen=True, kw_only=True) diff --git a/pytcp/protocols/tcp/options/tcp_option__eol.py b/pytcp/protocols/tcp/options/tcp_option__eol.py index 1fbd5969..2d0417a3 100644 --- a/pytcp/protocols/tcp/options/tcp_option__eol.py +++ b/pytcp/protocols/tcp/options/tcp_option__eol.py @@ -48,6 +48,7 @@ TCP__OPTION__EOL__LEN = 1 +TCP__OPTION__EOL__STRUCT = "! B" @dataclass(frozen=True, kw_only=True) diff --git a/pytcp/protocols/tcp/options/tcp_option__mss.py b/pytcp/protocols/tcp/options/tcp_option__mss.py index 4f8b5554..c86187a8 100644 --- a/pytcp/protocols/tcp/options/tcp_option__mss.py +++ b/pytcp/protocols/tcp/options/tcp_option__mss.py @@ -55,6 +55,7 @@ TCP__OPTION__MSS__LEN = 4 +TCP__OPTION__MSS__STRUCT = "! BB H" @dataclass(frozen=True, kw_only=True) @@ -102,7 +103,7 @@ def __bytes__(self) -> bytes: """ return struct.pack( - "! BB H", + TCP__OPTION__MSS__STRUCT, int(self.type), self.len, self.mss, diff --git a/pytcp/protocols/tcp/options/tcp_option__nop.py b/pytcp/protocols/tcp/options/tcp_option__nop.py index e0adbf2c..1f4f3027 100644 --- a/pytcp/protocols/tcp/options/tcp_option__nop.py +++ b/pytcp/protocols/tcp/options/tcp_option__nop.py @@ -48,6 +48,7 @@ TCP__OPTION__NOP__LEN = 1 +TCP__OPTION__NOP__STRUCT = "! B" @dataclass(frozen=True, kw_only=True) diff --git a/pytcp/protocols/tcp/options/tcp_option__sack.py b/pytcp/protocols/tcp/options/tcp_option__sack.py index 3b6c9656..d3e4d290 100644 --- a/pytcp/protocols/tcp/options/tcp_option__sack.py +++ b/pytcp/protocols/tcp/options/tcp_option__sack.py @@ -64,7 +64,9 @@ TCP__OPTION__SACK__LEN = 2 +TCP__OPTION__SACK__STRUCT = "! BB" TCP__OPTION__SACK__BLOCK_LEN = 8 +TCP__OPTION__SACK__BLOCK_STRUCT = "! LL" TCP__OPTION__SACK__MAX_BLOCK_NUM = 4 @@ -90,7 +92,7 @@ def __bytes__(self) -> bytes: """ return struct.pack( - "! LL", + TCP__OPTION__SACK__BLOCK_STRUCT, self.left, self.right, ) @@ -156,7 +158,7 @@ def __bytes__(self) -> bytes: """ return struct.pack( - f"! BB {TCP__OPTION__SACK__BLOCK_LEN * len(self.blocks)}s", + f"{TCP__OPTION__SACK__STRUCT} {TCP__OPTION__SACK__BLOCK_LEN * len(self.blocks)}s", int(self.type), self.len, b"".join([bytes(block) for block in self.blocks]), diff --git a/pytcp/protocols/tcp/options/tcp_option__sackperm.py b/pytcp/protocols/tcp/options/tcp_option__sackperm.py index 4feec05e..7b7610a0 100644 --- a/pytcp/protocols/tcp/options/tcp_option__sackperm.py +++ b/pytcp/protocols/tcp/options/tcp_option__sackperm.py @@ -54,6 +54,7 @@ TCP__OPTION__SACKPERM__LEN = 2 +TCP__OPTION__SACKPERM__STRUCT = "! BB" @dataclass(frozen=True, kw_only=True) @@ -94,7 +95,7 @@ def __bytes__(self) -> bytes: """ return struct.pack( - "! BB", + TCP__OPTION__SACKPERM__STRUCT, self.type.value, self.len, ) diff --git a/pytcp/protocols/tcp/options/tcp_option__timestamps.py b/pytcp/protocols/tcp/options/tcp_option__timestamps.py index 1fe481e8..46eda8c0 100644 --- a/pytcp/protocols/tcp/options/tcp_option__timestamps.py +++ b/pytcp/protocols/tcp/options/tcp_option__timestamps.py @@ -59,6 +59,7 @@ TCP__OPTION__TIMESTAMPS__LEN = 10 +TCP__OPTION__TIMESTAMPS__STRUCT = "! BB LL" @dataclass @@ -122,7 +123,7 @@ def __bytes__(self) -> bytes: """ return struct.pack( - "! BB LL", + TCP__OPTION__TIMESTAMPS__STRUCT, int(self.type), self.len, self.tsval, @@ -166,7 +167,9 @@ def from_bytes(_bytes: bytes, /) -> TcpOptionTimestamps: TcpOptionTimestamps._validate_integrity(_bytes) - _, _, tsval, tsecr = struct.unpack_from("! BB LL", _bytes) + _, _, tsval, tsecr = struct.unpack_from( + TCP__OPTION__TIMESTAMPS__STRUCT, _bytes + ) return TcpOptionTimestamps( tsval=tsval, diff --git a/pytcp/protocols/tcp/options/tcp_option__wscale.py b/pytcp/protocols/tcp/options/tcp_option__wscale.py index b6831a5e..3ec9d7df 100644 --- a/pytcp/protocols/tcp/options/tcp_option__wscale.py +++ b/pytcp/protocols/tcp/options/tcp_option__wscale.py @@ -55,6 +55,7 @@ TCP__OPTION__WSCALE__LEN = 3 +TCP__OPTION__WSCALE__STRUCT = "! BB B" TCP__OPTION__WSCALE__MAX_VALUE = 14 @@ -106,7 +107,7 @@ def __bytes__(self) -> bytes: """ return struct.pack( - "! BB B", + TCP__OPTION__WSCALE__STRUCT, int(self.type), self.len, self.wscale,