I’m stuck on Stage “Parse question section”.
I’ve tried
I set Rcode = 4 then, it was expecting for answer section. After that, I added the Answer section, and then again, it was expecting the Rcode to be 4.
import socket
import struct
def build_dns_response(query):
id = query[0:2]
flags = struct.unpack("!H", query[2:4])[0]
response_flags = (flags & 0x7FFF) | 0x8000
qdcount = 1
ancount = 1
nscount = 0
arcount = 0
header = struct.pack("!HHHHHH", struct.unpack("!H", id)[0], response_flags, qdcount, ancount, nscount, arcount)
question_start = 12
question_name_end = query.find(b'\x00', question_start) + 1
question_name = query[question_start:question_name_end]
qtype = struct.pack("!H", 1)
qclass = struct.pack("!H", 1)
question = question_name + qtype + qclass
answer_name = question_name
answer_type = struct.pack("!H", 1)
answer_class = struct.pack("!H", 1)
ttl = struct.pack("!I", 80)
rdata_length = struct.pack("!H", 4)
rdata = struct.pack("!BBBB", 8, 8, 8, 8)
answer = answer_name + answer_type + answer_class + ttl + rdata_length + rdata
response = header + question + answer
return response
def start_udp_server():
server_address = ('', 2053)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(server_address)
print("UDP server is running on port 2053...")
while True:
data, address = sock.recvfrom(4096)
print(f"Received packet from {address}: {data}")
response_message = build_dns_response(data)
sock.sendto(response_message, address)
print(f"Sent response to {address}")
if __name__ == "__main__":
start_udp_server()
Log:
remote: [tester::#HD8] Running tests for Stage #HD8 (Parse question section)
remote: [tester::#HD8] Starting DNS server on 127.0.0.1:2053
remote: [tester::#HD8] Running program
remote: [tester::#HD8] DNS resolver listening on 127.0.0.1:5354
remote: [tester::HD8] Connecting to 127.0.0.1:2053 using UDP
remote: [your_program] UDP server is running on port 2053...
remote: [your_program] Received packet from ('127.0.0.1', 42895): b'\xaeP\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0ccodecrafters\x02io\x00\x00\x01\x00\x01'
remote: [your_program] Sent response to ('127.0.0.1', 42895)
remote: [tester::#HD8] Querying: ;;codecrafters.i.o IN A
remote: [tester::#HD8] Sending Request: (Messages with >>> prefix are part of this log)
remote: [tester::#HD8] >>> ;; opcode: QUERY, status: NOERROR, id: 760
remote: [tester::#HD8] >>> ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#HD8] >>>
remote: [tester::#HD8] >>> ;; QUESTION SECTION:
remote: [tester::#HD8] >>> ;;codecrafters.i.o IN A
remote: [tester::#HD8] >>>
remote: [your_program] Received packet from ('127.0.0.1', 49974): b'\x02\xf8\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0ccodecrafters\x02io\x00\x00\x01\x00\x01'
remote: [your_program] Sent response to ('127.0.0.1', 49974)
remote: [tester::#HD8] Received Response: (Messages with >>> prefix are part of this log)
remote: [tester::#HD8] >>> ;; opcode: QUERY, status: NOERROR, id: 760
remote: [tester::#HD8] >>>; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#HD8] >>>
remote: [tester::#HD8] >>> ;; QUESTION SECTION:
remote: [tester::#HD8] >>> ;;codecrafters.i.o IN A
remote: [tester::#HD8] >>>
remote: [tester::#HD8] >>> ;; ANSWER SECTION:
remote: [tester::#HD8] >>> ;codecrafters.i.o. 80 IN A 8.8.8.8
remote: [tester::#HD8] >>>
remote: [tester::#HD8] Test passed.
remote: [tester::#HD8] Terminating program
remote: [tester::#HD8] Shutting down DNS resolver server...
remote: [tester::#HD8] Program terminated successfully
remote:
remote: [tester::#UC8] Running tests for Stage #UC8 (Parse header section)
remote: [tester::#UC8] Starting DNS server on 127.0.0.1:2053
remote: [tester::#UC8] Running program
remote: [tester::#UC8] DNS resolver listening on 127.0.0.1:5354
remote: [tester::#UC8] Connecting to 127.0.0.1:2053 using UDP
remote: [your_program] UDP server is running on port 2053...
remote: [your_program] Received packet from ('127.0.0.1', 43572): b'\x0f\x93\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0ccodecrafters\x02io\x00\x00\x01\x00\x01'
remote: [your_program] Sent response to ('127.0.0.1', 43572)
remote: [tester::#UC8] Querying: ;codecrafters.i.o IN A
remote: [tester::#UC8] Sending Request: (Messages with >>> prefix are part of this log)
remote: [tester::#UC8] >>>; opcode: QUERY, status: NOERROR, id: 54774
remote: [tester::#UC8] >>> ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#UC8] >>>
remote: [tester::#UC8] >>> ;; QUESTION SECTION:
remote: [tester::#UC8] >>> ;codecrafter.io. IN A
remote: [tester::#UC8] >>>
remote: [your_program] Received packet from ('127.0.0.1', 40773): b'\xd5\xf6\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0ccodecrafters\x02io\x00\x00\x01\x00\x01'
remote: [your_program] Sent response to ('127.0.0.1', 40773)
remote: [tester::#UC8] Received Response: (Messages with >>> prefix are part of this log)
remote: [tester::#UC8] >>> ;; opcode: QUERY, status: NOERROR, id: 54774
remote: [tester::#UC8] >>> ;; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#UC8] >>>
remote: [tester::#UC8] >>> ;; QUESTION SECTION:
remote: [tester::#UC8] >>> ;;codecrafters.i.o IN A
remote: [tester::#UC8] >>>
remote: [tester::#UC8] >>> ;; ANSWER SECTION:
remote: [tester::#UC8] >>> ;codecrafters.i.o 80 IN A 8.8.8.8
remote: [tester::#UC8] >>>
remote: [tester::#UC8]Querying: ;codecrafters.i.o IN A
remote: [tester::#UC8] Sending Request: (Messages with >>> prefix are part of this log)
remote: [tester::#UC8] >>> ;; opcode: IQUERY, status: NOERROR, id: 54774
remote: [tester::#UC8] >>> ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#UC8] >>>
remote: [tester::#UC8] >>> ;; QUESTION SECTION:
remote: [tester::#UC8] >>> ;codecrafters.i.o IN A
remote: [tester::#UC8] >>>
remote: [your_program] Received packet from ('127.0.0.1', 37832): b'\xd5\xf6\t\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0ccodecrafters\x02io\x00\x00\x01\x00\x01'
remote: [your_program] Sent response to ('127.0.0.1', 37832)
remote: [tester::#UC8] Received Response: (Messages with >>> prefix are part of this log)
remote: [tester::#UC8] >>>; opcode: IQUERY, status: NOERROR, id: 54774
remote: [tester::#UC8] >>>; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#UC8] >>>
remote: [tester::#UC8] >>> ;; QUESTION SECTION:
remote: [tester::#UC8] >>> ;codecrafters.i.o IN A
remote: [tester::#UC8] >>>
remote: [tester::#UC8] >>> ; ANSWER SECTION:
remote: [tester::#UC8] >>> ;codecrafters.i.o 80 IN A 8.8.8.8
remote: [tester::#UC8] >>>
remote: [tester::#UC8] Expected Rcode field to be 4 got 0
remote: [tester::#UC8] Test failed
remote: [tester::#UC8] Terminating program
remote: [tester::#UC8] Shutting down DNS resolver server...
remote: [tester::#UC8] Program terminated successfully