projects > from_scratch > dns.go
1
A DNS server, from scratch, in Go[github]
1
2
A DNS server built from scratch in Go with no DNS libraries — it
3
parses and serializes raw DNS packets byte by byte, across the
4
header, question, and answer sections.
5
6
Handles DNS name compression (pointer labels), bit-packed header
7
flags, and recursive forwarding to an upstream resolver.
8
Completed end-to-end as the CodeCrafters 'Build Your Own DNS
9
server' challenge.
10
11
[Go][GitHub]
12
14
15
func parseQuestionName(pointer int, data []byte) (string, int) {
16
labels := []string{}
17
for {
18
first := data[pointer]
19
pointer++
20
// if null byte - end
21
if first == 0 {
22
break
23
}
24
// if redirect - read labels from there
25
if (first & 0b11000000) == 0b11000000 {
26
second := data[pointer]
27
pointer++
28
redirectAddress := binary.BigEndian.Uint16([]byte{first & 0b00111111, second})
29
question, _ := parseQuestionName(int(redirectAddress), data)
30
labels = append(labels, question)
31
break
32
}
33
// else read from here
34
label := data[pointer : pointer+int(first)]
35
pointer = pointer + int(first)
36
labels = append(labels, string(label))
37
}
38
39
return strings.Join(labels, "."), pointer
40
}
NORMAL~/oleksandr/projects/from_scratch/dns.go.../from_scratch/dns.gomainutf-8
Enter to open · :q to quit
keys
j / kmove the cursor line, or the tree cursor
hfocus the tree; in the tree, fold or go to the parent
l / Enterin the tree, unfold or open the file
gg / Gfirst / last line
clickput the cursor on a line; on README.sh, run the session again
:qquit to the shell
:help ?this table
q / Escclose this