IP Address Fields¶
IP Addresses¶
- class pcapkit.corekit.fields.ipaddress.IPv4AddressField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv4AddressField.<lambda>>)[source]¶
Bases:
_IPAddressField[IPv4Address]IPv4 address value for protocol fields.
- Parameters:
default (
IPv4Address|NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
- class pcapkit.corekit.fields.ipaddress.IPv6AddressField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv6AddressField.<lambda>>)[source]¶
Bases:
_IPAddressField[IPv6Address]IPv6 address value for protocol fields.
- Parameters:
default (
IPv6Address|NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
IP Interface¶
- class pcapkit.corekit.fields.ipaddress.IPv4InterfaceField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv4InterfaceField.<lambda>>)[source]¶
Bases:
_IPInterfaceField[IPv4Interface]IPv4 interface value for protocol fields.
- Parameters:
default (
IPv4Interface|NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
- pre_process(value, packet)[source]¶
Process field value before packing.
- Parameters:
- Return type:
- Returns:
Processed field value.
- Raises:
FieldValueError – If
valueis abool(c.f._reject_bool()), is not a valid IP interface, or is the wrong IP version for this field.
- post_process(value, packet)[source]¶
Process field value after parsing (unpacking).
- Parameters:
- Return type:
- Returns:
Processed field value.
- Raises:
FieldValueError – If the trailing four octets are not a valid dotted netmask, or if the resulting interface is the wrong IP version for this field. The leading four octets cannot actually fail here – they are always exactly 4 octets, fixed by this field’s length, and any such octet string is a valid address – but the conversion is still wrapped for consistency with the rest of this module.
Notes
The trailing four octets are a dotted netmask, as written by
pre_process()– not a prefix length as inIPv6InterfaceField.post_process().
- class pcapkit.corekit.fields.ipaddress.IPv6InterfaceField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv6InterfaceField.<lambda>>)[source]¶
Bases:
_IPInterfaceField[IPv6Interface]IPv6 interface value for protocol fields.
- Parameters:
default (
IPv6Interface|NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
- pre_process(value, packet)[source]¶
Process field value before packing.
- Parameters:
- Return type:
- Returns:
Processed field value.
- Raises:
FieldValueError – If
valueis abool(c.f._reject_bool()), is not a valid IP interface, or is the wrong IP version for this field.
- post_process(value, packet)[source]¶
Process field value after parsing (unpacking).
- Parameters:
- Return type:
- Returns:
Processed field value.
- Raises:
FieldValueError – If the trailing octet is not a valid IPv6 prefix length, i.e. greater than 128, or if the resulting interface is the wrong IP version for this field. Neither the leading sixteen octets nor the final
ipaddress.ip_interface()call can actually fail here – the former is always exactly 16 octets, fixed by this field’s length, and any such octet string is a valid address; the latter is only ever reached once the prefix length has already been checked above, and any prefix length in0..128is valid. Both conversions are still wrapped for consistency with the rest of this module.
Notes
The trailing octet is the prefix length as a binary integer, as written by
pre_process()– not a dotted netmask as inIPv4InterfaceField.post_process().
Construction Helpers¶
- pcapkit.corekit.fields.ipaddress.parse_ip_address(value, description, version=None)[source]¶
Convert a caller-supplied address on the construction path.
- Parameters:
value (
IPv4Address|IPv6Address|bytes|int|str) – Address as the caller gave it – anipaddressobject, which is returned unchanged, or anythingipaddressaccepts.description (
str) – Human-readable description of whatvalueis, used to build theFieldValueErrormessage. Callers in a protocol should carry their usual context into it, e.g.f'{self.alias}: [OptNo {type}] care-of address'.version (
int|None) – IP version to demand,4or6, orNoneto take whichever familyvaluedescribes. Pass it where the wire format fixes the family, so that anintis widened to the right one –258is::102forversion=6but0.0.1.2foripaddress.ip_address().
- Return type:
- Returns:
The converted address.
- Raises:
FieldValueError – If
valueis abool(c.f._reject_bool()), is not a valid IP address, or is not ofversion.
Notes
This is the sanctioned way for a
_make_*method to turn a caller-supplied address into anipaddressobject, and it exists because doing it withipaddress.ip_address()directly is what #508 turned out to be: a_make_*that has to know the address family before it can build the schema – to size an option whose length is the only thing on the wire that carries the family – must convert the argument itself, and that conversion happens before the schema, so it launders aboolinto anIPv4Addressthat #500’s guard in_IPAddressField.pre_process()can then only see as a legitimate address. Seven such call sites tookTrue/Falsewithout complaint as0.0.0.1/0.0.0.0– or::1/::where the wire format fixes the family as IPv6 – and six of them went on to pack those octets. The seventh,TCP._make_mptcp_addaddr, built an equally corrupt schema and is only stopped from packing it by an unrelated defect of its own.Routing every one of them through here rather than giving each its own
isinstance()check is the whole point: #481 added exactly such a check toMH._make_opt_mn_id, and #491 was the same defect surviving at every site that had not been thought of. A guard that has to be remembered per call site is a guard that will be forgotten at the next one.The
boolrejection is the first statement here, ahead of any dispatch on the value’s type, for the placement reason #481 gives and_reject_bool()repeats.This raises
FieldValueErrorand notProtocolError, which is deliberate even though two sibling guards for the same mistake –MH._make_opt_mn_idfrom #481 andESP's SecurityAssociationfrom #491 – raise the latter. The layer decides: this is a field-level conversion, so it answers with what_IPAddressField.pre_process()answers with for the identical value, and a caller sees one exception whether theboolreached the field through the schema or through a_make_*. The two protocol-level guards answer for the option, alongside siblings that are not about addresses at all –_make_opt_mn_idrefuses aboolfor all eight MN-ID subtypes, only one of which is address-typed – so neither can route through here without losing the subtype-aware message that is the point of it. Both exception classes derive fromBaseErrorandValueError, so the difference is invisible toexcept BaseErrorandexcept ValueError, and nothing in the library catches either one specifically.
Internal Definitions¶
- class pcapkit.corekit.fields.ipaddress._IPField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)[source]¶
-
Internal IP related value for protocol fields.
- Parameters:
- class pcapkit.corekit.fields.ipaddress._IPAddressField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)[source]¶
-
Internal IP address value for protocol fields.
- Parameters:
- pre_process(value, packet)[source]¶
Process field value before packing.
- Parameters:
- Return type:
- Returns:
Processed field value.
- Raises:
FieldValueError – If
valueis abool(c.f._reject_bool()), is not a valid IP address, or is the wrong IP version for this field.
- post_process(value, packet)[source]¶
Process field value after parsing (unpacking).
- Parameters:
- Return type:
TypeVar(_AT, IPv4Address, IPv6Address)- Returns:
Processed field value.
- Raises:
FieldValueError – If
valueis the wrong IP version for this field.valuecannot actually fail the underlyingipaddress.ip_address()conversion here – it is always exactly 4 or 16 octets, fixed by this field’s length, and any such octet string is a valid address – but the conversion is still wrapped for consistency with the rest of this module.
- class pcapkit.corekit.fields.ipaddress._IPInterfaceField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)[source]¶
-
Internal IP interface value for protocol fields.
- Parameters:
length (
int|Callable[[dict[str,Any]],int]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.default (
TypeVar(_T) |NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
Type Variables¶
- pcapkit.corekit.fields.ipaddress._T: ipaddress.IPv4Address | ipaddress.IPv6Address | ipaddress.IPv4Interface | ipaddress.IPv6Interface¶
- pcapkit.corekit.fields.ipaddress._AT: ipaddress.IPv4Address | ipaddress.IPv6Address¶
- pcapkit.corekit.fields.ipaddress._IT: ipaddress.IPv4Interface | ipaddress.IPv6Interface¶