[docs]classEthernet(Link[Data_Ethernet,Schema_Ethernet],schema=Schema_Ethernet,data=Data_Ethernet):"""This class implements Ethernet Protocol."""########################################################################### Properties.##########################################################################@propertydefname(self)->'Literal["Ethernet Protocol"]':"""Name of current protocol."""return'Ethernet Protocol'@propertydeflength(self)->'Literal[14]':"""Header length of current protocol."""return14@propertydefprotocol(self)->'Enum_EtherType':"""Name of next layer protocol."""returnself._info.type# source mac address@propertydefsrc(self)->'str':"""Source mac address."""returnself._info.src# destination mac address@propertydefdst(self)->'str':"""Destination mac address."""returnself._info.dst########################################################################### Methods.##########################################################################
########################################################################### Data models.##########################################################################def__length_hint__(self)->'Literal[14]':"""Return an estimated length for the object."""return14
[docs]@classmethoddef__index__(cls)->'Enum_LinkType':# pylint: disable=invalid-index-returned"""Numeral registry index of the protocol. Returns: Numeral registry index of the protocol in `tcpdump`_ link-layer header types. .. _tcpdump: https://www.tcpdump.org/linktypes.html """returnEnum_LinkType.ETHERNET# type: ignore[return-value]
[docs]@classmethoddef_make_data(cls,data:'Data_Ethernet')->'dict[str, Any]':# type: ignore[override]"""Create key-value pairs from ``data`` for protocol construction. Args: data: protocol data Returns: Key-value pairs for protocol construction. """return{'dst':data.dst,'src':data.src,'type':data.type,'payload':cls._make_payload(data),}
def_read_mac_addr(self,addr:'bytes')->'str':"""Read MAC address. Args: addr: MAC address. Returns: Colon (``:``) seperated *hex* encoded MAC address. """ifpy38:_addr=addr.hex(':')else:_addr=':'.join(textwrap.wrap(addr.hex(),2))return_addrdef_make_mac_addr(self,addr:'str | bytes | bytearray')->'bytes':"""Make MAC address. Args: addr: MAC address. Returns: MAC address. """_addr=addr.encode()ifisinstance(addr,str)elseaddrifPAT_MAC_ADDR.fullmatch(_addr)isnotNone:return_addr.replace(b':',b'').replace(b'-',b'')raiseProtocolError(f'invalid MAC address: {addr!r}')