Skip to main content

Implement a DNS server/client in Rust

Ref:

Concepts of DNS protocol

  • Transport: UDP packages with maxium 512 bytes.
    • DNS can be used over TCP as well, eDNS format can extend the packet size.
  • Objects: Header, Question, Record
  • Format: Queries and responses use the same format, with these sections:
    • header: Header, fixed 12 bytes long.
    • question: list of Question
    • answer: list of Record
    • authority: list of Record
    • additional section: list of Record

The number of records in each section is provided by the header.

  • ID: Query packets and response packets must have save ID
  • OPCODE: Operation code.
  • RCODE: Response status code.
  • flages: Query Response (qr), Recursion Desired (rd), Recursion Available (ra).

Question

  • Name: Label Sequence, the domain name, encoded as a sequence of labels as described below.
  • Type: 2-byte Integer, the record type.
  • Class: 2-byte Integer, the class, in practice always set to 1.

Record

  • Name: Label Sequence, the domain name, encoded as a sequence of labels as described below.
  • Type: 2-byte Integer, the record type.
  • Class: 2-byte Integer, the class, in practice always set to 1.
  • TTL: 4-byte Integer, time-To-Live, i.e. how long a record can be cached before it should be requeried.
  • Len: 2-byte Integer, length of the record type specific data.