OSPF - Open Shortest Path First

pcapkit.protocols.application.ospf.protocol contains OSPF only, the abstract base of the Open Shortest Path First (OSPF) family [*]. Each version is a member of its own:

Version

Class

Specification

Header

1

OSPFv1

RFC 1131

24 octets, AuType and Authentication

2

OSPFv2

RFC 2328

24 octets, AuType and Authentication

3

OSPFv3

RFC 5340

16 octets, Instance ID and a reserved octet

All three share the first 14 octets alone, Version to Checksum, so the base parses no header. IP protocol 89 is OSPF’s in every version, so Internet.__proto__ binds this base, which picks the member by the Version octet. Each RFC requires its own version there (RFC 1131 Section 8.2, RFC 2328 Section 8.2, RFC 5340 Section 4.2.2), so a direct parse of any other version raises ProtocolError, and behind IP the packet is left Raw.

Added in version 1.5.0: Moved here from pcapkit.protocols.link.ospf, with its data model and header schema. OSPF derives from Application, so its layer is 'Application' (#719).

Removed in version 1.5.0: pcapkit.protocols.link.ospf, pcapkit.protocols.data.link.ospf and pcapkit.protocols.schema.link.ospf, the OSPF name that pcapkit.protocols.link, pcapkit.protocols.data.link and pcapkit.protocols.schema.link each exported, and the OSPF_CrytographicAuthentication name that pcapkit.protocols.data.link and pcapkit.protocols.schema.link exported, with no alias (#719, #1090).

Changed in version 1.5.0: A rebuild keeps the parsed Packet Length and the octets past it, where OSPF.make recomputed the length from the payload and dropped those octets; it takes them as packet_length and trailer (#1249, #1455).

Changed in version 1.8.1: OSPF became the abstract base of the family, giving OSPFv1, OSPFv2 or OSPFv3; it read every version with OSPFv2’s 24-octet header, OSPFv3’s 16-octet one included. A version none of them reads, 0 or 4 to 255, is refused, and left Raw behind IP (#3105).

Removed in version 1.8.1: pcapkit.protocols.application.ospf.schema and pcapkit.protocols.application.ospf.data, with their OSPF and CryptographicAuthentication classes and pcapkit.protocols.application.ospf.schema.ospf_auth_data_selector; pcapkit.protocols.application.ospf.Schema_OSPF, pcapkit.protocols.application.ospf.Schema_CryptographicAuthentication, pcapkit.protocols.application.ospf.Data_OSPF and pcapkit.protocols.application.ospf.Data_CryptographicAuthentication; and OSPF.read, OSPF.make and OSPF.length, with OSPF._make_data, OSPF._read_encrypt_auth, OSPF._make_encrypt_auth, OSPF._read_id_numbers and OSPF._make_id_numbers. None has an alias: the OSPFv2 header moved to pcapkit.protocols.application.ospfv2, as OSPFv2, and each member defines its own read, make and length (#3105).


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

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

This class implements all protocols in the OSPF family.

A routing protocol computes the forwarding table rather than forwarding packets, so it is a user of the stack rather than part of its forwarding path. RFC 1812 Section 7 places it accordingly, titling that chapter “APPLICATION LAYER - ROUTING PROTOCOLS” with OSPF at §7.2.2, while the Internet Layer protocols RFC 1812 Section 4.1 names are IP, ICMP and IGMP. Hence Application as the base, and layer == 'Application'. See Protocol Layer Placement.

It is abstract, as L2TP is: length, read() and make() are declared abstract by ProtocolBase, and none is defined here. It dispatches on both paths, never giving an instance of this class (#2574). The version field names the member; without it, octets give the one their Version octet names, and fields are refused. from_data() reads the version of its data. See __new__().

Note

The subpackage does not track the dispatch tier. OSPF is dispatched from Internet.__proto__ at OSPFIGP (IANA protocol number 89), since IP is what carries it.

Changed in version 1.6.0: Each enumeration field of its make takes a member or an int code, resolved with the enumeration’s get; a name, a bool for a field wider than one bit, or a code the field cannot carry raises ProtocolError, rather than resolving against an enumeration inferred from the caller’s frame or writing a different value (#2594).

Removed in version 1.6.0: The type_default, type_namespace, type_reversed, auth_type_default, auth_type_namespace and auth_type_reversed arguments of its make; resolve a name with resolve_index() first (#2594).

Changed in version 1.8.1: Became the abstract base of the family; its parsing moved to OSPFv2. Constructing it and from_data() give a member, and fields without version are refused (#3105).

Properties

property name: str

Name of current protocol.

property alias: str

Acronym of current protocol.

property info_name: Literal['ospf']

Key name of the info dict.

property type: Packet

OSPF packet type.

Methods

classmethod id()[source]

Index ID of the protocol.

Return type:

tuple[Literal['OSPFv1'], Literal['OSPFv2'], Literal['OSPFv3']]

Returns:

Index ID of the protocol, each member’s name, as IP.id lists its own; each member narrows it to its own name.

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.

Raises:

ProtocolError – If, on this class, data carries no version or one no member reads.

Data Models

classmethod __index__()[source]

Numeral registry index of the protocol.

Return type:

TransType

Returns:

Numeral registry index of the protocol in IANA, which every member inherits.

__length_hint__()[source]

Return an estimated length for the object.

The longest member’s header, OSPFv1’s and OSPFv2’s 24 octets, as L2TPv2.__length_hint__ takes its longest form.

Return type:

int

__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 is not a version of OSPF, or is not this class’s own version: that of the member it is or derives from.

Added in version 1.8.1.

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 their Version octet, the first, 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:

OSPF

Returns:

An uninitialised instance of the member.

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

  • ProtocolError – On this class, if version is not a version of OSPF; if, without it, the octets are empty; or if fields come without it.

Added in version 1.8.1.

Utilities

static _member_of_version(version)[source]

The member that reads a version of the Version octet.

Parameters:

version (Any) – Version of OSPF.

Return type:

Type[OSPF]

Returns:

OSPFv1 for 1, OSPFv2 for 2 and OSPFv3 for 3.

Raises:

ProtocolError – For any other version, or one that is no int, as a bool or a float is even where it equals one.

Added in version 1.8.1.

classmethod _dispatch(data, length)[source]

Pick the class that parses the OSPF packet at the start of data.

As IGMP._dispatch does for IP protocol 2, before any class is built.

Parameters:
  • data (bytes) – Payload of the IP layer, starting at the OSPF packet.

  • length (int) – The OSPF packet’s length, in octets.

Return type:

Type[ProtocolBase] | Type[Raw]

Returns:

The member its Version octet names, as _member_of_version() gives it; Raw for any other version, which then keeps its octets undissected.

Added in version 1.8.1.

classmethod _construction_member(fields)[source]

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

On this class, the member the version field names, which stays in fields; on another, the class itself.

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 or is not a version of OSPF.

Added in version 1.8.1.

_make_version(version)[source]

Check that version is one this class reads, for make() to write.

Parameters:

version (Any) – The version given to make().

Return type:

int

Returns:

version, as a plain int.

Raises:

ProtocolError – If version is not a version of OSPF, or names another member.

Added in version 1.8.1.

static _rebuilt_version(data, own)[source]

The version a rebuild of data writes: its own, or own if the capture ended first.

A capture that ends before the Version octet leaves it zero, which names no member’s own version; the rebuild is cut back before the octet, so the version it writes is never read (#2880).

Parameters:
  • data (Any) – The data model being rebuilt.

  • own (int) – The version of the member rebuilding it.

Return type:

int

Returns:

The version for make().

Added in version 1.8.1.

_verify_checksum(ospf, exempt=False)[source]

Record on ospf the outcome of verifying a 24-octet header’s checksum.

Only under CHECKSUM_VERIFY, as checksum_status. OSPFv1 and OSPFv2 define it alike, over the packet but the 8-octet Authentication field at octet 16, with no pseudo-header (RFC 1131 Section A.2, RFC 2328 Appendix A.3.1). OSPFv3’s covers an IPv6 pseudo-header instead (RFC 5340 Appendix A.3.1) and is not verified.

Parameters:
  • ospf (_PT) – Parsed packet data, with len and chksum.

  • exempt (bool) – Whether the packet leaves its checksum unused, as OSPFv2’s cryptographic authentication does (RFC 2328 Appendix D.4.3).

Return type:

None

Added in version 1.8.1.

static _compute_checksum(schema, packet_length)[source]

Fill in a made 24-octet header’s checksum, as _verify_checksum() checks it.

Parameters:
  • schema (_ST) – The header make() built, its checksum zero.

  • packet_length (int) – Its Packet Length.

Added in version 1.8.1.

Footnotes