Base Protocol¶
pcapkit.protocols.transport.transport contains
Transport,
which is a base class for transport layer protocols, eg.
TCP,
UDP and
SCTP.
- class pcapkit.protocols.transport.transport.Transport(file=None, length=None, **kwargs)[source]¶
Bases:
ProtocolBase[_PT,_ST],Generic[_PT,_ST]Abstract base class for transport layer protocol family.
- classmethod register(code, protocol)[source]¶
Register a new protocol class.
Notes
The full qualified class name of the new protocol class should be as
{protocol.module}.{protocol.name}.- Parameters:
code (
int) – port numberprotocol (
ModuleDescriptor[ProtocolBase] |Type[ProtocolBase]) – module descriptor or aProtocolsubclass
Important
This method must be called from a non-abstract class, as the protocol map should be associated directly with specific transport layer protocol type.
- Raises:
pcapkit.utilities.exceptions.UnsupportedCall – If called on
Transportitself.pcapkit.utilities.exceptions.RegistryError – If
protocolis not a class, or not aProtocolsubclass.
- Warns:
pcapkit.utilities.warnings.RegistryWarning – If this port is already registered and the incumbent differs from the replacement; the message names both. An unresolved
ModuleDescriptorcounts as different from the class it names. SeeProtocolBase.registerfor the guard this shares withregister_protocol, and for the shipped descriptor a restore puts back (#1504).
Note
TCPandUDPeach define their own__proto__rather than sharing one onTransport.register_apptypereaches this method once per protocol and inspects two different registries, so it cannot warn spuriously.Changed in version 1.3.0: Takes the protocol as one
protocolargument, aModuleDescriptoror aProtocolsubclass, in place of themoduleandclass_names.
- __post_init__(file=None, length=None, **kwargs)[source]¶
Post initialisation hook.
- Parameters:
Notes
A header the data ends inside keeps that in its
info, so thatfrom_data()rebuilds only what was captured (#1458).
- static _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.
Important
self.makeaccepts a bareintfor a port, but the schema field converts it only on the way out (inPortEnumField.pre_process), so the schema attribute keeps whatever it was handed. Normalising here makes a constructed packet carry the sameAppTypeas a parsed one, so reading.portoff it works on both paths.
- _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:
TypeVar(_PT, bound= Data)- Returns:
Current protocol with next layer extracted.
Important
The port is forwarded whether or not it is registered:
ProtocolBase._import_next_layerpasses it on asaliasandRawrecords it asData_Raw.protocol. Dropping it would make a payload on a port we do not decode indistinguishable from one on port 22. The lower port is the one carried, for the same reason it is the primary lookup key.SCTP._decode_next_layerandInternet._import_next_layerbehave the same way for an unregistered PPID and transport type.
- __proto__: DefaultDict[int, ModuleDescriptor[ProtocolBase] | Type[ProtocolBase]]¶
Protocol index mapping for decoding next layer, c.f.
self._decode_next_layer&self._import_next_layer.Important
The attribute must be defined and maintained in subclass.