Skip to content

Commit 45f80d9

Browse files
committed
fix(hip): Locator flags drop the seven bits other than Preferred on rebuild
- schema: Locator.flags names `reserved: (0, 7)` beside `preferred: (7, 1)`, per RFC 8046 section 4 and the #654 per-site policy. - data: Locator gains `reserved`, read verbatim from the flags octet. - make: `_make_locator` writes `reserved` (from the data model, or the `reserved` keyword, default 0). - new test: tests/protocols/internet/test_hip_locator_flags_unit.py. Closes #1365
1 parent 4849d16 commit 45f80d9

4 files changed

Lines changed: 113 additions & 1 deletion

File tree

‎pcapkit/protocols/data/internet/hip.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,16 @@ class Locator(Data):
177177
length: 'int'
178178
#: Preferred flag.
179179
preferred: 'bool'
180+
#: Reserved bits -- the 7 high-order bits of the flags octet, carried
181+
#: verbatim so that re-serialising reproduces them.
182+
reserved: 'int'
180183
#: Locator lifetime.
181184
lifetime: 'timedelta'
182185
#: Locator data.
183186
locator: 'LocatorData | IPv6Address'
184187

185188
if TYPE_CHECKING:
186-
def __init__(self, traffic: 'int', type: 'int', length: 'int', preferred: 'bool', lifetime: 'timedelta', locator: 'LocatorData | IPv6Address') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,multiple-statements,redefined-builtin,line-too-long
189+
def __init__(self, traffic: 'int', type: 'int', length: 'int', preferred: 'bool', reserved: 'int', lifetime: 'timedelta', locator: 'LocatorData | IPv6Address') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,multiple-statements,redefined-builtin,line-too-long
187190

188191

189192
@info_final

