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.iteritems()])
39
40
42 """Raised if an opcode is unknown."""
43 pass
44
46 """Convert text into an opcode.
47
48 @param text: the textual opcode
49 @type text: string
50 @raises UnknownOpcode: the opcode is unknown
51 @rtype: int
52 """
53
54 if text.isdigit():
55 value = int(text)
56 if value >= 0 and value <= 15:
57 return value
58 value = _by_text.get(text.upper())
59 if value is None:
60 raise UnknownOpcode
61 return value
62
64 """Extract an opcode from DNS message flags.
65
66 @param flags: int
67 @rtype: int
68 """
69
70 return (flags & 0x7800) >> 11
71
73 """Convert an opcode to a value suitable for ORing into DNS message
74 flags.
75 @rtype: int
76 """
77
78 return (value << 11) & 0x7800
79
81 """Convert an opcode to text.
82
83 @param value: the opcdoe
84 @type value: int
85 @raises UnknownOpcode: the opcode is unknown
86 @rtype: string
87 """
88
89 text = _by_value.get(value)
90 if text is None:
91 text = str(value)
92 return text
93
95 """True if the opcode in flags is UPDATE.
96
97 @param flags: DNS flags
98 @type flags: int
99 @rtype: bool
100 """
101
102 if (from_flags(flags) == UPDATE):
103 return True
104 return False
105