Skip to content

Commit

Permalink
Cleaning up TCP protocol support files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Jul 16, 2024
1 parent bdf1a3c commit b879305
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 60 deletions.
14 changes: 5 additions & 9 deletions pytcp/protocols/tcp/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# pylint: disable = too-many-arguments
# pylint: disable = too-many-locals


"""
Module contains assembler support class for the TCP protocol.
Expand Down Expand Up @@ -93,17 +94,12 @@ def __init__(

self._options = tcp__options or TcpOptions()

self._olen = len(self._options)
self._hlen = TCP_HEADER_LEN + self._olen
self._dlen = len(self._data)
self._plen = self._hlen + self._dlen

self._header = TcpHeader(
sport=tcp__sport,
dport=tcp__dport,
seq=tcp__seq,
ack=tcp__ack,
hlen=self._hlen,
hlen=TCP_HEADER_LEN + len(self._options),
flag_ns=tcp__flag_ns,
flag_cwr=tcp__flag_cwr,
flag_ece=tcp__flag_ece,
Expand All @@ -118,8 +114,8 @@ def __init__(
urg=tcp__urg,
)

assert self._hlen % 4 == 0, (
f"TCP header len {self._hlen!r} is not multiplication of 4 bytes, "
assert self._header.hlen % 4 == 0, (
f"TCP header len {self._header.hlen!r} is not multiplication of 4 bytes, "
f"check options: {self._options!r}"
)

Expand All @@ -128,7 +124,7 @@ def __len__(self) -> int:
Get length of the packet.
"""

return len(self._header) + self._olen + self._dlen
return len(self._header) + len(self._options) + len(self._data)

@property
def tracker(self) -> Tracker:
Expand Down
48 changes: 6 additions & 42 deletions pytcp/protocols/tcp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,13 @@ class Tcp(Proto, TcpHeaderProperties, TcpOptionsProperties):
Base class for TCP packet parser and assembler.
"""

__ip6_next = Ip6Next.TCP
__ip4_proto = Ip4Proto.TCP
__ip6__next = Ip6Next.TCP
__ip4__proto = Ip4Proto.TCP

_header: TcpHeader
_options: TcpOptions
_data: bytes

_plen: int
_hlen: int
_olen: int
_dlen: int

@override
def __str__(self) -> str:
"""
Expand All @@ -73,7 +68,8 @@ def __str__(self) -> str:
f"{'A' if self._header.flag_ack else ''}{'P' if self._header.flag_psh else ''}"
f"{'R' if self._header.flag_rst else ''}{'S' if self._header.flag_syn else ''}"
f"{'F' if self._header.flag_fin else ''}, seq {self._header.seq}, "
f"ack {self._header.ack}, win {self._header.win}, dlen {len(self._data)}"
f"ack {self._header.ack}, win {self._header.win}, "
f"len {len(self._header)}+{len(self._options)}+{len(self._data)}"
f", opts [{self._options}]"
)

Expand All @@ -99,15 +95,15 @@ def ip6_next(self) -> Ip6Next:
Get the '_ip6_next' attribute.
"""

return self.__ip6_next
return self.__ip6__next

@property
def ip4_proto(self) -> Ip4Proto:
"""
Get the '_ip4_proto' attribute.
"""

return self.__ip4_proto
return self.__ip4__proto

@property
def options(self) -> TcpOptions:
Expand All @@ -124,35 +120,3 @@ def data(self) -> bytes:
"""

return self._data

@property
def plen(self) -> int:
"""
Getter for '_plen' attribute.
"""

return self._plen

@property
def olen(self) -> int:
"""
Getter for '_olen' attribute.
"""

return self._olen

@property
def dlen(self) -> int:
"""
Getter for '_dlen' attribute.
"""

return self._dlen

@property
def hlen(self) -> int:
"""
Get the header length.
"""

return self._hlen
1 change: 1 addition & 0 deletions pytcp/protocols/tcp/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# pylint: disable = too-many-arguments
# pylint: disable = too-many-locals


"""
Module contains interface class for the FPP -> TCP Socket communication.
Expand Down
1 change: 1 addition & 0 deletions pytcp/protocols/tcp/packet_handler_rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# pylint: disable = import-outside-toplevel
# pylint: disable = unused-argument


"""
Module contains packet handler for the inbound TCP packets.
Expand Down
2 changes: 1 addition & 1 deletion pytcp/protocols/tcp/packet_handler_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
# #
############################################################################


# pylint: disable = expression-not-assigned
# pylint: disable = too-many-locals
# pylint: disable = too-many-branches
# pylint: disable = import-outside-toplevel
# pylint: disable = unused-argument


"""
Module contains packet handler for the outbound TCP packets.
Expand Down
12 changes: 4 additions & 8 deletions pytcp/protocols/tcp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(self, *, packet_rx: PacketRx) -> None:
self._validate_sanity()

packet_rx.tcp = self
packet_rx.frame = packet_rx.frame[self._hlen :]
packet_rx.frame = packet_rx.frame[self._header.hlen :]

def __len__(self) -> int:
"""
Expand Down Expand Up @@ -161,16 +161,12 @@ def _parse(self) -> None:
"""

self._header = TcpHeader.from_bytes(self._frame)

self._options = TcpOptions.from_bytes(
self._frame[TCP_HEADER_LEN : self._header.hlen]
self._frame[len(self._header) : self._header.hlen]
)

self._plen = self._ip__dlen
self._hlen = self._header.hlen
self._olen = self._hlen - TCP_HEADER_LEN
self._dlen = self._plen - self._hlen

self._data = self._frame[self._hlen : self._plen]
self._data = self._frame[self._header.hlen : self._ip__dlen]

@override
def _validate_sanity(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions pytcp/protocols/tcp/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# pylint: disable = protected-access
# pylint: disable = import-outside-toplevel


"""
Module contains class supporting TCP finite state machine.
Expand Down
1 change: 1 addition & 0 deletions pytcp/protocols/tcp/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# pylint: disable = too-many-instance-attributes
# pylint: disable = fixme


"""
Module contains BSD like socket interface for the stack.
Expand Down

0 comments on commit b879305

Please sign in to comment.