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 name
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 aProtocolsubclass.
- Warns:
pcapkit.utilities.warnings.RegistryWarning – If this port is already registered, naming the displaced entry and its replacement so a caller can tell what was lost. Fires only when the incumbent differs from the replacement – see
ProtocolBase.registerfor the guard this shares withregister_protocol.
Note
cls.__proto__belongs to the concrete protocol, not toTransport, soregister_apptypereaching this method twice for one call – once asTCP, once asUDP– inspects two different registries and cannot warn spuriously.
- 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, and the schema field only converts one on the way out (inPortEnumField.pre_process), leaving the schema attribute holding whatever it was handed. A constructed packet therefore reachedself.readwith anintwhere a parsed one carries anAppType, and reading.portoff it raisedAttributeError. Normalising here keeps the two paths agreeing on the type the schema declares.
- _decode_next_layer(dict_, ports, length=None, *, packet=None)[source]¶
Decode next layer protocol.
The method will check if the next layer protocol is supported based on the source and destination port numbers. We will use the lower port number from both ports as the primary key to lookup the next layer.
- 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, since
ProtocolBase._import_next_layerpasses it on asaliasandRawrecords it asData_Raw.protocol. Dropping it – as this used to, by falling back toNone– anonymised the very case the field is most useful for: a payload on a port we do not decode is then 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_layeralready behave this 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.