s4-python: import a copy of the python dns library
[ira/wip.git] / source4 / scripting / python / samba_external / dnspython / dns / opcode.py
1 # Copyright (C) 2001-2007, 2009, 2010 Nominum, Inc.
2 #
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.
7 #
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.
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 # 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.
37
38 _by_value = dict([(y, x) for x, y in _by_text.iteritems()])
39
40
41 class UnknownOpcode(dns.exception.DNSException):
42     """Raised if an opcode is unknown."""
43     pass
44
45 def from_text(text):
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
63 def from_flags(flags):
64     """Extract an opcode from DNS message flags.
65
66     @param flags: int
67     @rtype: int
68     """
69
70     return (flags & 0x7800) >> 11
71
72 def to_flags(value):
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
80 def to_text(value):
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
94 def is_update(flags):
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