Full source: ecekaptan/dig-from-scratch

Everyone says DNS is like a phone book for the internet, but most people never stop to ask how it actually does that. The goal is simple: look at the raw bytes of a DNS packet and see what each one means: the header, the flags, the question, and the answer records that come back. The code in dig.c is the companion here, and every idea below maps to a function you can read and follow.

A DNS lookup is really just two packets: you send a query, and the server sends back a response. Both use the same basic structure, made of five parts:

+-------------------+

| Header | always 12 bytes