# -*- coding: utf-8 -*-
"""Core Interface
====================
.. module:: pcapkit.interface.core
:mod:`pcapkit.interface.core` defines the user-facing functions and
macros, which wrap the classes of :mod:`pcapkit.foundation`.
"""
import sys
from typing import TYPE_CHECKING
from pcapkit.foundation.extraction import Extractor
from pcapkit.protocols.protocol import ProtocolBase
from pcapkit.utilities.exceptions import RegistryError
if TYPE_CHECKING:
from typing import IO, Iterable, Mapping, Optional, Type
from typing_extensions import Literal
from pcapkit.corekit.context import ContextRegistry, ProtocolContext
from pcapkit.foundation.extraction import Engines, Layers, Protocols, VerboseHandler
from pcapkit.foundation.options import (DumpOptions, ReassemblyOptions, StreamOptions,
TraceFlowOptions)
from pcapkit.foundation.reassembly.reassembly import ReassemblyBase
from pcapkit.foundation.traceflow.traceflow import TraceFlowBase
__all__ = [
'extract', 'reassemble', 'trace', # interface functions
'TREE', 'JSON', 'PLIST', 'PCAP', 'PCAP_NG', # format macros
'LINK', 'INET', 'TRANS', 'APP', 'RAW', # layer macros
'DPKT', 'Scapy', 'PyShark', 'PCAPKit', # engine macros
'PyPCAP', 'PCAP_CT', 'PyPCAPFile', # engine macros
]
# output file formats
TREE = 'tree'
JSON = 'json'
PLIST = 'plist'
PCAP = 'pcap'
# NOTE: not ``PCAPNG``, which ``pcapkit`` and ``pcapkit.all`` bind to the protocol (#3047).
PCAP_NG = 'pcapng'
# layer thresholds
RAW = 'none'
LINK = 'link'
INET = 'internet'
TRANS = 'transport'
APP = 'application'
# extraction engines
#
# NOTE: a third-party value is the engine's key in ``Extractor.__engine__``, not
# its display name; an unknown key only warns and falls back to ``'default'``.
DPKT = 'dpkt'
Scapy = 'scapy'
PCAPKit = 'default'
PyShark = 'pyshark'
PyPCAP = 'pypcap'
PCAP_CT = 'pcap_ct'
PyPCAPFile = 'pypcapfile'
[docs]
def reassemble(protocol: 'str | Type[ProtocolBase]', *, strict: 'bool' = True, store: 'bool' = True,
timeout: 'Optional[float]' = None) -> 'ReassemblyBase':
"""Reassemble fragmented datagrams.
Arguments:
protocol: protocol to be reassembled, a name registered in
:attr:`Reassembly.registry
<pcapkit.foundation.reassembly.reassembly.Reassembly.registry>`,
matched lower-cased, or a protocol class, by the first of its
:meth:`~pcapkit.protocols.protocol.ProtocolBase.id`
strict: if :data:`True`, report a datagram that is not completely
reassembled as the tuple of its received runs; otherwise as one
contiguous payload
store: if store reassembled datagrams
timeout: reassembly timeout in seconds, on the capture's own clock;
:data:`None` selects the protocol's own default
Returns:
A :class:`~pcapkit.foundation.reassembly.reassembly.ReassemblyBase` object of corresponding protocol.
Raises:
FormatError: If no reassembly is registered under ``protocol``.
RegistryError: If the protocol name is not a :obj:`str`.
.. versionchanged:: 1.5.0
``timeout`` is added. IPv4 and IPv6 buffers expire after 60 seconds of
capture time by default, where 1.4.x held them for the life of the
object.
.. versionchanged:: 1.7.0
Makes any registered reassembly, through
:meth:`Extractor.make_reassembly
<pcapkit.foundation.extraction.Extractor.make_reassembly>`, where it
made IPv4, IPv6 and TCP only (:issue:`2814`). Takes exactly the
keyword-only parameters of
:class:`~pcapkit.foundation.reassembly.reassembly.Reassembly`, so
``store`` is added and ``strict`` defaults to :data:`True`, where it
defaulted to :data:`False` (:issue:`2837`).
"""
if isinstance(protocol, type) and issubclass(protocol, ProtocolBase):
protocol = protocol.id()[0]
if not isinstance(protocol, str):
raise RegistryError(f'reassembly name must be a str, not {protocol!r}')
return Extractor.make_reassembly(protocol, strict=strict, store=store, timeout=timeout)
[docs]
def trace(protocol: 'str | Type[ProtocolBase]', *, fout: 'str' = './tmp',
format: 'str' = 'pcap', # pylint: disable=redefined-builtin
byteorder: 'Literal["little", "big"]' = sys.byteorder,
nanosecond: 'bool' = False, bidirectional: 'bool' = True,
analyze: 'bool' = False) -> 'TraceFlowBase':
"""Trace flows.
Arguments:
protocol: protocol whose flows are to be traced, a name registered in
:attr:`TraceFlow.registry
<pcapkit.foundation.traceflow.traceflow.TraceFlow.registry>`,
matched lower-cased, or a protocol class, by the first of its
:meth:`~pcapkit.protocols.protocol.ProtocolBase.id`
fout: output root path
format: output format
byteorder: output file byte order
nanosecond: output nanosecond-resolution file flag
bidirectional: whether both halves of a conversation are traced as one
flow, which is the default
analyze: whether each traced flow reassembles its application layer
Returns:
A :class:`~pcapkit.foundation.traceflow.traceflow.TraceFlowBase` object.
Raises:
FormatError: If no flow tracer is registered under ``protocol``.
RegistryError: If the protocol name is not a :obj:`str`.
.. versionchanged:: 1.5.0
``bidirectional`` is added, and by default both directions of a
conversation are traced as one flow; 1.4.x traced each direction as a
flow of its own.
.. versionchanged:: 1.7.0
Makes any registered flow tracer, where it made TCP's only, and takes
exactly the keyword-only parameters of
:class:`~pcapkit.foundation.traceflow.traceflow.TraceFlow`, so
``analyze`` is added and ``fout`` and ``format`` default to ``'./tmp'``
and ``'pcap'`` (:issue:`2837`). Makes it through
:meth:`Extractor.make_traceflow
<pcapkit.foundation.extraction.Extractor.make_traceflow>`, as the
extractor does (:issue:`2843`).
"""
if isinstance(protocol, type) and issubclass(protocol, ProtocolBase):
protocol = protocol.id()[0]
if not isinstance(protocol, str):
raise RegistryError(f'traceflow name must be a str, not {protocol!r}')
return Extractor.make_traceflow(protocol, fout=fout, format=format, byteorder=byteorder,
nanosecond=nanosecond, bidirectional=bidirectional, analyze=analyze)