Skip to content

Commit 7b4419e

Browse files
authored
fix(ipx): stop dispatching IPX payloads through the IP protocol registry (#2169) (#2187)
1 parent c210bba commit 7b4419e

3 files changed

Lines changed: 155 additions & 3 deletions

File tree

‎docs/source/pcapkit/protocols/internet/ipx.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Octets Bits Name Description
3737

3838
.. automethod:: __index__
3939

40+
.. autoattribute:: __proto__
41+
:no-value:
42+
4043
Header Schemas
4144
--------------
4245

‎pcapkit/protocols/internet/ipx.py‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@
2424
.. [*] https://en.wikipedia.org/wiki/Internetwork_Packet_Exchange
2525
2626
"""
27+
import collections
2728
import textwrap
2829
from typing import TYPE_CHECKING
2930

3031
from pcapkit.const.ipx.packet import Packet as Enum_Packet
3132
from pcapkit.const.ipx.socket import Socket as Enum_Socket
3233
from pcapkit.const.reg.transtype import TransType as Enum_TransType
34+
from pcapkit.corekit.module import ModuleDescriptor
3335
from pcapkit.protocols.data.internet.ipx import IPX as Data_IPX
3436
from pcapkit.protocols.data.internet.ipx import Address as Data_Address
3537
from pcapkit.protocols.internet.internet import Internet
3638
from pcapkit.protocols.schema.internet.ipx import IPX as Schema_IPX
3739

3840
if TYPE_CHECKING:
3941
from enum import IntEnum as StdlibEnum
40-
from typing import Any, Optional, Type
42+
from typing import Any, DefaultDict, Optional, Type
4143

4244
from aenum import IntEnum as AenumEnum
4345
from typing_extensions import Literal
@@ -61,8 +63,31 @@ class IPX(Internet[Data_IPX, Schema_IPX],
6163
:attr:`~pcapkit.protocols.schema.internet.ipx.IPX.trailer`
6264
(:issue:`1433`).
6365
66+
The payload is dissected by IPX Packet Type from a registry of its own,
67+
:attr:`__proto__`, rather than looked up as an IP protocol number
68+
(:issue:`2169`).
69+
6470
"""
6571

72+
##########################################################################
73+
# Defaults.
74+
##########################################################################
75+
76+
#: Protocol index mapping for decoding next layer, see
77+
#: :meth:`self._decode_next_layer
78+
#: <pcapkit.protocols.internet.internet.Internet._decode_next_layer>` &
79+
#: :meth:`self._import_next_layer
80+
#: <pcapkit.protocols.internet.internet.Internet._import_next_layer>`. Keyed by
81+
#: IPX Packet Type, as in :class:`~pcapkit.const.ipx.packet.Packet`, rather
82+
#: than by IP protocol number, so this class keeps a registry of its own
83+
#: instead of sharing :attr:`Internet.__proto__
84+
#: <pcapkit.protocols.internet.internet.Internet.__proto__>` (:issue:`2169`).
85+
#: No IPX-native protocol is shipped, so the payload is
86+
#: :class:`~pcapkit.protocols.misc.raw.Raw` unless one is registered.
87+
__proto__ = collections.defaultdict(
88+
lambda: ModuleDescriptor('pcapkit.protocols.misc.raw', 'Raw'),
89+
) # type: DefaultDict[int, ModuleDescriptor[ProtocolBase] | Type[ProtocolBase]]
90+
6691
##########################################################################
6792
# Properties.
6893
##########################################################################
@@ -78,8 +103,8 @@ def length(self) -> 'Literal[30]':
78103
return 30
79104

80105
@property
81-
def protocol(self) -> 'Enum_TransType':
82-
"""Name of next layer protocol."""
106+
def protocol(self) -> 'Enum_Packet':
107+
"""IPX Packet Type of next layer protocol."""
83108
return self._info.type
84109

85110
@property
@@ -234,6 +259,20 @@ def _make_data(cls, data: 'Data_IPX') -> 'dict[str, Any]': # type: ignore[overr
234259
'trailer': data.get('trailer', b''),
235260
}
236261

