networking · intermediate · ~15 min

Is it a DNS response?

Test a single flag bit in a header byte.

Challenge

Test the QR flag bit in a DNS header to tell a query from a response.

The QR bit is bit 7 (the top bit) of byte 2: 0 means query, 1 means response.

Task

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

Input

  • hdr: the 12-byte DNS header bytes.

Output

Return 1 if the QR bit (hdr[2] & 0x80) is set, else 0.

Example

{0,0,0x01,0,...}      ->  0   (query)
{0,0,0x81,0x80,...}   ->  1   (response)

Rules

  • The QR bit is the top bit of byte 2: mask with 0x80.

Input format

hdr: the 12-byte DNS header bytes.

Output format

1 if the QR bit (hdr[2] & 0x80) is set, else 0.

Constraints

QR is the top bit of byte 2.

Starter code

int dns_is_response(const unsigned char *hdr) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.