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