262+
def _next_exthdr_depth(self, proto: 'Optional[int]', extension: 'bool' = False) -> 'int': # pylint: disable=unused-argument
263+
"""Place the next layer in its chain of IPv6 extension headers.
264+
265+
Arguments:
266+
proto: next layer IPX Packet Type
267+
extension: if the next layer is parsed as an extension header
268+
269+
Returns:
270+
Always ``0``: an IPX Packet Type is not an IP protocol number, so it
271+
never names an IPv6 extension header (:issue:`2169`).
272+
273+
"""
274+
return 0
275+
237276
@staticmethod
238277
def _make_ipx_address(addr: 'Data_Address') -> 'bytes':
239278
"""Make IPX address field.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# -*- coding: utf-8 -*-
2+
"""An IPX payload is dissected by IPX Packet Type, not as an IP protocol number.
3+
4+
GitHub issue #2169: :meth:`IPX.read <pcapkit.protocols.internet.ipx.IPX.read>`
5+
handed the IPX Packet Type to the next-layer lookup it inherits from
6+
:class:`~pcapkit.protocols.internet.internet.Internet`, whose registry is keyed
7+
by IP protocol number. So Packet Type 17 (NCP) parsed as UDP, 4 (PEP) as IPv4
8+
and 6 as TCP; and Packet Type 0 counted as an IPv6 Hop-by-Hop Options header
9+
towards :data:`~pcapkit.utilities.config.EXTENSION_HEADER_LIMIT`.
10+
11+
:class:`~pcapkit.protocols.internet.ipx.IPX` keeps a registry of its own, keyed
12+
by Packet Type, and its payload is Raw unless an IPX-native protocol is
13+
registered there.
14+
15+
Every case builds its own octets in memory and reads no capture. Classes are
16+
imported inside each test, after :func:`~tests._support.reimport_once_per_class`.
17+
18+
"""
19+
20+
import io
21+
import unittest
22+
import warnings
23+
from unittest import mock
24+
25+
from tests._support import reimport_once_per_class
26+
27+
#: Destination and source IPX addresses.
28+
ADDRS = bytes(range(1, 25))
29+
30+
#: An IPv4 header carrying nothing, which parses as IPv4 when looked up by IP protocol number 4.
31+
IPV4 = bytes.fromhex('4500' '0014' '0000' '0000' '4011' '0000' '0a000001' '0a000002')
32+
#: A TCP header carrying nothing, which parses as TCP when looked up by IP protocol number 6.
33+
TCP = bytes.fromhex('0050' '1f90' '00000001' '00000000' '5002' 'ffff' '0000' '0000')
34+
#: An 8-octet payload, which parses as UDP when looked up by IP protocol number 17.
35+
UDP = bytes.fromhex('0035' '0035' '0008' '0000')
36+
37+
38+
def _ipx(packet_type: int, payload: bytes) -> bytes:
39+
"""Return an IPX packet of ``packet_type`` carrying ``payload``."""
40+
length = 30 + len(payload)
41+
return b'\xff\xff' + length.to_bytes(2, 'big') + b'\x00' + bytes([packet_type]) + ADDRS + payload
42+
43+
44+
#: Packet Types that share a number with a dissected IP protocol, and a payload
45+
#: that protocol would parse.
46+
PACKETS = {
47+
'NCP-17': _ipx(17, UDP),
48+
'PEP-4': _ipx(4, IPV4),
49+
'type-6': _ipx(6, TCP),
50+
}
51+
52+
53+
class TestIPXNextLayer(unittest.TestCase):
54+
"""Pin the IPX next-layer dispatch to its own Packet Type registry."""
55+
56+
def setUp(self) -> None:
57+
reimport_once_per_class(self)
58+
59+
def test_payload_is_raw_and_rebuilds_byte_for_byte(self) -> None:
60+
from pcapkit.protocols.internet.ipx import IPX
61+
from pcapkit.protocols.misc.raw import Raw
62+
63+
for name, packet in PACKETS.items():
64+
with self.subTest(packet=name), warnings.catch_warnings():
65+
warnings.simplefilter('ignore')
66+
parsed = IPX(io.BytesIO(packet), len(packet))
67+
self.assertIs(type(parsed.payload), Raw)
68+
self.assertEqual(IPX.from_data(parsed.info).data, packet)
69+
70+
def test_registered_ipx_protocol_is_dissected(self) -> None:
71+
from pcapkit.const.ipx.packet import Packet
72+
from pcapkit.protocols.internet.internet import Internet
73+
from pcapkit.protocols.internet.ipx import IPX
74+
from pcapkit.protocols.transport.udp import UDP as Proto_UDP
75+
76+
self.assertIsNot(IPX.__proto__, Internet.__proto__)
77+
packet = PACKETS['NCP-17']
78+
with mock.patch.dict(IPX.__proto__, {Packet.NCP: Proto_UDP}), warnings.catch_warnings():
79+
warnings.simplefilter('ignore')
80+
parsed = IPX(io.BytesIO(packet), len(packet))
81+
self.assertIs(type(parsed.payload), Proto_UDP)
82+
83+
def test_packet_type_zero_is_no_extension_header(self) -> None:
84+
from pcapkit.protocols.internet.ipx import IPX
85+
from pcapkit.protocols.misc.raw import Raw
86+
from pcapkit.utilities import config
87+
from pcapkit.utilities.warnings import ProtocolWarning
88+
89+
packet = _ipx(0, b'abcd')
90+
with mock.patch.object(config, 'EXTENSION_HEADER_LIMIT', 0), warnings.catch_warnings(record=True) as caught:
91+
warnings.simplefilter('always')
92+
parsed = IPX(io.BytesIO(packet), len(packet))
93+
self.assertIs(type(parsed.payload), Raw)
94+
self.assertIsNone(parsed.payload.info.error)
95+
self.assertEqual([w for w in caught if issubclass(w.category, ProtocolWarning)], [])
96+
97+
def test_protocol_is_the_packet_type(self) -> None:
98+
from pcapkit.const.ipx.packet import Packet
99+
from pcapkit.protocols.internet.ipx import IPX
100+
101+
packet = PACKETS['NCP-17']
102+
with warnings.catch_warnings():
103+
warnings.simplefilter('ignore')
104+
parsed = IPX(io.BytesIO(packet), len(packet))
105+
self.assertIs(parsed.protocol, Packet.NCP)
106+
self.assertEqual(IPX.protocol.fget.__annotations__['return'], 'Enum_Packet')
107+
108+
109+
if __name__ == '__main__':
110+
unittest.main()

0 commit comments

Comments
 (0)