IPv4 - Internet Protocol version 4¶
pcapkit.protocols.internet.ipv4 contains
IPv4 only,
which implements extractor for Internet Protocol
version 4 (IPv4) [*], whose structure is described
as below:
Octets |
Bits |
Name |
Description |
|---|---|---|---|
0 |
0 |
|
Version ( |
0 |
4 |
|
Internet Header Length (IHL) |
1 |
8 |
|
Precedence |
1 |
11 |
|
Delay |
1 |
12 |
|
Throughput |
1 |
13 |
|
Reliability |
1 |
14 |
|
Explicit Congestion Notification (ECN) |
2 |
16 |
|
Total Length |
4 |
32 |
|
Identification |
6 |
48 |
Reserved Bit (must be |
|
6 |
49 |
|
Don’t Fragment (DF) |
6 |
50 |
|
More Fragments (MF) |
6 |
51 |
|
Fragment Offset |
8 |
64 |
|
Time To Live (TTL) |
9 |
72 |
|
Protocol (Transport Layer) |
10 |
80 |
|
Header Checksum |
12 |
96 |
|
Source IP Address |
16 |
128 |
|
Destination IP Address |
20 |
160 |
|
IP Options (if IHL > |
- class pcapkit.protocols.internet.ipv4.IPv4(file=None, length=None, **kwargs)[source]¶
-
This class implements Internet Protocol version 4.
This class parses the following IPv4 options, which are registered in the
self.__option__attribute:Option Code
Option Parser
Option Constructor
Changed in version 1.5.0:
Options dispatch through
__option__rather than by method name (#429).A rebuild keeps the fragment offset in 8-octet units, and a header without options rebuilds (#494).
A rebuild from
from_data()keeps the Total Length a truncated capture declares (#1155), and the octets past it astrailer(#1209).A header length declared past the data raises
ProtocolErroron read (#1404).A rebuild reproduces the option area, the route and timestamp slots, the SEC and TS fields and the reserved bits byte for byte (#1316 to #1322, #1326, #1349, #1391 to #1393).
The TS option’s maker takes an address as a string as well (#1402).
- property src: IPv4Address¶
Source IP address.
- property dst: IPv4Address¶
Destination IP address.
- classmethod register_option(code, meth)[source]¶
Register an option parser.
- Parameters:
code (
OptionNumber) – IPv4 option code.meth (
str|tuple[Callable[[Option,OrderedMultiDict[OptionNumber,Option]],Option],Callable[[OptionNumber,Option|None,Any],Option]]) – Method name or callable to parse and/or construct the option.
- Warns:
RegistryWarning – If a different entry is already registered for
code; it is overwritten. Handing back the entry already stored, or one equal to it, is a silent no-op. The entrycodeshipped with is accepted like any other, so an override can be undone (#1362, #1363).
- read(length=None, *, __packet__=None, **kwargs)[source]¶
Read Internet Protocol version 4 (IPv4).
Structure of IPv4 header [RFC 791]:
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Parameters:
- Return type:
- Returns:
Parsed packet data.
- Raises:
ProtocolError – If the version is not 4, or if
ihldeclares more header than the data holds.
- make(tos_pre=<ToSPrecedence.Routine: 0>, tos_pre_default=None, tos_pre_namespace=None, tos_pre_reversed=False, tos_del=<ToSDelay.NORMAL: 0>, tos_del_default=None, tos_del_namespace=None, tos_del_reversed=False, tos_thr=<ToSThroughput.NORMAL: 0>, tos_thr_default=None, tos_thr_namespace=None, tos_thr_reversed=False, tos_rel=<ToSReliability.NORMAL: 0>, tos_rel_default=None, tos_rel_namespace=None, tos_rel_reversed=False, tos_ecn=<ToSECN.Not_ECT: 0>, tos_ecn_default=None, tos_ecn_namespace=None, tos_ecn_reversed=False, id=0, reserved=0, df=False, mf=False, offset=0, ttl=0, protocol=<TransType.UDP: 17>, protocol_default=None, protocol_namespace=None, protocol_reversed=False, checksum=b'\\x00\\x00', src='127.0.0.1', dst='0.0.0.0', options=None, total_length=None, padding=None, payload=b'', trailer=b'', **kwargs)[source]¶
Make (construct) packet data.
- Parameters:
tos_pre (
ToSPrecedence|IntEnum|IntEnum|int|str) – Precedence of the packet.tos_pre_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – Namespace oftos_pre.tos_pre_reversed (
bool) – If the namespace oftos_preis reversed.tos_del (
ToSDelay|IntEnum|IntEnum|int|str) – Delay of the packet.tos_del_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – Namespace oftos_del.tos_del_reversed (
bool) – If the namespace oftos_delis reversed.tos_thr (
ToSThroughput|IntEnum|IntEnum|int|str) – Throughput of the packet.tos_thr_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – Namespace oftos_thr.tos_thr_reversed (
bool) – If the namespace oftos_thris reversed.tos_rel (
ToSReliability|IntEnum|IntEnum|int|str) – Reliability of the packet.tos_rel_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – Namespace oftos_rel.tos_rel_reversed (
bool) – If the namespace oftos_relis reversed.tos_ecn (
ToSECN|IntEnum|IntEnum|int|str) – ECN of the packet.tos_ecn_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – Namespace oftos_ecn.tos_ecn_reversed (
bool) – If the namespace oftos_ecnis reversed.id (
int) – Identification of the packet.reserved (
int) – Reserved flag bit, i.e. the high-order bit of the flags.df (
bool) – Don’t fragment flag.mf (
bool) – More fragments flag.offset (
int) – Fragment offset.protocol (
TransType|IntEnum|IntEnum|int|str) – Payload protocol of the packet.protocol_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – Namespace ofprotocol.protocol_reversed (
bool) – If the namespace ofprotocolis reversed.checksum (
bytes) – Checksum of the packet.src (
IPv4Address|str|int|bytes) – Source address of the packet.dst (
IPv4Address|str|int|bytes) – Destination address of the packet.options (
list[Option|tuple[OptionNumber,dict[str,Any]] |bytes] |OrderedMultiDict[OptionNumber,Option] |None) – Options of the packet.total_length (
int|None) – Total length of the packet, header included; computed from the header and the payload when omitted.padding (
bytes|None) – Octets after the End of Option List, written as is whenoptionsis a container that carries anEOOL, c.f._make_ipv4_options().payload (
bytes|ProtocolBase|Schema) – Payload of the packet.trailer (
bytes) – Octets after the packet, outside the Total Length, such as link-layer padding; written as is.**kwargs (
Any) – Arbitrary keyword arguments.
- Return type:
- Returns:
Constructed packet data.
- _read_ipv4_options(length)[source]¶
Read IPv4 option list.
- Parameters:
length (
int) – length of options- Return type:
- Returns:
Extracted IPv4 options.
- Raises:
ProtocolError – If the parsed options total more than
lengthoctets.
- _read_opt_unassigned(schema, *, options)[source]¶
Read IPv4 unassigned options.
Structure of IPv4 unassigned options [RFC 791]:
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | type | length | option data ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Parameters:
schema (
UnassignedOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis LESS THAN2.
- _read_opt_eool(schema, *, options)[source]¶
Read IPv4 End of Option List (
EOOL) option.Structure of IPv4 End of Option List (
EOOL) option [RFC 791]:+--------+ |00000000| +--------+ Type=0
- Parameters:
schema (
EOOLOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- _read_opt_nop(schema, *, options)[source]¶
Read IPv4 No Operation (
NOP) option.Structure of IPv4 No Operation (
NOP) option [RFC 791]:+--------+ |00000001| +--------+ Type=1
- Parameters:
schema (
NOPOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- _read_opt_sec(schema, *, options)[source]¶
Read IPv4 Security (
SEC) option.Structure of IPv4 Security (
SEC) option [RFC 1108]:+------------+------------+------------+-------------//----------+ | 10000010 | XXXXXXXX | SSSSSSSS | AAAAAAA[1] AAAAAAA0 | | | | | [0] | +------------+------------+------------+-------------//----------+ TYPE = 130 LENGTH CLASSIFICATION PROTECTION LEVEL AUTHORITY FLAGS- Parameters:
schema (
SECOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis LESS THAN3.
Notes
Per RFC 1108 Section 2.4, the low-order bit of each protection authority octet is the field termination indicator:
0on the final octet,1when more octets follow.
- _read_opt_lsr(schema, *, options)[source]¶
Read IPv4 Loose Source Route (
LSR) option.Structure of IPv4 Loose Source Route (
LSR) option [RFC 791]:+--------+--------+--------+---------//--------+ |10000011| length | pointer| route data | +--------+--------+--------+---------//--------+
- Parameters:
schema (
LSROption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If option is malformed.
- _read_opt_ts(schema, *, options)[source]¶
Read IPv4 Time Stamp (
TS) option.Structure of IPv4 Time Stamp (
TS) option [RFC 791]:+--------+--------+--------+--------+ |01000100| length | pointer|oflw|flg| +--------+--------+--------+--------+ | internet address | +--------+--------+--------+--------+ | timestamp | +--------+--------+--------+--------+ | . | . .- Parameters:
schema (
TSOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If the option is malformed.
- _read_opt_e_sec(schema, *, options)[source]¶
Read IPv4 Extended Security (
E-SEC) option.Structure of IPv4 Extended Security (
E-SEC) option [RFC 1108]:+------------+------------+------------+-------//-------+ | 10000101 | 000LLLLL | AAAAAAAA | add sec info | +------------+------------+------------+-------//-------+ TYPE = 133 LENGTH ADDITIONAL ADDITIONAL SECURITY INFO SECURITY FORMAT CODE INFO- Parameters:
schema (
ESECOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis LESS THAN3.
- _read_opt_rr(schema, *, options)[source]¶
Read IPv4 Record Route (
RR) option.Structure of IPv4 Record Route (
RR) option [RFC 791]:+--------+--------+--------+---------//--------+ |00000111| length | pointer| route data | +--------+--------+--------+---------//--------+ Type=7
- Parameters:
schema (
RROption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If option is malformed.
- _read_opt_sid(schema, *, options)[source]¶
Read IPv4 Stream ID (
SID) option.Structure of IPv4 Stream ID (
SID) option [RFC 791][RFC 6814]:+--------+--------+--------+--------+ |10001000|00000010| Stream ID | +--------+--------+--------+--------+ Type=136 Length=4
- Parameters:
schema (
SIDOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis NOT4.
- _read_opt_ssr(schema, *, options)[source]¶
Read IPv4 Strict Source Route (
SSR) option.Structure of IPv4 Strict Source Route (
SSR) option [RFC 791]:+--------+--------+--------+---------//--------+ |10001001| length | pointer| route data | +--------+--------+--------+---------//--------+ Type=137
- Parameters:
schema (
SSROption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If option is malformed.
- _read_opt_mtup(schema, *, options)[source]¶
Read IPv4 MTU Probe (
MTUP) option.Structure of IPv4 MTU Probe (
MTUP) option [RFC 1063][RFC 1191]:+--------+--------+--------+--------+ |00001011|00000100| 2 octet value | +--------+--------+--------+--------+
- Parameters:
schema (
MTUPOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis NOT4.
- _read_opt_mtur(schema, *, options)[source]¶
Read IPv4 MTU Reply (
MTUR) option.Structure of IPv4 MTU Reply (
MTUR) option [RFC 1063][RFC 1191]:+--------+--------+--------+--------+ |00001100|00000100| 2 octet value | +--------+--------+--------+--------+
- Parameters:
schema (
MTUROption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis NOT4.
- _read_opt_tr(schema, *, options)[source]¶
Read IPv4 Traceroute (
TR) option.Structure of IPv4 Traceroute (
TR) option [RFC 1393][RFC 6814]:0 8 16 24 +-+-+-+-+-+-+-+-+---------------+---------------+---------------+ |F| C | Number | Length | ID Number | +-+-+-+-+-+-+-+-+---------------+---------------+---------------+ | Outbound Hop Count | Return Hop Count | +---------------+---------------+---------------+---------------+ | Originator IP Address | +---------------+---------------+---------------+---------------+
- Parameters:
schema (
TROption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis NOT12.
- _read_opt_rtralt(schema, *, options)[source]¶
Read IPv4 Router Alert (
RTRALT) option.Structure of IPv4 Router Alert (
RTRALT) option [RFC 2113]:+--------+--------+--------+--------+ |10010100|00000100| 2 octet value | +--------+--------+--------+--------+
- Parameters:
schema (
RTRALTOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If
lengthis NOT4.
- _read_opt_qs(schema, *, options)[source]¶
Read IPv4 Quick Start (
QS) option.Structure of IPv4 Quick Start (
QS) option [RFC 4782]:A Quick-Start Request
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Option | Length=8 | Func. | Rate | QS TTL | | | | 0000 |Request| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | QS Nonce | R | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Report of Approved Rate
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Option | Length=8 | Func. | Rate | Not Used | | | | 1000 | Report| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | QS Nonce | R | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Parameters:
schema (
QSOption) – parsed option schemaoptions (
OrderedMultiDict[OptionNumber,Option]) – extracted IPv4 options
- Return type:
- Returns:
Parsed option data.
- Raises:
ProtocolError – If the option is malformed.
Note
An option whose QS function is neither of the two above has no defined layout, so the schema keeps it as an unassigned option and it is parsed as one, c.f.
_read_opt_unassigned().
- _make_ipv4_options(options, padding=None)[source]¶
Make options for IPv4.
A
listis a fresh option list, so anyNOPandEOOLin it are dropped and the padding is generated. A container is whatread()hands back, so itsNOPandEOOLoptions are written in place, as parsed.Either way, the options are concatenated and then padded once, after the last one, to the 32-bit boundary: with
NOPoptions and a closingEOOL, or with zero octets if anEOOLhas already ended the list.paddinggoes after thatEOOLverbatim, before any alignment; it is dropped if noEOOLends the list, since the reader would take its octets for options.
- _make_opt_unassigned(kind, option=None, *, data=b'', **kwargs)[source]¶
Make IPv4 unassigned options.
- Parameters:
kind (
OptionNumber) – option type codeoption (
UnassignedOption|None) – option datadata (
bytes) – option payload, i.e. the octets after thelengthoctet**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_eool(kind, option=None, **kwargs)[source]¶
Make IPv4 End of Option List (
EOOL) option.- Parameters:
kind (
OptionNumber) – option type codeoption (
EOOLOption|None) – option data**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_nop(kind, option=None, **kwargs)[source]¶
Make IPv4 No Operation (
NOP) option.- Parameters:
kind (
OptionNumber) – option type code**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_sec(kind, option=None, *, level=<ClassificationLevel.Unclassified: 171>, level_default=None, level_namespace=None, level_reversed=False, authorities=None, **kwargs)[source]¶
Make IPv4 Security (
SEC) option.- Parameters:
kind (
OptionNumber) – option type codelevel (
ClassificationLevel|IntEnum|IntEnum|int|str) – classification levellevel_default (
int|None) – default value for classification levellevel_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – namespace for classification levellevel_reversed (
bool) – whether classification level is reversedauthorities (
list[ProtectionAuthority] |None) – list of protection authority flags**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- Raises:
ProtocolError – If
authoritiesnames a bit position that is not a protection authority – a negative one, or one that RFC 1108 reserves as a field termination indicator.
Notes
RFC 1108 Section 2.4 lays each protection authority octet out as seven authority bits followed by a field termination indicator in the low-order bit:
1means another octet follows,0means this is the last. So the authority numbering skips every position that is a termination bit – 7, 15, 23 – which is what_read_opt_sec()encodes by looping overrange(7)per octet, and the reasonField_Termination_Indicatoris rejected here rather than written: the enumeration names it as structure, and a value written there would be dropped on the way back in. See #537.The indicators themselves are set from position, except that a parsed
optionwhoseterminationcovers every octet written gets back the bits it carried, so that a non-conforming indicator rebuilds as read.
- _make_opt_lsr(kind, option=None, *, counts=10, route=None, remaining=None, **kwargs)[source]¶
Make IPv4 Loose Source and Record Route (
LSR) option.- Parameters:
kind (
OptionNumber) – option type codecounts (
int) – maximum number of addresses to recordroute (
list[IPv4Address|str|bytes|int] |None) – list of IPv4 addresses as recorded routes, i.e. the address slots before the pointerremaining (
list[IPv4Address|str|bytes|int] |None) – list of IPv4 addresses for the address slots at or beyond the pointer; the slots left over up tocountsare zero**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_ts(kind, option=None, *, counts=5, overflow=0, timestamp=None, remaining=None, **kwargs)[source]¶
Make IPv4 Timestamp (
TS) option.- Parameters:
kind (
OptionNumber) – option type codecounts (
int) – maximum number of timestamps to recordoverflow (
int) – overflow count, i.e. the number of modules that could not register a timestamp (RFC 791 Section 3.1)timestamp (
list[int|timedelta] |dict[IPv4Address|str|int,int|timedelta] |None) – list of timestamps, i.e. the slots before the pointer, or a mapping of the addresses to their timestamps; an address may be anythingparse_ip_address()accepts for IPv4remaining (
list[int] |None) – raw 32-bit words for the slots at or beyond the pointer; the slots left over up tocountsare zero**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- Raises:
FieldValueError – If an address is a
boolor is not a valid IPv4 address.
- _make_opt_e_sec(kind, option=None, *, format=0, info=None, **kwargs)[source]¶
Make IPv4 Extended Security (
E-SEC) option.- Parameters:
kind (
OptionNumber) – option type codeoption (
ESECOption|None) – option dataformat (
int) – additional security information format code**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_rr(kind, option=None, *, counts=10, route=None, remaining=None, **kwargs)[source]¶
Make IPv4 Record Route (
RR) option.- Parameters:
kind (
OptionNumber) – option type codecounts (
int) – maximum number of addresses to recordroute (
list[IPv4Address|str|bytes|int] |None) – list of IPv4 addresses as recorded routes, i.e. the address slots before the pointerremaining (
list[IPv4Address|str|bytes|int] |None) – list of IPv4 addresses for the address slots at or beyond the pointer; the slots left over up tocountsare zero**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_ssr(kind, option=None, *, counts=10, route=None, remaining=None, **kwargs)[source]¶
Make IPv4 Strict Source Route (
SSR) option.- Parameters:
kind (
OptionNumber) – option type codecounts (
int) – maximum number of addresses to recordroute (
list[IPv4Address|str|bytes|int] |None) – list of IPv4 addresses as recorded routes, i.e. the address slots before the pointerremaining (
list[IPv4Address|str|bytes|int] |None) – list of IPv4 addresses for the address slots at or beyond the pointer; the slots left over up tocountsare zero**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_mtup(kind, option=None, *, mtu=0, **kwargs)[source]¶
Make IPv4 MTU Probe (
MTUP) option.- Parameters:
kind (
OptionNumber) – option type codeoption (
MTUPOption|None) – option datamtu (
int) – MTU value**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_mtur(kind, option=None, *, mtu=0, **kwargs)[source]¶
Make IPv4 MTU Reply (
MTUR) option.- Parameters:
kind (
OptionNumber) – option type codeoption (
MTUROption|None) – option datamtu (
int) – MTU value**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_tr(kind, option=None, *, id=0, out=0, ret=0, origin='127.0.0.1', **kwargs)[source]¶
Make IPv4 Traceroute (
TR) option.- Parameters:
- Return type:
- Returns:
Constructed option schema.
- _make_opt_rtralt(kind, option=None, *, alert=<RouterAlert.Aggregated_Reservation_Nesting_Level_0: 1>, alert_default=None, alert_namespace=None, alert_reversed=False, **kwargs)[source]¶
Make IPv4 Router Alert (
RTRALT) option.- Parameters:
kind (
OptionNumber) – option type codeoption (
RTRALTOption|None) – option dataalert (
RouterAlert|IntEnum|IntEnum|int|str) – router alert typealert_default (
int|None) – default value for router alert typealert_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – namespace for router alert typealert_reversed (
bool) – whether router alert type is reversed**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema.
- _make_opt_qs(kind, option=None, *, func=<QSFunction.Quick_Start_Request: 0>, func_default=None, func_namespace=None, func_reversed=False, rate=0, ttl=0, unused=0, nonce=0, reserved=0, **kwargs)[source]¶
Make IPv4 Quick-Start (
QS) option.- Parameters:
kind (
OptionNumber) – option type codeoption (
QuickStartRequestOption|QuickStartReportOption|UnassignedOption|None) – option datafunc (
QSFunction|IntEnum|IntEnum|str|int) – QS function typefunc_default (
int|None) – default value for QS function typefunc_namespace (
dict[str,int] |dict[int,str] |Type[IntEnum] |Type[IntEnum] |None) – namespace for QS function typefunc_reversed (
bool) – reversed flag for QS function typerate (
int) – rate (in kbps)unused (
int) – the not-used fourth octet of a Report of Approved Ratenonce (
int) – nonce valuereserved (
int) – reserved bits (R) after the nonce**kwargs (
Any) – arbitrary keyword arguments
- Return type:
- Returns:
Constructed option schema. An unassigned option schema if
optionis a Quick-Start option parsed as unassigned, c.f._read_opt_qs(), or iffuncis neither of the assigned functions, in which case the option takes the eight-octet layout of a Quick-Start Request.- Raises:
ProtocolError – If
funcdoes not fit in four bits.
- __option__: DefaultDict[OptionNumber | int, str | tuple[Callable[[Option, OrderedMultiDict[OptionNumber, Option]], Option], Callable[[OptionNumber, Option | None, Any], Option]]]¶
DefaultDict[Enum_OptionNumber, str | tuple[OptionParser, OptionConstructor]]: Option code to method mapping, c.f.
_read_ipv4_options()and/or_make_ipv4_options(). Astrvalue names the methods_read_opt_${name}and_make_opt_${name}, falling back to_read_opt_unassigned/_make_opt_unassignedwhen they do not exist; any other value is a(parser, constructor)pair of callables.
Header Schemas¶
- pcapkit.protocols.schema.internet.ipv4.IPV4_HEADER_LEN = 20¶
Length of the fixed IPv4 header, i.e. with no options, RFC 791 Section 3.1. The one home of this value (#1739), here so that the schema’s option length and the protocol modules read the same one; it is fixed by the RFC, so it is never configurable.
Added in version 1.5.0.
- class pcapkit.protocols.schema.internet.ipv4.IPv4(*args: _VT, **kwargs: _VT)[source]¶
Bases:
SchemaHeader schema for IPv4 packet.
- vihl: VerIHLField = <BitField vihl>¶
Version and header length.
- src: IPv4Address = <IPv4AddressField src>¶
Source address.
- dst: IPv4Address = <IPv4AddressField dst>¶
Destination address.
- payload: bytes = <PayloadField payload>¶
Payload. A Total Length of 0, as TCP segmentation offload leaves it in a capture taken on the sending host, takes the rest of the data and leaves no trailer; any other Total Length shorter than the header leaves the payload empty and the rest to the trailer (#1547).
- trailer: bytes = <PayloadField trailer>¶
Octets captured past the Total Length, such as Ethernet minimum-frame padding. A
PayloadFieldwith no length takes whatever the payload leaves on unpack and writes its value verbatim on pack, so the octets survive whatever they hold.
- class pcapkit.protocols.schema.internet.ipv4.Option(dict_=None, **kwargs)[source]¶
Bases:
EnumSchema[OptionNumber]Header schema for IPv4 options.
- type: OptionNumber = <EnumField type>¶
Option type.
- class pcapkit.protocols.schema.internet.ipv4.UnassignedOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 unassigned options.
- class pcapkit.protocols.schema.internet.ipv4.EOOLOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 end of option list (
EOOL) option.
- class pcapkit.protocols.schema.internet.ipv4.NOPOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 no operation (
NOP) option.
- class pcapkit.protocols.schema.internet.ipv4.SECOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 security (
SEC) option.- level: ClassificationLevel = <EnumField level>¶
Classification level.
- class pcapkit.protocols.schema.internet.ipv4.LSROption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 loose source route (
LSR) option.- route: list[IPv4Address] = <ListField route>¶
every address slot of the option, those before the pointer and those at or beyond it alike.
- Type:
Route
- class pcapkit.protocols.schema.internet.ipv4.TSOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 timestamp (
TS) option.- ts_data: list[int] = <ListField ts_data>¶
every 32-bit slot of the option, those before the pointer and those at or beyond it alike.
- Type:
Timestamps and internet addresses
- remainder: bytes = <BytesField remainder>¶
Trailing octets that do not make up a whole 32-bit slot, kept as read rather than packed as zeros.
- post_process(packet)[source]¶
Revise
schemadata after unpacking process.- Parameters:
- Return type:
- Returns:
Revised schema.
- Raises:
FieldValueError – If an entry of
ts_datathat the timestamp flag makes an address is abool, is not a valid IP address, or is not IPv4 – c.f.parse_ip_address().
Notes
This runs on the packing path as well as the unpacking one –
Schema.packcalls it once the buffer is filled – so thets_dataentries it converts below are whatever the caller passed to the constructor, not octets read off the wire. That is why those conversions go throughparse_ip_address()rather thanipaddress.ip_address(): this schema is reachable from publicIPv4.make, which accepts a caller-built option schema and packs it, andts_data’sUInt32Fielditem type takes aboolas theintit is a subclass of, so nothing downstream can question it:ts_data=[True, 5]would otherwise pack as0000000100000005and reportIPv4Address('0.0.0.1')with no exception and no warning.
- class pcapkit.protocols.schema.internet.ipv4.ESECOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 extended security (
ESEC) option.
- class pcapkit.protocols.schema.internet.ipv4.RROption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 record route (
RR) option.- route: list[IPv4Address] = <ListField route>¶
every address slot of the option, those before the pointer and those at or beyond it alike.
- Type:
Route
- class pcapkit.protocols.schema.internet.ipv4.SIDOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 stream identifier (
SID) option.
- class pcapkit.protocols.schema.internet.ipv4.SSROption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 strict source route (
SSR) option.- route: list[IPv4Address] = <ListField route>¶
every address slot of the option, those before the pointer and those at or beyond it alike.
- Type:
Route
- class pcapkit.protocols.schema.internet.ipv4.MTUPOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 MTU probe (
MTUP) option.
- class pcapkit.protocols.schema.internet.ipv4.MTUROption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 MTU reply (
MTUR) option.
- class pcapkit.protocols.schema.internet.ipv4.TROption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 traceroute (
TR) option.- origin: IPv4Address = <IPv4AddressField origin>¶
Originator IP address.
- class pcapkit.protocols.schema.internet.ipv4.RTRALTOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
OptionHeader schema for IPv4 router alert (
RTRALT) option.- alert: RouterAlert = <EnumField alert>¶
Router alert value.
- class pcapkit.protocols.schema.internet.ipv4._QSOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
SchemaHeader schema for IPv4 quick start (
QS) options in generic representation.- flags: QSTestFlags = <ForwardMatchField flags>¶
Flags.
- data: QuickStartRequestOption | QuickStartReportOption = <SwitchField data>¶
QS data.
- class pcapkit.protocols.schema.internet.ipv4.QSOption(dict_=None, **kwargs)[source]¶
Bases:
Option,EnumSchema[QSFunction]Header schema for IPv4 quick start (
QS) options.- flags: QuickStartFlags = <BitField flags>¶
Flags.
- class pcapkit.protocols.schema.internet.ipv4.QuickStartRequestOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
QSOptionHeader schema for IPv4 quick start request options.
- class pcapkit.protocols.schema.internet.ipv4.QuickStartReportOption(*args: _VT, **kwargs: _VT)[source]¶
Bases:
QSOptionHeader schema for IPv4 quick start report of approved rate options.
- reserved: int = <UInt8Field reserved>¶
Not used. One octet, holding the place a Quick-Start Request fills with
QS TTL: RFC 4782 Section 3.1 says in as many words that “for a Report of Approved Rate, the fourth byte of the Quick-Start Option is not used”, and that “bytes 5-8 contain a 30-bit QS Nonce and a 2-bit Reserved field” – so the nonce begins at the fifth octet for both functions, and figure 4 gives this option asLength=8like figure 3 gives its sibling. Without this field the schema would be seven octets wide against the eight_make_opt_qs()writes intolengthand the eight_read_opt_qs()demands of it, and thenoncewould decode one octet early. Kept as an integer rather than as padding, so that whatever the sender put there is written back.
Type Stubs¶
- class pcapkit.protocols.schema.internet.ipv4.VerIHLField[source]¶
Bases:
TypedDictVersion and header length field.
- class pcapkit.protocols.schema.internet.ipv4.ToSField¶
Bases:
TypedDictType of service field.
- class pcapkit.protocols.schema.internet.ipv4.Flags[source]¶
Bases:
TypedDictFlags and fragment offset field.
- class pcapkit.protocols.schema.internet.ipv4.TSFlags[source]¶
Bases:
TypedDictTimestamp flags field.
- class pcapkit.protocols.schema.internet.ipv4.QuickStartFlags[source]¶
Bases:
TypedDictQuick-Start flags.
Auxiliary Functions¶
- pcapkit.protocols.schema.internet.ipv4.quick_start_option_length(schema)[source]¶
On-the-wire length, in octets, of a resolved Quick-Start (
QS) suboption.The Quick-Start suboption schemas carry the option’s own
typeandlengthoctets – they inherit them fromOption– so whatquick_start_data_selector()has to hand the nestedSchemaFieldis the length of the whole option, not of the data after its header. Per RFC 4782 Section 3.1 – figure 3 for a Quick-Start Request and figure 4 for a Report of Approved Rate – that is eight octets for both functions, which differ in what the fourth octet holds rather than in how many there are: “The second byte contains the length field, indicating an option length of eight bytes”, and “For a Report of Approved Rate, the fourth byte of the Quick-Start Option is not used”. And_read_opt_qs()rejects any other value in thelengthfield outright.It is summed from the resolved schema’s own fields rather than written as that literal, because the registry is open –
QSOptionis anEnumSchema, so a caller may register a further function code with a schema of its own width – and a number written here would have to be kept in step by hand with every field the suboptions declare.- Parameters:
schema (
Type[QSOption]) – Quick-Start suboption schema, as resolved from thefuncsub-field byquick_start_data_selector().- Return type:
- Returns:
Length, in octets, that
schemaoccupies on the wire.- Raises:
FieldValueError – If
schemadeclares a field whose width is not fixed. Every field of a Quick-Start suboption is fixed-width, because the option is, and a variable-width one cannot be summed here without a packet to size it against – which is the one thing a selector does not have for the schema it is about to return. It fails rather than guessing a width.
- pcapkit.protocols.schema.internet.ipv4.quick_start_data_selector(pkt)[source]¶
Selector function for
_QSOption.datafield.- Parameters:
- Return type:
- Returns:
If
funcis0, returns aSchemaFieldwrappedQuickStartRequestOptioninstance.If
funcis8, returns aSchemaFieldwrappedQuickStartReportOptioninstance.Otherwise, returns a
SchemaFieldwrappedUnassignedOptioninstance, sized by the option’s ownlengthoctet.
Notes
The length handed to the
SchemaFieldcomes fromquick_start_option_length(), i.e. from the suboption that was just resolved, rather than from a fixed literal: a wrong width mis-sizes the nested read, so thenoncedecodes wrongly and the leftover octets are read as a further, fabricated option.
Data Models¶
- class pcapkit.protocols.data.internet.ipv4.IPv4(*args: VT, **kwargs: VT)[source]¶
Bases:
ProtocolData model for IPv4 packet.
- src: IPv4Address¶
Source address.
- dst: IPv4Address¶
Destination address.
- options: OrderedMultiInfo[OptionNumber, Option]¶
Options, present only when the header carries any.
- class pcapkit.protocols.data.internet.ipv4.ToSField(*args: Any, **kwargs: Any)[source]¶
Bases:
DataData model for IPv4 ToS fields.
Important
Due to the reserved keyword conflict, please use
from_dict()to create an instance of this data model.- del: pcapkit.const.ipv4.tos_del.ToSDelay¶
Delay.
- pre: ToSPrecedence¶
Precedence.
- thr: ToSThroughput¶
Throughput.
- rel: ToSReliability¶
Reliability.
- class pcapkit.protocols.data.internet.ipv4.Flags(*args: VT, **kwargs: VT)[source]¶
Bases:
DataData model for IPv4 Flags.
Changed in version 1.5.0: New required field
reserved(#1326).
- class pcapkit.protocols.data.internet.ipv4.Option(dict_=None, **kwargs)[source]¶
Bases:
DataData model for IPv4 options.
- code: OptionNumber¶
Option code.
- type: OptionType¶
Option type.
- class pcapkit.protocols.data.internet.ipv4.OptionType(*args: Any, **kwargs: Any)[source]¶
Bases:
DataData model for IPv4 option type data.
- class: pcapkit.const.ipv4.option_class.OptionClass¶
Option class.
- class pcapkit.protocols.data.internet.ipv4.UnassignedOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 unassigned option.
- class pcapkit.protocols.data.internet.ipv4.EOOLOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 End of Option List (
EOOL) option.
- class pcapkit.protocols.data.internet.ipv4.NOPOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 No Operation (
NOP) option.
- class pcapkit.protocols.data.internet.ipv4.SECOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Security (
SEC) option.Changed in version 1.5.0: New required field
termination(#1393).- level: ClassificationLevel¶
Classification level.
- flags: tuple[ProtectionAuthority, ...]¶
Protection authority flags.
- class pcapkit.protocols.data.internet.ipv4.LSROption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Loose Source Route (
LSR) option.Changed in version 1.5.0: New required field
remaining(#1319).- route: tuple[IPv4Address, ...]¶
Route, i.e. the address slots before the pointer.
- remaining: tuple[IPv4Address, ...]¶
Address slots at or beyond the pointer.
- class pcapkit.protocols.data.internet.ipv4.TSOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Time Stamp (
TS) option.- timestamp: tuple[timedelta | int, ...] | OrderedMultiInfo[IPv4Address, timedelta | int]¶
Timestamp data, i.e. the slots before the pointer.
- class pcapkit.protocols.data.internet.ipv4.ESECOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Extended Security (
ESEC) option.
- class pcapkit.protocols.data.internet.ipv4.RROption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Record Route (
RR) option.Changed in version 1.5.0: New required field
remaining(#1319).- route: tuple[IPv4Address, ...] | None¶
Route, i.e. the address slots before the pointer.
- remaining: tuple[IPv4Address, ...]¶
Address slots at or beyond the pointer.
- class pcapkit.protocols.data.internet.ipv4.SIDOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Stream ID (
SID) option.
- class pcapkit.protocols.data.internet.ipv4.SSROption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Strict Source Route (
SSR) option.Changed in version 1.5.0: New required field
remaining(#1319).- route: tuple[IPv4Address, ...] | None¶
Route, i.e. the address slots before the pointer.
- remaining: tuple[IPv4Address, ...]¶
Address slots at or beyond the pointer.
- class pcapkit.protocols.data.internet.ipv4.MTUPOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 MTU Probe (
MTUP) option.
- class pcapkit.protocols.data.internet.ipv4.MTUROption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 MTU Reply (
MTUR) option.
- class pcapkit.protocols.data.internet.ipv4.TROption(*args: Any, **kwargs: Any)[source]¶
Bases:
OptionData model for IPv4 Traceroute (
TR) option.Important
Due to the reserved keyword conflict, please use
from_dict()to create an instance of this data model.- originator: IPv4Address¶
Originator IP address.
- class pcapkit.protocols.data.internet.ipv4.RTRALTOption(*args: VT, **kwargs: VT)[source]¶
Bases:
OptionData model for IPv4 Router Alert (
RTRALT) option.- alert: RouterAlert¶
Router alert.
- class pcapkit.protocols.data.internet.ipv4.QSOption(dict_=None, **kwargs)[source]¶
Bases:
OptionData model for IPv4 Quick Start (
QS) option.- func: QSFunction¶
QS function.
- class pcapkit.protocols.data.internet.ipv4.QuickStartRequestOption(*args: VT, **kwargs: VT)[source]¶
Bases:
QSOptionData model for IPv4 Quick Start request option.
Changed in version 1.5.0: New required field
reserved(#1326).
- class pcapkit.protocols.data.internet.ipv4.QuickStartReportOption(*args: VT, **kwargs: VT)[source]¶
Bases:
QSOptionData model for IPv4 Quick Start report of approved rate option.
Changed in version 1.5.0: New required fields
unusedandreserved(#1326).
Footnotes