# -*- coding: utf-8 -*-
"""TCP Datagram Analysis
===========================
.. module:: pcapkit.foundation.analysis.tcp
:mod:`pcapkit.foundation.analysis.tcp` contains
:func:`~pcapkit.foundation.analysis.tcp.tcp_datagram` only, which parses the
application-layer payload of a TCP datagram that
:class:`~pcapkit.foundation.reassembly.tcp.reassembly.TCP` reassembled.
.. versionadded:: 1.6.0
"""
from typing import TYPE_CHECKING
from pcapkit.const.reg.apptype import TransportProtocol as Enum_TransportProtocol
from pcapkit.protocols.transport.tcp.protocol import TCP
if TYPE_CHECKING:
from typing import Any, Iterable, Mapping, Optional, Type
from pcapkit.const.reg.apptype import TCP as AppType_TCP
from pcapkit.corekit.context import ContextRegistry, ProtocolContext
from pcapkit.foundation.reassembly.tcp.data import Datagram as TCP_Datagram
from pcapkit.protocols.protocol import ProtocolBase
__all__ = ['tcp_datagram']
[docs]
def tcp_datagram(datagram: 'TCP_Datagram', *,
layer: 'Optional[str]' = None,
protocol: 'Optional[str | ProtocolBase | Type[ProtocolBase]]' = None,
context: 'Optional[ContextRegistry | ProtocolContext | Mapping[str, ProtocolContext] | Iterable[ProtocolContext]]' = None) -> 'Optional[ProtocolBase]': # pylint: disable=line-too-long
"""Parse the payload of a TCP datagram reassembled by
:class:`~pcapkit.foundation.reassembly.tcp.reassembly.TCP`.
A reassembled datagram carries no protocol number, so its payload is
dispatched on its ``(source, destination)`` ports by
:meth:`Transport.analyze <pcapkit.protocols.transport.transport.Transport.analyze>`
of :class:`~pcapkit.protocols.transport.tcp.protocol.TCP`. Each port is resolved to
its :class:`~pcapkit.const.reg.apptype.tcp.TCP` member by
:meth:`Transport._make_port
<pcapkit.protocols.transport.transport.Transport._make_port>`, as
:meth:`TCP._make_ports <pcapkit.foundation.reassembly.tcp.reassembly.TCP._make_ports>`
resolves them for the reassembly.
Args:
datagram: Reassembled datagram (:term:`reasm.tcp.datagram`).
layer: Layer to parse until, by name; :data:`None` sets no stop.
protocol: Protocol to parse until, by name, class or instance;
:data:`None` sets no stop.
context: Caller supplied parsing context, in any shape
:func:`~pcapkit.interface.core.extract` takes (cf.
:mod:`pcapkit.corekit.context`); :data:`None` supplies none.
Returns:
The parsed payload. A payload reported as one contiguous run is parsed
whether or not ``datagram`` is complete, as ``strict=False`` reports an
incomplete one. :data:`None` if the payload is the tuple of runs
received, as ``strict=True`` reports an incomplete one.
Note:
:attr:`Datagram.packet <pcapkit.foundation.reassembly.tcp.data.Datagram.packet>`
holds what this returns given the stops and context the extraction's
engine parses its frames with (:issue:`2933`), parsed on its first
read, unless the reassembly's ``__protocol_type__`` names another
protocol, which then parses it instead.
.. versionchanged:: 1.8.0
Takes keyword-only ``layer``, ``protocol`` and ``context``, as the
parse behind :attr:`Datagram.packet
<pcapkit.foundation.reassembly.tcp.data.Datagram.packet>` does
(:issue:`2947`).
"""
if isinstance(datagram.payload, tuple):
return None
transport = Enum_TransportProtocol.tcp
ports = (TCP._make_port(datagram.id.src[1], transport), # pylint: disable=protected-access
TCP._make_port(datagram.id.dst[1], transport)) # pylint: disable=protected-access
# NOTE: Only those set, as the engine hands them (``EngineBase._setup_reassembly``).
options = (('layer', layer), ('protocol', protocol), ('context', context))
kwargs = {name: value for (name, value) in options if value is not None} # type: dict[str, Any]
return _analyze_payload(TCP, ports, datagram.payload, **kwargs)
def _analyze_payload(protocol: 'Type[TCP]', ports: 'tuple[AppType_TCP, AppType_TCP]',
payload: 'bytes', /, **kwargs: 'Any') -> 'ProtocolBase':
"""Parse a reassembled payload by its datagram's ports.
:func:`tcp_datagram` runs this, and so does the
:class:`~pcapkit.foundation.reassembly.data.Deferred` that
:meth:`TCP.submit <pcapkit.foundation.reassembly.tcp.reassembly.TCP.submit>` stores as
a datagram's ``packet``, with the reassembly's own protocol bound as
``protocol`` and the keywords its engine hands it as ``kwargs``.
Args:
protocol: The TCP protocol whose ``analyze`` parses ``payload``.
ports: The datagram's ``(source, destination)`` ports.
payload: The reassembled payload.
**kwargs: Keyword arguments for ``analyze``: the stops and context
the engine parses its frames with, which the reassembly hands on,
or those :func:`tcp_datagram` is given.
Returns:
The parsed payload.
"""
return protocol.analyze(ports, payload, **kwargs)