Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions pcapkit/protocols/application/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,37 +414,6 @@ def _read_id_numbers(self, id: 'bytes') -> 'IPv4Address':
#_addr = '.'.join(str(_) for _ in _byte)
return ipaddress.ip_address(id) # type: ignore[return-value]

def _read_encrypt_auth(self, schema: 'Schema_CryptographicAuthentication') -> 'Data_CryptographicAuthentication':
"""Read Authentication field when Cryptographic Authentication is employed,
i.e. :attr:`~pcapkit.protocols.data.application.ospf.OSPF.autype` is ``2``.

Structure of Cryptographic Authentication [:rfc:`2328`]:

.. code-block:: text

0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 0 | Key ID | Auth Data Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Cryptographic sequence number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Args:
schema: parsed authentication data

Returns:
Parsed packet data.

"""
auth = Data_CryptographicAuthentication(
reserved=schema.reserved,
key_id=schema.key_id,
len=schema.len,
seq=schema.seq,
)
return auth

def _make_id_numbers(self, id: 'IPv4Address | str | bytes | bytearray') -> 'bytes':
"""Make router and area IDs.

Expand Down Expand Up @@ -478,6 +447,37 @@ def _make_id_numbers(self, id: 'IPv4Address | str | bytes | bytearray') -> 'byte
return cast('IPv4Address', parse_ip_address(
id, f'{self.__class__.__name__}: invalid ID', version=4)).packed

def _read_encrypt_auth(self, schema: 'Schema_CryptographicAuthentication') -> 'Data_CryptographicAuthentication':
"""Read Authentication field when Cryptographic Authentication is employed,
i.e. :attr:`~pcapkit.protocols.data.application.ospf.OSPF.autype` is ``2``.

Structure of Cryptographic Authentication [:rfc:`2328`]:

.. code-block:: text

0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 0 | Key ID | Auth Data Len |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Cryptographic sequence number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Args:
schema: parsed authentication data

Returns:
Parsed packet data.

"""
auth = Data_CryptographicAuthentication(
reserved=schema.reserved,
key_id=schema.key_id,
len=schema.len,
seq=schema.seq,
)
return auth

def _make_encrypt_auth(self,
auth_data: 'bytes | Schema_CryptographicAuthentication | Data_CryptographicAuthentication | dict[str, Any]' # pylint: disable=line-too-long
) -> 'Schema_CryptographicAuthentication':
Expand Down
98 changes: 49 additions & 49 deletions pcapkit/protocols/internet/igmpv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,55 @@ def make(self,

raise ProtocolError(f'{self.alias}: unsupported message type: {type_value!r}')

##########################################################################
# Utilities.
##########################################################################

@classmethod
def _make_data(cls, data: 'Data_IGMPv3') -> '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.

Note:
Dispatches on ``data.type`` rather than on
:func:`isinstance(data, Data_Report) <isinstance>`, so that a
hand-built :obj:`dict` carrying the right ``type`` but not
actually one of the two concrete data classes -- as a test double
legitimately might -- still rebuilds correctly, the same way
:meth:`make` itself dispatches on the ``type`` it is given rather
than requiring a particular input class.

"""
if data.type == Enum_Type.IGMPv3_Membership_Report:
report = cast('Data_Report', data)
return {
'type': report.type,
'reserved_1': report.reserved_1,
'checksum': report.checksum,
'reserved_2': report.reserved_2,
'records': report.records,
'payload': cls._make_payload(data),
}

query = cast('Data_Query', data)
return {
'type': query.type,
'max_resp_code': query.max_resp_code,
'checksum': query.checksum,
'group': query.group,
'resv': query.resv,
's_flag': query.s_flag,
'qrv': query.qrv,
'qqic': query.qqic,
'sources': query.sources,
'payload': cls._make_payload(data),
}

@staticmethod
def _make_group_record(record: 'Optional[Data_GroupRecord]' = None, *,
record_type: 'Enum_RecordType | int' = Enum_RecordType.MODE_IS_INCLUDE,
Expand Down Expand Up @@ -458,52 +507,3 @@ def _make_group_record(record: 'Optional[Data_GroupRecord]' = None, *,
sources=source_list,
aux_data=aux_data,
)

##########################################################################
# Utilities.
##########################################################################

@classmethod
def _make_data(cls, data: 'Data_IGMPv3') -> '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.

Note:
Dispatches on ``data.type`` rather than on
:func:`isinstance(data, Data_Report) <isinstance>`, so that a
hand-built :obj:`dict` carrying the right ``type`` but not
actually one of the two concrete data classes -- as a test double
legitimately might -- still rebuilds correctly, the same way
:meth:`make` itself dispatches on the ``type`` it is given rather
than requiring a particular input class.

"""
if data.type == Enum_Type.IGMPv3_Membership_Report:
report = cast('Data_Report', data)
return {
'type': report.type,
'reserved_1': report.reserved_1,
'checksum': report.checksum,
'reserved_2': report.reserved_2,
'records': report.records,
'payload': cls._make_payload(data),
}

query = cast('Data_Query', data)
return {
'type': query.type,
'max_resp_code': query.max_resp_code,
'checksum': query.checksum,
'group': query.group,
'resv': query.resv,
's_flag': query.s_flag,
'qrv': query.qrv,
'qqic': query.qqic,
'sources': query.sources,
'payload': cls._make_payload(data),
}
Loading
Loading