diff --git a/pcapkit/protocols/transport/tcp.py b/pcapkit/protocols/transport/tcp.py index c16fb7cd4d..5296c43b3c 100644 --- a/pcapkit/protocols/transport/tcp.py +++ b/pcapkit/protocols/transport/tcp.py @@ -2034,30 +2034,29 @@ def _make_tcp_options(self, options: 'list[Schema_Option | tuple[Enum_Option, di Returns: Tuple of options and total length of options. + Note: + The options are emitted in the given order, ``NOP`` and ``EOOL`` + included, so that a parsed option list re-emits the same octets. + Padding is added only once, after the last option, when the list + does not end on a 32-bit boundary: zero octets if the list already + ends with ``EOOL``, otherwise ``NOP`` options followed by an ``EOOL``. + """ total_length = 0 + options_list = [] # type: list[Schema_Option | bytes] + code = None # type: Optional[Enum_Option] if isinstance(options, list): - options_list = [] # type: list[Schema_Option | bytes] for schema in options: if isinstance(schema, bytes): code = Enum_Option.get(schema[0]) - if code in (Enum_Option.No_Operation, Enum_Option.End_of_Option_List): # ignore padding options by default - continue - data = schema # type: Schema_Option | bytes data_len = len(data) elif isinstance(schema, Schema): code = schema.kind - if code in (Enum_Option.No_Operation, Enum_Option.End_of_Option_List): # ignore padding options by default - continue - data = schema data_len = len(schema.pack()) else: code, args = cast('tuple[Enum_Option, dict[str, Any]]', schema) - if code in (Enum_Option.No_Operation, Enum_Option.End_of_Option_List): # ignore padding options by default - continue - name = self._lookup_registry(self.__option__, code) if isinstance(name, str): meth_name = f'_make_mode_{name}' @@ -2071,44 +2070,31 @@ def _make_tcp_options(self, options: 'list[Schema_Option | tuple[Enum_Option, di options_list.append(data) total_length += data_len + else: + for code, option in options.items(multi=True): + name = self._lookup_registry(self.__option__, code) + if isinstance(name, str): + meth_name = f'_make_mode_{name}' + meth = cast('OptionConstructor', + getattr(self, meth_name, self._make_mode_donone)) + else: + meth = name[1] - # force alignment to 32-bit boundary - if data_len % 4: - pad_len = 4 - (data_len % 4) - pad_opt = self._make_mode_nop(Enum_Option.No_Operation) # type: ignore[arg-type] - total_length += pad_len + data = meth(code, option) + data_len = len(data.pack()) - for _ in range(pad_len - 1): - options_list.append(pad_opt) - options_list.append(self._make_mode_eool(Enum_Option.End_of_Option_List)) # type: ignore[arg-type] - return options_list, total_length + options_list.append(data) + total_length += data_len - options_list = [] - for code, option in options.items(multi=True): - # ignore padding options by default - if code in (Enum_Option.No_Operation, Enum_Option.End_of_Option_List): - continue + # force alignment to 32-bit boundary + if total_length % 4: + pad_len = 4 - (total_length % 4) + total_length += pad_len - name = self._lookup_registry(self.__option__, code) - if isinstance(name, str): - meth_name = f'_make_mode_{name}' - meth = cast('OptionConstructor', - getattr(self, meth_name, self._make_mode_donone)) + if code == Enum_Option.End_of_Option_List: + options_list.append(bytes(pad_len)) else: - meth = name[1] - - data = meth(code, option) - data_len = len(data.pack()) - - options_list.append(data) - total_length += data_len - - # force alignment to 32-bit boundary - if data_len % 4: - pad_len = 4 - (data_len % 4) pad_opt = self._make_mode_nop(Enum_Option.No_Operation) # type: ignore[arg-type] - total_length += pad_len - for _ in range(pad_len - 1): options_list.append(pad_opt) options_list.append(self._make_mode_eool(Enum_Option.End_of_Option_List)) # type: ignore[arg-type] diff --git a/tests/protocols/transport/test_tcp_options_roundtrip_unit.py b/tests/protocols/transport/test_tcp_options_roundtrip_unit.py new file mode 100644 index 0000000000..9109a61e69 --- /dev/null +++ b/tests/protocols/transport/test_tcp_options_roundtrip_unit.py @@ -0,0 +1,104 @@ +"""#1151 -- ``from_data`` re-emits a parsed TCP option list octet for octet. + +:meth:`TCP._make_tcp_options ` +keeps the ``NOP`` and ``EOOL`` options it is given, in place, and pads only the +end of the list to a 32-bit boundary. A parsed segment therefore rebuilds with +the same options in the same order and the same data offset. + +The option lists below are copied from the SYN segments of the fixture captures, +so the module reads no capture and runs on a fresh checkout. + +""" +from __future__ import annotations + +import importlib.util +import unittest + +from tests._support import reimport_once_per_class + +RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') +HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) + +#: TCP option lists as captured, keyed by where they were seen. +CAPTURED_OPTIONS = { + # MSS, SACK-permitted, timestamps, NOP, window scale: 20 octets. + 'http.pcap': '020405b40402080a31d626a00000000001030306', + # MSS, NOP, window scale, NOP, NOP, timestamps, SACK-permitted, EOOL and + # one octet of zero padding after it: 24 octets. + 'tcp.pcap': '020405b4010303060101080a31bcd7fd92520e1704020000', + # NOP, NOP, timestamps: 12 octets. + 'stream.pcap': '0101080a31d42b65925ed8b7', +} + + +def segment(options: 'bytes') -> 'bytes': + """Return a TCP header carrying ``options`` and no payload. + + The ports are not registered to any application protocol, so the rebuild + does not depend on a payload parser. + + """ + offset = (20 + len(options)) // 4 + return (b'\x30\x39\x30\x3a' # ports 12345 -> 12346 + + bytes.fromhex('0000000100000000') + + bytes([offset << 4, 0x02]) # data offset, SYN + + b'\xff\xff\x00\x00\x00\x00' # window, checksum, urgent pointer + + options) + + +@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed') +class TCPOptionsRoundTripUnitTests(unittest.TestCase): + """TCP option lists survive a parse and ``from_data`` rebuild unchanged.""" + + def setUp(self) -> None: + reimport_once_per_class(self) + + def test_from_data_reproduces_captured_options(self) -> None: + """A parsed segment rebuilds to its own octets, data offset included.""" + from pcapkit.protocols.transport.tcp import TCP + + for source, options in CAPTURED_OPTIONS.items(): + with self.subTest(source=source): + raw = segment(bytes.fromhex(options)) + rebuilt = TCP.from_data(TCP(raw).info) + self.assertEqual(rebuilt.data.hex(), raw.hex()) + + def test_from_data_keeps_option_order(self) -> None: + """The rebuilt segment parses to the same option kinds, in the same order.""" + from pcapkit.protocols.transport.tcp import TCP + + for source, options in CAPTURED_OPTIONS.items(): + with self.subTest(source=source): + parsed = TCP(segment(bytes.fromhex(options))).info.options + rebuilt = TCP.from_data(TCP(segment(bytes.fromhex(options))).info).info.options + self.assertEqual([code for code, _ in rebuilt.items(multi=True)], + [code for code, _ in parsed.items(multi=True)]) + + def test_make_pads_the_end_of_the_list_only(self) -> None: + """Options built from scratch are padded once, after the last option.""" + from pcapkit.const.tcp.option import Option as Enum_Option + from pcapkit.protocols.transport.tcp import TCP + + tcp = TCP(srcport=12345, dstport=12346, options=[ + (Enum_Option.Window_Scale, {'shift': 7}), + (Enum_Option.Maximum_Segment_Size, {'mss': 1460}), + ]) + self.assertEqual(tcp.data[12] >> 4, 7) + self.assertEqual(tcp.data[20:].hex(), '030307020405b400') + + def test_make_keeps_explicit_nop_options(self) -> None: + """``NOP`` options passed to ``make()`` are emitted where they were given.""" + from pcapkit.const.tcp.option import Option as Enum_Option + from pcapkit.protocols.transport.tcp import TCP + + tcp = TCP(srcport=12345, dstport=12346, options=[ + (Enum_Option.No_Operation, {}), + (Enum_Option.No_Operation, {}), + (Enum_Option.Timestamps, {'tsval': 1, 'tsecr': 2}), + ]) + self.assertEqual(tcp.data[12] >> 4, 8) + self.assertEqual(tcp.data[20:].hex(), '0101080a0000000100000002') + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/protocols/transport/test_tcp_udp_unit.py b/tests/protocols/transport/test_tcp_udp_unit.py index 07efc13d81..3223206df9 100644 --- a/tests/protocols/transport/test_tcp_udp_unit.py +++ b/tests/protocols/transport/test_tcp_udp_unit.py @@ -396,7 +396,7 @@ def test_tcp_option_constructors_cover_common_branches(self) -> None: ]) self.assertEqual(tuple_length, 4) self.assertEqual([type(item).__name__ for item in tuple_options], - ['WindowScale', 'EndOfOptionList']) + ['WindowScale', 'NoOperation']) with self.assertRaises(ProtocolError): proto._make_mode_timeout(Option.User_Timeout_Option, timeout=1 << 30) @@ -515,9 +515,11 @@ def test_tcp_option_constructors_cover_data_model_and_mapping_paths(self) -> Non proto._make_mode_nop(Option.No_Operation), proto._make_mode_mss(Option.Maximum_Segment_Size, mss=1460), ]) - self.assertEqual(schema_total, 4) - self.assertEqual(len(schema_options), 1) - self.assertEqual(schema_options[0].mss, 1460) + self.assertEqual(schema_total, 8) + self.assertEqual(schema_options[0], b'\x01') + self.assertEqual([type(item).__name__ for item in schema_options[1:]], + ['NoOperation', 'MaximumSegmentSize', 'NoOperation', 'EndOfOptionList']) + self.assertEqual(schema_options[2].mss, 1460) option_map = OrderedMultiDict([ (Option.No_Operation, @@ -532,7 +534,8 @@ def test_tcp_option_constructors_cover_data_model_and_mapping_paths(self) -> Non ]) mapped_options, mapped_total = proto._make_tcp_options(option_map) self.assertEqual(mapped_total, 12) - self.assertEqual(mapped_options[0].mss, 1300) + self.assertEqual(type(mapped_options[0]).__name__, 'NoOperation') + self.assertEqual(mapped_options[1].mss, 1300) self.assertEqual(type(mapped_options[-1]).__name__, 'EndOfOptionList') # NOTE: the MP_JOIN forms reach ``_make_mode_mp`` through :func:`mptcp_option`,