networking · intermediate · ~15 min · safe pentest lab
Bounds-safe label decoding with loop-proof refusal of pointers.
#include <stdint.h>
#include <stddef.h>
int parse_dns_qname(const uint8_t *pkt, size_t n, char *out, size_t cap);
A DNS name is a series of labels [len][bytes...] ending with a 0x00 length.
Decode it into a dotted string in out. Return bytes written (excluding NUL),
or -1 on NULL input, a length that runs past n, output overflow, a missing
terminator, or a compression/reserved length byte (top two bits set, & 0xC0).
len; 0 ends the name.. between labels (not before the first).len & 0xC0 — compression pointers can loop; refuse them here.Every DNS tool starts by decoding the label-encoded QNAME. Doing it safely means refusing compression pointers that could loop.
#include <stdint.h>
#include <stddef.h>
int parse_dns_qname(const uint8_t *pkt, size_t n, char *out, size_t cap) {
/* TODO */
(void)pkt; (void)n; (void)out; (void)cap;
return -1;
}
Following compression pointers (can loop forever). Forgetting the inter-label dot. Reading past n.
Root name (single 0x00) → 0. Truncated label. Output too small.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.