‎pcapkit/protocols/internet/hip.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ class Locator(TypedDict):
247247
type: 'int'
248248
#: Preferred flag.
249249
preferred: 'bool'
250+
#: Reserved bits of the flags octet.
251+
reserved: 'NotRequired[int]'
250252
#: Lifetime.
251253
lifetime: 'timedelta | int'
252254
#: IP address.
@@ -1031,6 +1033,7 @@ def _read_locator(locator: 'Schema_Locator') -> 'Data_LocatorData | IPv6Address'
10311033
type=_loct,
10321034
length=_locl,
10331035
preferred=bool(_resp['preferred']),
1036+
reserved=_resp['reserved'],
10341037
lifetime=datetime.timedelta(seconds=_life),
10351038
locator=_lobj,
10361039
))
@@ -3157,6 +3160,7 @@ def _make_locator(locator: 'Optional[Data_Locator]' = None, *,
31573160
traffic: 'int' = 0,
31583161
type: 'int' = 0,
31593162
preferred: 'bool' = False,
3163+
reserved: 'int' = 0,
31603164
lifetime: 'timedelta | int' = 0,
31613165
ip: 'IPv6Address | bytes | int | str' = '::',
31623166
spi: 'Optional[int]' = None,
@@ -3168,6 +3172,7 @@ def _make_locator(locator: 'Optional[Data_Locator]' = None, *,
31683172
traffic: traffic type
31693173
type: locator type
31703174
preferred: preferred flag
3175+
reserved: the 7 reserved high-order bits of the flags octet
31713176
lifetime: lifetime
31723177
ip: IP address
31733178
spi: SPI
@@ -3193,6 +3198,7 @@ def _make_locator(locator: 'Optional[Data_Locator]' = None, *,
31933198
type = locator.type
31943199
length = locator.length // 4
31953200
preferred = locator.preferred
3201+
reserved = locator.reserved
31963202
lifetime = math.floor(locator.lifetime.total_seconds())
31973203
else:
31983204
# NOTE: Through ``parse_ip_address`` because the locator is packed
@@ -3225,6 +3231,7 @@ def _make_locator(locator: 'Optional[Data_Locator]' = None, *,
32253231
type=type,
32263232
len=length,
32273233
flags={
3234+
'reserved': reserved,
32283235
'preferred': preferred,
32293236
},
32303237
lifetime=lifetime,

‎pcapkit/protocols/schema/internet/hip.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class ControlsType(TypedDict):
9696
class LocatorFlags(TypedDict):
9797
"""Locator flags."""
9898

99+
#: Reserved bits.
100+
reserved: int
99101
#: Preferred flag.
100102
preferred: bool
101103

@@ -594,6 +596,7 @@ class Locator(Schema):
594596
flags: 'LocatorFlags' = BitField(
595597
length=1,
596598
namespace={
599+
'reserved': (0, 7),
597600
'preferred': (7, 1),
598601
},
599602
)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# -*- coding: utf-8 -*-
2+
"""HIP ``LOCATOR_SET`` keeps the seven reserved bits of each locator's flags octet.
3+
4+
GitHub issue #1365. :rfc:`8046#section-4` lays the fourth octet of a locator out
5+
as ``Reserved`` (7 bits) followed by ``P`` (1 bit). ``Locator.flags`` declared only
6+
``preferred: (7, 1)``, so the seven high-order bits were dropped on rebuild: flags
7+
``0xfe`` parsed and rebuilt through ``from_data`` as ``0x00``. The fix names them
8+
``reserved: (0, 7)``, per the #654 per-site policy, and carries them through
9+
:class:`~pcapkit.protocols.data.internet.hip.Locator` and ``make()``.
10+
11+
Every case builds its own octets in memory and reads no capture. Classes are
12+
imported inside each test, after :func:`~tests._support.reimport_once_per_class`.
13+
"""
14+
15+
import unittest
16+
import warnings
17+
18+
from tests._support import reimport_once_per_class
19+
20+
#: Header fields shared by every HIP packet built here.
21+
HIP_BASE = {
22+
'next': 6, 'packet': 1, 'version': 2, 'checksum': b'\x00\x00',
23+
'controls_anonymous': False, 'shit': 0, 'rhit': 0, 'payload': b'',
24+
}
25+
26+
#: Offset of the first locator's flags octet: the 40-octet HIP header, the
27+
#: parameter's ``Type`` and ``Length``, then ``Traffic``, ``Type`` and ``Length``.
28+
FLAGS_OFFSET = 40 + 4 + 3
29+
30+
31+
class TestHIPLocatorFlags(unittest.TestCase):
32+
"""Pin every bit of the locator flags octet across parse, rebuild and make."""
33+
34+
def setUp(self) -> None:
35+
reimport_once_per_class(self)
36+
37+
def _frame(self, flags: int) -> bytes:
38+
"""Build a one-locator ``LOCATOR_SET`` packet whose flags octet is ``flags``."""
39+
from pcapkit.const.hip.parameter import Parameter
40+
from pcapkit.protocols.internet.hip import HIP
41+
42+
octets = bytearray(HIP(
43+
parameters=[(Parameter.LOCATOR_SET,
44+
{'locator_set': [{'ip': '2001:db8::1'}]})],
45+
extension=True, **HIP_BASE).data)
46+
self.assertEqual(octets[FLAGS_OFFSET], 0)
47+
octets[FLAGS_OFFSET] = flags
48+
return bytes(octets)
49+
50+
def _parse(self, frame: bytes): # type: ignore[no-untyped-def]
51+
from pcapkit.protocols.internet.hip import HIP
52+
53+
with warnings.catch_warnings():
54+
# Nested ``Locator`` schemas warn about their placeholder length;
55+
# that is pre-existing noise unrelated to the flags octet.
56+
warnings.simplefilter('ignore')
57+
return HIP(frame, len(frame), extension=True)
58+
59+
def test_from_data_rebuilds_every_flags_bit(self) -> None:
60+
from pcapkit.const.hip.parameter import Parameter
61+
from pcapkit.protocols.internet.hip import HIP
62+
63+
for flags in (0x00, 0x01, 0xfe, 0xff, 0x80, 0x55):
64+
with self.subTest(flags=hex(flags)):
65+
frame = self._frame(flags)
66+
parsed = self._parse(frame)
67+
rebuilt = HIP.from_data(parsed.info)
68+
self.assertEqual(rebuilt.data[FLAGS_OFFSET], flags)
69+
self.assertEqual(rebuilt.data, frame)
70+
71+
locator = parsed.info.parameters[Parameter.LOCATOR_SET].locator_set[0]
72+
self.assertEqual(locator.reserved, flags >> 1)
73+
self.assertEqual(locator.preferred, bool(flags & 1))
74+
75+
def test_make_writes_reserved_from_keywords(self) -> None:
76+
from pcapkit.const.hip.parameter import Parameter
77+
from pcapkit.protocols.internet.hip import HIP
78+
79+
built = HIP(
80+
parameters=[(Parameter.LOCATOR_SET,
81+
{'locator_set': [{'ip': '2001:db8::1', 'reserved': 0x7f,
82+
'preferred': False}]})],
83+
extension=True, **HIP_BASE)
84+
self.assertEqual(built.data[FLAGS_OFFSET], 0xfe)
85+
self.assertEqual(built.data, self._frame(0xfe))
86+
87+
def test_make_defaults_reserved_to_zero(self) -> None:
88+
from pcapkit.const.hip.parameter import Parameter
89+
from pcapkit.protocols.internet.hip import HIP
90+
91+
built = HIP(
92+
parameters=[(Parameter.LOCATOR_SET,
93+
{'locator_set': [{'ip': '2001:db8::1', 'preferred': True}]})],
94+
extension=True, **HIP_BASE)
95+
self.assertEqual(built.data[FLAGS_OFFSET], 0x01)
96+
97+
98+
if __name__ == '__main__':
99+
unittest.main()

0 commit comments

Comments
 (0)