networking · intermediate · ~15 min

DNS question count

Read a big-endian field at a fixed offset.

Challenge

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.

Task

Implement int dns_qdcount(const unsigned char *hdr). The harness supplies a fixed 12-byte header buffer.

Input

  • hdr: the 12-byte DNS header bytes.

Output

Return the QDCOUNT value: (hdr[4] << 8) | hdr[5].

Example

{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

Rules

  • QDCOUNT is big-endian: high byte at offset 4, low byte at offset 5.

Input format

hdr: the 12-byte DNS header bytes.

Output format

The QDCOUNT value (hdr[4]<<8)|hdr[5].

Constraints

QDCOUNT is a big-endian u16 at offset 4.

Starter code

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.