Karta.DNS.Wire (Karta v1.0.0)

The DNS wire format (RFC 1035 §4) with EDNS(0) (RFC 6891) and the DNSSEC header bits AD and CD (RFC 4035 §3.2), in pure Elixir.

Erlang's inet_dns cannot be used: its header record has no AD or CD bit and it has no TLSA type, and DANE needs both.

Decoding is strict and bounded, because responses come from the network:

  • every section must fit the message, and nothing may follow the last record (:truncated, :trailing_data);
  • labels are at most 63 octets, names at most 255 octets on the wire, and the label types 01 and 10 are refused (:bad_name, :name_too_long);
  • a compression pointer must point strictly before the pointer and before every earlier target of the same name, and a name may follow at most 128 pointers, so a pointer loop cannot occur (:bad_pointer);
  • the RDATA of a known type must have exactly the length it declares (:bad_rdata);
  • at most one OPT record, only in the additional section and owned by the root (:bad_opt);
  • a TTL with the top bit set counts as 0 (RFC 2181 §8).

Encoding never compresses names (always valid, if not minimal) and checks every value it writes, so encode/1 either produces a well-formed message or returns an error. Names are in the presentation form described in Karta.DNS.Record; escapes (\., \\, \DDD) are understood.

A record with data: :empty is written with an empty RDATA, as a dynamic update's prerequisites and RRset deletions carry them (RFC 2136 §2.4, §2.5).

Summary

Types

Why a message could not be decoded or encoded.

Functions

Decodes a DNS message.

Decodes one name at the start of data and returns it with the rest of data after it, for the fields a message's records carry as raw data (the TSIG key and algorithm names, Karta.RFC2136.TSIG).

Decodes the name that starts at offset in packet, following compression pointers into the whole packet, and returns it with the position after it. For a name that must be read in the context of the message (Karta.RFC2136.TSIG's owner name).

Encodes a message. Names are written without compression.

The wire form of a name in presentation form (see Karta.DNS.Record): "." is the root, one final dot is optional, escapes are understood.

The numeric code of a record type (atoms as in Karta.DNS.Record, or a number).

The atom for a numeric record type when Karta decodes it, else the number.

Types

error()

@type error() ::
  :truncated
  | :trailing_data
  | :too_large
  | :bad_name
  | :name_too_long
  | :bad_pointer
  | :bad_rdata
  | :bad_opt
  | :bad_message

Why a message could not be decoded or encoded.

Functions

decode(packet)

@spec decode(binary()) :: {:ok, Karta.DNS.Message.t()} | {:error, error()}

Decodes a DNS message.

decode_name(data)

@spec decode_name(binary()) :: {:ok, String.t(), binary()} | {:error, error()}

Decodes one name at the start of data and returns it with the rest of data after it, for the fields a message's records carry as raw data (the TSIG key and algorithm names, Karta.RFC2136.TSIG).

decode_name_at(packet, offset)

@spec decode_name_at(binary(), non_neg_integer()) ::
  {:ok, String.t(), non_neg_integer()} | {:error, error()}

Decodes the name that starts at offset in packet, following compression pointers into the whole packet, and returns it with the position after it. For a name that must be read in the context of the message (Karta.RFC2136.TSIG's owner name).

encode(message)

@spec encode(Karta.DNS.Message.t()) :: {:ok, binary()} | {:error, error()}

Encodes a message. Names are written without compression.

encode_name(name)

@spec encode_name(String.t()) ::
  {:ok, binary()} | {:error, :bad_name | :name_too_long}

The wire form of a name in presentation form (see Karta.DNS.Record): "." is the root, one final dot is optional, escapes are understood.

type_code(type)

@spec type_code(Karta.DNS.Record.type()) :: {:ok, 0..65535} | :error

The numeric code of a record type (atoms as in Karta.DNS.Record, or a number).

type_name(code)

@spec type_name(0..65535) :: Karta.DNS.Record.type()

The atom for a numeric record type when Karta decodes it, else the number.