1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """DNS Opcodes."""
19
20 import dns.exception
21
22
23 QUERY = 0
24
25 IQUERY = 1
26
27 STATUS = 2
28
29 NOTIFY = 4
30
31 UPDATE = 5
32
33 _by_text = {
34 'QUERY': QUERY,
35 'IQUERY': IQUERY,
36 'STATUS': STATUS,
37 'NOTIFY': NOTIFY,
38 'UPDATE': UPDATE
39 }
40
41
42
43
44
45 _by_value = {y: x for x, y in _by_text.items()}
46
47
49 """An DNS opcode is unknown."""
50
51
53 """Convert text into an opcode.
54
55 *text*, a ``text``, the textual opcode
56
57 Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown.
58
59 Returns an ``int``.
60 """
61
62 if text.isdigit():
63 value = int(text)
64 if value >= 0 and value <= 15:
65 return value
66 value = _by_text.get(text.upper())
67 if value is None:
68 raise UnknownOpcode
69 return value
70
71
73 """Extract an opcode from DNS message flags.
74
75 *flags*, an ``int``, the DNS flags.
76
77 Returns an ``int``.
78 """
79
80 return (flags & 0x7800) >> 11
81
82
84 """Convert an opcode to a value suitable for ORing into DNS message
85 flags.
86
87 *value*, an ``int``, the DNS opcode value.
88
89 Returns an ``int``.
90 """
91
92 return (value << 11) & 0x7800
93
94
96 """Convert an opcode to text.
97
98 *value*, an ``int`` the opcode value,
99
100 Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown.
101
102 Returns a ``text``.
103 """
104
105 text = _by_value.get(value)
106 if text is None:
107 text = str(value)
108 return text
109
110
112 """Is the opcode in flags UPDATE?
113
114 *flags*, an ``int``, the DNS message flags.
115
116 Returns a ``bool``.
117 """
118
119 return from_flags(flags) == UPDATE
120