Building a DNS Server from Scratch in Zig! After completing Ziglings, I'm taking on the #codecrafters DNS challenge to push deeper into systems programming. Live and unfiltered.
In the last session, I managed to add the CLI, and send a request to an upstream DNS server. However, to parse that to the format Codecrafters expects, I actually need to parse it. So, that's what I'm going to pick up today.
I'm sure it'll be fun, so CHALLENGE ACCEPTED 💪🏼🤓.
Related Playlists:
• Build to Learn: DNS Server in Zig
• Ziglings: Livecoding
Notes:
✅ Session 1: https://sourcery.zone/articles/2025/0...
✅ Session 2: https://sourcery.zone/articles/2025/0...
✅ Session 3: https://sourcery.zone/articles/2025/0...
✅ Session 4: https://sourcery.zone/articles/2025/0...
✅ Session 5: https://sourcery.zone/articles/2025/0...
✅ Session 6, 7, and 8: https://sourcery.zone/articles/2025/0...
✅ Session 9, and 10: https://sourcery.zone/articles/2025/0...
⭐ Sessions 11 to 17: https://sourcery.zone/articles/2025/1...
Reach out and Chat
Matrix: https://matrix.to/#/#sourcery-zone:matrix.org
Timestamps
00:00:00 Stream Warm Up
00:01:41 intro; starting part 15 of DNS vlog
00:02:08 recap; final CodeCrafters stage with pending refactors
00:02:26 adds `question.toBytes()` and `answer.parse()` notes
00:02:33 reviews refactor work done before stream
00:02:48 introduces new `Arguments` and `Message` modules
00:03:16 adds `parse()` and `toBytes()` to unify message handling
00:03:35 simplifies main logic to two calls: parse + respond
00:04:43 exposes `parseLabel()` for reuse in answers
00:06:18 passes `final_offset` to detect question/answer boundaries
00:07:03 adds `Answer.parseSection()` mirroring question parser
00:07:20 main function cleanup and shorter lines
00:07:55 new issue: memory leak and core dump
00:08:42 traces leak to allocator misuse
00:09:47 replaces GPA allocator with ArrayList(u8)
00:11:02 tests append-based pattern for simpler writes
00:12:36 notes missing endian handling for numeric fields
00:15:44 confirms DNS uses big endian; adds byte shifts + mask
00:17:05 explains 0xFF masking to extract low byte
00:19:55 demonstrates bitwise masking in binary
00:21:07 reviews integer sizes for class, TTL, length
00:22:47 tests narrowing U32 to U8 safely
00:24:48 memory leak persists; investigates further
00:27:57 finds allocator not root cause; data corruption suspected
00:30:22 traces invalid initialization in `answer` struct
00:32:02 digs into `question` and `response` call stacks
00:37:38 identifies wrong allocator used for answers
00:41:26 realizes parsing miscalculates answer length
00:42:27 detects malformed `rdata` bytes (too long)
00:42:57 memory error gone; now parsing wrong response
00:48:33 isolates error to wrong extraction in `Answer.parse()`
00:50:18 verifies response buffer mismatch
00:53:39 corrects argument type mismatch when passing to function
00:55:45 second answer parsing fails; prints first only
00:57:16 fixes partial output; second answer still broken
00:58:44 wraps session; defers debugging to next stream
#zig #lowlevelprogramming #systemprogramming #coding #codinglive