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
8 changes: 6 additions & 2 deletions pcapkit/protocols/data/internet/hopopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ class RPLFlags(Data):
rank_err: 'bool'
#: Forwarding error flag.
fwd_err: 'bool'
#: Reserved flag bits, carried verbatim so that a rebuild reproduces them.
reserved: 'int'

if TYPE_CHECKING:
def __init__(self, down: 'bool', rank_err: 'bool', fwd_err: 'bool') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long
def __init__(self, down: 'bool', rank_err: 'bool', fwd_err: 'bool', reserved: 'int') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long


@info_final
Expand All @@ -241,9 +243,11 @@ class RPLOption(Option):
id: 'int'
#: Sender rank.
rank: 'int'
#: Sub-TLVs [:rfc:`6553#section-3`], kept as raw octets.
sub_tlvs: 'bytes'

if TYPE_CHECKING:
def __init__(self, type: 'Enum_Option', action: 'int', change: 'bool', length: 'int', flags: 'RPLFlags', id: 'int', rank: 'int') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long
def __init__(self, type: 'Enum_Option', action: 'int', change: 'bool', length: 'int', flags: 'RPLFlags', id: 'int', rank: 'int', sub_tlvs: 'bytes') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long


@info_final
Expand Down
8 changes: 6 additions & 2 deletions pcapkit/protocols/data/internet/ipv6_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ class RPLFlags(Data):
rank_err: 'bool'
#: Forwarding error flag.
fwd_err: 'bool'
#: Reserved flag bits, carried verbatim so that a rebuild reproduces them.
reserved: 'int'

if TYPE_CHECKING:
def __init__(self, down: 'bool', rank_err: 'bool',
fwd_err: 'bool') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long
fwd_err: 'bool', reserved: 'int') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long


@info_final
Expand All @@ -252,10 +254,12 @@ class RPLOption(Option):
id: 'int'
#: Sender rank.
rank: 'int'
#: Sub-TLVs [:rfc:`6553#section-3`], kept as raw octets.
sub_tlvs: 'bytes'

if TYPE_CHECKING:
def __init__(self, type: 'Enum_Option', action: 'int', change: 'bool', length: 'int', flags: 'RPLFlags', id: 'int',
rank: 'int') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long
rank: 'int', sub_tlvs: 'bytes') -> 'None': ... # pylint: disable=super-init-not-called,unused-argument,redefined-builtin,multiple-statements,line-too-long


@info_final
Expand Down
28 changes: 25 additions & 3 deletions pcapkit/protocols/internet/hopopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,9 @@ def _read_opt_rpl(self, schema: 'Schema_RPLOption', *, options: 'Option') -> 'Da
| (sub-TLVs) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

The sub-TLVs, which :rfc:`6553#section-3` says MUST be skipped when
unrecognised, are kept as raw octets in ``sub_tlvs``.

Args:
schema: parsed parameter schema
options: extracted HOPOPT options
Expand All @@ -1000,10 +1003,10 @@ def _read_opt_rpl(self, schema: 'Schema_RPLOption', *, options: 'Option') -> 'Da
Parsed option data.

Raises:
ProtocolError: If ``schema.len`` is **NOT** ``4``.
ProtocolError: If ``schema.len`` is less than ``4``.

"""
if schema.len != 4:
if schema.len < 4:
raise ProtocolError(f'{self.alias}: [OptNo {schema.type}] invalid format')

opt = Data_RPLOption(
Expand All @@ -1015,9 +1018,11 @@ def _read_opt_rpl(self, schema: 'Schema_RPLOption', *, options: 'Option') -> 'Da
down=bool(schema.flags['down']),
rank_err=bool(schema.flags['rank_err']),
fwd_err=bool(schema.flags['fwd_err']),
reserved=schema.flags['reserved'],
),
id=schema.id,
rank=schema.rank,
sub_tlvs=schema.sub_tlvs,
)
return opt

Expand Down Expand Up @@ -1815,6 +1820,8 @@ def _make_opt_rpl(self, code: 'Enum_Option', opt: 'Optional[Data_RPLOption]' = N
fwd_err: 'bool' = False,
id: 'int' = 0,
rank: 'int' = 0,
reserved: 'int' = 0,
sub_tlvs: 'bytes' = b'',
**kwargs: 'Any') -> 'Schema_RPLOption':
"""Make HOPOPT RPL option.

