networking · intermediate · ~15 min
Read a big-endian field at a fixed offset.
Read the question count (QDCOUNT) out of a DNS message header.
A DNS header is 12 bytes; QDCOUNT is the big-endian 16-bit field at offset 4.
Implement int dns_qdcount(const unsigned char *hdr). The harness supplies a fixed 12-byte header buffer.
hdr: the 12-byte DNS header bytes.Return the QDCOUNT value: (hdr[4] << 8) | hdr[5].
{0xAB,0xCD,0x01,0x00,0x00,0x01,0,0,0,0,0,0} -> 1
{0,0,0,0,0x00,0x02,0,0,0,0,0,0} -> 2
hdr: the 12-byte DNS header bytes.
The QDCOUNT value (hdr[4]<<8)|hdr[5].
QDCOUNT is a big-endian u16 at offset 4.
int dns_qdcount(const unsigned char *hdr) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.