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
27 changes: 27 additions & 0 deletions pcapkit/protocols/internet/igmpv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ def _encode_resp_code(value: 'int') -> 'int':
return 0x80 | (exp << 4) | mant


def _keep_body_short_read(schema: 'Schema_IGMPv3') -> 'None':
"""Record on ``schema`` a short read only the schemas in its body recorded (:issue:`2660`).

Every schema in the body reads to the end of the data, so a field there that
reads short has reached it. Elsewhere a nested record need not mean that:
hoisting one in :func:`~pcapkit.protocols.schema.schema.keep_short_read` marks
a complete TCP segment with a malformed MPTCP DSS option as cut.

Args:
schema: The header just parsed.

"""
body = schema.body
if isinstance(body, Schema_Query):
parts = (body, body.ext) # type: tuple[Optional[Schema], ...]
else:
parts = (body, *body.records)
if '__short_read__' not in schema.__dict__ and any(
part is not None and '__short_read__' in part.__dict__ for part in parts):
schema.__dict__['__short_read__'] = ('body', len(schema.__buffer__['body']))


class IGMPv3(IGMPBase[Data_IGMPv3, Schema_IGMPv3],
schema=Schema_IGMPv3, data=Data_IGMPv3):
"""This class implements Internet Group Management Protocol version 3.
Expand Down Expand Up @@ -214,6 +236,10 @@ def read(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'Data_IGMPv3'
: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)
Expand Down Expand Up @@ -278,6 +304,7 @@ def read(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'Data_IGMPv3'
else: # pragma: no cover
raise ProtocolError(f'{self.alias}: unsupported message type: {schema.type!r}')

_keep_body_short_read(schema)
self._verify_checksum(igmp, body.checksum, length)
return self._decode_next_layer(igmp, None, len(trailing))

Expand Down
106 changes: 105 additions & 1 deletion tests/protocols/internet/test_igmp_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,10 +1283,108 @@ def test_record_list_reads_bytes_and_a_live_stream_alike(self) -> None:
self.assertEqual(field.length, 36)


#: An IGMPv3 Group-and-Source-Specific Query for 239.1.2.3 with S set, QRV 2,
#: QQIC 125 and two sources: the fixed fields are octets 0-11, the sources 12-19.
IGMPV3_TWO_SOURCE_QUERY = igmpv3_query(100, '239.1.2.3', s_flag=1, qrv=2, qqic=125,
sources=('10.0.0.1', '10.0.0.2'))


class IGMPv3DirectCutTests(unittest.TestCase):
"""A message cut short and parsed on its own, as ``IGMPv3(raw, len(raw))``,
rebuilds as captured, as it does inside IPv4 (#2660). IGMP has no length
field, so the body's own read is never short there: the cut is recorded on
the body, after the octets it holds, wherever the data ends inside a field."""

def setUp(self) -> None:
reimport_once_per_class(self)

def parse(self, raw: 'bytes'):
"""Parse ``raw`` with the IGMPv3 class, its length as the message's."""
from pcapkit.protocols.internet.igmpv3 import IGMPv3

with warnings.catch_warnings():
# a cut field warns that its length runs past the data, which is the point
warnings.simplefilter('ignore')
return IGMPv3(raw, len(raw))

def assert_rebuilds(self, raw: 'bytes', igmp, record: 'object') -> None:
"""``igmp`` records ``record`` and rebuilds ``raw``, from ``info`` and its dict."""
from pcapkit.protocols.internet.igmpv3 import IGMPv3

self.assertEqual(igmp.info.get('__short_read__'), record)
self.assertEqual(igmp.data, raw)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
self.assertEqual(IGMPv3.from_data(igmp.info).data, raw)
self.assertEqual(IGMPv3.from_data(igmp.info.to_dict()).data, raw)

def test_whole_messages_are_not_marked(self) -> None:
query = self.parse(IGMPV3_TWO_SOURCE_QUERY)
self.assertEqual((str(query.info.group), query.info.s_flag, query.info.qrv, query.info.qqic),
('239.1.2.3', True, 2, 125))
self.assertEqual(tuple(map(str, query.info.sources)), ('10.0.0.1', '10.0.0.2'))
report = self.parse(IGMPV3_TWO_RECORD_REPORT)
self.assertEqual([(str(record.multicast_address), len(record.sources))
for record in report.info.records], [('239.1.1.1', 2), ('239.2.2.2', 3)])

for name, packet, igmp in (('query', IGMPV3_TWO_SOURCE_QUERY, query),
('report', IGMPV3_TWO_RECORD_REPORT, report)):
with self.subTest(name):
self.assertEqual(igmp.info.length, len(packet))
self.assert_rebuilds(packet, igmp, None)

def test_a_query_cut_at_every_offset(self) -> None:
"""Fewer than 12 octets is no IGMPv3 Query (:rfc:`3376#section-7.1`), so
those cuts are refused; every longer one ends inside the source list."""
from pcapkit.utilities.exceptions import ProtocolError

packet = IGMPV3_TWO_SOURCE_QUERY
for keep in range(1, len(packet)):
raw = packet[:keep]
with self.subTest(keep=keep):
if keep < 12:
with self.assertRaisesRegex(ProtocolError, r'is not an IGMPv3 message'):
self.parse(raw)
continue
igmp = self.parse(raw)
self.assertEqual((str(igmp.info.group), igmp.info.qqic, len(igmp.info.sources)),
('239.1.2.3', 125, 2))
self.assert_rebuilds(raw, igmp, ('body', keep - 1))

def test_a_report_cut_at_every_offset(self) -> None:
"""A cut at the end of the header or of the first record ends inside no
field, so it is not marked, and the count as parsed rebuilds it (#2650)."""
packet = IGMPV3_TWO_RECORD_REPORT
for keep in range(1, len(packet)):
raw = packet[:keep]
with self.subTest(keep=keep):
igmp = self.parse(raw)
if keep >= 8:
self.assertEqual(igmp.info.num_group_records, 2)
record = None if keep in (8, 24) else ('body', keep - 1)
self.assert_rebuilds(raw, igmp, record)

def test_a_cut_is_recorded_as_inside_ipv4(self) -> None:
"""The record a direct parse makes is the one the enclosing IPv4 layer's
longer length gives the body."""
from pcapkit.protocols.internet.ipv4 import IPv4

for name, packet, keep in (('query', IGMPV3_TWO_SOURCE_QUERY, 17),
('report', IGMPV3_TWO_RECORD_REPORT, 30)):
with self.subTest(name), warnings.catch_warnings():
warnings.simplefilter('ignore')
outer = IPv4(protocol=2, src='10.0.0.1', dst='224.0.0.22', payload=packet).data
carried = IPv4(outer[:len(outer) - len(packet) + keep]).payload
direct = self.parse(packet[:keep])
self.assertEqual(direct.info['__short_read__'], carried.info['__short_read__'])
self.assertEqual(direct.info.to_dict(), carried.info.to_dict())


class IGMPv3RecordCountTests(unittest.TestCase):
"""``IGMPv3.make`` writes the ``num_group_records`` it is given only as an
:obj:`int` from the length of ``records`` to 65535; any other is refused
rather than written altered or losing records or payload (#2617, #2650)."""
rather than written altered or losing records or payload, as ruled on #2617
(#2650)."""

RECORDS = (
{'record_type': 1, 'multicast_address': '239.1.1.1'},
Expand All @@ -1311,6 +1409,12 @@ def test_a_count_the_records_or_the_field_cannot_carry_is_refused(self) -> None:
proto.make(type=Type.IGMPv3_Membership_Report, records=self.RECORDS,
num_group_records=count, payload=payload)

# True equals one record's count, so only the bool check refuses it
with self.subTest(count=True, records=1), self.assertRaisesRegex(
ProtocolError, r'^IGMPv3: invalid num_group_records: True for 1 record'):
proto.make(type=Type.IGMPv3_Membership_Report, records=self.RECORDS[:1],
num_group_records=True)

def test_a_count_from_the_records_to_the_field_maximum_is_written(self) -> None:
from pcapkit.const.igmp.type import Type
from pcapkit.protocols.internet.igmpv3 import IGMPv3
Expand Down
7 changes: 7 additions & 0 deletions tests/protocols/test_short_read_roundtrip_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def _ipv6(code: int, body: bytes) -> bytes:
('igmpv3-report', 'internet.ipv4:IPv4', {}, bytes.fromhex(
'45000040123400000102bd710a000001e0000016' '2200c7e100000002'
'01000002ef0101010a0000010a000002' '02000003ef0202020a0000030a0000040a000005')),
('igmpv3-query', 'internet.ipv4:IPv4', {}, bytes.fromhex(
'45000028123400000102bd9e0a000001e0000001' '1164df14ef0102030a7d0002' '0a0000010a000002')),
# #2660: parsed on its own, with no IPv4 length to run past
('igmpv3-report-direct', 'internet.igmpv3:IGMPv3', {}, bytes.fromhex(
'2200c7e100000002' '01000002ef0101010a0000010a000002' '02000003ef0202020a0000030a0000040a000005')),
('igmpv3-query-direct', 'internet.igmpv3:IGMPv3', {}, bytes.fromhex(
'1164df14ef0102030a7d0002' '0a0000010a000002')),
('ipv6', IPV6, {}, _ipv6(59, b'')),
('ipv6-chain', IPV6, {}, bytes.fromhex(
'600000000020004000000000000000000000000000000001000000000000000000000000000000023c'
Expand Down
Loading