# -*- coding: utf-8 -*-"""PCAP Dumper=================.. module:: pcapkit.dumper.pcap:mod:`pcapkit.dumpkit.pcap` is the dumper for :mod:`pcapkit` implementation,specifically for PCAP format, which is alike those described in:mod:`dictdumper`."""importsysfromtypingimportTYPE_CHECKINGfrompcapkit.dumpkit.commonimportDumperBaseasDumperfrompcapkit.protocols.data.misc.pcap.headerimportHeaderasData_Headerfrompcapkit.protocols.misc.pcap.frameimportFramefrompcapkit.protocols.misc.pcap.headerimportHeaderifTYPE_CHECKING:fromenumimportIntEnumasStdlibIntEnumfromtypingimportIO,Any,OptionalfromaenumimportIntEnumasAenumIntEnumfromtyping_extensionsimportLiteralfrompcapkit.const.reg.linktypeimportLinkTypeasEnum_LinkTypefrompcapkit.protocols.data.misc.pcap.frameimportFrameasData_Frame__all__=['PCAPIO',]
[docs]classPCAPIO(Dumper):"""PCAP file dumper. Args: fname: output file name protocol: data link type byteorder: header byte order nanosecond: nanosecond-resolution file flag **kwargs: arbitrary keyword arguments """ifTYPE_CHECKING:#: PCAP file global header._ghdr:'Data_Header'########################################################################### Properties.##########################################################################@propertydefkind(self)->'Literal["pcap"]':"""File format of current dumper."""return'pcap'########################################################################### Data models.##########################################################################def__init__(self,fname:'str',*,protocol:'Enum_LinkType | StdlibIntEnum | AenumIntEnum | str | int',byteorder:'Literal["big", "little"]'=sys.byteorder,nanosecond:'bool'=False,**kwargs:'Any')->'None':# pylint: disable=arguments-differ"""Initialise dumper. Args: fname: output file name protocol: data link type byteorder: header byte order nanosecond: nanosecond-resolution file flag **kwargs: arbitrary keyword arguments """#: int: Frame counter.self._fnum=1#: bool: Nanosecond-resolution file flag.self._nsec=nanosecond#: Enum_LinkType | StdlibIntEnum | AenumIntEnum | str | int: Data link type.self._link=protocolsuper().__init__(fname,protocol=protocol,byteorder=byteorder,nanosecond=nanosecond,**kwargs)def__call__(self,value:'Data_Frame',name:'Optional[str]'=None)->'PCAPIO':"""Dump a new frame. Args: value: content to be dumped name: name of current content block Returns: The dumper class itself (to support chain calling). """withopen(self._file,'ab')asfile:self._append_value(value,file,nameor'')returnself########################################################################### Utilities.##########################################################################def_dump_header(self,*,protocol:'Enum_LinkType | StdlibIntEnum | AenumIntEnum | str | int',# pylint: disable=arguments-differbyteorder:'Literal["big", "little"]'=sys.byteorder,nanosecond:'bool'=False,**kwargs:'Any')->'None':# pylint: disable=unused-argument"""Initially dump file heads and tails. Args: protocol: data link type byteorder: header byte order nanosecond: nanosecond-resolution file flag **kwargs: arbitrary keyword arguments """header=Header(network=protocol,byteorder=byteorder,nanosecond=nanosecond,)packet=header.datawithopen(self._file,'wb')asfile:file.write(packet)self._ghdr=header.infodef_append_value(self,value:'Data_Frame',file:'IO[bytes]',name:'str')->'None':# pylint: disable=unused-argument"""Call this function to write contents. Args: value: content to be dumped file: output file name: name of current content block """packet=Frame(nanosecond=self._nsec,num=self._fnum,proto=self._link,packet=value.packet,header=self._ghdr,**value.frame_info,).datafile.write(packet)self._fnum+=1