Skip to content

Commit 31a76d1

Browse files
committed
refactor(schema): share the IPv6-option and byte-order schema helpers (#1519)
- Move the nine HOPOPT / IPv6-Opts option helpers into a new `pcapkit.protocols.schema.internet.ipv6_option`, parameterised by the error prefix and, for the selectors, by the option schemas to return. `hopopt.py` and `ipv6_opts.py` bind them in same-named functions, and use `rpl_opt_sub_tlv_len` and `pad_opt_data_len` unchanged. - Correct the TaggerID values in the `smf_i_dpd_tid_selector` docstring (IPv4 is 2 and IPv6 is 3, not 1 and 2). - Move `byteorder_callback` and `packet_byteorder` into a new `pcapkit.protocols.schema.misc.byteorder`, with the PCAP-NG body, and use it from both the PCAP frame header and PCAP-NG. - Let the callback introspection in `tests/corekit/test_fields_misc.py` (address-typed switches) and `tests/corekit/test_schema_roundtrip_unit.py` (driver fields) read the shared helper a callback delegates to. - Document both shared modules on new docs pages, and point the HOPOPT, IPv6-Opts and PCAP-NG pages at them. Byte-identical on every sample capture; new tests pin each helper, both error prefixes and the `__packet__` fallback.
1 parent b6b95bc commit 31a76d1

22 files changed

Lines changed: 843 additions & 463 deletions

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,15 @@ Type Stubs
220220
Auxiliary Functions
221221
~~~~~~~~~~~~~~~~~~~
222222

223+
The option helpers are shared with
224+
:mod:`~pcapkit.protocols.schema.internet.ipv6_opts` and documented in
225+
:mod:`pcapkit.protocols.schema.internet.ipv6_option`. The functions below bind
226+
them to this module's error prefix and option schemas;
227+
:func:`~pcapkit.protocols.schema.internet.ipv6_option.rpl_opt_sub_tlv_len` and
228+
:func:`~pcapkit.protocols.schema.internet.ipv6_option.pad_opt_data_len` are
229+
used unchanged.
230+
223231
.. autofunction:: pcapkit.protocols.schema.internet.hopopt.mpl_opt_seed_id_len
224-
.. autofunction:: pcapkit.protocols.schema.internet.hopopt.pad_opt_data_len
225232
.. autofunction:: pcapkit.protocols.schema.internet.hopopt.calipso_pad_len
226233
.. autofunction:: pcapkit.protocols.schema.internet.hopopt.mpl_opt_pad_len
227234

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internet layer, with detailed implementation and methods.
2020
ipv6_opts
2121
ipv6_route
2222
hopopt
23+
ipv6_option
2324
ipsec
2425
ah
2526
esp
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
IPv6 Options - Shared Option Helpers
2+
====================================
3+
4+
.. module:: pcapkit.protocols.schema.internet.ipv6_option
5+
6+
:mod:`pcapkit.protocols.schema.internet.ipv6_option` contains the field helpers
7+
shared by the option schemas of
8+
:mod:`~pcapkit.protocols.schema.internet.hopopt` (HOPOPT) and
9+
:mod:`~pcapkit.protocols.schema.internet.ipv6_opts` (IPv6-Opts), since both
10+
headers carry the same IPv6 options [*]_. A helper that reports an error takes
11+
the header's name as ``prefix``, and a selector takes the header's option
12+
schemas. Each header module binds them in a function of the same name, which
13+
is what its fields use.
14+
15+
Auxiliary Functions
16+
-------------------
17+
18+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.rpl_opt_sub_tlv_len
19+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.mpl_opt_seed_id_len
20+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.pad_opt_data_len
21+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.calipso_pad_len
22+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.mpl_opt_pad_len
23+
24+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.smf_dpd_data_selector
25+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.smf_i_dpd_tid_selector
26+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.smf_i_dpd_id_len
27+
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_option.quick_start_data_selector
28+
29+
.. rubric:: Footnotes
30+
31+
.. [*] https://www.rfc-editor.org/rfc/rfc8200#section-4.2

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,15 @@ Type Stubs
220220
Auxiliary Functions
221221
~~~~~~~~~~~~~~~~~~~
222222

223+
The option helpers are shared with
224+
:mod:`~pcapkit.protocols.schema.internet.hopopt` and documented in
225+
:mod:`pcapkit.protocols.schema.internet.ipv6_option`. The functions below bind
226+
them to this module's error prefix and option schemas;
227+
:func:`~pcapkit.protocols.schema.internet.ipv6_option.rpl_opt_sub_tlv_len` and
228+
:func:`~pcapkit.protocols.schema.internet.ipv6_option.pad_opt_data_len` are
229+
used unchanged.
230+
223231
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_opts.mpl_opt_seed_id_len
224-
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_opts.pad_opt_data_len
225232
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_opts.calipso_pad_len
226233
.. autofunction:: pcapkit.protocols.schema.internet.ipv6_opts.mpl_opt_pad_len
227234

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Byte Order - Shared Byte-Order Helpers
2+
======================================
3+
4+
.. module:: pcapkit.protocols.schema.misc.byteorder
5+
6+
:mod:`pcapkit.protocols.schema.misc.byteorder` contains the field callback that
7+
reads a capture file's byte order out of the packet data. It is shared by the
8+
PCAP frame header (:mod:`~pcapkit.protocols.schema.misc.pcap.frame`) and the
9+
PCAP-NG blocks (:mod:`~pcapkit.protocols.schema.misc.pcapng`).
10+
11+
Auxiliary Functions
12+
-------------------
13+
14+
.. autofunction:: pcapkit.protocols.schema.misc.byteorder.byteorder_callback
15+
.. autofunction:: pcapkit.protocols.schema.misc.byteorder.packet_byteorder

‎docs/source/pcapkit/protocols/misc/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ indication of empty payload, the PCAP header classes, and the PCAP-NG
1616

1717
pcap
1818
pcapng
19+
byteorder
1920
raw
2021
null

‎docs/source/pcapkit/protocols/misc/pcapng.rst‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,11 @@ Type Stubs
510510
Auxiliary Functions
511511
-------------------
512512

513-
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.byteorder_callback
513+
The byte order of every block but the Section Header Block is read by
514+
:func:`~pcapkit.protocols.schema.misc.byteorder.byteorder_callback`, which is
515+
shared with the PCAP frame header and documented in
516+
:mod:`pcapkit.protocols.schema.misc.byteorder`.
517+
514518
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.shb_byteorder_callback
515519
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.pcapng_block_selector
516520
.. autofunction:: pcapkit.protocols.schema.misc.pcapng.dsb_secrets_selector

‎examples/generators/endian.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
instead of the file's produced the right answer anyway.
1212
1313
That is how GitHub issue #605 survived: ``Frame.unpack`` seeded the byte order
14-
under the key ``bytesorder`` where ``byteorder_callback`` in
15-
:file:`pcapkit/protocols/schema/misc/pcap/frame.py` reads ``byteorder``, so the
14+
under the key ``bytesorder`` where ``byteorder_callback`` -- then in
15+
:file:`pcapkit/protocols/schema/misc/pcap/frame.py`, now in
16+
:file:`pcapkit/protocols/schema/misc/byteorder.py` -- reads ``byteorder``, so the
1617
lookup always missed and always fell back to :data:`sys.byteorder`. Reading a
1718
big-endian capture then byte-swapped ``ts_sec``, ``ts_usec``, ``incl_len`` and
1819
``orig_len`` -- and because ``incl_len`` is the payload length, the first record

‎pcapkit/corekit/fields/misc.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def nested_packet_context(packet: 'dict[str, Any]') -> 'dict[str, Any]':
567567
reserved ``__packet__`` key, for a callback that needs to name the
568568
outer schema specifically rather than whichever schema happens to
569569
declare a given field -- see
570-
:func:`pcapkit.protocols.schema.misc.pcapng.packet_byteorder` and
570+
:func:`pcapkit.protocols.schema.misc.byteorder.packet_byteorder` and
571571
:meth:`~pcapkit.protocols.schema.misc.pcapng.BlockType.post_process`
572572
for why that distinction matters, and note that both already
573573
hand-roll this exact fallback and so are unaffected by (and do not

0 commit comments

Comments
 (0)