# -*- coding: utf-8 -*-
"""IGMPv3 - Internet Group Management Protocol version 3
===========================================================
.. module:: pcapkit.protocols.internet.igmpv3.protocol
:mod:`pcapkit.protocols.internet.igmpv3.protocol` contains
:class:`~pcapkit.protocols.internet.igmpv3.protocol.IGMPv3`, which implements an
extractor for Internet Group Management Protocol version 3 (IGMPv3)
[:rfc:`3376`]. It handles both IGMPv3 message shapes:
An IGMPv3 Membership Query (type ``0x11``, shared with v1/v2) reuses the
8-octet layout IGMPv1/v2 share and, when at least 12 octets long overall,
appends a source-list extension:
======= ========= ================================== ===================================
Octets Bits Name Description
======= ========= ================================== ===================================
0 0 ``igmpv3.type`` Type
1 8 ``igmpv3.max_resp_code`` Max Response Code
2 16 ``igmpv3.checksum`` Checksum
4 32 ``igmpv3.group`` Group Address
8 64 ``igmpv3.resv`` Reserved (Resv)
8 68 ``igmpv3.s_flag`` Suppress Router-Side Processing (S)
8 69 ``igmpv3.qrv`` Querier's Robustness Variable (QRV)
9 72 ``igmpv3.qqic`` Querier's Query Interval Code
10 80 ``igmpv3.num_sources`` Number of Sources (N)
12 96 ``igmpv3.sources`` Source Address [1..N]
======= ========= ================================== ===================================
An IGMPv3 Membership Report (type ``0x22``) has a layout of its own:
======= ========= ================================== ===================================
Octets Bits Name Description
======= ========= ================================== ===================================
0 0 ``igmpv3.type`` Type
1 8 ``igmpv3.reserved_1`` Reserved
2 16 ``igmpv3.checksum`` Checksum
4 32 ``igmpv3.reserved_2`` Reserved
6 48 ``igmpv3.num_group_records`` Number of Group Records (M)
8 64 ``igmpv3.records`` Group Record [1..M]
======= ========= ================================== ===================================
:meth:`~pcapkit.protocols.internet.igmp.protocol.IGMP._classify` is what keeps an
IGMPv1/v2 message out of this class; :attr:`~pcapkit.protocols.internet.igmp.protocol.IGMP.version`
and :meth:`id` are this class's own.
.. versionadded:: 1.6.0
"""
import ipaddress
from typing import TYPE_CHECKING, cast
from pcapkit.const.igmp.record_type import RecordType as Enum_RecordType
from pcapkit.const.igmp.type import Type as Enum_Type
from pcapkit.corekit.schema.shortread import hoist_short_read
from pcapkit.protocols.internet.igmp.protocol import IGMP as IGMPBase
from pcapkit.protocols.internet.igmpv3.data import GroupRecord as Data_GroupRecord
from pcapkit.protocols.internet.igmpv3.data import IGMPv3 as Data_IGMPv3
from pcapkit.protocols.internet.igmpv3.data import Query as Data_Query
from pcapkit.protocols.internet.igmpv3.data import Report as Data_Report
from pcapkit.protocols.internet.igmpv3.schema import GroupRecord as Schema_GroupRecord
from pcapkit.protocols.internet.igmpv3.schema import IGMPv3 as Schema_IGMPv3
from pcapkit.protocols.internet.igmpv3.schema import Query as Schema_Query
from pcapkit.protocols.internet.igmpv3.schema import QueryExtension as Schema_QueryExtension
from pcapkit.protocols.internet.igmpv3.schema import Report as Schema_Report
from pcapkit.utilities import config
from pcapkit.utilities.exceptions import ProtocolError
if TYPE_CHECKING:
from ipaddress import IPv4Address
from typing import Any, Iterable, Optional
from typing_extensions import Literal
from pcapkit.protocols.protocol import ProtocolBase
from pcapkit.protocols.schema import Schema
__all__ = ['IGMPv3']
[docs]
class IGMPv3(IGMPBase[Data_IGMPv3, Schema_IGMPv3],
schema=Schema_IGMPv3, data=Data_IGMPv3):
"""This class implements Internet Group Management Protocol version 3.
Unlike :class:`~pcapkit.protocols.internet.igmpv1.protocol.IGMPv1` and
:class:`~pcapkit.protocols.internet.igmpv2.protocol.IGMPv2`, which share the base's
own 8-octet schema and data model, IGMPv3's wire format differs
materially by message -- a Query's source-list extension, a Report's
group-record list -- so this class carries its own (:issue:`2439`,
:issue:`2448`).
"""
##########################################################################
# Properties.
##########################################################################
@property
def name(self) -> 'Literal["Internet Group Management Protocol version 3"]': # type: ignore[override]
"""Name of corresponding protocol."""
return 'Internet Group Management Protocol version 3'
@property
def version(self) -> 'Literal[3]':
"""IGMP version this class reads, ``3``."""
return 3
@property
def length(self) -> 'int':
"""Header length of current protocol.
Every message starts with an 8-octet header. A Query may carry the
source-list extension, and a Report's group-record list has no fixed
size, so this is taken during :meth:`read` from the octets consumed
(see :attr:`~pcapkit.protocols.internet.igmpv3.data.IGMPv3.length`)
rather than returned as a constant, unlike the base's own
:attr:`~pcapkit.protocols.internet.igmp.protocol.IGMP.length`.
"""
return self._info.length
##########################################################################
# Methods.
##########################################################################
[docs]
@classmethod
def id(cls) -> 'tuple[Literal["IGMPv3"]]': # type: ignore[override]
"""Index ID of the protocol.
:rfc:`3376` names the protocol ``IGMPv3``, carrying its own version.
Returns:
Index ID of the protocol.
"""
return ('IGMPv3',)
[docs]
def read(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'Data_IGMPv3':
"""Read Internet Group Management Protocol version 3.
Args:
length: Length of packet data.
**kwargs: Arbitrary keyword arguments.
Returns:
Parsed packet data.
Raises:
ProtocolError: If :meth:`_classify` does not place the message in
:attr:`version` 3 -- an IGMPv1/v2-shaped Query, or a type
neither version of IGMP defines.
Note:
A message the data ends inside records the cut on its ``body``, with or
without an enclosing length, so ``from_data(info)`` rebuilds it as captured.
"""
if length is None:
length = len(self)
schema = cast('Schema_IGMPv3', self.__header__)
body = schema.body
if isinstance(body, Schema_Query):
if self._classify(schema.type, body.max_resp_code, length) != self.version:
raise ProtocolError(f'{self.alias}: {schema.type!r} is not an {self.alias} '
f'message ({length} octets)')
# NOTE: The classify check above accepts only a Query of 12 or more octets, exactly when
# ``query_extension_present`` parses ``ext``, so it is never ``None`` here.
ext = cast('Schema_QueryExtension', body.ext)
resv = ext.flags['resv']
s_flag = bool(ext.flags['s_flag'])
qrv = ext.flags['qrv']
qqic = ext.qqic
qqi = self._read_float_code(qqic, bits=8)
max_resp_time = self._read_float_code(body.max_resp_code, bits=8)
sources = tuple(ext.sources)
trailing = ext.get_payload()
igmp = Data_Query(
version=self.version,
type=schema.type,
length=1 + len(body) - len(trailing),
max_resp_code=body.max_resp_code,
max_resp_time=max_resp_time,
checksum=body.checksum,
group=body.group,
resv=resv,
s_flag=s_flag,
qrv=qrv,
qqic=qqic,
qqi=qqi,
num_sources=ext.num_sources,
sources=sources,
) # type: Data_IGMPv3
elif isinstance(body, Schema_Report):
records = tuple(
Data_GroupRecord(
record_type=record.record_type,
aux_data_len=record.aux_data_len,
num_sources=record.num_sources,
multicast_address=record.multicast_address,
sources=tuple(record.sources),
aux_data=record.aux_data,
) for record in body.records
)
trailing = body.payload
igmp = Data_Report(
version=self.version,
type=schema.type,
length=1 + len(body) - len(trailing),
reserved_1=body.reserved_1,
checksum=body.checksum,
reserved_2=body.reserved_2,
num_group_records=body.num_group_records,
records=records,
)
else: # pragma: no cover
raise ProtocolError(f'{self.alias}: unsupported message type: {schema.type!r}')
if isinstance(body, Schema_Query):
parts = (body, body.ext) # type: tuple[Optional[Schema], ...]
else:
parts = (body, *body.records)
# NOTE: Every schema in the body reads to the end of the data (#2660).
hoist_short_read(schema, 'body', parts)
self._verify_checksum(igmp, body.checksum, length)
return self._decode_next_layer(igmp, None, len(trailing))
[docs]
def make(self, *,
type: 'Enum_Type | int' = Enum_Type.IGMP_Membership_Query, # pylint: disable=redefined-builtin
max_resp_code: 'Optional[int]' = None,
max_resp_time: 'int' = 0,
checksum: 'Optional[bytes]' = None,
group: 'IPv4Address | str | bytes | int' = '0.0.0.0', # nosec: B104
resv: 'int' = 0,
s_flag: 'bool' = False,
qrv: 'int' = 0,
qqic: 'Optional[int]' = None,
qqi: 'Optional[int]' = None,
sources: 'Optional[Iterable[IPv4Address | str | bytes | int]]' = None,
num_sources: 'Optional[int]' = None,
reserved_1: 'int' = 0,
reserved_2: 'bytes' = b'\x00\x00',
num_group_records: 'Optional[int]' = None,
records: 'Optional[Iterable[dict[str, Any] | Data_GroupRecord]]' = None,
payload: 'bytes | ProtocolBase | Schema' = b'',
**kwargs: 'Any') -> 'Schema_IGMPv3':
"""Make (construct) packet data.
Building a Query (the default) always writes the source-list
extension: this class is specifically IGMPv3, unlike the base's own
:meth:`~pcapkit.protocols.internet.igmp.protocol.IGMP.make`, which writes the
extension only when asked, since there a plain 8-octet message is
the more natural default.
Args:
type: Message type.
max_resp_code: Max response code, as transmitted on the wire. When
not given, derived from ``max_resp_time`` via
:meth:`~pcapkit.protocols.protocol.ProtocolBase._make_float_code`;
only meaningful for a Query.
max_resp_time: Max response time, in units of 1/10 second: an
:obj:`int` from 0 to 255 (not a :obj:`bool`), else
:exc:`ProtocolError`. Ignored when ``max_resp_code`` is given.
checksum: Checksum; the one's complement of the one's complement
sum of the message, with this field taken as zero and no
pseudo-header -- the whole IP payload (:rfc:`3376#section-4.1.2`,
:rfc:`3376#section-4.2.2`). If :data:`None`, computed so, over
``payload`` as well, when
:data:`~pcapkit.utilities.config.CHECKSUM_COMPUTE` is on, zero
when it is off.
group: Group address; ``0.0.0.0`` for a General Query; only
meaningful for a Query.
resv: The 4 reserved high-order bits of the flags octet
(:rfc:`3376#section-4.1.4`); ``0`` on a standards-conformant
sender; only meaningful for a Query.
s_flag: Suppress Router-Side Processing flag
(:rfc:`3376#section-4.1.5`); only meaningful for a Query.
qrv: Querier's Robustness Variable (:rfc:`3376#section-4.1.6`);
only meaningful for a Query.
qqic: Querier's Query Interval Code, as transmitted on the wire;
derived from ``qqi`` via
:meth:`~pcapkit.protocols.protocol.ProtocolBase._make_float_code`
when not given; only meaningful for a Query.
qqi: Querier's Query Interval, in seconds; ignored when ``qqic``
is given; only meaningful for a Query.
sources: Source addresses being queried
(:rfc:`3376#section-4.1.9`); only meaningful for a Query.
num_sources: Number of sources, as transmitted on the wire
(:rfc:`3376#section-4.1.8`); the length of ``sources`` when not
given; only meaningful for a Query. A count above the length of
``sources`` is written as given, as on a Query the capture cut
inside its sources.
reserved_1: The reserved octet following ``type``
(:rfc:`3376#section-4.2.1`); only meaningful for a Report.
reserved_2: The reserved 16 bits before the group-record count
(:rfc:`3376#section-4.2.1`); only meaningful for a Report.
num_group_records: Number of group records, as transmitted on the
wire; the length of ``records`` when not given, so a capture cut
inside its record list rebuilds as captured; only meaningful for
a Report. A count above the length of ``records`` is written as
given, as on a Report the capture cut short.
records: Group records (:rfc:`3376#section-4.2.4`), each either
a :class:`~pcapkit.protocols.internet.igmpv3.data.GroupRecord`
or a :obj:`dict` of the keyword arguments
:meth:`_make_group_record` takes; only meaningful for a
Report.
payload: Payload of current instance.
**kwargs: Arbitrary keyword arguments.
Returns:
Constructed packet data.
Raises:
ProtocolError: If ``type`` is a name, a :obj:`bool`, or a member
or code outside 0 to 255, or is neither a Query nor a Report;
if ``max_resp_time`` is not an :obj:`int` from 0 to 255, or
``qqi`` a non-negative :obj:`int`; if ``num_group_records`` is
not an :obj:`int` from the length of ``records`` to 65535, or
``num_sources`` one from the length of ``sources``; if
``checksum`` or ``reserved_2`` is not two octets; or if a
record's ``record_type`` is refused, see
:meth:`_make_group_record`.
"""
type_value = self._make_index(type, Enum_Type, bits=8, name='type')
if isinstance(max_resp_time, bool) or not isinstance(max_resp_time, int) \
or not 0 <= max_resp_time <= 255:
raise ProtocolError(f'{self.alias}: invalid max_resp_time: {max_resp_time!r}')
field = b'\x00\x00' if checksum is None else self._make_octets(checksum, size=2, name='checksum')
if type_value == Enum_Type.IGMPv3_Membership_Report:
record_list = [] if records is None else [
self._make_group_record(record) if not isinstance(record, dict) else
self._make_group_record(**record) for record in records
]
if num_group_records is None:
num_group_records = len(record_list)
elif isinstance(num_group_records, bool) or not isinstance(num_group_records, int) \
or not len(record_list) <= num_group_records <= 0xFFFF:
raise ProtocolError(f'{self.alias}: invalid num_group_records: {num_group_records!r} '
f'for {len(record_list)} record(s)')
schema = Schema_IGMPv3(
type=type_value,
body=Schema_Report(
reserved_1=reserved_1,
checksum=field,
reserved_2=self._make_octets(reserved_2, size=2, name='reserved_2'),
num_group_records=num_group_records,
records=record_list,
payload=payload,
),
)
elif type_value == Enum_Type.IGMP_Membership_Query:
if qqic is None:
qqic = self._make_float_code(0 if qqi is None else qqi, bits=8, name='qqi')
source_list = [] if sources is None else list(sources)
if num_sources is None:
num_sources = len(source_list)
elif self._make_uint(num_sources, bits=16, name='num_sources') < len(source_list):
raise ProtocolError(f'{self.alias}: invalid num_sources: {num_sources!r} '
f'for {len(source_list)} source(s)')
ext = Schema_QueryExtension(
flags={'resv': resv, 's_flag': int(bool(s_flag)), 'qrv': qrv},
qqic=qqic,
num_sources=num_sources,
sources=source_list,
payload=b'',
)
if max_resp_code is None:
max_resp_code = self._make_float_code(max_resp_time, bits=8, name='max_resp_time')
schema = Schema_IGMPv3(
type=type_value,
body=Schema_Query(
max_resp_code=max_resp_code,
checksum=field,
group=group,
ext=ext,
payload=payload,
),
)
else:
raise ProtocolError(f'{self.alias}: unsupported message type: {type_value!r}')
if checksum is None and config.CHECKSUM_COMPUTE:
from pcapkit.corekit.checksum.internet import compute_checksum # isort: skip # pylint: disable=import-outside-toplevel
schema.body.checksum = compute_checksum(schema.pack())
return schema
##########################################################################
# Utilities.
##########################################################################
[docs]
@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 {
'version': report.get('version'),
'type': report.type,
'reserved_1': report.reserved_1,
'checksum': report.checksum,
'reserved_2': report.reserved_2,
# NOTE: The count as parsed, so a Report cut inside its records rebuilds as captured.
'num_group_records': report.get('num_group_records'),
'records': report.records,
'payload': cls._make_payload(data),
}
query = cast('Data_Query', data)
# NOTE: The count as parsed, while the sources are those a cut begins (11 octets into the body,
# octet 12 of the message), zeros past it; the info keeps no octets captured to compare (#2760).
num_sources = None
record = query.get('__short_read__')
if record is not None and record[0] == 'body':
cut = max(record[1] - 11, 0)
try:
packed = b''.join(ipaddress.IPv4Address(source).packed for source in query.sources)
except (TypeError, ValueError): # edited, to be refused by ``make``
packed = b''
if len(packed) == -(-cut // 4) * 4 and not packed[cut:].strip(b'\x00'):
num_sources = query.get('num_sources')
return {
'version': query.get('version'),
'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,
'num_sources': num_sources,
'payload': cls._make_payload(data),
}
[docs]
def _make_group_record(self, record: 'Optional[Data_GroupRecord]' = None, *,
record_type: 'Enum_RecordType | int' = Enum_RecordType.MODE_IS_INCLUDE,
aux_data_len: 'Optional[int]' = None,
num_sources: 'Optional[int]' = None,
multicast_address: 'IPv4Address | str | bytes | int' = '0.0.0.0', # nosec: B104
sources: 'Optional[Iterable[IPv4Address | str | bytes | int]]' = None,
aux_data: 'bytes' = b'',
**kwargs: 'Any') -> 'Schema_GroupRecord': # pylint: disable=unused-argument
"""Make an IGMPv3 group record (:rfc:`3376#section-4.2.4`).
Args:
record: An already-built group record, taking precedence over
the keyword arguments below (mirrors
:meth:`HIP._make_param_locator_set
<pcapkit.protocols.internet.hip.protocol.HIP._make_param_locator_set>`'s
``param``/keyword split).
record_type: Record type.
aux_data_len: Auxiliary data length, in 32-bit words; derived
from ``aux_data`` when not given.
num_sources: Number of source addresses; derived from
``sources`` when not given.
multicast_address: Multicast address.
sources: Source addresses.
aux_data: Auxiliary data; :rfc:`3376#section-4.2.10` requires an
IGMPv3 sender to leave this empty.
**kwargs: Arbitrary keyword arguments.
Returns:
Group record schema.
Raises:
ProtocolError: If ``record_type`` is a name, a :obj:`bool`, or a
member or code outside 0 to 255.
"""
if record is not None:
return Schema_GroupRecord(
record_type=record.record_type,
aux_data_len=record.aux_data_len,
num_sources=record.num_sources,
multicast_address=record.multicast_address,
sources=list(record.sources),
aux_data=record.aux_data,
)
source_list = [] if sources is None else list(sources)
return Schema_GroupRecord(
record_type=self._make_index(record_type, Enum_RecordType, bits=8, name='record type'),
aux_data_len=len(aux_data) // 4 if aux_data_len is None else aux_data_len,
num_sources=len(source_list) if num_sources is None else num_sources,
multicast_address=multicast_address,
sources=source_list,
aux_data=aux_data,
)