networking · advanced · ~25 min
Length-prefixed wire-format parsing.
DNS encodes a domain name as a sequence of length-prefixed labels followed by a 0-length terminator:
[3]'w' 'w' 'w' [7]'e' 'x' 'a' 'm' 'p' 'l' 'e' [3]'c' 'o' 'm' [0]
For this exercise, we IGNORE pointer compression (top 2 bits of length =
11). We just decode raw labels into name.name.name\0.
Implement
int decode_dns_labels(const unsigned char *in, int in_len, char *out, int cap).
Return the number of consumed input bytes on success, -1 on parse failure.
DNS wire format uses length-prefixed labels. A parser that does pointer-compression wrong can loop forever — a classic CVE primitive in resolvers.
DNS-wire bytes + length + output buffer.
Bytes consumed or -1.
Refuse labels >63 bytes (DNS spec). Refuse output overflow.
#include <stddef.h>
int decode_dns_labels(const unsigned char *in, int in_len, char *out, int cap) { /* TODO */ (void)in; (void)in_len; (void)out; (void)cap; return -1; }
Forgetting that each label is <= 63 bytes (top 2 bits clear).
Root label (just 00) → empty output. Truncated input.
O(in_len).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.