DNS query failed: dns: overflowing header size (#YC9)

I’m stuck on state 7 on dns-server problem. I am getting this error when running the test and I am wondering what this means.

[stage-7] DNS query failed: dns: overflowing header size.
[stage-7] If you are seeing this after a while then it is likely that your server is not responding with appropriate id
[stage-7] Test failed

Previous stage test passed so I am assuming header id was correctly parsed. This is after I added a code to parse compressed label.

I have tried logging to see if I am missing something, but I don’t understand. I tried to log the full byte that was received and first 2 byte seems to resolve to number I am parsing.

Here are my logs:

Debug = true

[stage-7] Running tests for Stage #7: Parse compressed packet
[stage-7] Starting DNS server on 127.0.0.1:2053
[stage-7] Running program
[stage-7] DNS resolver listening on 127.0.0.1:5354
[stage-7] Connecting to 127.0.0.1:2053 using UDP
[your_program] 2024-05-10T04:38:45.748576Z  INFO ThreadId(01) src/main.rs:21: Logs from your program will appear here!
[your_program] 2024-05-10T04:38:46.687170Z  INFO ThreadId(01) src/main.rs:29: Received 33 bytes from 127.0.0.1:34781
[stage-7] Querying the following in the same request (Messages with >> prefix are part of this log)
[stage-7] >> ;abc.longassdomainname.com.	IN	 A
[stage-7] >> ;def.longassdomainname.com.	IN	 A
[stage-7] Sending Request: (Messages with >>> prefix are part of this log)
[stage-7] >>> ;; opcode: QUERY, status: NOERROR, id: 35338
[stage-7] >>> ;; flags: rd; QUERY: 2, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
[stage-7] >>>
[your_program] 2024-05-10T04:38:46.687288Z DEBUG ThreadId(01) src/main.rs:30: Received buffer [214, 32, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 12, 99, 111, 100, 101, 99, 114, 97, 102, 116, 101, 114, 115, 2, 105, 111, 0, 0, 1, 0, 1]
[stage-7] >>> ;; QUESTION SECTION:
[stage-7] >>> ;abc.longassdomainname.com.	IN	 A
[stage-7] >>> ;def.longassdomainname.com.	IN	 A
[stage-7] >>>
[your_program] 2024-05-10T04:38:46.687311Z DEBUG ThreadId(01) src/main.rs:33: Received packet: Packet {
[your_program]     header: Header {
[your_program]         id: 54816,
[your_program]         qr: 1,
[your_program]         opcode: 0,
[your_program]         aa: 0,
[your_program]         tc: 0,
[your_program]         rd: 1,
[your_program]         ra: 0,
[your_program]         z: 0,
[your_program]         rcode: 0,
[your_program]         qdcount: 1,
[your_program]         ancount: 0,
[your_program]         nscount: 0,
[your_program]         arcount: 0,
[your_program]     },
[your_program]     questions: [
[your_program]         Question {
[your_program]             name: Label(
[your_program]                 "codecrafters.io",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]         },
[your_program]     ],
[your_program]     answers: [],
[your_program] }
[stage-7] DNS query failed: dns: overflowing header size.
[stage-7] If you are seeing this after a while then it is likely that your server is not responding with appropriate id
[stage-7] Test failed
[stage-7] Terminating program
[your_program] 2024-05-10T04:38:46.687324Z DEBUG ThreadId(01) src/main.rs:52: Response packet: Packet {
[stage-7] Shutting down DNS resolver server...
[your_program]     header: Header {
[your_program]         id: 54816,
[your_program]         qr: 1,
[your_program]         opcode: 0,
[your_program]         aa: 0,
[your_program]         tc: 0,
[your_program]         rd: 1,
[your_program]         ra: 0,
[your_program]         z: 0,
[your_program]         rcode: 0,
[your_program]         qdcount: 1,
[your_program]         ancount: 1,
[your_program]         nscount: 0,
[your_program]         arcount: 0,
[your_program]     },
[your_program]     questions: [
[your_program]         Question {
[your_program]             name: Label(
[your_program]                 "codecrafters.io",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]         },
[your_program]     ],
[your_program]     answers: [
[your_program]         Answer {
[your_program]             name: Label(
[your_program]                 "codecrafters.io",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]             ttl: 60,
[your_program]             rdata: RData(
[your_program]                 "8.8.8.8",
[your_program]             ),
[your_program]         },
[your_program]     ],
[your_program] }
[your_program] 2024-05-10T04:38:46.687336Z DEBUG ThreadId(01) src/main.rs:54: Response bytes: [214, 32, 129, 0, 0, 1, 0, 1, 0, 0, 0, 0, 12, 99, 111, 100, 101, 99, 114, 97, 102, 116, 101, 114, 115, 2, 105, 111, 0, 0, 1, 0, 1, 12, 99, 111, 100, 101, 99, 114, 97, 102, 116, 101, 114, 115, 2, 105, 111, 0, 0, 1, 0, 1, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 4, 8, 8, 8, 8]
[your_program] 2024-05-10T04:38:46.688075Z  INFO ThreadId(01) src/main.rs:29: Received 53 bytes from 127.0.0.1:33276
[your_program] 2024-05-10T04:38:46.688132Z DEBUG ThreadId(01) src/main.rs:30: Received buffer [138, 10, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 97, 98, 99, 17, 108, 111, 110, 103, 97, 115, 115, 100, 111, 109, 97, 105, 110, 110, 97, 109, 101, 3, 99, 111, 109, 0, 0, 1, 0, 1, 3, 100, 101, 102, 192, 16, 0, 1, 0, 1]
[your_program] 2024-05-10T04:38:46.688140Z DEBUG ThreadId(01) src/dns/mod.rs:153: Reading from pointer offset=16
[your_program] 2024-05-10T04:38:46.688144Z DEBUG ThreadId(01) src/main.rs:33: Received packet: Packet {
[your_program]     header: Header {
[your_program]         id: 35338,
[your_program]         qr: 1,
[your_program]         opcode: 0,
[your_program]         aa: 0,
[your_program]         tc: 0,
[your_program]         rd: 1,
[your_program]         ra: 0,
[your_program]         z: 0,
[your_program]         rcode: 0,
[your_program]         qdcount: 2,
[your_program]         ancount: 0,
[your_program]         nscount: 0,
[your_program]         arcount: 0,
[your_program]     },
[your_program]     questions: [
[your_program]         Question {
[your_program]             name: Label(
[your_program]                 "abc.longassdomainname.com",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]         },
[your_program]         Question {
[your_program]             name: Label(
[your_program]                 "def.longassdomainname.com",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]         },
[your_program]     ],
[your_program]     answers: [],
[your_program] }
[your_program] 2024-05-10T04:38:46.688162Z DEBUG ThreadId(01) src/main.rs:52: Response packet: Packet {
[your_program]     header: Header {
[your_program]         id: 35338,
[your_program]         qr: 1,
[your_program]         opcode: 0,
[your_program]         aa: 0,
[your_program]         tc: 0,
[your_program]         rd: 1,
[your_program]         ra: 0,
[your_program]         z: 0,
[your_program]         rcode: 0,
[your_program]         qdcount: 2,
[your_program]         ancount: 2,
[your_program]         nscount: 0,
[your_program]         arcount: 0,
[your_program]     },
[your_program]     questions: [
[your_program]         Question {
[your_program]             name: Label(
[your_program]                 "abc.longassdomainname.com",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]         },
[your_program]         Question {
[your_program]             name: Label(
[your_program]                 "def.longassdomainname.com",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]         },
[your_program]     ],
[your_program]     answers: [
[your_program]         Answer {
[your_program]             name: Label(
[your_program]                 "abc.longassdomainname.com",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]             ttl: 60,
[your_program]             rdata: RData(
[your_program]                 "8.8.8.8",
[your_program]             ),
[your_program]         },
[your_program]         Answer {
[your_program]             name: Label(
[your_program]                 "def.longassdomainname.com",
[your_program]             ),
[your_program]             typez: A,
[your_program]             class: IN,
[your_program]             ttl: 60,
[your_program]             rdata: RData(
[your_program]                 "8.8.8.8",
[your_program]             ),
[your_program]         },
[your_program]     ],
[your_program] }
[your_program] 2024-05-10T04:38:46.688199Z DEBUG ThreadId(01) src/main.rs:54: Response bytes: [138, 10, 129, 0, 0, 2, 0, 2, 0, 0, 0, 0, 3, 97, 98, 99, 17, 108, 111, 110, 103, 97, 115, 115, 100, 111, 109, 97, 105, 110, 110, 97, 109, 101, 3, 99, 111, 109, 0, 0, 1, 0, 1, 3, 100, 101, 102, 17, 108, 111, 110, 103, 97, 115, 115, 100, 111, 109, 97, 105, 110, 110, 97, 109, 101, 3, 99, 111, 109, 0, 0, 1, 0, 1, 3, 97, 98, 99, 17, 108, 111, 110, 103, 97, 115, 115, 100, 111, 109, 97, 105, 110, 110, 97, 109, 101, 3, 99, 111, 109, 0, 0, 1, 0, 1, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 4, 8, 8, 8, 8, 3, 100, 101, 102, 17, 108, 111, 110, 103, 97, 115, 115, 100, 111, 109, 97, 105, 110, 110, 97, 109, 101, 3, 99, 111, 109, 0, 0, 1, 0, 1, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 4, 8, 8, 8, 8]
[stage-7] Program terminated successfully

And here’s a snippet of my code: This is how I am parsing header id.

        let mut buf: [u8; 2] = [0; 2];
        reader
            .read_exact(&mut buf)
            .expect("unable to read dns header id");
        self.id = u16::from_be_bytes(buf);

And this is how I am creating header bytes

impl AsBytes for Header {
    fn as_bytes(&self) -> Vec<u8> {
        let mut bytes: [u8; 12] = [0; 12];
        bytes[0..2].copy_from_slice(&self.id.to_be_bytes());
        bytes[2..4].copy_from_slice(&self.flags_as_bytes());
        bytes[4..6].copy_from_slice(&self.qdcount.to_be_bytes());
        bytes[6..8].copy_from_slice(&self.ancount.to_be_bytes());
        bytes[8..10].copy_from_slice(&self.nscount.to_be_bytes());
        bytes[10..12].copy_from_slice(&self.arcount.to_be_bytes());
        return bytes.to_vec();
    }
}

Ah, we’re due an update to the parser here (similar to Improvements to error messages on the HTTP server challenge). These errors can be super tricky to debug unfortunately, and without that new parser it’s going to be really difficult for us to help figure out what’s wrong.

I’ll treat this as an internal +1 to prioritize the DNS parser work!

In the meantime, if anyone happens to know what’s wrong here, please chime in!

1 Like

I was able to debug and find out what the issue was. Looks like I was setting RDATA (answer) length incorrectly.
I was using *.len() blindly and it when converted to [u8] takes more than 2 bytes of memory.

The fix was to use correct data type and make sure length is encoded with correct number of bytes.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

Note: I’ve updated the title of this post to include the stage ID (#YC9). You can learn about the stages rename here: Upcoming change: Stages overhaul.