2024-06-12 03:58:32 +00:00
|
|
|
import io
|
|
|
|
import socket
|
2017-12-17 14:29:38 +00:00
|
|
|
|
|
|
|
import udnspkt
|
|
|
|
|
|
|
|
|
2024-06-12 03:58:32 +00:00
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
dns_addr = socket.getaddrinfo("127.0.0.1", 53)[0][-1]
|
2017-12-17 14:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def resolve(domain, is_ipv6):
|
2024-06-12 03:58:32 +00:00
|
|
|
buf = io.BytesIO(48)
|
2017-12-17 14:29:38 +00:00
|
|
|
udnspkt.make_req(buf, "google.com", is_ipv6)
|
|
|
|
v = buf.getvalue()
|
|
|
|
print("query: ", v)
|
|
|
|
s.sendto(v, dns_addr)
|
|
|
|
|
|
|
|
resp = s.recv(1024)
|
|
|
|
print("resp:", resp)
|
2024-06-12 03:58:32 +00:00
|
|
|
buf = io.BytesIO(resp)
|
2017-12-17 14:29:38 +00:00
|
|
|
|
|
|
|
addr = udnspkt.parse_resp(buf, is_ipv6)
|
|
|
|
print("bin addr:", addr)
|
2024-06-12 03:58:32 +00:00
|
|
|
print("addr:", socket.inet_ntop(socket.AF_INET6 if is_ipv6 else socket.AF_INET, addr))
|
2017-12-17 14:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
resolve("google.com", False)
|
|
|
|
print()
|
|
|
|
resolve("google.com", True)
|