| 123456789101112131415161718192021222324252627282930313233343536373839 |
- # Python container probe: raw DNS query against the unbound container.
- # Checks AD bit (DNSSEC validated) with and without DO bit, plus basic resolution.
- import socket
- import struct
- import sys
- server = sys.argv[1] if len(sys.argv) > 1 else "unbound"
- port = int(sys.argv[2]) if len(sys.argv) > 2 else 5053
- def query(name, do_bit):
- flags = 0x0100 | (0x0020 if do_bit else 0) # RD (+ DO)
- header = struct.pack(">HHHHHH", 0x1234, flags, 1, 0, 0, 0)
- qname = b"".join(bytes([len(p)]) + p.encode() for p in name.split(".")) + b"\x00"
- q = header + qname + struct.pack(">HH", 1, 1)
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.settimeout(10)
- s.sendto(q, (server, port))
- data, _ = s.recvfrom(4096)
- s.close()
- rf = struct.unpack(">H", data[2:4])[0]
- an = struct.unpack(">H", data[6:8])[0]
- return an, (rf >> 5) & 1, rf & 0xF
- ok = True
- for name in ("example.com", "unbound.net"):
- an, ad, rc = query(name, do_bit=1)
- good = an > 0 and ad == 1 and rc == 0
- ok = ok and good
- print(f"{name}: answers={an} AD={ad} rcode={rc} -> {'PASS' if good else 'FAIL'}")
- # plain query must also resolve
- an, ad, rc = query("example.com", do_bit=0)
- good = an > 0 and rc == 0
- ok = ok and good
- print(f"example.com (no DO): answers={an} rcode={rc} -> {'PASS' if good else 'FAIL'}")
- sys.exit(0 if ok else 1)
|