Master the fundamental concepts of protocol implementation through this focused micro-challenge.
DNS translates names like example.com into IP addresses like 93.184.216.34. A query fits in a single UDP datagram to port 53 on a resolver such as 8.8.8.8.
The 12-byte header contains:
0x1234)A type-A query (type 1, class 1) asks for an IPv4 address. The name is encoded in length-prefixed labels: 7example3com0 for example.com.
The answer section returns the resolved name, type, TTL, and the 4-byte IPv4 address. Other record types include AAAA for IPv6, MX for mail routing, and NS for delegating authority to another nameserver. Recursive resolvers set the RD (recursion desired) bit and rely on upstream servers to chase referrals through the authority section.
This task requires you to build a DNS query packet and parse the response. glibc's getaddrinfo() does this internally against resolvers like 8.8.8.8 or 1.1.1.1, encoding names into DNS's length-prefixed label format before parsing the answer section. Dan Kaminsky's 2008 cache-poisoning attack exploited weak randomization in the very transaction ID field you set here, which is why modern resolvers randomize both the ID and the source port.
Write a C program that encodes a DNS query name into the RFC 1035 wire format and computes the total query size.
Input: a single line containing a fully-qualified domain name (a trailing dot may be present and must be ignored).
Output:
wire= followed by the RFC 1035-encoded QNAME as space-separated hex bytes: each label printed as its one-byte length (hex) followed by the label's ASCII bytes in hex, ending with the single 00 terminator octetlabels=N where N is the number of labelsqname_len=N where N is the QNAME encoding length in bytes (sum of label length octets + label bytes + the terminating zero octet)query_len=N where N is the total DNS query size: 12-byte header + QNAME + 4-byte QTYPE/QCLASSExample: for example.com the QNAME encoding is 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 (13 bytes), so the query is 12 + 13 + 4 = 29 bytes. No sockets needed: this is pure wire-format simulation on stdin/stdout.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works