Expand All @@ -1826,29 +1833,44 @@ def _make_opt_rpl(self, code: 'Enum_Option', opt: 'Optional[Data_RPLOption]' = N
fwd_err: forwarding error flag
id: RPL instance ID
rank: sender rank
reserved: reserved flag bits, i.e. the low five bits of the flags octet
sub_tlvs: sub-TLVs, as raw octets
**kwargs: arbitrary keyword arguments

Returns:
Constructed option schema.

Raises:
ProtocolError: If ``reserved`` does not fit in five bits, or
``sub_tlvs`` does not fit in the option.

"""
if opt is not None:
down = opt.flags.down
rank_err = opt.flags.rank_err
fwd_err = opt.flags.fwd_err
reserved = opt.flags.reserved
id = opt.id
rank = opt.rank
sub_tlvs = opt.sub_tlvs

if not 0 <= reserved < 32:
raise ProtocolError(f'{self.alias}: [OptNo {code}] invalid RPL reserved flags: {reserved}')
if len(sub_tlvs) > 251:
raise ProtocolError(f'{self.alias}: [OptNo {code}] too long RPL sub-TLVs: {len(sub_tlvs)}')

return Schema_RPLOption(
type=code,
len=4,
len=4 + len(sub_tlvs),
flags={
'down': down,
'rank_err': rank_err,
'fwd_err': fwd_err,
'reserved': reserved,
},
id=id,
rank=rank,
sub_tlvs=sub_tlvs,
)

def _make_opt_mpl(self, code: 'Enum_Option', opt: 'Optional[Data_MPLOption]' = None, *,
Expand Down
28 changes: 25 additions & 3 deletions pcapkit/protocols/internet/ipv6_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,9 @@ def _read_opt_rpl(self, schema: 'Schema_RPLOption', *, options: 'Option') -> 'Da
| (sub-TLVs) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

The sub-TLVs, which :rfc:`6553#section-3` says MUST be skipped when
unrecognised, are kept as raw octets in ``sub_tlvs``.

Args:
schema: parsed parameter schema
options: extracted IPv6-Opts options
Expand All @@ -989,10 +992,10 @@ def _read_opt_rpl(self, schema: 'Schema_RPLOption', *, options: 'Option') -> 'Da
Parsed option data.

Raises:
ProtocolError: If ``schema.len`` is **NOT** ``4``.
ProtocolError: If ``schema.len`` is less than ``4``.

"""
if schema.len != 4:
if schema.len < 4:
raise ProtocolError(f'{self.alias}: [OptNo {schema.type}] invalid format')

opt = Data_RPLOption(
Expand All @@ -1004,9 +1007,11 @@ def _read_opt_rpl(self, schema: 'Schema_RPLOption', *, options: 'Option') -> 'Da
down=bool(schema.flags['down']),
rank_err=bool(schema.flags['rank_err']),
fwd_err=bool(schema.flags['fwd_err']),
reserved=schema.flags['reserved'],
),
id=schema.id,
rank=schema.rank,
sub_tlvs=schema.sub_tlvs,
)
return opt

Expand Down Expand Up @@ -1804,6 +1809,8 @@ def _make_opt_rpl(self, code: 'Enum_Option', opt: 'Optional[Data_RPLOption]' = N
fwd_err: 'bool' = False,
id: 'int' = 0,
rank: 'int' = 0,
reserved: 'int' = 0,
sub_tlvs: 'bytes' = b'',
**kwargs: 'Any') -> 'Schema_RPLOption':
"""Make IPv6-Opts RPL option.

Expand All @@ -1815,29 +1822,44 @@ def _make_opt_rpl(self, code: 'Enum_Option', opt: 'Optional[Data_RPLOption]' = N
fwd_err: forwarding error flag
id: RPL instance ID
rank: sender rank
reserved: reserved flag bits, i.e. the low five bits of the flags octet
sub_tlvs: sub-TLVs, as raw octets
**kwargs: arbitrary keyword arguments

Returns:
Constructed option schema.

Raises:
ProtocolError: If ``reserved`` does not fit in five bits, or
``sub_tlvs`` does not fit in the option.

"""
if opt is not None:
down = opt.flags.down
rank_err = opt.flags.rank_err
fwd_err = opt.flags.fwd_err
reserved = opt.flags.reserved
id = opt.id
rank = opt.rank
sub_tlvs = opt.sub_tlvs

if not 0 <= reserved < 32:
raise ProtocolError(f'{self.alias}: [OptNo {code}] invalid RPL reserved flags: {reserved}')
if len(sub_tlvs) > 251:
raise ProtocolError(f'{self.alias}: [OptNo {code}] too long RPL sub-TLVs: {len(sub_tlvs)}')

return Schema_RPLOption(
type=code,
len=4,
len=4 + len(sub_tlvs),
flags={
'down': down,
'rank_err': rank_err,
'fwd_err': fwd_err,
'reserved': reserved,
},
id=id,
rank=rank,
sub_tlvs=sub_tlvs,
)

