Source code for pcapkit.protocols.schema.misc.pcap.header
# -*- coding: utf-8 -*-# mypy: disable-error-code=assignment"""header schema for global header of PCAP file format"""fromtypingimportTYPE_CHECKINGfrompcapkit.const.reg.linktypeimportLinkTypeasEnum_LinkTypefrompcapkit.corekit.fields.numbersimportEnumField,Int32Field,UInt16Field,UInt32Fieldfrompcapkit.corekit.fields.stringsimportBytesFieldfrompcapkit.protocols.schema.schemaimportSchema,schema_finalfrompcapkit.utilities.exceptionsimportProtocolError__all__=['Header']ifTYPE_CHECKING:fromtypingimportAnyfrompcapkit.corekit.fields.numbersimportNumberFielddefmagic_number_callback(field:'NumberField',packet:'dict[str, Any]')->'None':"""Calculate byte order of PCAP file. Args: field: Field instance. packet: Packet data. """magic_number=packet['magic_number']ifmagic_number==b'\xd4\xc3\xb2\xa1':field._byteorder='little'elifmagic_number==b'\xa1\xb2\xc3\xd4':field._byteorder='big'elifmagic_number==b'\x4d\x3c\xb2\xa1':field._byteorder='little'elifmagic_number==b'\xa1\xb2\x3c\x4d':field._byteorder='big'else:raiseProtocolError('invalid magic number')
[docs]@schema_finalclassHeader(Schema):"""Global header of PCAP file."""#: Magic number.magic_number:'bytes'=BytesField(length=4)#: Version number major.version_major:'int'=UInt16Field(callback=magic_number_callback)#: Version number minor.version_minor:'int'=UInt16Field(callback=magic_number_callback)#: GMT to local correction.thiszone:'int'=Int32Field(callback=magic_number_callback)#: Accuracy of timestamps.sigfigs:'int'=UInt32Field(callback=magic_number_callback)#: Max length of captured packets, in octets.snaplen:'int'=UInt32Field(callback=magic_number_callback)#: Data link type.network:'Enum_LinkType'=EnumField(length=4,namespace=Enum_LinkType,callback=magic_number_callback)ifTYPE_CHECKING:def__init__(self,magic_number:'bytes',version_major:'int',version_minor:'int',# pylint: disable=unused-argument,super-init-not-called,multiple-statementsthiszone:'int',sigfigs:'int',snaplen:'int',network:'int')->'None':...