Transport Layer Base Protocol¶
pcapkit.protocols.transport.transport contains
Transport,
which is a base class for transport layer protocols, e.g.
TCP,
UDP and
SCTP.
- class pcapkit.protocols.transport.transport.Transport[_PT: Data, _ST: Schema](file=None, length=None, **kwargs)[source]¶
Bases:
ProtocolBase[_PT,_ST],Generic[_PT,_ST]Abstract base class for transport layer protocol family.
- __proto__: DefaultDict[int, ModuleDescriptor[ProtocolBase] | Type[ProtocolBase]]¶
Protocol index mapping for decoding next layer, see
self._decode_next_layer&self._import_next_layer.Important
The attribute must be defined and maintained in subclass.
- classmethod register(code, protocol)[source]¶
Register a new protocol class.
Notes
The fully qualified class name of the new protocol class should be
{protocol.module}.{protocol.name}.- Parameters:
code (
int) – port numberprotocol (
ModuleDescriptor[ProtocolBase] |Type[ProtocolBase]) – module descriptor or aProtocolBasesubclass
Important
Call it on a concrete transport protocol, whose own
__proto__it updates.- Raises:
pcapkit.utilities.exceptions.UnsupportedCall – If called on
Transportitself.pcapkit.utilities.exceptions.RegistryError – If
protocolis not a class, or not aProtocolBasesubclass.
- Warns:
pcapkit.utilities.warnings.RegistryWarning– If this port is already registered and the incumbent differs from the replacement; the message names both. An unresolvedModuleDescriptorcounts as different from the class it names. The guard is the oneProtocol.registerdescribes (#1504).
Note
Each transport protocol defines its own
__proto__, soregister_apptype, which calls this once per transport protocol, never warns spuriously.Changed in version 1.3.0: Takes the protocol as one
protocolargument, aModuleDescriptoror aProtocolBasesubclass, in place of themoduleandclass_names.
- classmethod analyze(ports, payload, **kwargs)[source]¶
Analyse packet payload.
- Parameters:
- Return type:
- Returns:
Parsed payload as a
ProtocolBaseinstance.
Important
The annotation names the base registry because every transport protocol shares this method while resolving its ports in a registry of its own, such as
TCP. A caller that knows its transport gets the narrower type from whatever handed it the pair, e.g.TCP._make_ports.A bare
intport still resolves: a member hashes as its port (AppType.__hash__) and compares equal to it (AppType.__eq__). So each port is tested against__proto__as it arrived, and the chosen one is unwrapped throughAppType.__int__only when it is a member; unwrapping anything else would makeNoneraise rather than fall back toRaw, and'80'dispatch as a port.A miss writes nothing back, so an unregistered port does not grow the process-wide registry – see
Protocol._lookup_registry.Changed in version 1.5.0:
portsis a pair ofAppTypemembers rather than of bareintport numbers, soTCP.submit, throughDeferred, hands over the same named ports a parsed header carries (#2417).
- __post_init__(file=None, length=None, **kwargs)[source]¶
Post initialisation hook.
- Parameters:
- Return type:
Notes
A header the data ends inside keeps that in its
info, so thatfrom_data()rebuilds only what was captured (#1458).
- classmethod _make_port(port, proto)[source]¶
Resolve a port number to its application type.
- Parameters:
port (
AppType|int) – port number, or the application type itselfproto (
TransportProtocol) – transport protocol the port belongs to, which is what distinguishes e.g. TCP/80 from UDP/80
- Return type:
- Returns:
The
AppTypeforport.- Raises:
ProtocolError – If
portis abool(#2287), or is neither anintthat fits in 16 bits nor anAppType.
Important
The schema field converts a bare
intport only on the way out (PortEnumField.pre_process), so normalising here gives a constructed packet the sameAppTypeas a parsed one.Changed in version 1.6.0: A port past 16 bits, a negative one, or one that is not an
intraisesProtocolError, rather than a bareEnumValueError(#2594).
- _decode_next_layer(dict_, ports, length=None, *, packet=None)[source]¶
Decode next layer protocol.
The next layer is looked up by port number. The lower of the two ports is the primary key; the higher one is used only when it alone is registered.
- Parameters:
- Return type:
_PT- Returns:
Current protocol with next layer extracted.
Important
The port is forwarded whether or not it is registered:
Protocol._import_next_layerpasses it on asaliasandRawrecords it asData_Raw.protocol, which tells payloads on different undecoded ports apart. The port carried is the one looked up.SCTP._decode_next_layerandInternet._import_next_layerbehave the same way for an unregistered PPID and transport type.