Debian Codegraph @

examples/edns.py

raw
1 #!/usr/bin/env python3
2
3 import dns.edns
4 import dns.message
5 import dns.query
6 import dns.resolver
7
8 n = "."
9 t = dns.rdatatype.SOA
10 l = "199.7.83.42" # Address of l.root-servers.net
11 i = "149.20.1.73" # Address of ns1.isc.org, for COOKIEs
12
13 q_list = []
14
15 # A query without EDNS0
16 q_list.append((l, dns.message.make_query(n, t)))
17
18 # The same query, but with EDNS0 turned on with no options
19 q_list.append((l, dns.message.make_query(n, t, use_edns=0)))
20
21 # Use use_edns() to specify EDNS0 options, such as buffer size
22 this_q = dns.message.make_query(n, t)
23 this_q.use_edns(0, payload=2000)
24 q_list.append((l, this_q))
25
26 # With an NSID option
27 # use_edns=0 is not needed if options are specified)
28 q_list.append(
29 (
30 l,
31 dns.message.make_query(
32 n, t, options=[dns.edns.GenericOption(dns.edns.OptionType.NSID, b"")]
33 ),
34 )
35 )
36
37 # With an NSID option, but with use_edns() to specify the options
38 this_q = dns.message.make_query(n, t)
39 this_q.use_edns(0, options=[dns.edns.GenericOption(dns.edns.OptionType.NSID, b"")])
40 q_list.append((l, this_q))
41
42 # With a COOKIE
43 q_list.append(
44 (
45 i,
46 dns.message.make_query(
47 n,
48 t,
49 options=[
50 dns.edns.GenericOption(
51 dns.edns.OptionType.COOKIE, b"0xfe11ac99bebe3322"
52 )
53 ],
54 ),
55 )
56 )
57
58 # With an ECS option using dns.edns.ECSOption to form the option
59 q_list.append(
60 (l, dns.message.make_query(n, t, options=[dns.edns.ECSOption("192.168.0.0", 20)]))
61 )
62
63 for addr, q in q_list:
64 r = dns.query.udp(q, addr)
65 if not r.options:
66 print("No EDNS options returned")
67 else:
68 for o in r.options:
69 print(o.otype.value, o.data)
70 print()