IP - Internet Protocol

pcapkit.protocols.internet.ip.protocol contains IP only, which is a base class for Internet Protocol (IP) protocol family [*], e.g. IPv4 and IPv6. Parsing octets through the base, or building it from fields, gives the one its version keyword names, which a member checks as its own on both paths (#2840); without it, octets give the one their Version field names, and fields are refused. Its from_data() reads the version of its data (#2574).

class pcapkit.protocols.internet.ip.protocol.IP[_PT: Data, _ST: Schema](file, length=None, /, *, version=None, layer=None, protocol=None, context=None)[source]

Bases: Internet[_PT, _ST], Generic[_PT, _ST]

Base class for the IP protocol family.

The IPsec protocols (AH and ESP) derive from IPsec instead.

It dispatches on both paths, never giving an instance of this class (#2574). version names the member; without it, octets give the one their Version field names, and fields are refused, since no field picks a member. from_data() reads the version of its data. See __new__().

Methods

classmethod id()[source]

Index ID of the protocol.

Return type:

tuple[Literal['IPv4'], Literal['IPv6']]

Returns:

Index ID of the protocol.

classmethod from_data(data, /, **kwargs)[source]

Create protocol instance from data.

Parameters:
Return type:

Self

Returns:

Protocol instance; on this class, an instance of the member the version of data names, which both data models carry.

Raises:

ProtocolError – If that version is neither 4 nor 6, on this class.

Data Models

__init__(file=NO_VALUE, length=None, /, *, version=None, layer=None, protocol=None, context=None, **fields)[source]

Parse a protocol instance from its octets, or construct one from its fields.

Overloads:
Parameters:
Raises:

ProtocolError – If version names neither member, or is not this class’s own version: that of the member it is or derives from, which a subclass outside the members does not have.

Changed in version 1.8.0: version is checked against the member, as from_fields() checks it (#2840).

static __new__(cls, file=NO_VALUE, length=None, /, **kwargs)[source]

Create the member a parse or a construction of this class describes.

The version keyword names the member. Without it, the octets give the member the Version field in their first four bits names, and fields are refused. That member’s __init__ then runs with the same arguments. A subclass of this class is created as itself.

Parameters:
  • file (IO[bytes] | bytes | None | NoValueType) – Source packet stream, read here without being consumed; omitted to construct.

  • length (int | None) – Length of packet data.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

IP

Returns:

An uninitialised instance of the member.

Raises:
  • TypeError – If file is None, on this class.

  • ProtocolError – On this class, if version names neither member; if, without it, the octets are empty or their first four bits name neither version; or if fields come without it.

Utilities

static _member_of_version(version)[source]

The member a version of 4 or 6 names.

Parameters:

version (Any) – Version of IP.

Return type:

Type[IP]

Returns:

IPv4 or IPv6.

Raises:

ProtocolError – If version is neither, or is no int: a bool or a float is refused even where it equals one.

classmethod _construction_member(fields)[source]

The member from_fields(), from_data() and a call given no file construct.

On this class, the member version names; on another, the class itself, of which version must name the member it is or derives from. version is taken out of fields, as make() does not read it.

Parameters:

fields (dict[str, Any]) – Construction keywords.

Return type:

Type[Self]

Returns:

The member, or the class itself.

Raises:

ProtocolError – If, on this class, version is missing; if it names neither member; or if it is not the class’s own version, which a subclass outside the members does not have.

Added in version 1.8.0.

Footnotes