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.
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."""
46 """Convert text into an opcode.
48 @param text: the textual opcode
50 @raises UnknownOpcode: the opcode is unknown
56 if value
>= 0 and value
<= 15:
58 value
= _by_text
.get(text
.upper())
63 def from_flags(flags
):
64 """Extract an opcode from DNS message flags.
70 return (flags
& 0x7800) >> 11
73 """Convert an opcode to a value suitable for ORing into DNS message
78 return (value
<< 11) & 0x7800
81 """Convert an opcode to text.
83 @param value: the opcdoe
85 @raises UnknownOpcode: the opcode is unknown
89 text
= _by_value
.get(value
)
95 """True if the opcode in flags is UPDATE.
97 @param flags: DNS flags
102 if (from_flags(flags
) == UPDATE
):