def _make_opt_mpl(self, code: 'Enum_Option', opt: 'Optional[Data_MPLOption]' = None, *,
Expand Down
22 changes: 21 additions & 1 deletion pcapkit/protocols/schema/internet/hopopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class RPLFlags(TypedDict):
rank_err: int
#: Forwarding error flag.
fwd_err: int
#: Reserved flag bits.
reserved: int

class MPLFlags(TypedDict):
"""MPL flags."""
Expand Down Expand Up @@ -119,6 +121,21 @@ class QSNonce(TypedDict):
nonce: int


def rpl_opt_sub_tlv_len(pkt: 'dict[str, Any]') -> 'int':
"""Return RPL option sub-TLV length.

Args:
pkt: RPL option unpacked schema.

Returns:
RPL option sub-TLV length, i.e. ``Opt Data Len`` less the four octets
of the fixed fields. An ``Opt Data Len`` below ``4`` gives ``0``, and
the reader then rejects the option.

"""
return max(pkt['len'] - 4, 0)


def mpl_opt_seed_id_len(pkt: 'dict[str, Any]') -> 'int':
"""Return MPL Seed-ID length.

Expand Down Expand Up @@ -667,15 +684,18 @@ class RPLOption(Option, code=[Enum_Option.RPL_Option_0x23,
'down': (0, 1),
'rank_err': (1, 1),
'fwd_err': (2, 1),
'reserved': (3, 5),
})
#: RPL instance ID.
id: 'int' = UInt8Field()
#: Sender rank.
rank: 'int' = UInt16Field()
#: Sub-TLVs, kept as raw octets.
sub_tlvs: 'bytes' = BytesField(length=rpl_opt_sub_tlv_len)

if TYPE_CHECKING:
def __init__(self, type: 'Enum_Option', len: 'int', flags: 'RPLFlags', id: 'int',
rank: 'int') -> 'None': ...
rank: 'int', sub_tlvs: 'bytes') -> 'None': ...


@schema_final
Expand Down
22 changes: 21 additions & 1 deletion pcapkit/protocols/schema/internet/ipv6_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class RPLFlags(TypedDict):
rank_err: int
#: Forwarding error flag.
fwd_err: int
#: Reserved flag bits.
reserved: int

class MPLFlags(TypedDict):
"""MPL flags."""
Expand Down Expand Up @@ -119,6 +121,21 @@ class QSNonce(TypedDict):
nonce: int


def rpl_opt_sub_tlv_len(pkt: 'dict[str, Any]') -> 'int':
"""Return RPL option sub-TLV length.

Args:
pkt: RPL option unpacked schema.

Returns:
RPL option sub-TLV length, i.e. ``Opt Data Len`` less the four octets
of the fixed fields. An ``Opt Data Len`` below ``4`` gives ``0``, and
the reader then rejects the option.

"""
return max(pkt['len'] - 4, 0)


def mpl_opt_seed_id_len(pkt: 'dict[str, Any]') -> 'int':
"""Return MPL Seed-ID length.

Expand Down Expand Up @@ -672,15 +689,18 @@ class RPLOption(Option, code=[Enum_Option.RPL_Option_0x23,
'down': (0, 1),
'rank_err': (1, 1),
'fwd_err': (2, 1),
'reserved': (3, 5),
})
#: RPL instance ID.
id: 'int' = UInt8Field()
#: Sender rank.
rank: 'int' = UInt16Field()
#: Sub-TLVs, kept as raw octets.
sub_tlvs: 'bytes' = BytesField(length=rpl_opt_sub_tlv_len)

if TYPE_CHECKING:
def __init__(self, type: 'Enum_Option', len: 'int', flags: 'RPLFlags', id: 'int',
rank: 'int') -> 'None': ...
rank: 'int', sub_tlvs: 'bytes') -> 'None': ...


@schema_final
Expand Down
9 changes: 5 additions & 4 deletions tests/protocols/internet/test_ipv6_extension_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,9 @@ def _assert_option_data_model_constructor_edges(self, protocol_cls: type) -> Non
rpl = proto._make_opt_rpl(
Option.RPL_Option_0x63,
data.RPLOption(type=Option.RPL_Option_0x63, length=6,
flags=data.RPLFlags(down=True, rank_err=True, fwd_err=False),
id=5, rank=6, **base),
flags=data.RPLFlags(down=True, rank_err=True, fwd_err=False,
reserved=0),
id=5, rank=6, sub_tlvs=b'', **base),
)
self.assertTrue(rpl.flags['down'])
mpl = proto._make_opt_mpl(
Expand Down Expand Up @@ -1222,8 +1223,8 @@ def assert_bad(reader, option_schema) -> None:

rpl = proto._read_opt_rpl(
schema.RPLOption(type=Option.RPL_Option_0x63, len=4,
flags={'down': 1, 'rank_err': 0, 'fwd_err': 1},
id=9, rank=10),
flags={'down': 1, 'rank_err': 0, 'fwd_err': 1, 'reserved': 0},
id=9, rank=10, sub_tlvs=b''),
options=options,
)
self.assertTrue(rpl.flags.down)
Expand Down
Loading
Loading