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 3577492 commit 8f7ce9d
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions pytcp/protocols/ip4/options/ip4_option__eol.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@


IP4__OPTION__EOL__LEN = 1
IP4__OPTION__EOL__STRUCT = "! B"


@dataclass(frozen=True, kw_only=True)
Expand Down
1 change: 1 addition & 0 deletions pytcp/protocols/ip4/options/ip4_option__nop.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@


IP4__OPTION__NOP__LEN = 1
IP4__OPTION__NOP__STRUCT = "! B"


@dataclass(frozen=True, kw_only=True)
Expand Down
1 change: 1 addition & 0 deletions pytcp/protocols/tcp/options/tcp_option__eol.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@


TCP__OPTION__EOL__LEN = 1
TCP__OPTION__EOL__STRUCT = "! B"


@dataclass(frozen=True, kw_only=True)
Expand Down
3 changes: 2 additions & 1 deletion pytcp/protocols/tcp/options/tcp_option__mss.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@


TCP__OPTION__MSS__LEN = 4
TCP__OPTION__MSS__STRUCT = "! BB H"


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -102,7 +103,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
"! BB H",
TCP__OPTION__MSS__STRUCT,
int(self.type),
self.len,
self.mss,
Expand Down
1 change: 1 addition & 0 deletions pytcp/protocols/tcp/options/tcp_option__nop.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@


TCP__OPTION__NOP__LEN = 1
TCP__OPTION__NOP__STRUCT = "! B"


@dataclass(frozen=True, kw_only=True)
Expand Down
6 changes: 4 additions & 2 deletions pytcp/protocols/tcp/options/tcp_option__sack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -90,7 +92,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
"! LL",
TCP__OPTION__SACK__BLOCK_STRUCT,
self.left,
self.right,
)
Expand Down Expand Up @@ -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]),
Expand Down
3 changes: 2 additions & 1 deletion pytcp/protocols/tcp/options/tcp_option__sackperm.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@


TCP__OPTION__SACKPERM__LEN = 2
TCP__OPTION__SACKPERM__STRUCT = "! BB"


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -94,7 +95,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
"! BB",
TCP__OPTION__SACKPERM__STRUCT,
self.type.value,
self.len,
)
Expand Down
7 changes: 5 additions & 2 deletions pytcp/protocols/tcp/options/tcp_option__timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@


TCP__OPTION__TIMESTAMPS__LEN = 10
TCP__OPTION__TIMESTAMPS__STRUCT = "! BB LL"


@dataclass
Expand Down Expand Up @@ -122,7 +123,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
"! BB LL",
TCP__OPTION__TIMESTAMPS__STRUCT,
int(self.type),
self.len,
self.tsval,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion pytcp/protocols/tcp/options/tcp_option__wscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@


TCP__OPTION__WSCALE__LEN = 3
TCP__OPTION__WSCALE__STRUCT = "! BB B"
TCP__OPTION__WSCALE__MAX_VALUE = 14


Expand Down Expand Up @@ -106,7 +107,7 @@ def __bytes__(self) -> bytes:
"""

return struct.pack(
"! BB B",
TCP__OPTION__WSCALE__STRUCT,
int(self.type),
self.len,
self.wscale,
Expand Down

0 comments on commit 8f7ce9d

Please sign in to comment.