Phase 6: DNS Stub Resolver#
Implement a minimal DNS stub resolver in the net crate for resolving hostnames to IP addresses.
Requirements#
- DNS message format (RFC 1035):
- Header: ID, flags (QR, OPCODE, RD), QDCOUNT, ANCOUNT
- Question section: QNAME, QTYPE, QCLASS
- Answer section: NAME (with compression pointer support), TYPE, CLASS, TTL, RDATA
- Query types: A (IPv4) and AAAA (IPv6) records
- Transport: UDP via
std::net::UdpSocket(port 53) - System resolver config: parse
/etc/resolv.confto find nameserver addresses - Resolution function:
resolve(hostname: &str) -> Result<Vec<IpAddr>>- Send A and AAAA queries
- Parse responses and collect IP addresses
- Timeout handling (2-3 second default)
- Retry logic (try next nameserver on failure)
- DNS name encoding: labels with length prefix, domain compression pointer handling
Dependencies#
std::net::UdpSocket(standard library)- TCP socket wrapper (for TCP fallback on truncated responses — optional, can defer)
Acceptance Criteria#
- Build and serialize DNS query messages
- Parse DNS response messages with A and AAAA records
- Handle name compression pointers in responses
- Read nameserver from
/etc/resolv.conf - Resolve real hostnames to IP addresses
- Timeout and retry handling
- Comprehensive tests for message serialization/parsing (20+ tests)
-
cargo clippyandcargo fmtclean