ICMPv4 - Internet Control Message Protocol¶
pcapkit.protocols.internet.icmpv4 contains
ICMPv4 only,
which implements extractor for Internet Control
Message Protocol (ICMP) [*], whose structure is
described as below:
Octets |
Bits |
Name |
Description |
|---|---|---|---|
0 |
0 |
|
Type |
1 |
8 |
|
Code |
2 |
16 |
|
Checksum |
4 |
32 |
|
Message Body |
- class pcapkit.protocols.internet.icmpv4.ICMPv4(file=None, length=None, **kwargs)[source]¶
-
This class implements Internet Control Message Protocol.
This class parses the following ICMP message types, which are registered in the
self.__type__attribute:Type
Data Parser
Data Constructor
Echo Reply / Echo Request
Destination Unreachable
Time Exceeded
Parameter Problem
Any other type is kept as its raw message body, see
_read_data_type_none()and_make_data_type_none().Changed in version 1.6.0: The checksum is computed when
make()is given none, and verified on read underCHECKSUM_VERIFY, unless the enclosing IP layer is a fragment (#2470). A message whose header records a cut readsTRUNCATED(#2711).- classmethod id()[source]¶
Index ID of the protocol.
RFC 792 names the protocol
ICMP, without a version.String
==compares against(cls, *id()), so== 'ICMPv4'isFalse, while'ICMPv4' inmatches by class.
- classmethod register_type(code, meth)[source]¶
Register a message body parser.
- Parameters:
- Warns:
RegistryWarning– If a different entry is already registered forcode; it is overwritten.
- read(length=None, *, __packet__=None, **kwargs)[source]¶
Read Internet Control Message Protocol (ICMP).
Structure of ICMP header [RFC 792]:
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 | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | . Message Body . . . +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- make(type=Type.Echo, code=0, checksum=None, body=b'\x00\x00\x00\x00', **kwargs)[source]¶
Make (construct) packet data.
- Parameters:
code (
int) – ICMP code; meaning is entirelytype-dependent.checksum (
bytes|None) – Checksum; ifNone, computed over the message (RFC 792) whenCHECKSUM_COMPUTEis on, zero when it is off.body (
bytes|ICMPv4|ICMPv4Type|dict[str,Any]) – Message body.bytesis parsed againsttype’s own schema; adictor aICMPv4data model is handed to that type’s own constructor method (see__type__); any otherICMPv4Typeis used as is.**kwargs (
Any) – Arbitrary keyword arguments.
- Return type:
- Returns:
Constructed packet data.
- Raises:
ProtocolError – If
typeis a name, abool, or a member or code that does not fit in one octet; ifbodyisbytesthat do not parse as the message body oftype, or is of none of the accepted types; or ifchecksumis not two octets.
- _read_data_type_echo(schema, *, header, length)[source]¶
Read ICMP Echo Request/Reply message.
Structure of ICMP Echo Request/Reply message [RFC 792]:
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 | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identifier | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | . Data . . . +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- _read_data_type_destination_unreachable(schema, *, header, length)[source]¶
Read ICMP Destination Unreachable message.
Structure of ICMP Destination Unreachable message [RFC 792]:
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 | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | unused | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Internet Header + 64 bits of Original Data Datagram | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Parameters:
schema (
DestinationUnreachable) – parsed message body schemaheader (
ICMPv4) – parsed ICMP header schemalength (
int) – total length of the current ICMP message
- Return type:
- Returns:
Parsed message data.
- _read_data_type_time_exceeded(schema, *, header, length)[source]¶
Read ICMP Time Exceeded message.
Structure of ICMP Time Exceeded message [RFC 792] is identical to that of
_read_data_type_destination_unreachable(), but for the type code.- Parameters:
schema (
TimeExceeded) – parsed message body schemaheader (
ICMPv4) – parsed ICMP header schemalength (
int) – total length of the current ICMP message
- Return type:
- Returns:
Parsed message data.
- _read_data_type_parameter_problem(schema, *, header, length)[source]¶
Read ICMP Parameter Problem message.
Structure of ICMP Parameter Problem message [RFC 792]:
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 | Code | Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Pointer | unused | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Internet Header + 64 bits of Original Data Datagram | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Parameters:
schema (
ParameterProblem) – parsed message body schemaheader (
ICMPv4) – parsed ICMP header schemalength (
int) – total length of the current ICMP message
- Return type:
- Returns:
Parsed message data.
- _make_data_type_none(type, message=None, *, data=b'', **kwargs)[source]¶
Make ICMP message of unknown type.
- _make_data_type_echo(type, message=None, *, id=0, seq=0, data=b'', **kwargs)[source]¶
Make ICMP Echo Request/Reply message.
- Parameters:
- Return type:
- Returns:
Constructed message body schema.
- _make_data_type_destination_unreachable(type, message=None, *, unused=b'\x00\x00\x00\x00', data=b'', **kwargs)[source]¶
Make ICMP Destination Unreachable message.
- Parameters:
- Return type:
- Returns:
Constructed message body schema.
- Raises:
ProtocolError – If
unusedis not four octets.
- _make_data_type_time_exceeded(type, message=None, *, unused=b'\x00\x00\x00\x00', data=b'', **kwargs)[source]¶
Make ICMP Time Exceeded message.
- Parameters:
- Return type:
- Returns:
Constructed message body schema.
- Raises:
ProtocolError – If
unusedis not four octets.
- _make_data_type_parameter_problem(type, message=None, *, pointer=0, unused=b'\x00\x00\x00', data=b'', **kwargs)[source]¶
Make ICMP Parameter Problem message.
- Parameters:
type (
Type) – ICMP type.message (
ParameterProblem|None) – message data.pointer (
int) – pointer to the octet of the original datagram that caused the error.unused (
bytes) – the three unused octets.data (
bytes) – internet header plus the first eight octets of the original datagram’s payload.**kwargs (
Any) – arbitrary keyword arguments.
- Return type:
- Returns:
Constructed message body schema.
- Raises:
ProtocolError – If
unusedis not three octets.
- __type__: DefaultDict[Type | int, str | tuple[Callable[[ICMPv4Type, ICMPv4, int], ICMPv4], Callable[[Type, ICMPv4 | None, Any], ICMPv4Type]]]¶
Type code to method mapping. A
strvalue names the methods_read_data_type_${name}and_make_data_type_${name}, falling back to_read_data_type_none/_make_data_type_nonewhen they do not exist; any other value is a(parser, constructor)pair of callables.
Header Schemas¶
- class pcapkit.protocols.schema.internet.icmpv4.ICMPv4(type=NO_VALUE, code=NO_VALUE, checksum=NO_VALUE, data=NO_VALUE, *, __packet__=None, **kwargs)[source]¶
Bases:
SchemaHeader schema for ICMP packet.
- data: ICMPv4Type = <SwitchField data>¶
Message body, selected by
type.
- class pcapkit.protocols.schema.internet.icmpv4.ICMPv4Type(*args, **kwargs)[source]¶
Bases:
EnumSchema[Type]Header schema for ICMP type-specific message body.
- class pcapkit.protocols.schema.internet.icmpv4.Unknown(data=NO_VALUE, *, __packet__=None, **kwargs)[source]¶
Bases:
ICMPv4TypeHeader schema for ICMP message of unknown type.
- class pcapkit.protocols.schema.internet.icmpv4.Echo(id=NO_VALUE, seq=NO_VALUE, data=NO_VALUE, *, __packet__=None, **kwargs)[source]¶
Bases:
ICMPv4TypeHeader schema for ICMP Echo Request/Reply message.
- class pcapkit.protocols.schema.internet.icmpv4.DestinationUnreachable(unused=NO_VALUE, data=NO_VALUE, *, __packet__=None, **kwargs)[source]¶
Bases:
ICMPv4TypeHeader schema for ICMP Destination Unreachable message.
- class pcapkit.protocols.schema.internet.icmpv4.TimeExceeded(unused=NO_VALUE, data=NO_VALUE, *, __packet__=None, **kwargs)[source]¶
Bases:
ICMPv4TypeHeader schema for ICMP Time Exceeded message.
- class pcapkit.protocols.schema.internet.icmpv4.ParameterProblem(pointer=NO_VALUE, unused=NO_VALUE, data=NO_VALUE, *, __packet__=None, **kwargs)[source]¶
Bases:
ICMPv4TypeHeader schema for ICMP Parameter Problem message.
Auxiliary Functions¶
- pcapkit.protocols.schema.internet.icmpv4.icmpv4_data_selector(pkt)[source]¶
Selector function for
ICMPv4.datafield.- Parameters:
- Return type:
- Returns:
A
SchemaFieldwrappedICMPv4Typeinstance based onICMPv4.type.
Data Models¶
- class pcapkit.protocols.data.internet.icmpv4.ICMPv4(*args, **kwargs)[source]¶
Bases:
ProtocolData model for ICMP protocol.
The fields below are the only ones every ICMP message shares (RFC 792); what follows them is read entirely differently depending on
type, which is why each message kind below adds its own fields rather than this base class growing a union of them all.- code: int¶
ICMP code. Left untyped as a bare
inthere because its meaning is entirelytype-dependent; each subclass below documents which registry (if any) its owncodevalues are drawn from.
- length: int¶
Message length, in octets, header included. ICMP messages carry no length field of their own – this is simply how many octets
ICMPv4.readwas handed, kept here so the whole message can be told apart from a truncated capture.
- checksum_status: ChecksumStatus¶
Outcome of verifying
checksum, present only whenCHECKSUM_VERIFYis on.
- class pcapkit.protocols.data.internet.icmpv4.Unknown(type, code, checksum, length, data)[source]¶
Bases:
ICMPv4Data model for ICMP packet of unknown type.
codecarries nopcapkit.constregistry for a type this package does not recognise, so it is read as a bareint.
- class pcapkit.protocols.data.internet.icmpv4.Echo(type, code, checksum, length, id, seq, data)[source]¶
Bases:
ICMPv4Data model for ICMP Echo Request/Reply message (types 8 and 0).
codeis always0for both types (RFC 792), so it is read as a bareintrather than typed against a registry of one member.
- class pcapkit.protocols.data.internet.icmpv4.DestinationUnreachable(type, code, checksum, length, unused, data)[source]¶
Bases:
ICMPv4Data model for ICMP Destination Unreachable message (type 3).
- unused: bytes¶
Unused (four octets, RFC 792 Page 4), as captured.
- data: bytes¶
Internet header plus the first eight octets of the original datagram’s payload (RFC 792 Page 4), as captured.
- class pcapkit.protocols.data.internet.icmpv4.TimeExceeded(type, code, checksum, length, unused, data)[source]¶
Bases:
ICMPv4Data model for ICMP Time Exceeded message (type 11).
- unused: bytes¶
Unused (four octets, RFC 792 Page 5), as captured.
- data: bytes¶
Internet header plus the first eight octets of the original datagram’s payload (RFC 792 Page 5), as captured.
- class pcapkit.protocols.data.internet.icmpv4.ParameterProblem(type, code, checksum, length, pointer, unused, data)[source]¶
Bases:
ICMPv4Data model for ICMP Parameter Problem message (type 12).
- pointer: int¶
Pointer, identifying the octet of the original datagram that caused the error (RFC 792 Page 5); meaningless for codes other than
0, but read and written regardless, as the wire layout requires.
- unused: bytes¶
Unused (three octets, RFC 792 Page 5), as captured.
- data: bytes¶
Internet header plus the first eight octets of the original datagram’s payload (RFC 792 Page 5), as captured.
Footnotes