I’m stuck at this stage
import socket
import struct
known_domains = {
"mail.reddit.com.i": "151.101.1.140",
}
def build_dns_response(query):
id = query[0:2]
flags = struct.unpack("!H", query[2:4])[0]
response_flags = (flags & 0x7FFF) | 0x8000 # Set QR to 1
opcode = (flags >> 11) & 0x0F
if opcode == 0:
rcode = 0
else:
rcode = 4
question_start = 12
question_name_end = query.find(b'\x00', question_start) + 1
question_name = query[question_start:question_name_end].decode()
ancount = 0
answer = b''
if rcode == 0 and question_name in known_domains:
ip_address = known_domains[question_name]
ancount = 1
answer_name = b'\x00\x00'
answer_type = struct.pack("!H", 1)
answer_class = struct.pack("!H", 1)
ttl = struct.pack("!I", 80)
rdata = socket.inet_aton(ip_address)
rdata_length = struct.pack("!H", len(rdata))
answer = answer_name + answer_type + answer_class + ttl + rdata_length + rdata
response_flags = (response_flags & 0xFFEF) | (rcode << 0)
header = struct.pack("!HHHHHH", struct.unpack("!H", id)[0], response_flags, 1, ancount, 0, 0)
response = header + query[12:] + 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:
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', 48296): b'\x17\x08\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', 48296)
remote: [tester::#HD8] Querying: ;mail.reddit.com.i IN A
remote: [tester::#HD8] Sending Request: (Messages with >>> prefix are part of this log)
remote: [tester::#HD8] >>> ;; opcode: QUERY, status: NOERROR, id: 53510
remote: [tester::#HD8] >>> ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#HD8] >>>
remote: [tester::#HD8] >>> ;; QUESTION SECTION:
remote: [tester::#HD8] >>> ;mail.reddit.com.i IN A
remote: [tester::#HD8] >>>
remote: [your_program] Received packet from ('127.0.0.1', 43502): b'\xd1\x06\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x04mail\x06reddit\x03com\x00\x00\x01\x00\x01'
remote: [tester::#HD8] Received Response: (Messages with >>> prefix are part of this log)
remote: [tester::#HD8] >>> ;; opcode: QUERY, status: NOERROR, id: 53510
remote: [tester::#HD8] >>> ;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
remote: [tester::#HD8] >>>
remote: [tester::#HD8] >>> ;; QUESTION SECTION:
remote: [your_program] Sent response to ('127.0.0.1', 43502)
remote: [tester::#HD8] >>> ;mail.reddit.com.i IN A
remote: [tester::#HD8] >>>
remote: [tester::#HD8] Shutting down DNS resolver server...
remote: [tester::#HD8] Expected answer section to have at least one entry got 0
remote: [tester::#HD8] Test failed
remote: [tester::#HD8] Terminating program
remote: [tester::#HD8] Program terminated successfully