Domain Name Codec

pcapkit.corekit.schema.domain_name reads and writes the label sequence of RFC 1035 Section 3.1, with the compression pointers of RFC 1035 Section 4.1.4, and renders it in presentation form, for DNS and for ICMPv6’s DNS Search List option (RFC 8106 Section 5.2).

Added in version 1.9.0: Moved here from pcapkit.protocols.application.dns.schema (#2934).

pcapkit.corekit.schema.domain_name.read_name(data, pos, *, compression=True, alias='DNS')[source]

Read the domain name at pos in data.

Parameters:
  • data (bytes) – The octets compression pointers are offsets into, i.e. the whole DNS message for DNS.

  • pos (int) – Offset of the name in data.

  • compression (bool) – Whether a compression pointer may end the name.

  • alias (str) – Prefix of the error message.

Return type:

tuple[tuple[bytes, ...], int, int | None, int]

Returns:

Every label of the name, the root’s left out; how many of them are written in full at pos; the offset the pointer ending the name there points to, or None where the root label ends it; and the offset just past the name at pos.

Raises:

ProtocolError – If the name runs past the end of data, holds a label type other than a plain label or, where compression allows one, a pointer (RFC 6891 Section 5), or its pointers loop.

pcapkit.corekit.schema.domain_name.make_name(labels, *, literal=None, pointer=None, alias='DNS')[source]

Write a domain name, the inverse of read_name().

Parameters:
  • labels (Sequence[bytes]) – Every label of the name, the root’s left out.

  • literal (int | None) – How many of labels to write in full; all of them if None.

  • pointer (int | None) – Offset a compression pointer after them points to, or None to end the name with the root label.

  • alias (str) – Prefix of the error message.

Return type:

bytes

Returns:

The name’s octets.

Raises:

ProtocolError – If a label written is empty or longer than 63 octets, literal is not a count of labels, or pointer does not fit in 14 bits.

pcapkit.corekit.schema.domain_name.name_to_text(labels)[source]

Render labels in presentation form (RFC 1035 Section 5.1).

Parameters:

labels (Sequence[bytes]) – Every label of the name, the root’s left out.

Return type:

str

Returns:

The absolute name, e.g. www.example.com., with . and \ escaped by a backslash, and any octet outside printable ASCII or a space as \DDD; the root is ..

pcapkit.corekit.schema.domain_name.text_to_labels(text, *, alias='DNS')[source]

Read a name in presentation form into labels, the inverse of name_to_text().

Parameters:
  • text (str) – The name; the trailing . is optional, and '' or . is the root.

  • alias (str) – Prefix of the error message.

Return type:

tuple[bytes, ...]

Returns:

Every label of the name, the root’s left out.

Raises:

ProtocolError – If text holds an empty label, or a backslash not followed by a character or by three decimal digits up to 255.