Source code for pcapkit.protocols.schema.transport.udp
# -*- coding: utf-8 -*-# mypy: disable-error-code=assignment"""header schema for user datagram protocol"""fromtypingimportTYPE_CHECKINGfrompcapkit.const.reg.apptypeimportAppTypeasEnum_AppTypefrompcapkit.const.reg.apptypeimportTransportProtocolasEnum_TransportProtocolfrompcapkit.corekit.fields.miscimportPayloadFieldfrompcapkit.corekit.fields.numbersimportEnumField,UInt16Fieldfrompcapkit.corekit.fields.stringsimportBytesFieldfrompcapkit.protocols.schema.schemaimportSchema,schema_final__all__=['UDP']ifTYPE_CHECKING:fromtypingimportAnyfrompcapkit.protocols.protocolimportProtocolBaseasProtocolclassPortEnumField(EnumField):"""Enumerated value for protocol fields. Args: length: Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument. default: Field default value, if any. signed: Whether the field is signed. byteorder: Field byte order. bit_length: Field bit length. callback: Callback function to be called upon :meth:`self.__call__ <pcapkit.corekit.fields.field.FieldBase.__call__>`. Important: This class is specifically designed for :class:`~pcapkit.const.reg.apptype.AppType` as it is actually a :class:`~enum.StrEnum` class. """ifTYPE_CHECKING:_namespace:'Enum_AppType'defpre_process(self,value:'int | Enum_AppType',packet:'dict[str, Any]')->'int | bytes':"""Process field value before construction (packing). Arguments: value: Field value. packet: Packet data. Returns: Processed field value. """ifisinstance(value,Enum_AppType):value=value.portreturnsuper().pre_process(value,packet)defpost_process(self,value:'int | bytes',packet:'dict[str, Any]')->'Enum_AppType':"""Process field value after parsing (unpacked). Args: value: Field value. packet: Packet data. Returns: Processed field value. """value=super(EnumField,self).post_process(value,packet)returnself._namespace.get(value,proto=Enum_TransportProtocol.udp)
[docs]@schema_finalclassUDP(Schema):"""Header schema for UDP packet."""#: Source port.srcport:'Enum_AppType'=PortEnumField(length=2,namespace=Enum_AppType)#: Destination port.dstport:'Enum_AppType'=PortEnumField(length=2,namespace=Enum_AppType)#: Length of UDP packet.len:'int'=UInt16Field()#: Checksum of UDP packet.checksum:'bytes'=BytesField(length=2)#: Payload.payload:'bytes'=PayloadField()ifTYPE_CHECKING:def__init__(self,srcport:'Enum_AppType | int',dstport:'Enum_AppType | int',len:'int',checksum:'bytes',payload:'bytes | Schema | Protocol')->'None':...