networking · intermediate · ~15 min
Test a single flag bit in a header byte.
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.
Implement int dns_is_response(const unsigned char *hdr). The harness supplies a fixed 12-byte header.
hdr: the 12-byte DNS header bytes.Return 1 if the QR bit (hdr[2] & 0x80) is set, else 0.
{0,0,0x01,0,...} -> 0 (query)
{0,0,0x81,0x80,...} -> 1 (response)
0x80.hdr: the 12-byte DNS header bytes.
1 if the QR bit (hdr[2] & 0x80) is set, else 0.
QR is the top bit of byte 2.
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.