A DNS resolver
Stage 2 of 8v2 · 38dd9282

Question and UDP transport

Encode domain labels, type, and class, and prepare a complete UDP query.

Question and UDP transport

Encode domain labels, type, and class, and prepare a complete UDP query. Keep the capabilities from earlier stages working.

Observable contract

The harness uses JSON and represents DNS packets as lowercase hexadecimal. Network exchanges are modelled with responses supplied in the request so assessment stays deterministic.

{ "op": "encode-question", "name": "www.example.com", "type": "A" }

must produce exactly:

{ "hex": "03777777076578616d706c6503636f6d0000010001" }

Output is compact JSON followed by a newline. Invalid input leaves stdout empty, writes a diagnostic containing error to stderr, and exits non-zero.

Implementation

Keep encoding, validation, and traversal separate. Check every length before advancing the cursor, interpret integers in network byte order, and bound all pointer or reference following. Fixtures replace transport only: the logic must later be reusable with real UDP.

Also test truncated packets, overlong labels, out-of-range offsets, and cycles. Do not use the system DNS resolver for the mechanism taught in this stage.

Acceptance criteria

  • The example matches byte for byte.
  • Malformed data fails in a controlled way.
  • Record and result ordering is deterministic.