# -*- coding: utf-8 -*-# mypy: disable-error-code=dict-item"""UDP - User Datagram Protocol==================================.. module:: pcapkit.protocols.transport.udp:mod:`pcapkit.protocols.transport.udp` contains:class:`~pcapkit.protocols.transport.udp.UDP` only,which implements extractor for User Datagram Protocol(UDP) [*]_, whose structure is described as below:======= ========= ===================== ===============================Octets Bits Name Description======= ========= ===================== =============================== 0 0 ``udp.srcport`` Source Port 2 16 ``udp.dstport`` Destination Port 4 32 ``udp.len`` Length (header includes) 6 48 ``udp.checksum`` Checksum======= ========= ===================== ===============================.. [*] https://en.wikipedia.org/wiki/User_Datagram_Protocol"""importcollectionsfromtypingimportTYPE_CHECKINGfrompcapkit.const.reg.transtypeimportTransTypeasEnum_TransTypefrompcapkit.corekit.moduleimportModuleDescriptorfrompcapkit.protocols.data.transport.udpimportUDPasData_UDPfrompcapkit.protocols.schema.transport.udpimportUDPasSchema_UDPfrompcapkit.protocols.transport.transportimportTransportifTYPE_CHECKING:fromtypingimportAny,Optionalfromtyping_extensionsimportLiteralfrompcapkit.const.reg.apptypeimportAppTypeasEnum_AppTypefrompcapkit.protocols.protocolimportProtocolBaseasProtocolfrompcapkit.protocols.schema.schemaimportSchema__all__=['UDP']
[docs]classUDP(Transport[Data_UDP,Schema_UDP],schema=Schema_UDP,data=Data_UDP):"""This class implements User Datagram Protocol. This class currently supports parsing of the following protocols, which are registered in the :attr:`self.__proto__ <pcapkit.protocols.transport.udp.UDP.__proto__>` attribute: .. list-table:: :header-rows: 1 * - Port Number - Protocol * - 80 - :class:`pcapkit.protocols.application.http.HTTP` """########################################################################### Defaults.###########################################################################: DefaultDict[int, ModuleDescriptor[Protocol] | Type[Protocol]]: Protocol#: index mapping for decoding next layer, c.f.#: :meth:`self._decode_next_layer <pcapkit.protocols.transport.transport.Transport._decode_next_layer>`#: & :meth:`self._import_next_layer <pcapkit.protocols.protocol.Protocol._import_next_layer>`.__proto__=collections.defaultdict(lambda:ModuleDescriptor('pcapkit.protocols.misc.raw','Raw'),{80:ModuleDescriptor('pcapkit.protocols.application.http','HTTP'),# HTTP},)########################################################################### Properties.##########################################################################@propertydefname(self)->'Literal["User Datagram Protocol"]':"""Name of current protocol."""return'User Datagram Protocol'@propertydeflength(self)->'Literal[8]':"""Header length of current protocol."""return8@propertydefsrc(self)->'Enum_AppType':"""Source port."""returnself._info.srcport@propertydefdst(self)->'Enum_AppType':"""Destination port."""returnself._info.dstport########################################################################### Methods.##########################################################################
[docs]defread(self,length:'Optional[int]'=None,**kwargs:'Any')->'Data_UDP':# pylint: disable=unused-argument"""Read User Datagram Protocol (UDP). Structure of UDP header [:rfc:`768`]: .. code-block:: text 0 7 8 15 16 23 24 31 +--------+--------+--------+--------+ | Source | Destination | | Port | Port | +--------+--------+--------+--------+ | | | | Length | Checksum | +--------+--------+--------+--------+ | | data octets ... +---------------- ... Args: length: Length of packet data. **kwargs: Arbitrary keyword arguments. Returns: Parsed packet data. """iflengthisNone:length=len(self)schema=self.__header__udp=Data_UDP(srcport=schema.srcport,dstport=schema.dstport,len=schema.len,checksum=schema.checksum,)returnself._decode_next_layer(udp,(udp.srcport.port,udp.dstport.port),udp.len-8)
########################################################################### Data models.##########################################################################def__length_hint__(self)->'Literal[8]':"""Return an estimated length for the object."""return8
[docs]@classmethoddef__index__(cls)->'Enum_TransType':# pylint: disable=invalid-index-returned"""Numeral registry index of the protocol. Returns: Numeral registry index of the protocol in `IANA`_. .. _IANA: https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml """returnEnum_TransType.UDP# type: ignore[return-value]