DNS-SERVER challenge,Stuck on last stage

I’m stuck on Stage #GT1.

I’ve tried multiple times to debug the code ,but i am quite unsure of where am i lagging in this

Here are my logs :


[tester::#YC9] Running tests for Stage #YC9 (Parse compressed packet)
[tester::#YC9] Starting DNS server on 127.0.0.1:2053
[tester::#YC9] Running program
[tester::#YC9] DNS resolver listening on 127.0.0.1:5354
[tester::#YC9] Connecting to 127.0.0.1:2053 using UDP
[your_program] Logs from your program will appear here!
[your_program] resolver 127.0.0.1:5354
[your_program] total questions 1
[your_program] new connection established
[your_program] n [202 60 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 14 16 0 4 76 76 21 21]
[your_program] answeroffset,n 33 64
[your_program] Found answer of length 31
[your_program] total answers [[12 99 111 100 101 99 114 97 102 116 101 114 115 2 105 111 0 0 1 0 1 0 0 14 16 0 4 76 76 21 21]]
[tester::#YC9] Querying the following in the same request (Messages with >> prefix are part of this log)
[tester::#YC9] >> ;abc.longassdomainname.com.   IN       A
[tester::#YC9] >> ;def.longassdomainname.com.   IN       A
[tester::#YC9] Sending Request: (Messages with >>> prefix are part of this log)
[tester::#YC9] >>> ;; opcode: QUERY, status: NOERROR, id: 10421
[tester::#YC9] >>> ;; flags: rd; QUERY: 2, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
[tester::#YC9] >>> 
[tester::#YC9] >>> ;; QUESTION SECTION:
[tester::#YC9] >>> ;abc.longassdomainname.com.  IN       A
[tester::#YC9] >>> ;def.longassdomainname.com.  IN       A
[tester::#YC9] >>> 
[your_program] answerresp 1
[your_program] answers [12 99 111 100 101 99 114 97 102 116 101 114 115 2 105 111 0 0 1 0 1 0 0 14 16 0 4 76 76 21 21]
[your_program] total questions 2
[your_program] new connection established
[your_program] n [40 181 129 0 0 1 0 1 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 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 14 16 0 4 127 0 0 1]
[your_program] answeroffset,n 43 84
[your_program] Found answer of length 41
[your_program] new connection established
[tester::#YC9] Received Response: (Messages with >>> prefix are part of this log)
[tester::#YC9] >>> ;; opcode: QUERY, status: NOERROR, id: 10421
[tester::#YC9] >>> ;; flags: qr rd; QUERY: 2, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
[tester::#YC9] >>> 
[tester::#YC9] >>> ;; QUESTION SECTION:
[tester::#YC9] >>> ;abc.longassdomainname.com.  IN       A
[tester::#YC9] >>> ;def.        IN       A
[tester::#YC9] >>> 
[tester::#YC9] >>> ;; ANSWER SECTION:
[tester::#YC9] >>> abc.longassdomainname.com.   3600    IN      A       127.0.0.1
[tester::#YC9] >>> 
[tester::#YC9] Expected answer section to have 2 entries got 1
[tester::#YC9] Test failed
[tester::#YC9] Terminating program
[tester::#YC9] Shutting down DNS resolver server...
[your_program] n [40 181 129 0 0 1 0 0 0 0 0 0 3 100 101 102 0 0 1 0 1]
[your_program] answeroffset,n 21 21
[your_program] total answers [[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 14 16 0 4 127 0 0 1]]
[your_program] answerresp 1
[your_program] answers [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 14 16 0 4 127 0 0 1]
[tester::#YC9] Program terminated successfully

include relevant logs here (please make sure to keep the backticks around this!)


And here's a snippet of my code: 

```goLang
   func forwardDNSServer(buf []byte, resolverAddr string) [][]byte {
	header := buf[0:12]
	header[2] &= 0b01111111
	qdCount := binary.BigEndian.Uint16(buf[4:6])
	var offset uint16 = 12
	totalQuestions := [][]byte{}
	totalAnswers := [][]byte{}
	

	for i := uint16(0); i < qdCount; i++ {
		questionResponse := createQuestionSection(buf[offset:])
		totalQuestions = append(totalQuestions, questionResponse)
		offset += uint16(len(questionResponse))
	}

	resolverIP := strings.Split(resolverAddr, ":")[0]
	resolverPort, err := strconv.Atoi(strings.Split(resolverAddr, ":")[1])
	if err != nil {
		fmt.Printf("cannot convert %s port to int", resolverIP)
	}
	fmt.Println("total questions", len(totalQuestions))
	for _, question := range totalQuestions {
		newHeader := make([]byte, len(header))
        copy(newHeader, header)
        binary.BigEndian.PutUint16(newHeader[4:6], 1)
        packet := make([]byte, 0)
        packet = append(packet, newHeader...)
        packet = append(packet, question...)

		resolverAddr := &net.UDPAddr{
			IP:   net.ParseIP(resolverIP),
			Port: resolverPort,
		}

		conn, err := net.DialUDP("udp", nil, resolverAddr)
		if err != nil {
			fmt.Println("Error connecting to resolver:", err)
			break
		}
		fmt.Println("new connection established")
		defer conn.Close()

		_, err = conn.Write(packet)
		if err != nil {
			fmt.Println("Error sending to resolver:", err)
			break
		}

		// conn.SetReadDeadline(time.Now().Add(2 * time.Second))

		response := make([]byte, 512)
		n, _, err := conn.ReadFromUDP(response)
		if err != nil {
			fmt.Println("Error reading from resolver:", err)
			break
		}
		fmt.Println("n",response[:n])
		answerOffset := 12
		questionLen := createQuestionSection(response[answerOffset:])
        answerOffset +=len(questionLen)

		fmt.Println("answeroffset,n",answerOffset,n)
        if answerOffset < n {
            answer := response[answerOffset:n]
            if len(answer) > 0 {
                fmt.Printf("Found answer of length %d\n", len(answer))
                totalAnswers = append(totalAnswers, answer)
            }
		}
	}
	fmt.Println("total answers", totalAnswers)
	return totalAnswers
}

@TusharAbhinav I ran your code after adding a log here:

func forwardDNSServer(buf []byte, resolverAddr string) [][]byte {
	...
	for _, question := range totalQuestions {
		fmt.Println("🚩 question", question)

Looks like the second question is not well formed:

Could you try investigating how the questions are being constructed?

Thanks,fixed it @andy1li

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