vfs_glusterfs: Implement AIO support
[Samba.git] / lib / dnspython / dns / opcode.py
blob3258c34cd93491cfd9a42988a8a453226eeaff32
1 # Copyright (C) 2001-2007, 2009-2011 Nominum, Inc.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 """DNS Opcodes."""
18 import dns.exception
20 QUERY = 0
21 IQUERY = 1
22 STATUS = 2
23 NOTIFY = 4
24 UPDATE = 5
26 _by_text = {
27 'QUERY' : QUERY,
28 'IQUERY' : IQUERY,
29 'STATUS' : STATUS,
30 'NOTIFY' : NOTIFY,
31 'UPDATE' : UPDATE
34 # We construct the inverse mapping programmatically to ensure that we
35 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
36 # would cause the mapping not to be true inverse.
38 _by_value = dict([(y, x) for x, y in _by_text.iteritems()])
41 class UnknownOpcode(dns.exception.DNSException):
42 """Raised if an opcode is unknown."""
43 pass
45 def from_text(text):
46 """Convert text into an opcode.
48 @param text: the textual opcode
49 @type text: string
50 @raises UnknownOpcode: the opcode is unknown
51 @rtype: int
52 """
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
63 def from_flags(flags):
64 """Extract an opcode from DNS message flags.
66 @param flags: int
67 @rtype: int
68 """
70 return (flags & 0x7800) >> 11
72 def to_flags(value):
73 """Convert an opcode to a value suitable for ORing into DNS message
74 flags.
75 @rtype: int
76 """
78 return (value << 11) & 0x7800
80 def to_text(value):
81 """Convert an opcode to text.
83 @param value: the opcdoe
84 @type value: int
85 @raises UnknownOpcode: the opcode is unknown
86 @rtype: string
87 """
89 text = _by_value.get(value)
90 if text is None:
91 text = str(value)
92 return text
94 def is_update(flags):
95 """True if the opcode in flags is UPDATE.
97 @param flags: DNS flags
98 @type flags: int
99 @rtype: bool
102 if (from_flags(flags) == UPDATE):
103 return True
104 return False